243 lines
8.3 KiB
CMake
243 lines
8.3 KiB
CMake
include(FetchContent)
|
|
|
|
|
|
# 保存环境变量,自动用 _saved_<VAR_NAME> 变量保存
|
|
function(_save_env_var VAR_NAME)
|
|
set(_saved_var_name_ "__saved_${VAR_NAME}__")
|
|
if(DEFINED ENV{${VAR_NAME}})
|
|
set(${_saved_var_name_} "$ENV{${VAR_NAME}}" PARENT_SCOPE)
|
|
else()
|
|
set(${_saved_var_name_} "" PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
# 恢复环境变量,自动读取 _saved_<VAR_NAME> 变量
|
|
function(_restore_env_var VAR_NAME)
|
|
# 用变量引用技巧取得保存的值
|
|
set(_saved_var_name_ "__saved_${VAR_NAME}__")
|
|
if(DEFINED ${_saved_var_name_} AND NOT "${${_saved_var_name_}}" STREQUAL "")
|
|
set(ENV{${VAR_NAME}} "${${_saved_var_name_}}")
|
|
else()
|
|
unset(ENV{${VAR_NAME}})
|
|
endif()
|
|
endfunction()
|
|
|
|
|
|
function(_find_candidate_dir name candidate_dir_list_name ret_name)
|
|
# 在候选目录里 查找出来目录
|
|
list(LENGTH ${candidate_dir_list_name} list_length)
|
|
if(list_length EQUAL 0)
|
|
message(FATAL_ERROR "${candidate_dir_list_name} The directory list is empty.")
|
|
endif()
|
|
foreach(candidate_dir IN LISTS ${candidate_dir_list_name})
|
|
if(IS_DIRECTORY "${candidate_dir}/${name}") # 判断目录是否存在
|
|
set(${ret_name} "${candidate_dir}" PARENT_SCOPE) # 将 src 的值传递回 ret_name
|
|
return()
|
|
endif()
|
|
endforeach()
|
|
list(GET ${candidate_dir_list_name} 0 candidate_dir) # 获取列表中的第一个目录
|
|
set(${ret_name} "${candidate_dir}" PARENT_SCOPE) # 将第一个目录设置为 ret_name
|
|
endfunction()
|
|
|
|
|
|
|
|
|
|
function(cmake_escape_for_quoted_string INPUT_VAR OUTPUT_VAR)
|
|
set(_s "${${INPUT_VAR}}")
|
|
|
|
# 反斜杠必须先转义
|
|
string(REPLACE "\\" "\\\\" _s "${_s}")
|
|
|
|
# 双引号转义
|
|
string(REPLACE "\"" "\\\"" _s "${_s}")
|
|
|
|
# 美元符号转义,避免生成后的 CMakeLists.txt 再展开 ${xxx}
|
|
string(REPLACE "$" "\\$" _s "${_s}")
|
|
|
|
# 分号转义,避免被当成 CMake list 分隔符
|
|
string(REPLACE ";" "\\;" _s "${_s}")
|
|
|
|
set(${OUTPUT_VAR} "${_s}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
|
|
|
|
|
|
|
|
#配置为:./configure
|
|
#--prefix=/home/alientek/ATK-DLRK3568/buildroot/output/rockchip_rk3568/host
|
|
#--sysconfdir=/home/alientek/ATK-DLRK3568/buildroot/output/rockchip_rk3568/host/etc
|
|
#--enable-static
|
|
#--target=aarch64-buildroot-linux-gnu
|
|
#--with-sysroot=/home/alientek/ATK-DLRK3568/buildroot/output/rockchip_rk3568/host/aarch64-buildroot-linux-gnu/sysroot
|
|
#--enable-__cxa_atexit
|
|
#--with-gnu-ld --disable-libssp
|
|
#--disable-multilib
|
|
#--disable-decimal-float
|
|
#--with-gmp=/home/alientek/ATK-DLRK3568/buildroot/output/rockchip_rk3568/host
|
|
#--with-mpc=/home/alientek/ATK-DLRK3568/buildroot/output/rockchip_rk3568/host
|
|
#--with-mpfr=/home/alientek/ATK-DLRK3568/buildroot/output/rockchip_rk3568/host
|
|
#--with-pkgversion='Buildroot 2018.02-rc3-g30e3a128'
|
|
#--with-bugurl=http://bugs.buildroot.net/
|
|
#--without-zstd --disable-libquadmath
|
|
#--disable-libquadmath-support
|
|
#--enable-tls --enable-threads
|
|
#--without-isl --without-cloog
|
|
#--with-abi=lp64
|
|
#--with-cpu=cortex-a55
|
|
#--enable-languages=c,c++
|
|
#--with-build-time-tools=/home/alientek/ATK-DLRK3568/buildroot/output/rockchip_rk3568/host/aarch64-buildroot-linux-gnu/bin
|
|
#--enable-shared
|
|
#--disable-libgomp
|
|
function(_remove_trailing_colon_from_list list_name)
|
|
# 获取列表的长度
|
|
list(LENGTH ${list_name} list_length)
|
|
# 如果列表非空,检查尾部元素是否是冒号
|
|
if(list_length GREATER 0)
|
|
# 将 list_length 转换为整数
|
|
math(EXPR last_index "${list_length} - 1")
|
|
# 获取列表的最后一个元素
|
|
list(GET ${list_name} ${last_index} last_element)
|
|
# 如果尾部是冒号,移除它
|
|
if(last_element STREQUAL ":")
|
|
list(REMOVE_AT ${list_name} ${last_index})
|
|
endif()
|
|
endif()
|
|
endfunction()
|
|
|
|
|
|
# 定义函数用于转义字符串中的特殊字符
|
|
function(escape_string var_name)
|
|
# 获取原始字符串
|
|
set(original_string "${${var_name}}")
|
|
|
|
# 替换反斜杠 \ 为 \\
|
|
string(REPLACE "\\" "\\\\" escaped_string "${original_string}")
|
|
|
|
# 替换美元符号 $ 为 \$
|
|
string(REPLACE "\$" "\\\$" escaped_string "${escaped_string}")
|
|
|
|
# 替换双引号 " 为 \"
|
|
string(REPLACE "\"" "\\\"" escaped_string "${escaped_string}")
|
|
|
|
# 将转义后的字符串设置回原始变量
|
|
set(${var_name} "${escaped_string}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
|
|
|
|
set(IFS "@")
|
|
function(_register_gnu_project_ex _name_ gnu_url env_list_string)
|
|
set(src_dir)
|
|
_find_candidate_dir("" EXTERNAL_SRC_DIR src_dir)
|
|
|
|
set(cmd_list)
|
|
string(APPEND cmd_list "export _install_=${_system_build_tool_install_dir}")
|
|
string(APPEND cmd_list "${IFS}export _src_=${src_dir}")
|
|
string(APPEND cmd_list "${IFS}export _build_=${_system_build_tool_build_dir}")
|
|
|
|
#
|
|
# set(my_string [[\$$ORIGIN:\$$ORIGIN/../lib]])
|
|
#
|
|
# escape_string(my_string)
|
|
# escape_string(my_string)
|
|
# string(APPEND cmd_list "${IFS}export LDFLAGS=-Wl,-rpath=${my_string}")
|
|
#
|
|
|
|
set(list "${env_list_string}")
|
|
foreach(li IN LISTS list)
|
|
string(APPEND cmd_list "${IFS}${li}")
|
|
endforeach()
|
|
|
|
message("cmd_list[${_name_}]: ${cmd_list}")
|
|
|
|
set(configure_option_list "${ARGN}")
|
|
__call_bash("_gnu_${_name_}" "${CMAKE_SOURCE_DIR}/0_linux_pro/make/main.bash" "build_download_ex"
|
|
"${cmd_list}" "${_name_}" "${gnu_url}" ${configure_option_list})
|
|
endfunction()
|
|
|
|
|
|
|
|
function(__call_bash _target_name_ _bash_file_ _func_name_)
|
|
#set(cmd ". ${_bash_file_} && ${_func_name_} \"${ARGN}\" ")
|
|
set(cmd ". ${_bash_file_} && ${_func_name_} ")
|
|
foreach(arg IN LISTS ARGN)
|
|
string(APPEND cmd " \"${arg}\"")
|
|
# message("${_target_name_} arg: ${arg}")
|
|
endforeach()
|
|
|
|
|
|
if(WIN32)
|
|
set(COMMAND_PATH "C:/Program Files/Git/bin/bash.exe")
|
|
elseif(UNIX)
|
|
set(COMMAND_PATH "/bin/bash")
|
|
elseif(CYGWIN)
|
|
elseif(MSYS)
|
|
set(COMMAND_PATH "C:/msys64/usr/bin/bash.exe")
|
|
_save_env_var("MSYSTEM")
|
|
set(ENV{MSYSTEM} MINGW64)
|
|
endif()
|
|
|
|
message("[${_target_name_}]: ${COMMAND_PATH} -c -l ${cmd}")
|
|
|
|
|
|
add_custom_target(
|
|
${_target_name_}
|
|
COMMAND "${COMMAND_PATH}" -c "${cmd}"
|
|
#COMMAND "${COMMAND_PATH}" -c "${cmd}" | tee "./${_name_}.log"
|
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
VERBATIM
|
|
)
|
|
if(WIN32)
|
|
|
|
elseif(UNIX)
|
|
|
|
elseif(CYGWIN)
|
|
|
|
elseif(MSYS)
|
|
_restore_env_var("MSYSTEM")
|
|
endif()
|
|
endfunction()
|
|
|
|
#function(_register_gnu_project _name_ gnu_url)
|
|
# set(env_var_list)
|
|
# _find_candidate_dir("" EXTERNAL_SRC_DIR src_dir)
|
|
# string(APPEND env_var_list "_install_=${_system_build_tool_install_dir}:")
|
|
# string(APPEND env_var_list "_src_=${src_dir}:")
|
|
# string(APPEND env_var_list "_build_=${_system_build_tool_build_dir}:")
|
|
# string(APPEND env_var_list "CC=${CMAKE_C_COMPILER}:")
|
|
# string(APPEND env_var_list "CXX=${CMAKE_CXX_COMPILER}:")
|
|
# set(bin "/opt/atk-dlrk356x-toolchain/bin:/opt/atk-dlrk356x-toolchain/aarch64-buildroot-linux-gnu/bin")
|
|
# set(lib "/opt/atk-dlrk356x-toolchain/lib:/opt/atk-dlrk356x-toolchain/aarch64-buildroot-linux-gnu/lib")
|
|
# set(configure_option_list "${ARGN}")
|
|
# __call_bash("_gnu_${_name_}" "${CMAKE_SOURCE_DIR}/0_linux_pro/make/main.bash" "build_download_ex"
|
|
# ""
|
|
# ""
|
|
# "${env_var_list}" "${_name_}" "${gnu_url}" ${configure_option_list})
|
|
#endfunction()
|
|
|
|
|
|
function(_register_archive_project _name_ archive_url )
|
|
set(env_var_list)
|
|
_find_candidate_dir("" EXTERNAL_SRC_DIR src_dir)
|
|
string(APPEND env_var_list "_install_=${EXTERNAL_INSTALL_DIR}:")
|
|
string(APPEND env_var_list "_src_=${src_dir}:")
|
|
set(configure_option_list "${ARGN}")
|
|
__call_bash("__archive_${_name_}" "${CMAKE_SOURCE_DIR}/0_linux_pro/make/main.bash" "build_download_ex" "" "" "\"${env_var_list}\"" "${_name_}" "${archive_url}" ${configure_option_list})
|
|
endfunction()
|
|
|
|
function(_register_git_autotools_project _name_ git_url git_tag)
|
|
set(env_var_list)
|
|
_find_candidate_dir("" EXTERNAL_SRC_DIR src_dir)
|
|
list(APPEND env_var_list "_install_=${EXTERNAL_INSTALL_DIR}")
|
|
list(APPEND env_var_list "_src_=${src_dir}")
|
|
set(configure_option_list "${ARGN}")
|
|
__call_bash("__git_autotools_${_name_}" "${CMAKE_SOURCE_DIR}/0_linux_pro/make/main.bash" "build_git_ex" "" "" "\"${env_var_list}\"" "${_name_}" "${git_url}" "${git_tag}" ${configure_option_list})
|
|
endfunction()
|
|
|
|
|
|
|
|
|
|
#__call_bash(bbb "${CMAKE_SOURCE_DIR}/0_linux_pro/make/main.bash" "print" "222333" "3335566")
|
|
|