How to direct FindX11 and FindOpenGL to look in specific directory

I am using cmake v 3.22.1.
On our MacOS systems we want to use XQuartz for X11 and OpenGL includes and libs, which are installed in /opt/X11/include and /opt/X11/lib, respectively. My CMakeLists.txt calls FindOpenGL and FindX11, but the result is

X11_INCLUDE_DIR: /usr/local/include;/usr/X11R6/include
X11_LIBRARIES: /usr/local/lib/libSM.dylib;/usr/local/lib/libICE.dylib;/usr/local/lib/libX11.dylib;/usr/local/lib/libXext.dylib
OPENGL_INCLUDE_DIR: /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenGL.framework
OPENGL_LIBRARY: /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenGL.framework;/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenGL.framework

So those are the wrong places - they should be in /opt/X11.
Based on FindX11 and FindOpenGL documentation I try this before calling the FindModules:

if (APPLE)
  set(X11_X11_INCLUDE_PATH /opt/X11/include/)
  set(X11_X11_LIB /opt/X11/lib/)
  set(OPENGL_INCLUDE_DIR /opt/X11/include/)
  set(OPENGL_gl_LIBRARY /opt/X11/lib/)
endif()

And the result is:

X11_INCLUDE_DIR: /opt/X11/include/;/usr/local/include;/usr/X11R6/include
X11_LIBRARIES: /opt/X11/lib/;/usr/local/lib/libXext.dylib
OPENGL_INCLUDE_DIR: /opt/X11/include/
OPENGL_LIBRARIES: /opt/X11/lib/;/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenGL.framework

I also get these warnings:

WARNING: Target "mbgsfShared" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbgsfShared" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbgrdviz" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbgrdviz" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbedit" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbeditviz" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbeditviz" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbnavadjust" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbnavadjust" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbnavedit" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbnavedit" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbvelocitytool" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.
WARNING: Target "mbvelocitytool" requests linking to directory "/opt/X11/lib/".  Targets may link only to libraries.  CMake is dropping the item.

So how do I force FindX11 and FindOpenGL to look in /opt/X11/include and /opt/X11/lib, without generating these warnings?

Thanks
Tom

Well, you added the directory paths to the library lists, so those warnings are from your _LIB and _LIBRARY settings to containing paths directly. I think setting X11_ROOT and OpenGL_ROOT should help push /opt/X11 to the front of the list. See the first bullet point of the search procedure in the docs.

1 Like

I see your point about the _LIB/_LIBRARY warnings.
Prepending X11_ROOT and OpenGL_ROOT with /opt/X11 didn’t affect the FindX11 and FindOpenGL results (e.g. X11_INCLUDE_DIR: /usr/local/include;/usr/X11R6/include):

if (APPLE)
  message("MACOS: assume X11 and OpenGL are in /opt/X11")
  set(CMAKE_FIND_USE_PACKAGE_ROOT_PATH TRUE)
  set(X11_ROOT /opt/X11/ ${X11_ROOT})
  set(OpenGL_ROOT /opt/X11/ ${OpenGL_ROOT})
endif()

Here’s the actual content of /opt/X11, which contains the X11/OpenGL includes and libraries:

% ls /opt/X11
bin/		include/	libexec/	var/
etc/		lib/		share/

Moreover I see the FindOpenGL documentaton says

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

Does this mean that FindOpenGL will never find OpenGL stuff in /opt/X11 on OSX?

Hmm, it would seem so. There might be a (silent) policy associated with _ROOT variables, but it isn’t mentioned.

Setting CMAKE_PREFIX_PATH to /opt/X11 should work I think.

Alex