286 lines
7.8 KiB
CMake
286 lines
7.8 KiB
CMake
include_guard(GLOBAL)
|
|
|
|
|
|
function(cbl_get_target_prop target prop out_var)
|
|
get_property(_has_prop TARGET "${target}" PROPERTY "${prop}" SET)
|
|
|
|
if(_has_prop)
|
|
get_property(_value TARGET "${target}" PROPERTY "${prop}")
|
|
else()
|
|
message(FATAL_ERROR "target[${target}] 找不到属性[${prop}]")
|
|
endif()
|
|
|
|
set("${out_var}" "${_value}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
|
|
function(cbl_get_target_prop_create target prop out_var)
|
|
get_property(_has_prop TARGET "${target}" PROPERTY "${prop}" SET)
|
|
|
|
if(_has_prop)
|
|
get_property(_value TARGET "${target}" PROPERTY "${prop}")
|
|
else()
|
|
set(_value "")
|
|
endif()
|
|
|
|
set("${out_var}" "${_value}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
|
|
function(cbl_list_find_duplicates list_var out_var)
|
|
set(_seen "")
|
|
set(_dups "")
|
|
|
|
foreach(_item IN LISTS ${list_var})
|
|
list(FIND _seen "${_item}" _idx)
|
|
|
|
if(_idx EQUAL -1)
|
|
list(APPEND _seen "${_item}")
|
|
else()
|
|
list(APPEND _dups "${_item}")
|
|
endif()
|
|
endforeach()
|
|
|
|
if(_dups)
|
|
list(REMOVE_DUPLICATES _dups)
|
|
endif()
|
|
|
|
set("${out_var}" "${_dups}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
|
|
function(cbl_escape_param_value value out_var)
|
|
set(_value "${value}")
|
|
|
|
string(REPLACE "\r" "\\r" _value "${_value}")
|
|
string(REPLACE "\n" "\\n" _value "${_value}")
|
|
|
|
set("${out_var}" "${_value}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
|
|
macro(cbl_require_registered_prop target prop_name)
|
|
endmacro()
|
|
|
|
|
|
function(cbl_append_list_prop_create target prop)
|
|
cbl_get_target_prop_create("${target}" "${prop}" _cbl_temp_list)
|
|
|
|
foreach(_cbl_item IN LISTS ARGN)
|
|
if("${_cbl_item}" STREQUAL "")
|
|
continue()
|
|
endif()
|
|
|
|
list(APPEND _cbl_temp_list "${_cbl_item}")
|
|
endforeach()
|
|
|
|
if(_cbl_temp_list)
|
|
list(REMOVE_DUPLICATES _cbl_temp_list)
|
|
endif()
|
|
|
|
set_property(
|
|
TARGET "${target}"
|
|
PROPERTY "${prop}"
|
|
"${_cbl_temp_list}"
|
|
)
|
|
endfunction()
|
|
|
|
|
|
function(cbl_append_str_prop target prop)
|
|
cbl_get_target_prop("${target}" "${prop}" _cbl_old_text)
|
|
|
|
string(JOIN "" _cbl_append_text ${ARGN})
|
|
|
|
set_property(
|
|
TARGET "${target}"
|
|
PROPERTY "${prop}"
|
|
"${_cbl_old_text}${_cbl_append_text}"
|
|
)
|
|
endfunction()
|
|
|
|
|
|
macro(cbl_get_props_ex ctx_target prop_name_list_var_name)
|
|
foreach(prop_name IN LISTS ${prop_name_list_var_name})
|
|
if("${prop_name}" STREQUAL "")
|
|
continue()
|
|
endif()
|
|
|
|
cbl_get_target_prop("${ctx_target}" "${prop_name}" "${prop_name}")
|
|
endforeach()
|
|
endmacro()
|
|
|
|
|
|
function(cbl_register_prop_names_ex ctx_target collect_prop_name prop_name_list_var_name)
|
|
foreach(prop_name IN LISTS ${prop_name_list_var_name})
|
|
if("${prop_name}" STREQUAL "")
|
|
continue()
|
|
endif()
|
|
|
|
if(NOT "${collect_prop_name}" STREQUAL "")
|
|
cbl_append_list_prop_create("${ctx_target}" "${collect_prop_name}" "${prop_name}")
|
|
endif()
|
|
endforeach()
|
|
endfunction()
|
|
|
|
|
|
function(cbl_register_prop_values_ex ctx_target collect_prop_name prop_value_list_var_name)
|
|
foreach(_cbl_item IN LISTS ${prop_value_list_var_name})
|
|
string(FIND "${_cbl_item}" "=" _cbl_eq_pos)
|
|
|
|
if(_cbl_eq_pos EQUAL -1)
|
|
message(FATAL_ERROR
|
|
"invalid property item, expected key=value\n"
|
|
" target: ${ctx_target}\n"
|
|
" item: ${_cbl_item}"
|
|
)
|
|
endif()
|
|
|
|
if(_cbl_eq_pos EQUAL 0)
|
|
message(FATAL_ERROR
|
|
"empty property name\n"
|
|
" target: ${ctx_target}\n"
|
|
" item: ${_cbl_item}"
|
|
)
|
|
endif()
|
|
|
|
string(SUBSTRING "${_cbl_item}" 0 ${_cbl_eq_pos} prop_name)
|
|
|
|
math(EXPR _cbl_value_pos "${_cbl_eq_pos} + 1")
|
|
string(SUBSTRING "${_cbl_item}" ${_cbl_value_pos} -1 prop_value)
|
|
|
|
string(STRIP "${prop_name}" prop_name)
|
|
|
|
if("${prop_name}" STREQUAL "")
|
|
message(FATAL_ERROR
|
|
"empty property name\n"
|
|
" target: ${ctx_target}\n"
|
|
" item: ${_cbl_item}"
|
|
)
|
|
endif()
|
|
|
|
set_property(
|
|
TARGET "${ctx_target}"
|
|
PROPERTY "${prop_name}"
|
|
"${prop_value}"
|
|
)
|
|
|
|
if(NOT "${collect_prop_name}" STREQUAL "")
|
|
cbl_append_list_prop_create("${ctx_target}" "${collect_prop_name}" "${prop_name}")
|
|
endif()
|
|
endforeach()
|
|
endfunction()
|
|
|
|
|
|
function(cbl_register_props_ex ctx_target collect_prop_name var_name_list_var_name)
|
|
foreach(var_name IN LISTS ${var_name_list_var_name})
|
|
if("${var_name}" STREQUAL "")
|
|
message(FATAL_ERROR "${ctx_target} 空属性(${collect_prop_name})??")
|
|
endif()
|
|
|
|
if(NOT "${collect_prop_name}" STREQUAL "")
|
|
cbl_append_list_prop_create("${ctx_target}" "${collect_prop_name}" "${var_name}")
|
|
endif()
|
|
|
|
set_property(
|
|
TARGET "${ctx_target}"
|
|
PROPERTY "${var_name}"
|
|
"${${var_name}}"
|
|
)
|
|
endforeach()
|
|
endfunction()
|
|
|
|
|
|
function(cbl_set_props_ex ctx_target var_name_list_var_name)
|
|
foreach(var_name IN LISTS ${var_name_list_var_name})
|
|
if("${var_name}" STREQUAL "")
|
|
continue()
|
|
endif()
|
|
set_property(
|
|
TARGET "${ctx_target}"
|
|
PROPERTY "${var_name}"
|
|
"${${var_name}}"
|
|
)
|
|
endforeach()
|
|
endfunction()
|
|
|
|
|
|
# 外部调用:读取属性值到同名变量
|
|
macro(cbl_get_props ctx_target)
|
|
set(_cbl_get_props_list "${ARGN}")
|
|
cbl_get_props_ex("${ctx_target}" _cbl_get_props_list)
|
|
endmacro()
|
|
|
|
|
|
# 外部调用:只注册属性名,不设置属性值
|
|
macro(cbl_register_prop_names ctx_target collect_prop_name)
|
|
set(_cbl_register_prop_names_list "${ARGN}")
|
|
cbl_register_prop_names_ex("${ctx_target}" "${collect_prop_name}" _cbl_register_prop_names_list)
|
|
endmacro()
|
|
|
|
|
|
# 外部调用:注册属性名,并直接用 key=value 设置初始值
|
|
# 空值:
|
|
# key=
|
|
#
|
|
# value 里面可以包含 =
|
|
# path=D:/a=b/c
|
|
#
|
|
# 如果 value 是 CMake list,并且直接写在调用参数里,需要转义分号:
|
|
# "libs=brpc\;protobuf\;gflags"
|
|
macro(cbl_register_prop_values ctx_target collect_prop_name)
|
|
set(_cbl_prop_value_items "${ARGN}")
|
|
cbl_register_prop_values_ex("${ctx_target}" "${collect_prop_name}" _cbl_prop_value_items)
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(cbl_register_props ctx_target collect_prop_name)
|
|
set(_cbl_register_props_list "${ARGN}")
|
|
cbl_register_props_ex("${ctx_target}" "${collect_prop_name}" _cbl_register_props_list)
|
|
endmacro()
|
|
|
|
|
|
macro(cbl_set_props ctx_target)
|
|
set(_cbl_set_props_list "${ARGN}")
|
|
cbl_set_props_ex("${ctx_target}" _cbl_set_props_list)
|
|
endmacro()
|
|
|
|
|
|
function(cbl_write_params_file ctx_target param_name_list_prop output_file)
|
|
if("${param_name_list_prop}" STREQUAL "")
|
|
return()
|
|
endif()
|
|
|
|
if("${output_file}" STREQUAL "")
|
|
message(FATAL_ERROR "output_file must be provided to cbl_write_params_file")
|
|
endif()
|
|
|
|
cbl_get_target_prop("${ctx_target}" "${param_name_list_prop}" _param_name_list)
|
|
|
|
if(NOT _param_name_list)
|
|
return()
|
|
endif()
|
|
|
|
list(REMOVE_DUPLICATES _param_name_list)
|
|
|
|
set(_params_file_content "")
|
|
|
|
foreach(_param_name IN LISTS _param_name_list)
|
|
if("${_param_name}" STREQUAL "")
|
|
continue()
|
|
endif()
|
|
|
|
cbl_get_target_prop("${ctx_target}" "${_param_name}" _param_value)
|
|
cbl_escape_param_value("${_param_value}" _param_value_escaped)
|
|
|
|
string(APPEND _params_file_content "${_param_name}=${_param_value_escaped}\n")
|
|
endforeach()
|
|
|
|
get_filename_component(_params_dir "${output_file}" DIRECTORY)
|
|
|
|
if(NOT "${_params_dir}" STREQUAL "")
|
|
file(MAKE_DIRECTORY "${_params_dir}")
|
|
endif()
|
|
|
|
file(WRITE "${output_file}" "${_params_file_content}")
|
|
endfunction() |