Search code examples
c++cmakemakefilebuildcompilation

How to fetch a CMake dependency without building the entire project from the CMakeLists.txt file?


I have the following problem with my project. I have a project structured in different subdirectories, but the important thing to know is that the main CMakeLists.txt file contains these lines:

...
add_subdirectory( deps )
...

where deps is the name of the directory containing the CMakeLists.txt for dealing with dependencies. In it, the relevant lines of code are:

include(FetchContent)

FetchContent_Declare(
   arsenalgear
   GIT_REPOSITORY "https://github.com/JustWhit3/arsenalgear-cpp"
   GIT_TAG main
)

message( STATUS "Fetching arsenalgear..." )
FetchContent_MakeAvailable( arsenalgear )

The problem is that FetchContent_MakeAvailable( arsenalgear ) will build the entire arsenalgear repository. I want to be able to perform only the installation part, not the whole build of tests and examples.

Is there any way to do this inside the deps/CMakeLists.txt? Thanks.


Solution

  • looking at this project we can see

    option( ARSENALGEAR_TESTS "Enable / disable tests." ON )
    

    src: https://github.com/JustWhit3/arsenalgear-cpp/blob/538a169d295d7ce8578419b0c96b1b0b4a845a11/CMakeLists.txt#L38

    so if you have CMP0077 set to NEW, then you could use:

    FetchContent_Declare(
       arsenalgear
       GIT_REPOSITORY "https://github.com/JustWhit3/arsenalgear-cpp"
       GIT_TAG main
    )
    set(ARSENALGEAR_TESTS OFF)
    FetchContent_MakeAvailable( arsenalgear )
    

    note: you can also Fetch in a subdirectory (i.e. creating a sub scope) so you can use set(BUILD_TESTING OFF)... while still having BUILD_TESTING set to ON (or whatever) for your own project...