CMake building targets conditionally based on library existence

Go To StackoverFlow.com

6

I have a large cross-platform project which needs to build in various places; in some places, different UI toolkits, sound APIs, etc. may be available, and I am trying to figure out the best way to automatically configure which targets get configured based on which libraries are present.

The code I am trying for that is, for example:

find_library(PC_EGL EGL)
find_library(PC_GLESv2 GLESv2)
find_library(PC_Xxf86vm Xxf86vm)
if (DEFINED PC_EGL AND DEFINED PC_GLESv2 AND DEFINED PC_Xxf86vm)
  add_executable(foo foo.cpp)
  target_link_libraries(foo ${PC_EGL} ${PC_GLESv2} ${PC_Xxf86vm})
endif()

However, in the case that I build this on a system which doesn't have libGLESv2 available, I get the error:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
PC_GLESv2
    linked by target "foo" in directory /path/to/platform

The find_library documentation implies that the variable PC_EGL_NOTFOUND should be getting set, but it isn't (CMake 2.8.5). So, what is the appropriate way to use find_library to determine whether a target should be made to exist at all? It seems like using

if (NOT PC_EGL MATCH "-NOTFOUND")

is a bit fragile and fiddly, so is there a better mechanism for determining a CMake command path based on wheter a library was found at all?

2012-04-04 00:09
by fluffy


10

It's simply

if(PC_EGL AND PC_GLESv2 AND PC_GLESv2)

CMake treats 0, FALSE, OFF, ANYTHING-NOTFOUND as false.

2012-04-04 06:16
by arrowd
Thanks! That seems like it should have been easy to find in the documentation or examples, which is a common problem I have with CMake - once I know how to do something it's easy, but finding how to do it, not so much - fluffy 2012-04-04 17:44
Ads