LidarView超级构建(二):pvsb/superbuild/CMakeLists.txt

该文详细介绍了如何使用CMake配置一个超级构建系统,包括设置编译器、管理依赖关系、配置编译顺序、源码获取方式以及处理多配置生成器的支持。它强调了使用Ninja作为构建工具,并涉及CMake模块路径、库属性、编译选项的设定,以及测试和打包过程。文章还提到了针对不同平台的项目配置和版本信息的管理。
摘要由CSDN通过智能技术生成

该cmake文件对整个superbuild进行配置,包括所有项目,依赖关系,根据依赖关系配置编译顺序,源码获取方式,编译器设置。

调用cmake时需要添加 -GNinja 命令,使用Ninja进行编译,以下进行了Ninja构建管理检查。

if (CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS "3.18")
  cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
endif ()

if (POLICY CMP0114)
  cmake_policy(SET CMP0114 NEW)
endif ()
zyh("CMAKE_CURRENT_LIST_DIR=${CMAKE_CURRENT_LIST_DIR}")

get_property(is_multiconfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)

if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  message(FATAL_ERROR
    "This project is not meant to be run directly, but instead used via "
    "add_subdirectory.")
elseif (is_multiconfig)
  # These generators aren't tested at all and non-CMake subprojects do not
  # change their configuration properly without a reconfigure. This could
  # change in the future, but it is not in the plan right now.
  message(FATAL_ERROR
    "The superbuild does not currently support multi-config generators "
    "(such as Visual Studio or Xcode). The recommended generators are "
    "Ninja and Unix Makefiles, but NMake Makefiles should work as well.")
elseif (NOT COMMAND superbuild_find_projects)
  message(FATAL_ERROR
    "The superbuild_find_projects function is not implemented; no projects "
    "can be enabled.")
endif ()

if (superbuild_replace_uncacheable_flags) # 未进入该分支
  string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type_upper)
  foreach (lang IN ITEMS C CXX Fortran)
    foreach (prefix IN ITEMS - /)
      foreach (flag IN ITEMS Zi ZI)
        string(REPLACE "${prefix}${flag}" "-Z7" "CMAKE_${lang}_FLAGS" "${CMAKE_${lang}_FLAGS}")
        string(REPLACE "${prefix}${flag}" "-Z7" "CMAKE_${lang}_FLAGS_${build_type_upper}" "${CMAKE_${lang}_FLAGS_${build_type_upper}}")
      endforeach ()
    endforeach ()
  endforeach ()
endif ()

添加cmake搜索路径 CMAKE_MODULE_PATH,pvsb\superbuild\cmake文件夹中是一系列配置相关的模块。SuperbuildUtils及SuperbuildCrossMacros也在该文件夹中。SuperbuildUtils中定义了一些列配置构建相关的宏和函数,如superbuild_detect_64bit_target、wuperubild_setup_flags函数。

list(INSERT CMAKE_MODULE_PATH 0
  "${CMAKE_CURRENT_LIST_DIR}/cmake")
include("SuperbuildUtils")
include("SuperbuildCrossMacros")

_superbuild_check_up_to_date() # 在SuperbuildUtils中定义

_superbuild_check_up_to_date中CMAKE_SOURCE_DIR为源码中顶层CMakeLists.txt的目录,所有调用的git命令都是在该目录下执行的。

设置库的属性,检查构建设置

superbuild_detect_64bit_target()

