|
| 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(get_flags_unix_makefiles preprocessor_defines_variable compiler_flags_variable linker_flags_variable) |
| 8 | + file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/flags.make preprocessor_defines REGEX "^CXX_DEFINES( +)=") |
| 9 | + string(REGEX REPLACE "^CXX_DEFINES( +)=( +)" "" preprocessor_defines "${preprocessor_defines}") |
| 10 | + set(${preprocessor_defines_variable} "${preprocessor_defines}" PARENT_SCOPE) |
| 11 | + |
| 12 | + file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/flags.make compiler_flags REGEX "^CXX_FLAGS( +)=") |
| 13 | + string(REGEX REPLACE "^CXX_FLAGS( +)=( +)" "" compiler_flags "${compiler_flags}") |
| 14 | + string(STRIP "${compiler_flags} ${APPEND_CPPFLAGS}" compiler_flags) |
| 15 | + string(STRIP "${compiler_flags} ${APPEND_CXXFLAGS}" compiler_flags) |
| 16 | + set(${compiler_flags_variable} "${compiler_flags}" PARENT_SCOPE) |
| 17 | + |
| 18 | + file(STRINGS ${CMAKE_BINARY_DIR}/${internal_binary_dir}/CMakeFiles/mock.dir/link.txt linker_flags LIMIT_COUNT 1) |
| 19 | + string(REPLACE "${CMAKE_CXX_COMPILER} " "" linker_flags "${linker_flags}") |
| 20 | + string(REPLACE " CMakeFiles/mock.dir/mock.cpp.o -o mock" "" linker_flags "${linker_flags}") |
| 21 | + set(${linker_flags_variable} "${linker_flags}" PARENT_SCOPE) |
| 22 | +endfunction() |
| 23 | + |
| 24 | +function(get_flags_ninja preprocessor_defines_variable compiler_flags_variable linker_flags_variable config) |
| 25 | + if(config) |
| 26 | + cmake_path(APPEND CMAKE_BINARY_DIR ${internal_binary_dir} CMakeFiles impl-${config}.ninja OUTPUT_VARIABLE build_file) |
| 27 | + set(object_build_statement_re "^build CMakeFiles/mock\\.dir/${config}/mock\\.cpp\\.o: ") |
| 28 | + set(link_build_statement_re "^build ${config}/mock: ") |
| 29 | + else() |
| 30 | + cmake_path(APPEND CMAKE_BINARY_DIR ${internal_binary_dir} build.ninja OUTPUT_VARIABLE build_file) |
| 31 | + set(object_build_statement_re "^build CMakeFiles/mock\\.dir/mock\\.cpp\\.o: ") |
| 32 | + set(link_build_statement_re "^build mock: ") |
| 33 | + endif() |
| 34 | + file(STRINGS ${build_file} build_content) |
| 35 | + |
| 36 | + set(is_object_build_statement FALSE) |
| 37 | + foreach(line IN LISTS build_content) |
| 38 | + if(is_object_build_statement) |
| 39 | + if(line MATCHES "^( +)DEFINES( +)=") |
| 40 | + string(REGEX REPLACE "^( +)DEFINES( +)=( +)" "" preprocessor_defines "${line}") |
| 41 | + elseif(line MATCHES "^( +)FLAGS( +)=") |
| 42 | + string(REGEX REPLACE "^( +)FLAGS( +)=( +)" "" compiler_flags "${line}") |
| 43 | + elseif(line STREQUAL "") |
| 44 | + break() |
| 45 | + endif() |
| 46 | + elseif(line MATCHES ${object_build_statement_re}) |
| 47 | + set(is_object_build_statement TRUE) |
| 48 | + endif() |
| 49 | + endforeach() |
| 50 | + set(${preprocessor_defines_variable} "${preprocessor_defines}" PARENT_SCOPE) |
| 51 | + string(STRIP "${compiler_flags} ${APPEND_CPPFLAGS}" compiler_flags) |
| 52 | + string(STRIP "${compiler_flags} ${APPEND_CXXFLAGS}" compiler_flags) |
| 53 | + set(${compiler_flags_variable} "${compiler_flags}" PARENT_SCOPE) |
| 54 | + |
| 55 | + set(is_link_build_statement FALSE) |
| 56 | + foreach(line IN LISTS build_content) |
| 57 | + if(is_link_build_statement) |
| 58 | + if(line MATCHES "^( +)FLAGS( +)=") |
| 59 | + string(REGEX REPLACE "^( +)FLAGS( +)=( +)" "" flags "${line}") |
| 60 | + elseif(line MATCHES "^( +)LINK_FLAGS( +)=") |
| 61 | + string(REGEX REPLACE "^( +)LINK_FLAGS( +)=( +)" "" link_flags "${line}") |
| 62 | + elseif(line STREQUAL "") |
| 63 | + break() |
| 64 | + endif() |
| 65 | + elseif(line MATCHES ${link_build_statement_re}) |
| 66 | + set(is_link_build_statement TRUE) |
| 67 | + endif() |
| 68 | + endforeach() |
| 69 | + string(STRIP "${flags} ${link_flags}" linker_flags) |
| 70 | + string(STRIP "${linker_flags} ${APPEND_LDFLAGS}" linker_flags) |
| 71 | + set(${linker_flags_variable} "${linker_flags}" PARENT_SCOPE) |
| 72 | +endfunction() |
| 73 | + |
| 74 | +function(flags_summary) |
| 75 | + if(INTERNAL_BUILD) |
| 76 | + file(WRITE ${PROJECT_BINARY_DIR}/mock.cpp "") |
| 77 | + add_executable(mock ${PROJECT_BINARY_DIR}/mock.cpp) |
| 78 | + target_link_libraries(mock PRIVATE core_interface) |
| 79 | + else() |
| 80 | + file(WRITE ${CMAKE_BINARY_DIR}/internal_build_cache "set(INTERNAL_BUILD ON CACHE BOOL \"\" FORCE)\n") |
| 81 | + get_cmake_property(cache_variable_names CACHE_VARIABLES) |
| 82 | + foreach (v ${cache_variable_names}) |
| 83 | + get_property(type CACHE ${v} PROPERTY TYPE) |
| 84 | + if(NOT type MATCHES "^(INTERNAL|STATIC)$") |
| 85 | + file(APPEND ${CMAKE_BINARY_DIR}/internal_build_cache "set(${v} \"$CACHE{${v}}\" CACHE ${type} \"\" FORCE)\n") |
| 86 | + endif() |
| 87 | + endforeach() |
| 88 | + |
| 89 | + string(RANDOM internal_binary_dir) |
| 90 | + |
| 91 | + # DEBUG ONLY |
| 92 | + set(internal_binary_dir "internal") |
| 93 | + |
| 94 | + execute_process( |
| 95 | + COMMAND ${CMAKE_COMMAND} -S${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR}/${internal_binary_dir} -C${CMAKE_BINARY_DIR}/internal_build_cache -G${CMAKE_GENERATOR} |
| 96 | + OUTPUT_QUIET |
| 97 | + ERROR_QUIET |
| 98 | + ) |
| 99 | + |
| 100 | + get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) |
| 101 | + if(is_multi_config) |
| 102 | + list(JOIN CMAKE_CONFIGURATION_TYPES ", " configs) |
| 103 | + message("Available build configurations ........ ${configs}") |
| 104 | + if(CMAKE_GENERATOR MATCHES "Visual Studio") |
| 105 | + set(default_config "Debug") |
| 106 | + else() |
| 107 | + list(GET CMAKE_CONFIGURATION_TYPES 0 default_config) |
| 108 | + endif() |
| 109 | + message("Default build configuration ........... ${default_config}") |
| 110 | + foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES) |
| 111 | + message("") |
| 112 | + message("'${config}' build configuration:") |
| 113 | + if(CMAKE_GENERATOR STREQUAL "Ninja Multi-Config") |
| 114 | + get_flags_ninja(preprocessor_defines compiler_flags linker_flags ${config}) |
| 115 | + else() |
| 116 | + message("Printing build options is not supported for the generator \"${CMAKE_GENERATOR}\"") |
| 117 | + endif() |
| 118 | + message(" Preprocessor defined macros ......... ${preprocessor_defines}") |
| 119 | + message(" C++ flags ........................... ${compiler_flags}") |
| 120 | + message(" Linker flags ........................ ${linker_flags}") |
| 121 | + endforeach() |
| 122 | + message("") |
| 123 | + else() |
| 124 | + message("CMAKE_BUILD_TYPE ...................... ${CMAKE_BUILD_TYPE}") |
| 125 | + if(CMAKE_GENERATOR STREQUAL "Unix Makefiles") |
| 126 | + get_flags_unix_makefiles(preprocessor_defines compiler_flags linker_flags) |
| 127 | + elseif(CMAKE_GENERATOR STREQUAL "Ninja") |
| 128 | + get_flags_ninja(preprocessor_defines compiler_flags linker_flags "") |
| 129 | + else() |
| 130 | + message("Printing build options is not supported for the generator \"${CMAKE_GENERATOR}\"") |
| 131 | + endif() |
| 132 | + message("Preprocessor defined macros ........... ${preprocessor_defines}") |
| 133 | + message("C++ flags ............................. ${compiler_flags}") |
| 134 | + message("Linker flags .......................... ${linker_flags}") |
| 135 | + endif() |
| 136 | + endif() |
| 137 | +endfunction() |
0 commit comments