Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake building targets conditionally based on library existence

Tags:

build

cmake

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?

like image 970
fluffy Avatar asked Apr 04 '12 00:04

fluffy


1 Answers

It's simply

if(PC_EGL AND PC_GLESv2 AND PC_GLESv2)

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

like image 82
arrowd Avatar answered Sep 16 '22 21:09

arrowd