Ok…I’m working with an old software package that still uses X11, and OpenGL via X11 on MacOS. X11 and openGL are deprecated anyway, but meantime we’d like to keep the software going.
First problem: FindX11 consistently finds the wrong directories. In a MacOS environment using XQuartz, the X11 system is always in /opt/X11, and is symlinked at /usr/X11 and /usr/X11R6. In addition, I have Homebrew on the system, and it has some of the X11 libraries, but I don’t want to use them at all. find_package(X11) gives a jumble of references to /usr/X11R6, /usr/local/lib/X11, and /usr/local/include/X11. Short of explicitly calling out the appropriate directories, is there a way to direct find_package(X11) to use /opt/X11 only?
Second problem: according to the docs, “On OSX FindOpenGL defaults to using the framework version of OpenGL. People will have to change the cache values of OPENGL_glu_LIBRARY and OPENGL_gl_LIBRARY to use OpenGL with X11 on OSX.” Is there any way I can include this in the CMake scripts? Should I just explicitly call out libraries I want on MacOS?
Does setting the X11_ROOT and OpenGL_ROOT variables to /opt/X11 and whatever prefix OpenGL lives under work? You can use cmake --debug-find to get better information about search details.
It did. And the answer is the XQuartz include directory and library file names are not what FindOpenGL.cmake expects. In addition, cmake prioritizes frameworks over plain libraries. So the fix is:
Tell cmake not to look for frameworks
Fix FindOpenGL.cmake to look for the proper include file and library names.
Now, when I copy my modified FindOpenGL to a local directory, it cannot find FindPackageHandleStandardArgs.cmake. Do I need to copy that also, and whatever it includes? Is there perhaps a variable that gives the default module directory? Or…?
Here’s a diff of a version of FindOpenGL.cmake which works when in the Modules directory of the app:
210,214c210,216
< # The OpenGL.framework provides both gl and glu
< find_library(OPENGL_gl_LIBRARY OpenGL DOC "OpenGL library for OS X")
< find_library(OPENGL_glu_LIBRARY OpenGL DOC
< "GLU library for OS X (usually same as OpenGL library)")
< find_path(OPENGL_INCLUDE_DIR OpenGL/gl.h DOC "Include for OpenGL on OS X")
---
> # The OpenGL.framework provides both gl and glu in OpenGL
> # XQuartz provides libgl and libglu
> find_library(OPENGL_gl_LIBRARY NAMES OpenGL GL DOC
> "OpenGL library for OS X")
> find_library(OPENGL_glu_LIBRARY NAMES OpenGL GLU DOC
> "GLU library for OS X")
> find_path(OPENGL_INCLUDE_DIR NAMES OpenGL/gl.h GL/gl.h DOC "Include for OpenGL on OS X")
And, yes, if I copy FindPackageMessage.cmake and FindPackageHandleStandardArgs.cmake to my local module directory, it all works, but what an awful hack, and one which may fail with future versions of CMake. Any suggestions?
Fine point: what’s common practice for naming a local modules directory? I used CMakeModules at the top level of the project, but I’m not sure that’s a good idea; it looks like it might be part of CMake instead of a local directory. Is there a common practice or style recommendation?