Cmake c根据生成/安装生成可变内容
使用Cmake c根据生成/安装生成可变内容,cmake,cmake-modules,Cmake,Cmake Modules,使用$和$生成器表达式,我可以根据目标是在当前生成目录中导出还是全局安装,将目标属性设置为不同的值。我正在编写一个自定义宏来附带我的CMake包和目标,并希望根据宏的导出位置(在构建目录中)或安装位置,使宏的行为有所不同。宏包含在-config.cmake文件中的-macros.cmake.in中,该文件包含在我的-config.cmake文件中,使用configure_文件将其配置到生成目录中,并在以后安装。我尝试使用configure\u file命令在变量集中使用生成器表达式,但显然它们不
$
和$
生成器表达式,我可以根据目标是在当前生成目录中导出还是全局安装,将目标属性设置为不同的值。我正在编写一个自定义宏来附带我的CMake包和目标,并希望根据宏的导出位置(在构建目录中)或安装位置,使宏的行为有所不同。宏包含在-config.cmake
文件中的-macros.cmake.in
中,该文件包含在我的-config.cmake
文件中,使用configure_文件将其配置到生成目录中,并在以后安装。我尝试使用configure\u file
命令在变量集中使用生成器表达式,但显然它们不打算这样工作。我假设我的需求并不罕见,通常使用CMake是如何实现的?只需为export()
和安装(导出)
创建不同的-config.CMake
文件即可。在这些文件中,您可能有一个变量来区分它们
您甚至可以使用带有不同CMake环境(变量)的configure_file
命令从同一模式创建两个文件:
-config.cmake.in:
set(IS_BUILD_INTERFACE @IS_BUILD_INTERFACE@)
# other commands, inclusion of other files, etc.
if(IS_BUILD_INTERFACE)
# Part of build interface
else()
# Part of install interface
endif()
# Prepare the file for build interface exporting
set(IS_BUILD_INTERFACE ON)
configure_file(<package>-config.cmake.in <package>-config.cmake @ONLY)
export(PACKAGE <package>)
# Prepare the file for install interface exporting
set(IS_BUILD_INTERFACE OFF)
configure_file(<package>-config.cmake.in <package>-config.cmake.install @ONLY)
install(FILES <package>-config.cmake.install DESTINATION cmake)
-macros.cmake:
set(IS_BUILD_INTERFACE @IS_BUILD_INTERFACE@)
# other commands, inclusion of other files, etc.
if(IS_BUILD_INTERFACE)
# Part of build interface
else()
# Part of install interface
endif()
# Prepare the file for build interface exporting
set(IS_BUILD_INTERFACE ON)
configure_file(<package>-config.cmake.in <package>-config.cmake @ONLY)
export(PACKAGE <package>)
# Prepare the file for install interface exporting
set(IS_BUILD_INTERFACE OFF)
configure_file(<package>-config.cmake.in <package>-config.cmake.install @ONLY)
install(FILES <package>-config.cmake.install DESTINATION cmake)
CMakeLists.txt:
set(IS_BUILD_INTERFACE @IS_BUILD_INTERFACE@)
# other commands, inclusion of other files, etc.
if(IS_BUILD_INTERFACE)
# Part of build interface
else()
# Part of install interface
endif()
# Prepare the file for build interface exporting
set(IS_BUILD_INTERFACE ON)
configure_file(<package>-config.cmake.in <package>-config.cmake @ONLY)
export(PACKAGE <package>)
# Prepare the file for install interface exporting
set(IS_BUILD_INTERFACE OFF)
configure_file(<package>-config.cmake.in <package>-config.cmake.install @ONLY)
install(FILES <package>-config.cmake.install DESTINATION cmake)
#准备用于生成接口导出的文件
设置(是否打开了构建接口)
配置_文件(-config.cmake.in-config.cmake@ONLY)
出口(包装)
#准备用于导出安装接口的文件
设置(构建接口是否关闭)
配置_文件(-config.cmake.in-config.cmake.install@ONLY)
安装(文件-config.cmake.install-DESTINATION-cmake)