# Setup shared library defaults.  If explicitly specified somehow, then default
# to that.  Otherwise base the default on whether or not shared libs are even
# supported.
if (NOT _superbuild_no_static_everywhere)
  if (NOT DEFINED BUILD_SHARED_LIBS)
    get_property(superbuild_shared_libs_supported
      GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
    cmake_dependent_option(BUILD_SHARED_LIBS
      "Build shared libraries (so/dylib/dll)." "${superbuild_shared_libs_supported}"
      "superbuild_shared_libs_supported" OFF)
    mark_as_advanced(BUILD_SHARED_LIBS)
  endif ()
else ()
  set(BUILD_SHARED_LIBS ON)
endif ()

# Setup PIC defaults.
cmake_dependent_option(CMAKE_POSITION_INDEPENDENT_CODE
  "Build with Position Independent Code" ON
  "superbuild_shared_libs_supported" OFF)
mark_as_advanced(CMAKE_POSITION_INDEPENDENT_CODE)

# Verify shared libs and PIC compatibility
if(BUILD_SHARED_LIBS AND NOT CMAKE_POSITION_INDEPENDENT_CODE)
  if (NOT _superbuild_no_static_everywhere)
    message(WARNING "Forcing CMAKE_POSITION_INDEPENDENT_CODE to be ON as required by BUILD_SHARED_LIBS.")
  endif ()
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

然后进行一些标准变量的设置

# Setup some standard variables that control various locations and flags.
if (NOT superbuild_install_location)
  set(superbuild_install_location "${CMAKE_BINARY_DIR}/install")
endif ()

if (NOT IS_ABSOLUTE "${superbuild_install_location}")
  message(WARNING
    "The given install location `${superbuild_install_location}` is relative; "
    "replacing with an absolute path.")
  get_filename_component(superbuild_install_location
    "${superbuild_install_location}" ABSOLUTE)
endif ()

set(superbuild_prefix_path "${superbuild_install_location}")
set(superbuild_download_location "${CMAKE_BINARY_DIR}/downloads"
  CACHE PATH "Location for downloaded source tarballs")
mark_as_advanced(superbuild_download_location)

if(BUILD_SHARED_LIBS AND CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
  string(APPEND superbuild_ld_flags " -shared-intel")
endif()
# 设置编译器配置选项,这些选项在项目配置过程中进行设定
superbuild_setup_flags()

# merge in default/user-specified CMake flags.
string(PREPEND superbuild_ld_flags "${CMAKE_SHARED_LINKER_FLAGS} ")
string(PREPEND superbuild_cpp_flags "${CMAKE_CXX_FLAGS} ")
string(PREPEND superbuild_cxx_flags "${CMAKE_CXX_FLAGS} ")
string(PREPEND superbuild_c_flags "${CMAKE_C_FLAGS} ")
string(PREPEND superbuild_f_flags "${CMAKE_Fortran_FLAGS} ")

foreach (var IN ITEMS ld_flags cpp_flags cxx_flags c_flags f_flags)
  string(STRIP "${superbuild_${var}}" "superbuild_${var}")
endforeach ()

# 创建文件夹
# build/install/lib
# build/pvsb/superbuild/CMakeFiles/cmake,并将该目录加到 CMAKE_MODULE_PATH 中
superbuild_prepare_build_tree()

CMAKE_BINARY_DIR是编译的目录的地址(build目录)

superbuild_install_location是工程安装的目录,为build/install,为绝对目录。

superbuild_prefix_path和superbuild_install_location,为安装目录。

superbuild_download_location为下载目录,为build/downloads。

build/pvsb/superbuild/CMakeFiles/cmake中有两个文件,为lidarview-version.cmake和paraview-version.cmake

# Setup CMAKE_MODULE_PATH so that platform specific configurations are
# processed before the generic ones.
set(project_path)
list(APPEND superbuild_project_roots
  "${CMAKE_CURRENT_LIST_DIR}/projects") # projects文件夹中存储着各个项目的构建参数

foreach (root IN LISTS superbuild_project_roots)
  foreach (platform IN LISTS platforms)
    list(APPEND project_path
      "${root}/${platform}")
  endforeach ()
  list(APPEND project_path
    "${root}/common"
    "${root}")
endforeach ()
list(INSERT CMAKE_MODULE_PATH 0
  ${project_path})

if (CMAKE_CROSSCOMPILING AND
    COMMAND superbuild_cross_prepare_target)
  # 未执行该if分支
  superbuild_cross_prepare_target()
endif ()

CMAKE_CURRENT_LIST_DIR为src/pvsb/superbuild

superbuild_project_roots为src/projects; src/pvsb/projects;src/pvsb/superbuild/projects

到此,CMAKE_MODULE_PATH中有以下路径:

src/projects/win32
src/projects/common
src/projects
src/pvsb/projects/win32
src/pvsb/projects/common
src/pvsb/projects
src/pvsb/superbuild/projects/win32
src/pvsb/superbuild/projects/common
src/pvsb/superbuild/projects
src/pvsb/superbuild/cmake
src/pvsb/cmake
build/pvsb/superbuild/CMakeFiles/cmake

# 定义了一些版本控制,源码构建配置的宏及函数
include(SuperbuildRevisionMacros)

调用SuperbuildRevisionMacros,定义一些用于源码版本控制的函数及宏。

# Gather version information.
# 该步骤中设定了所有项目下载地址以版本信息
list(APPEND superbuild_version_files
  "${CMAKE_CURRENT_LIST_DIR}/versions.cmake") 
foreach (version_file IN LISTS superbuild_version_files)
  include("${version_file}") # 加载每个version中的项目源码获取方式等信息
endforeach ()

获取版本信息,superbuild_version_files变量里有3个文件路径:

src/versions.cmake
src/pvsb/versions.cmake
src/pvsb/superbuild/versions.cmake

这3个文件中存储了依赖项的相关信息,如获取地址,版本号。该步骤中设定了所有项目下载地址以版本信息

if (superbuild_package_roots) # 未进该分支
  include(SuperbuildPackageMacros)

  _superbuild_package_discover()
  _superbuild_package_select()
endif ()

当前未定义superbuild_package_roots,所以未执行if中的语句。

if (COMMAND superbuild_setup_variables) 
  # 如果superbuild_setup_variables是可调用命令,宏或函数,则调用
  # 设置paraview及lidarview的版本。
  superbuild_setup_variables()
endif ()

在superbuild_setup_variables()对paraview和lidarview的版本信息进行了相关变量的定义,paraview_SOURCE_SELECTION和lidarview_SOURCE_SELECTION都是"git",因此都是从git上进行版本查找,将这两个变量改成"source",会出现错误。

include(SuperbuildMacros)
include(SuperbuildPluginMacros)

以上两个文件定了了一些列构建时的函数及宏,用于配置项目。

# 在src/CMakeLists.txt中定义 superbuild_projects中为所有的项目名称
superbuild_find_projects(superbuild_projects) # 将所有的项目名称存放在superbuild_projects变量中
list(APPEND superbuild_projects ${superbuild_extra_package_projects})

if (NOT superbuild_projects)
  message(FATAL_ERROR "No projects are configured!")
endif ()

# Discover the projects. 
# 在SuperbuildMacros中定义
# 在该函数中,一次执行include("${project}"),如include(paraview)
# 对应的*.cmake基本位于以下两个文件夹中
# src\pvsb\projects
# src\pvsb\superbuild\projects
_superbuild_discover_projects(${superbuild_projects})

对所有的项目进行构建配置, 相关的配置属性都位于${project}.cmake文件中。

# Enable projects, etc.
# 在SuperbuildMacros中定义
# 在该函数中,通过get_property获取superbuild_projects
# 然后对所有的项目根据依赖关系进行排序
# 定义${project}_enabled变量,并设置成TRUE 和 ${project}_needed_by列表
# 输出project信息以及其依赖项信息
# set(superbuild_build_phase TRUE)
# 根据${project}_system和${project}_system_force判断使用或者强制使用系统原有的库
# set("${project}_built_by_superbuild" TRUE)
# 调用了include("${project}")
# 调用了ExternalProject_add
superbuild_process_dependencies()

 上面代码分别对每个项目的依赖进行设置

# Add packaging.
if (COMMAND superbuild_add_packaging)
  include(SuperbuildPackageMacros)

  set(superbuild_export_variables)
  if (superbuild_python_executable)
    list(APPEND superbuild_export_variables
      superbuild_python_executable)
  endif ()

  if (python_version)
    list(APPEND superbuild_export_variables
      python_version)
  endif ()

  if (superbuild_python_version)
    list(APPEND superbuild_export_variables
      superbuild_python_version)
  endif ()
  # 在src/CMakeLists.txt中定义
  # 向superbuild_export_variables添加了qt,paraview,lidarview的一些信息
  superbuild_add_packaging()
endif ()
# Add any tests.
cmake_dependent_option(BUILD_TESTING "Build testing" ON
  "COMMAND superbuild_add_tests" OFF)
if (BUILD_TESTING)
  if (COMMAND superbuild_setup_tests)
    superbuild_setup_tests()
  endif ()

  # Enable testing support.
  include(CTest)
  configure_file(
    "${CMAKE_CURRENT_LIST_DIR}/cmake/CTestCustom.cmake.in"
    "${CMAKE_BINARY_DIR}/CTestCustom.cmake"
    @ONLY)

  file(RELATIVE_PATH superbuild_relative_dir
    "${CMAKE_BINARY_DIR}"
    "${CMAKE_CURRENT_BINARY_DIR}")
  configure_file(
    "${CMAKE_CURRENT_LIST_DIR}/cmake/CTestTestfile.cmake.in"
    "${CMAKE_BINARY_DIR}/CTestTestfile.cmake"
    @ONLY)

  include(SuperbuildTestingMacros)

  superbuild_add_tests()

  get_property(superbuild_test_projects GLOBAL
    PROPERTY superbuild_test_projects)
  foreach (project IN LISTS superbuild_test_projects)
    cmake_dependent_option("TEST_${project}" "Run ${project} test from the superbuild" ON
      "${project}_enabled;TARGET ${project};NOT DEVELOPER_MODE_${project}" OFF)
    if (TEST_${project})
      set(binary_dir "<BINARY_DIR>")
      _ep_replace_location_tags("${project}" binary_dir)
      list(APPEND superbuild_extra_ctest_dirs
        "${binary_dir}")
    endif ()
  endforeach ()

  configure_file(
    "${CMAKE_CURRENT_LIST_DIR}/cmake/superbuild_testing_trampoline.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/superbuild_testing_trampoline.cmake"
    @ONLY)
  set_property(
    DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
    PROPERTY
      TEST_INCLUDE_FILE "${CMAKE_CURRENT_BINARY_DIR}/superbuild_testing_trampoline.cmake")
endif ()

添加测试

if (python3_enabled)
  include(python3.functions)
  superbuild_python_write_reqs("${CMAKE_BINARY_DIR}/requirements.txt")
endif ()

至此配置信息结束,然后可以调用Ninja进行每个项目的CMakeLists.txt的配置及编译。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值