Search code examples
installationcmakegenerator-expression

Conditional exclusion of installed file in CMake


I need to exclude a file when installing directory in CMake if the condition is met. So I need something like this:

install(
    DIRECTORY include
    COMPONENT dev
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    PATTERN $<$<NOT:$<STREQUAL:${MY_VAR},SOME_VALUE>>:my_header.h> EXCLUDE
)

But unfortunately it doesn't work


Solution

  • Generator expressions are not available in install() rules. But since yours doesn't actually depend on any generator-time information, anyway, you can use CMake's argument expansion to accomplish this.

    if (MY_VAR STREQUAL "SOME_VALUE")
        set(exclusion_patterns "")
    else ()
        set(exclusion_patterns PATTERN my_header.h EXCLUDE)
    endif ()
    
    install(
        DIRECTORY include
        COMPONENT dev
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
        ${exclusion_patterns}
    )