Search code examples
cmakeconfigconfiguration-files

Cmake config file when optional libraries are built


I have a project for which you can or can not build the GUI if the user wants to. It's a library that is optional.

I'm trying to create a Cmake config file for installation and I don't know if there is any clever way to know this and adapt the file for it. For now I have:

set(MYPROG_INCLUDE_DIRS @CMAKE_INSTALL_PREFIX@/include/myprog)

foreach(lib feature geometry gui sensors sensorstream utils)
    list(APPEND MYPROG_LIBRARIES @CMAKE_INSTALL_PREFIX@/lib/libmyprog_${lib}.so)
endforeach()

But the moment I build the gui, this file is giving MYPROG_LIBRARY linking toward gui also, even though it wasn't build. I thought about separating the two as in:

set(MYPROG_INCLUDE_DIRS @CMAKE_INSTALL_PREFIX@/include/myprog)

foreach(lib feature geometry sensors sensorstream utils)
    list(APPEND MYPROG_LIBRARIES @CMAKE_INSTALL_PREFIX@/lib/libmyprog_${lib}.so)
 endforeach()

set(MYPROG_LIBRARIES_GUI CMAKE_INSTALL_PREFIX@/lib/libmyprog_gui.so)

But I now have to different calls to do to get all libraries if I built the gui.

In my Cmake I have BUILD_GUI set to 0 if I cannot build the GUI. Is there any way to use this?


Solution

  • You can build up your component list and then put it into config file:

    set(COMPONENTS lib feature geometry sensors sensorstream utils)
    if(BUILD_GUI)
      list(APPEND COMPONENTS gui)
    endif()
    

    And in the config file

    foreach(lib @COMPONENTS@)
        list(APPEND MYPROG_LIBRARIES @CMAKE_INSTALL_PREFIX@/lib/libmyprog_${lib}.so)
    endforeach()