该文件在src/pvsb/superbuild/cmake文件中中,其中定义一系列用于配置构建的相关宏和函数。
首先检查是否设定了构建类型,然后检查构建类型是否符合要求。
superbuild只接受Release以及RelWithDebInfo两种类型。
get_property(multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
# TODO: In 3.9.0, use the GENERATOR_IS_MULTI_CONFIG global property.
if (NOT multi_config)
set(_superbuild_build_type_force)
if (NOT CMAKE_BUILD_TYPE)
set(_superbuild_build_type_force FORCE)
endif ()
# Default to a Release build.
set(CMAKE_BUILD_TYPE "Release"
CACHE STRING "The build mode" ${_superbuild_build_type_force})
mark_as_advanced(CMAKE_BUILD_TYPE)
set_property(CACHE CMAKE_BUILD_TYPE
PROPERTY
STRINGS "Release;RelWithDebInfo")
if (NOT WIN32)
# Windows debug builds is not really supported at the moment. Getting all
# projects to agree on the C++ runtime library is not easy. Also, many
# projects hard-code library paths and on Windows, many projects change
# their names for debug builds.
set_property(CACHE CMAKE_BUILD_TYPE APPEND
PROPERTY
STRINGS "Debug")
endif ()
if (NOT CMAKE_BUILD_TYPE)
message(FATAL_ERROR "A build type (CMAKE_BUILD_TYPE) must be set.")
endif ()
# Ensure that the chosen build type is valid.
get_property(build_type_options
CACHE CMAKE_BUILD_TYPE
PROPERTY STRINGS)
if (NOT CMAKE_BUILD_TYPE IN_LIST build_type_options)
string(REPLACE ";" ", " build_type_options "${build_type_options}")
message(FATAL_ERROR "CMAKE_BUILD_TYPE must be one of: ${build_type_options} (found ${CMAKE_BUILD_TYPE}).")
endif ()
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
if (SUPERBUILD_ALLOW_DEBUG)
message(WARNING
"Debug builds are probably not what you want. This is an unsupported "
"configuration. Please consider using the CMAKE_BUILD_TYPE_ "
"options if you are debugging specific projects.")
else ()
message(FATAL_ERROR
"Debug builds are probably not what you want. Set the "
"SUPERBUILD_ALLOW_DEBUG variable using either the GUI ('Add Entry') "
"or pass -DSUPERBUILD_ALLOW_DEBUG:BOOL=ON on the command to indicate "
"this is what you intended.")
endif ()
endif ()
endif ()
superbuild只支持64位构建
This functiond detects 32-bit targets and errors if it occurs.
#]==]
# 只能进行64位构建
function (superbuild_detect_64bit_target)
# 判断是否为交叉编译,如果是,直接退出
if (CMAKE_CROSSCOMPILING)
return ()
endif ()
# Collect information about the build platform.
include(CheckTypeSize)
check_type_size(void* void_ptr_size
BUILTIN_TYPES_ONLY)
if (void_ptr_size EQUAL 8)
# OK
elseif (void_ptr_size EQUAL 4)
message(FATAL_ERROR
"32-bit targets are not supported.")
else ()
if (WIN32)
set(extra_message "Are you in a Visual Studio command prompt?")
else ()
set(extra_message "Do you have working compilers?")
endif ()
message(FATAL_ERROR
"Failed to determine whether the target architecture is 32bit or 64bit. "
"${extra_message}")
endif ()
endfunction ()
# A utility function to create a PATH-link environment variable value.
macro (_superbuild_make_path_var var)
set(${var} ${ARGN}) # 将参数列表之外的变量赋值给 var
list(REMOVE_ITEM ${var} "")
if (UNIX)
# 将 ${${var}} 中 ";"替换成":",保存在${var}中
string(REPLACE ";" ":" ${var} "${${var}}")
endif ()
endmacro ()
superbuild可以通过标准环境变量设置添加附加语言标志,以便在项目构建期间对项目可用。该函数通过在配置时集成环境(superbuild的项目安装目录)来处理项目可用标志的设置。
function (superbuild_setup_flags)
if (WIN32) # WIN32直接退出
return ()
endif ()
_superbuild_make_path_var(superbuild_ld_library_path
"${superbuild_install_location}/lib"
"$ENV{LD_LIBRARY_PATH}")
set(superbuild_ld_library_path "${superbuild_ld_library_path}" PARENT_SCOPE)
_superbuild_make_path_var(superbuild_pkg_config_path
"${superbuild_install_location}/lib/pkgconfig"
"${superbuild_install_location}/share/pkgconfig"
"$ENV{PKG_CONFIG_PATH}")
set(superbuild_pkg_config_path "${superbuild_pkg_config_path}" PARENT_SCOPE)
if (CMAKE_CROSSCOMPILING)
return ()
endif ()
set(superbuild_cpp_flags "$ENV{CPPFLAGS} ${superbuild_extra_cpp_flags}")
set(superbuild_cxx_flags "$ENV{CXXFLAGS} -fPIC ${superbuild_extra_cxx_flags}")
set(superbuild_c_flags "$ENV{CFLAGS} -fPIC ${superbuild_extra_c_flags}")
set(superbuild_f_flags "$ENV{FFLAGS} -fPIC ${superbuild_extra_f_flags}")
set(superbuild_ld_flags "$ENV{LDFLAGS} ${superbuild_extra_ld_flags}")
superbuild_osx_add_version_flags()
foreach (var IN ITEMS cpp_flags cxx_flags c_flags f_flags ld_flags)
set("superbuild_${var}"
"${superbuild_${var}}"
PARENT_SCOPE)
endforeach ()
endfunction ()
创建项目安装文件夹,为构建项目做准备
macro (superbuild_prepare_build_tree)
if (WIN32)
# Windows doesn't like it if that directory does not exist even if it is
# empty.
file(MAKE_DIRECTORY "${superbuild_install_location}/lib")
endif ()
set(_superbuild_module_gen_dir "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/cmake")
file(MAKE_DIRECTORY "${_superbuild_module_gen_dir}")
list(APPEND CMAKE_MODULE_PATH
"${_superbuild_module_gen_dir}")
endmacro ()
该函数用来处理变量值的变动,检查一个变量是否存在,如果存在,将${${old}}赋值给default。
function (superbuild_deprecated_setting output_default new old intended_default)
set(default "${intended_default}")
if (DEFINED "${old}")
message(DEPRECATION
"The `${old}` variable is deprecated for `${new}`.")
set(default "${${old}}")
endif ()
set("${output_default}" "${default}" PARENT_SCOPE)
endfunction ()
通用超级构建旨在用作项目的子模块。 然而,在更新主超级构建时更新子模块是一个容易忘记的步骤。该函数试图确定公共超级构建是否是最新的,如果不是,则出错。
首先检查是否位于git仓库中
function (_superbuild_check_up_to_date)
# 推断出"${CMAKE_CURRENT_SOURCE_DIR}"相对于"${CMAKE_SOURCE_DIR}"的路径
file(RELATIVE_PATH common_superbuild_path
"${CMAKE_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}")
if (common_superbuild_path MATCHES "^\\.\\./")
# We're in a project shipped by the common-superbuild.
return ()
endif ()
# 使用git进行版本检查
find_package(Git QUIET)
if (NOT Git_FOUND)
# No Git; can't perform the check.
return ()
endif ()
# 检查是否位于git仓库中,是则返回true,否则返回false
zyh("GIT_EXECUTABLE=${GIT_EXECUTABLE}")
execute_process(
COMMAND "${GIT_EXECUTABLE}"
rev-parse
--is-inside-work-tree
RESULT_VARIABLE res
OUTPUT_VARIABLE out
ERROR_VARIABLE err
WORKING_DIRECTORY
"${CMAKE_SOURCE_DIR}"
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE)
if (res)
# We're not in a git repository; assume all is well.
return ()
endif ()
set(out_of_date
"The common superbuild may be out of date, but cannot be verified.")
# 列出书对象的内容
execute_process(
COMMAND "${GIT_EXECUTABLE}"
ls-tree
HEAD
--
"${common_superbuild_path}"
RESULT_VARIABLE res
OUTPUT_VARIABLE out
ERROR_VARIABLE err
WORKING_DIRECTORY
"${CMAKE_SOURCE_DIR}"
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE)
zyh("out=${out}")
if (res)
message(WARNING
"Failed to get information about the common superbuild from the "
"containing repository. ${out_of_date}: ${err}")
return ()
endif ()
if (NOT out MATCHES "^160000 ")
# The superbuild is not a submodule; assume all is well.
# 进入这里了,所以后面的都没执行
return ()
endif ()
string(REGEX REPLACE "^160000 commit \([a-f0-9]+\)\t.*$" "\\1"
expected_commit "${out}")
execute_process(
COMMAND "${GIT_EXECUTABLE}"
rev-parse
HEAD
RESULT_VARIABLE res
OUTPUT_VARIABLE actual_commit
ERROR_VARIABLE err
WORKING_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE)
if (res)
message(WARNING
"Failed to get the current commit of the common superbuild. "
"${out_of_date}: ${err}")
return ()
endif ()
execute_process(
COMMAND "${GIT_EXECUTABLE}"
merge-base
--is-ancestor
"${expected_commit}"
"${actual_commit}"
RESULT_VARIABLE res
OUTPUT_VARIABLE out
ERROR_VARIABLE err
WORKING_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE)
if (res EQUAL 1)
set(severity FATAL_ERROR)
if (superbuild_can_be_out_of_date)
set(severity WARNING)
endif ()
message("${severity}"
"The common superbuild is out of date and should be updated. Please run "
"`git submodule update` in the source directory to update.")
elseif (res)
message(WARNING
"Failed to determine if the common superbuild is an old checkout. "
"${out_of_date}: ${err}")
endif ()
endfunction ()