What makes cmake variables important?

Alexander Neundorf neundorf at kde.org
Mon Sep 17 01:07:51 BST 2007


On Sunday 16 September 2007 19:29, you wrote:
> I'm messing with kdebase on Solaris, which is missing a bunch of libraries
> assumed present everywhere else. With CMake 2.4.7, I get the following
> during configuration:
>
> CMake Error: This project requires some variables to be set,
> and cmake can not find them.
> Please set the following variables:
> UTIL_LIBRARIES (ADVANCED)
>
> This library comes from kdm's configure checks, like so:
>
> find_library(UTIL_LIBRARIES util)
>
> I don't have such a library, so this gets the NOTFOUND value. Why is that a
> problem? Why *must* UTIL_LIBRARIES have a value?

CMake gives this error message if "NOTFOUND" is used with 
TARGET_LINK_LIBRARIES() or INCLUDE_DIRECTORIES(). It's intended as a hint 
that you use something you shouldn't use and you forgot to handle that case.

The "NOTFOUND" value is handled in a special way by cmake, if it is 
the "return value" of a command (like FIND_LIBRARY()), this command searches 
again. So if something wasn't found, the variable is set to NOTFOUND, then 
you install the package, and when cmake runs again it will find the new 
library.
Setting the variable empty will disable this feature and break builds (we did 
that once when trying to optimize cmake time here).

> There is a bit in workspace/kdm/backend/CMakeLists.txt which uses
> ${UTIL_LIBRARIES} in a target_link_libraries macro; removing UTIL_LIBRARIES
>
> >from there makes the CMake error shown above disappear and lets me get on
>
> with kdebase (next step: XRender libraries with the same thing). Strangely,
> S_LIBRARIES in the same macro does not trigger this same error message
> (it's never checked for, either, though).
>
> Is there a generic way of dealing with this? I added an additional
> target_link_libraries macro call wrapped in if(UTIL_LIBRARIES), and that

The correct way to handle this is really to check the results of function 
calls. Two ways how this can be done:

find_library(SOME_LIB util)

...
if(SOME_LIB)
   target_link_libraries(foo ${SOME_LIB})
endif(SOME_LIB)

OR:

set(extraLibraries)

find_library(SOME_LIB util)

if(SOME_LIB)
  set(extraLibraries ${extraLibraries} ${SOME_LIB})
endif(SOME_LIB)

...
target_link_libraries(foo ... ${extraLibraries})

I think this second way is nicer.

With a macro like this:

macro( APPEND_CONDITIONAL list var)
  if(${var})
    set(${list} ${${list}} ${var} )
  endif(${var})
endmacro(APPEND_CONDITIONAL)

it will be shorter:

set(extraLibraries)

find_library(SOME_LIB util)
append_conditional(extraLibraries SOME_LIB)

...
target_link_libraries(foo ... ${extraLibraries})

But the macro needs a better name.
Suggestions ?

Bye
Alex




More information about the kde-core-devel mailing list