CMake中define_property的使用

      CMake中的define_property命令用于定义和记录自定义属性,其格式如下:

define_property(<GLOBAL | DIRECTORY | TARGET | SOURCE |
                 TEST | VARIABLE | CACHED_VARIABLE>
                 PROPERTY <name> [INHERITED]
                 [BRIEF_DOCS <brief-doc> [docs...]]
                 [FULL_DOCS <full-doc> [docs...]]
                 [INITIALIZE_FROM_VARIABLE <variable>])

      在作用域内定义一个属性以与set_property和get_property命令一起使用它主要用于定义属性初始化或继承(initialized or inherited)的方式。从历史上看,该命令还将文档(documentation )与属性关联,但这不再被视为主要用例。
      第一个参数决定了该属性被使用的作用域类型(kind of scope)。它必须是以下之一:

GLOBAL    = associated with the global namespace
DIRECTORY = associated with one directory
TARGET    = associated with one target
SOURCE    = associated with one source file
TEST      = associated with a test named with add_test
VARIABLE  = documents a CMake language variable
CACHED_VARIABLE = documents a CMake cache variable

      注意:与set_property和get_property命令不同,不需要给出实际作用域;只有作用域类型是重要的。
      必需的PROPERTY选项紧跟要定义的属性的名称。
      如果给出了INHERITED选项,则当请求的属性(requested property)未在给定命令的作用域内设置时,get_property命令将链接到下一个更高的作用域。
      注意:此作用域链接行为仅适用于对get_property, get_directory_property, get_target_property, get_source_file_property和get_test_property命令的调用。设置属性时没有继承行为,因此将APPEND或APPEND_STRING和set_property命令一起使用在计算要追加到的内容时不会考虑继承的值。
      BRIEF_DOCS和FULL_DOCS选项后跟与属性关联的字符串,作为其简短和完整的文档(brief and full documentation).CMake不使用此文档,只是通过get_property命令的相应选项将其提供给项目(project).
      INITIALIZE_FROM_VARIABLE选项指定初始化属性的变量。它只能用于target属性。<variable>名称必须以属性名称结尾,并且不能以CMAKE_或_CMAKE_开头。属性名称必须至少包含一个下划线。建议属性名称具有特定于项目的前缀。

# 3.23版本BRIEF_DOCS和FULL_DOCS选项是可选的
define_property(GLOBAL PROPERTY pro_global
    BRIEF_DOCS "A test property"
    FULL_DOCS "A long description of this test property"
)

if(pro_global)
    message("define property") # won't print
endif()

get_property(global_result GLOBAL PROPERTY pro_global DEFINED)
if(global_result)
    message("global property: ${global_result}") # global property: 1
endif()

define_property(TARGET PROPERTY pro_target
    BRIEF_DOCS "A test property"
    FULL_DOCS "A long description of this test property"
)

get_property(target_result TARGET PROPERTY pro_target DEFINED)
if(target_result)
    message("target property: ${target_result}") # target property: 1

    include_directories(include)
    add_library(add STATIC source/add.cpp)
    set_target_properties(add PROPERTIES pro_target xxxx)
    get_target_property(var add pro_target)
    message("var: ${var}") # var: xxxx
endif()

      执行测试代码需要多个文件

      build.sh内容如下:

#! /bin/bash

# supported input parameters(cmake commands)
params=(function macro cmake_parse_arguments \
		find_library find_path find_file find_program find_package \
		cmake_policy cmake_minimum_required project include \
		string list set foreach message option if while return \
		math file configure_file \
		include_directories add_executable add_library target_link_libraries install \
		target_sources add_custom_command add_custom_target \
		add_subdirectory aux_source_directory \
		set_property set_target_properties define_property \
		add_definitions target_compile_definitions)

usage()
{
	echo "Error: $0 needs to have an input parameter"

	echo "supported input parameters:"
	for param in ${params[@]}; do
		echo "  $0 ${param}"
	done

	exit -1
}

if [ $# != 1 ]; then
	usage
fi

flag=0
for param in ${params[@]}; do
	if [ $1 == ${param} ]; then
		flag=1
		break
	fi
done

if [ ${flag} == 0 ]; then
	echo "Error: parameter \"$1\" is not supported"
	usage
	exit -1
fi

if [[ ! -d "build" ]]; then
	mkdir build
	cd build
else
	cd build
fi

echo "==== test $1 ===="

# test_set.cmake: cmake -DTEST_CMAKE_FEATURE=$1 --log-level=verbose ..
# test_option.cmake: cmake -DTEST_CMAKE_FEATURE=$1 -DBUILD_PYTORCH=ON ..
cmake -DTEST_CMAKE_FEATURE=$1 ..
# It can be executed directly on the terminal, no need to execute build.sh, for example: cmake -P test_set.cmake
make
# make install # only used in cmake files with install command

      CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.22)
project(cmake_feature_usage)

message("#### current cmake version: ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")
include(test_${TEST_CMAKE_FEATURE}.cmake)
message("==== test finish ====")

      test_define_property.cmake内容为上面的所有测试代码段

      另外还包括三个目录:include,source,samples,它们都是非常简单的实现,仅用于测试,如下:

      可能的执行结果如下图所示:

 

      GitHub: https://github.com/fengbingchun/Linux_Code_Test

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值