FlagsSummary.cmake raw

   1  # Copyright (c) 2024-present The Bitcoin Core developers
   2  # Distributed under the MIT software license, see the accompanying
   3  # file COPYING or https://opensource.org/license/mit/.
   4  
   5  include_guard(GLOBAL)
   6  
   7  function(indent_message header content indent_num)
   8    if(indent_num GREATER 0)
   9      string(REPEAT " " ${indent_num} indentation)
  10      string(REPEAT "." ${indent_num} tail)
  11      string(REGEX REPLACE "${tail}$" "" header "${header}")
  12    endif()
  13    message("${indentation}${header} ${content}")
  14  endfunction()
  15  
  16  # Print tools' flags on best-effort. Include the abstracted
  17  # CMake flags that we touch ourselves.
  18  function(print_flags_per_config config indent_num)
  19    string(TOUPPER "${config}" config_uppercase)
  20  
  21    include(GetTargetInterface)
  22    get_target_interface(definitions "${config}" core_interface COMPILE_DEFINITIONS)
  23    indent_message("Preprocessor defined macros ..........." "${definitions}" ${indent_num})
  24  
  25    string(STRIP "${CMAKE_CXX_COMPILER_ARG1} ${CMAKE_CXX_FLAGS}" combined_cxx_flags)
  26    string(STRIP "${combined_cxx_flags} ${CMAKE_CXX_FLAGS_${config_uppercase}}" combined_cxx_flags)
  27    string(STRIP "${combined_cxx_flags} ${CMAKE_CXX${CMAKE_CXX_STANDARD}_STANDARD_COMPILE_OPTION}" combined_cxx_flags)
  28    if(CMAKE_POSITION_INDEPENDENT_CODE)
  29      string(JOIN " " combined_cxx_flags ${combined_cxx_flags} ${CMAKE_CXX_COMPILE_OPTIONS_PIC})
  30    endif()
  31    get_target_interface(core_cxx_flags "${config}" core_interface COMPILE_OPTIONS)
  32    string(STRIP "${combined_cxx_flags} ${core_cxx_flags}" combined_cxx_flags)
  33    string(STRIP "${combined_cxx_flags} ${APPEND_CPPFLAGS}" combined_cxx_flags)
  34    string(STRIP "${combined_cxx_flags} ${APPEND_CXXFLAGS}" combined_cxx_flags)
  35    indent_message("C++ compiler flags ...................." "${combined_cxx_flags}" ${indent_num})
  36  
  37    string(STRIP "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${config_uppercase}}" combined_linker_flags)
  38    string(STRIP "${combined_linker_flags} ${CMAKE_EXE_LINKER_FLAGS}" combined_linker_flags)
  39    get_target_interface(common_link_options "${config}" core_interface LINK_OPTIONS)
  40    string(STRIP "${combined_linker_flags} ${common_link_options}" combined_linker_flags)
  41    if(CMAKE_CXX_LINK_PIE_SUPPORTED)
  42      string(JOIN " " combined_linker_flags ${combined_linker_flags} ${CMAKE_CXX_LINK_OPTIONS_PIE})
  43    endif()
  44    string(STRIP "${combined_linker_flags} ${APPEND_LDFLAGS}" combined_linker_flags)
  45    indent_message("Linker flags .........................." "${combined_linker_flags}" ${indent_num})
  46  endfunction()
  47  
  48  function(flags_summary)
  49    get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  50    if(is_multi_config)
  51      list(JOIN CMAKE_CONFIGURATION_TYPES ", " configs)
  52      message("Available build configurations ........ ${configs}")
  53      if(CMAKE_GENERATOR MATCHES "Visual Studio")
  54        set(default_config "Debug")
  55      else()
  56        list(GET CMAKE_CONFIGURATION_TYPES 0 default_config)
  57      endif()
  58      message("Default build configuration ........... ${default_config}")
  59      foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
  60        message("")
  61        message("'${config}' build configuration:")
  62        print_flags_per_config("${config}" 2)
  63      endforeach()
  64    else()
  65      message("CMAKE_BUILD_TYPE ...................... ${CMAKE_BUILD_TYPE}")
  66      print_flags_per_config("${CMAKE_BUILD_TYPE}" 0)
  67    endif()
  68    message("")
  69    message([=[
  70  NOTE: The summary above may not exactly match the final applied build flags
  71        if any additional CMAKE_* or environment variables have been modified.
  72        To see the exact flags applied, build with the --verbose option.
  73  ]=])
  74  endfunction()
  75