Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I block or skip add_executable from third party repos? CMake FetchContent_Declare

Tags:

github

cmake

    enable_testing()
    include(FetchContent)
    FetchContent_Declare(
            ut
            GIT_REPOSITORY https://github.com/boost-ext/ut.git
            GIT_TAG        v1.1.8
    )
    #FetchContent_MakeAvailable(ut)
    FetchContent_GetProperties(ut)
    if(NOT ut_POPULATED)
        FetchContent_Populate(ut)
        add_subdirectory(${ut_SOURCE_DIR} ${ut_BINARY_DIR} EXCLUDE_FROM_ALL)
    endif()

When including 3rd party repos using cmake, the list in clion shows all the executables and tests. Though discord suggested using EXCLUDE_FROM_ALL which helps. As it prevents these extra things from building when I build all.

What I want is just the libraries. Skip over add_executable lines in the third party cmake files.

If I have an executable named the same as them it'll show an error, example:

add_executable cannot create target "ut_1" because another target with the same name already exists. The existing target is an executable created in source directory

Blockquote

like image 533
Robert Russell Avatar asked Oct 20 '25 10:10

Robert Russell


1 Answers

No, sadly, this is a fundamental limitation with the FetchContent approach. CMake offers no ways to remove targets once they've been added, and FetchContent is ultimately a call to add_subdirectory, which has no way of distinguishing between first-party and third-party targets.

Your only recourse is to patch the third-party build. You can do this by forking the project, trying to use the glitchy PATCH_COMMAND option, or by working with upstream to namespace their targets.

When designing libraries to be consumed by FetchContent, one should always use a <ProjName>_ prefix (or just <ProjName>).

like image 122
Alex Reinking Avatar answered Oct 22 '25 04:10

Alex Reinking



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!