May or may not be an answer but this doesn't fit into comment section so posting as an answer.
Building opencv
from source for ROS is documented on this section on its wiki page, which seems to cover not just building from source but mixed with how to make binary packages out of built artifact IINM. I haven't tried the whole steps by myself, but I'd say go through the steps in that section up to the following line, then start building:
44 echo "install(FILES package.xml DESTINATION share/opencv3)" >> ./CMakeLists.txt
Originally posted by 130s with karma: 10937 on 2019-07-01
This answer was ACCEPTED on the original site
Post score: 2
Original comments
Comment by mogumbo on 2019-07-02:
Thanks 130s. I had stopped looking at that page about 24 hours ago because I found the instructions confusing and it all looked pretty outdated. I just took another stab at it, changing the OpenCV version to 2.4.6 and using opencv3-release's package.xml for lunar because there isn't one for melodic.
All that gets me a package that compiles in my workspace. Yay! Problem solved.
Unfortunately, now I'm stuck on a different problem: not all libraries link against my local OpenCV. I get the exact same problem now building OpenCV in my workspace. Ug.