[CMake] How can I automatically optionally build a submodule?

Albrecht Schlosser AlbrechtS.fltk at online.de
Tue Mar 12 07:54:01 EDT 2019


On 12.03.2019 06:37 Steve Keller wrote:
> How can I build a module in a subdirectory automatically if a required
> package is available, but not fail if it's not. Say I have a
> top-level CMakeLists.txt with
> 
>      add_subdirectory(foo)
>      add_subdirectory(bar)
> 
> and in directory foo I have in CMakeLists.txt
> 
>      find_package(yadda, REQUIRED)
> 
> I want the submodule foo to be build automatically if the package
> yadda is found. Otherwise, foo should be left out and the top-level
> build should not fail but continue.

If you want yadda to be optional then don't use REQUIRED.

find_package(yadda)
if (yadda_FOUND)
   message(STATUS "found yadda, building bar ...")

   # add your code to build bar here

endif ()

This should do what you want - unless I misunderstood your question.



More information about the CMake mailing list