如何在CMake中的ExternalProject_Add命令之间添加IF ESLE ENDIF

q43xntqr  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(167)

有没有办法在ExternalProject_Add行之间添加IF ELSE ENDIF?例如

ExternalProject_Add(my_lib
    URL "https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.zip"
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
IF(WIN32)
        # Some Commands Here #
ELSE()
        # Another Some Commands Here #
ENDIF(WIN32)
)

由于上面的行,您会看到一些错误。
或者我必须为每个条件创建不同的命令?

tyky79it

tyky79it1#

使用临时变量.....

if(WIN32)
   set(ARGS some arguments)
else()
   set(ARGS other arguments)
endif()
ExternalProject_Add(my_lib
    URL "https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.zip"
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
    ${ARGS}
)
km0tfn4u

km0tfn4u2#

如果您需要处理多个目标系统,则可以引入名称中包含CMAKE_SYSTEM_NAME的变量,并在命令中引用合适的变量:

set(ARGS_Windows some arguments)
set(ARGS_Linux   other arguments)
set(ARGS_Darwin  other other arguments)

ExternalProject_Add(my_lib
    URL "https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.zip"
    CMAKE_ARGS
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
    ${ARGS_${CMAKE_SYSTEM_NAME}} # evaluates to the var value corresponding to the target system
)

相关问题