Robert Russell
Robert Russell

Reputation: 305

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

    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

Upvotes: 4

Views: 1554

Answers (1)

Alex Reinking
Alex Reinking

Reputation: 19866

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>).

Upvotes: 3

Related Questions