Mark de Wever | cbaa359 | 2023-05-24 16:12:32 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.20.0) |
Siva Chandra | 4380647 | 2019-10-04 17:30:54 | [diff] [blame] | 2 | |
Tue Ly | 60d2dbf | 2023-03-13 20:14:53 | [diff] [blame] | 3 | # Include LLVM's cmake policies. |
| 4 | if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS) |
| 5 | set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) |
| 6 | endif() |
| 7 | include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake |
| 8 | NO_POLICY_SCOPE) |
| 9 | |
Nick Desaulniers | f1f1875 | 2024-01-16 16:14:06 | [diff] [blame] | 10 | if (LIBC_CMAKE_VERBOSE_LOGGING) |
| 11 | get_directory_property(LIBC_OLD_PREPROCESSOR_DEFS COMPILE_DEFINITIONS) |
| 12 | foreach(OLD_DEF ${LIBC_OLD_PREPROCESSOR_DEFS}) |
| 13 | message(STATUS "Undefining ${OLD_DEF}") |
| 14 | endforeach() |
| 15 | endif() |
| 16 | set_directory_properties(PROPERTIES |
| 17 | # `llvm-project/llvm/CMakeLists.txt` adds the following directive |
| 18 | # `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})` We |
| 19 | # undo it to be able to precisely control what is getting included. |
| 20 | INCLUDE_DIRECTORIES "" |
| 21 | # `llvm/cmake/modules/HandleLLVMOptions.cmake` uses `add_compile_definitions` |
| 22 | # to set a few preprocessor defines which we do not want. |
| 23 | COMPILE_DEFINITIONS "" |
| 24 | ) |
| 25 | if (CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 26 | add_definitions("-D_DEBUG") |
| 27 | endif() |
Guillaume Chatelet | 4670777 | 2023-09-20 09:21:46 | [diff] [blame] | 28 | |
Tue Ly | 700aeba | 2022-02-03 05:03:27 | [diff] [blame] | 29 | # Default to C++17 |
| 30 | set(CMAKE_CXX_STANDARD 17) |
| 31 | |
Joe Loser | 59cf6a7 | 2021-10-25 20:29:14 | [diff] [blame] | 32 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") |
Siva Chandra | 4380647 | 2019-10-04 17:30:54 | [diff] [blame] | 33 | |
Aiden Grossman | ea8f4b9 | 2023-06-03 22:37:21 | [diff] [blame] | 34 | # The top-level source directory. |
Siva Chandra | 4380647 | 2019-10-04 17:30:54 | [diff] [blame] | 35 | set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| 36 | # The top-level directory in which libc is being built. |
| 37 | set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}) |
| 38 | |
Aiden Grossman | 14a06b8 | 2023-06-05 19:57:13 | [diff] [blame] | 39 | set(LIBC_ENABLE_USE_BY_CLANG OFF CACHE BOOL "Whether or not to place libc in a build directory findable by a just built clang") |
| 40 | |
Joseph Huber | 4792ae5 | 2023-09-07 17:47:36 | [diff] [blame] | 41 | # Defining a global namespace to enclose all libc functions. |
Joseph Huber | a30233f | 2024-03-06 04:03:20 | [diff] [blame] | 42 | set(default_namespace "__llvm_libc") |
| 43 | if(LLVM_VERSION_MAJOR) |
| 44 | set(default_namespace "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}") |
| 45 | endif() |
| 46 | set(LIBC_NAMESPACE ${default_namespace} |
Joseph Huber | 4792ae5 | 2023-09-07 17:47:36 | [diff] [blame] | 47 | CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'." |
| 48 | ) |
| 49 | |
Joseph Huber | 47b7c91 | 2024-02-22 21:29:29 | [diff] [blame] | 50 | if(LLVM_LIBC_FULL_BUILD OR LLVM_LIBC_GPU_BUILD) |
Siva Chandra Reddy | 5274a46 | 2023-01-10 19:33:45 | [diff] [blame] | 51 | if(NOT LIBC_HDRGEN_EXE) |
| 52 | # We need to set up hdrgen first since other targets depend on it. |
| 53 | add_subdirectory(utils/LibcTableGenUtil) |
| 54 | add_subdirectory(utils/HdrGen) |
Petr Hosek | c20811b | 2024-01-16 13:39:06 | [diff] [blame] | 55 | # Calling add_tablegen sets variables like LIBC_TABLEGEN_EXE in |
| 56 | # PARENT_SCOPE which get lost until saved in the cache. |
| 57 | set(LIBC_TABLEGEN_EXE "${LIBC_TABLEGEN_EXE}" CACHE INTERNAL "") |
| 58 | set(LIBC_TABLEGEN_TARGET "${LIBC_TABLEGEN_TARGET}" CACHE INTERNAL "") |
Siva Chandra Reddy | 5274a46 | 2023-01-10 19:33:45 | [diff] [blame] | 59 | else() |
| 60 | message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.") |
| 61 | endif() |
| 62 | endif() |
Joseph Huber | 9bc294f | 2024-03-11 14:18:47 | [diff] [blame] | 63 | # We will build the GPU utilities if we are not doing a runtimes build. |
Joseph Huber | 8937174 | 2024-03-21 00:58:53 | [diff] [blame] | 64 | option(LIBC_BUILD_GPU_LOADER "Always build the GPU loader utilities" OFF) |
| 65 | if(LIBC_BUILD_GPU_LOADER OR (LLVM_LIBC_GPU_BUILD AND NOT LLVM_RUNTIMES_BUILD)) |
Joseph Huber | 9bc294f | 2024-03-11 14:18:47 | [diff] [blame] | 66 | add_subdirectory(utils/gpu) |
| 67 | endif() |
Siva Chandra Reddy | 5274a46 | 2023-01-10 19:33:45 | [diff] [blame] | 68 | |
Petr Hosek | 9dbedca | 2024-02-22 14:28:12 | [diff] [blame] | 69 | set(NEED_LIBC_HDRGEN FALSE) |
| 70 | if(NOT LLVM_RUNTIMES_BUILD) |
| 71 | if("libc" IN_LIST LLVM_ENABLE_RUNTIMES) |
| 72 | set(NEED_LIBC_HDRGEN TRUE) |
| 73 | else() |
| 74 | foreach(_name ${LLVM_RUNTIME_TARGETS}) |
| 75 | if("libc" IN_LIST RUNTIMES_${_name}_LLVM_ENABLE_RUNTIMES) |
| 76 | set(NEED_LIBC_HDRGEN TRUE) |
| 77 | break() |
| 78 | endif() |
| 79 | endforeach() |
| 80 | endif() |
| 81 | endif() |
Joseph Huber | 4fafa39 | 2023-05-22 21:00:41 | [diff] [blame] | 82 | option(LIBC_HDRGEN_ONLY "Only build the 'libc-hdrgen' executable" OFF) |
Petr Hosek | 9dbedca | 2024-02-22 14:28:12 | [diff] [blame] | 83 | if(LIBC_HDRGEN_ONLY OR NEED_LIBC_HDRGEN) |
Siva Chandra Reddy | 5274a46 | 2023-01-10 19:33:45 | [diff] [blame] | 84 | # When libc is build as part of the runtimes/bootstrap build's CMake run, we |
| 85 | # only need to build the host tools to build the libc. So, we just do enough |
| 86 | # to build libc-hdrgen and return. |
| 87 | return() |
Joseph Huber | 93d1a7b | 2022-11-15 16:25:27 | [diff] [blame] | 88 | endif() |
Petr Hosek | 9dbedca | 2024-02-22 14:28:12 | [diff] [blame] | 89 | unset(NEED_LIBC_HDRGEN) |
Joseph Huber | 93d1a7b | 2022-11-15 16:25:27 | [diff] [blame] | 90 | |
Siva Chandra Reddy | 23872aae | 2023-01-28 09:26:46 | [diff] [blame] | 91 | option(LIBC_CMAKE_VERBOSE_LOGGING |
Guillaume Chatelet | 56426c6 | 2023-09-06 08:27:56 | [diff] [blame] | 92 | "Log details warnings and notifications during CMake configuration." OFF) |
| 93 | |
Siva Chandra | 4380647 | 2019-10-04 17:30:54 | [diff] [blame] | 94 | # Path libc/scripts directory. |
| 95 | set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts") |
| 96 | |
Guillaume Chatelet | 56426c6 | 2023-09-06 08:27:56 | [diff] [blame] | 97 | if(NOT LIBC_NAMESPACE MATCHES "^__llvm_libc") |
| 98 | message(FATAL_ERROR "Invalid LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LIBC_NAMESPACE}'") |
| 99 | endif() |
| 100 | |
| 101 | message(STATUS "Setting LIBC_NAMESPACE namespace to '${LIBC_NAMESPACE}'") |
| 102 | add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE}) |
| 103 | |
Guillaume Chatelet | ed4f4ed | 2021-05-10 07:53:48 | [diff] [blame] | 104 | # Flags to pass down to the compiler while building the libc functions. |
| 105 | set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)") |
| 106 | |
Michael Jones | a3b7458 | 2022-07-14 22:17:18 | [diff] [blame] | 107 | list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS}) |
| 108 | |
Paula Toth | 2a4c309 | 2020-05-22 00:39:03 | [diff] [blame] | 109 | # Check --print-resource-dir to find the compiler resource dir if this flag |
| 110 | # is supported by the compiler. |
| 111 | execute_process( |
| 112 | OUTPUT_STRIP_TRAILING_WHITESPACE |
| 113 | COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir |
| 114 | RESULT_VARIABLE COMMAND_RETURN_CODE |
| 115 | OUTPUT_VARIABLE COMPILER_RESOURCE_DIR |
| 116 | ) |
| 117 | # Retrieve the host compiler's resource dir. |
| 118 | if(COMMAND_RETURN_CODE EQUAL 0) |
| 119 | set(COMPILER_RESOURCE_DIR |
| 120 | "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir" |
| 121 | ) |
Siva Chandra Reddy | 10bb03c | 2020-05-28 21:52:39 | [diff] [blame] | 122 | message(STATUS "Set COMPILER_RESOURCE_DIR to " |
| 123 | "${COMPILER_RESOURCE_DIR} using --print-resource-dir") |
Paula Toth | 2a4c309 | 2020-05-22 00:39:03 | [diff] [blame] | 124 | else() |
Joseph Huber | 47b7c91 | 2024-02-22 21:29:29 | [diff] [blame] | 125 | if (LIBC_TARGET_OS_IS_GPU) |
Joseph Huber | ead92ae | 2023-09-05 13:10:25 | [diff] [blame] | 126 | message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds") |
| 127 | else() |
| 128 | set(COMPILER_RESOURCE_DIR OFF) |
| 129 | message(STATUS "COMPILER_RESOURCE_DIR not set |
| 130 | --print-resource-dir not supported by host compiler") |
| 131 | endif() |
Paula Toth | 2a4c309 | 2020-05-22 00:39:03 | [diff] [blame] | 132 | endif() |
| 133 | |
Guillaume Chatelet | 0e97e84 | 2021-05-03 08:39:26 | [diff] [blame] | 134 | option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF) |
Dominic Chen | 74ec467 | 2022-09-29 21:57:56 | [diff] [blame] | 135 | option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON) |
Petr Hosek | 1daaa64 | 2021-01-22 06:55:12 | [diff] [blame] | 136 | option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF) |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 137 | |
Siva Chandra Reddy | 4f9c10e | 2022-12-17 00:04:23 | [diff] [blame] | 138 | set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.") |
Joseph Huber | 55151e1 | 2022-11-22 20:45:38 | [diff] [blame] | 139 | |
michaelrj-google | 4db99c8 | 2023-11-17 19:32:27 | [diff] [blame] | 140 | option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF) |
| 141 | |
Siva Chandra Reddy | 1e8960c | 2023-04-18 22:50:26 | [diff] [blame] | 142 | set(LIBC_ENABLE_UNITTESTS ON) |
Siva Chandra Reddy | 7b37f73 | 2023-04-24 23:07:22 | [diff] [blame] | 143 | set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD}) |
Siva Chandra Reddy | 1e8960c | 2023-04-18 22:50:26 | [diff] [blame] | 144 | |
Joseph Huber | 55151e1 | 2022-11-22 20:45:38 | [diff] [blame] | 145 | # Defines LIBC_TARGET_ARCHITECTURE and associated macros. |
| 146 | include(LLVMLibCArchitectures) |
Joseph Huber | 55151e1 | 2022-11-22 20:45:38 | [diff] [blame] | 147 | |
michaelrj-google | 4db99c8 | 2023-11-17 19:32:27 | [diff] [blame] | 148 | set(LIBC_CONFIG_JSON_FILE_LIST "") |
| 149 | |
| 150 | if(NOT LIBC_CONFIG_PATH) |
| 151 | list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}") |
| 152 | if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}") |
| 153 | list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}") |
| 154 | set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}") |
| 155 | elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}") |
| 156 | set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}") |
| 157 | endif() |
| 158 | else() |
| 159 | list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}") |
| 160 | endif() |
| 161 | |
| 162 | if(NOT LIBC_CONFIG_PATH) |
| 163 | message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.") |
| 164 | elseif(LIBC_CMAKE_VERBOSE_LOGGING) |
| 165 | message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}") |
| 166 | endif() |
| 167 | |
Joseph Huber | 69c0b2f | 2024-02-23 22:34:00 | [diff] [blame] | 168 | # option(LIBC_ENABLE_WIDE_CHARACTERS |
michaelrj-google | 4db99c8 | 2023-11-17 19:32:27 | [diff] [blame] | 169 | # "Whether to enable wide character functions on supported platforms. This may |
| 170 | # also set flags to enable or disable wide character support within other |
| 171 | # functions (e.g. printf)." ON) |
| 172 | |
| 173 | #TODO: Add carve-out specific config files to the list here. |
| 174 | |
Siva Chandra Reddy | 0f1507a | 2023-08-29 07:32:52 | [diff] [blame] | 175 | include(LibcConfig) |
| 176 | # Config loading happens in three steps: |
| 177 | # 1. Load the config file config/config.json and set up config vars. |
| 178 | # 2. Load config/${LIBC_TARGET_OS}/config.json if available and override |
| 179 | # vars as suitable. |
| 180 | # 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is |
| 181 | # available and override vars as suitable. |
| 182 | # All the three steps will not override options already set from the |
| 183 | # CMake command line. That is, the CMake command line option values take |
| 184 | # precedence over the values in config.json files. |
| 185 | set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json) |
| 186 | read_libc_config(${main_config_file} global_config) |
| 187 | foreach(opt IN LISTS global_config) |
| 188 | string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0) |
| 189 | if(json_error) |
| 190 | message(FATAL_ERROR ${json_error}) |
| 191 | endif() |
| 192 | if(DEFINED ${opt_name}) |
| 193 | # The option is already defined from the command line so we ignore it here. |
| 194 | # We still make note of it so that further config load can also ignore |
| 195 | # this option. |
| 196 | message(STATUS "${opt_name}: ${${opt_name}} (from command line)") |
| 197 | list(APPEND cmd_line_conf ${opt_name}) |
| 198 | continue() |
| 199 | endif() |
| 200 | |
| 201 | string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name}) |
| 202 | if(json_error) |
| 203 | message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}") |
| 204 | endif() |
| 205 | string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value") |
| 206 | if(json_error) |
| 207 | message(FATAL_ERROR ${json_error}) |
| 208 | endif() |
| 209 | message(STATUS "${opt_name}: ${opt_value}") |
| 210 | set(${opt_name} ${opt_value}) |
| 211 | endforeach() |
Siva Chandra | ca2a4e7 | 2023-09-08 20:11:09 | [diff] [blame] | 212 | generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst) |
michaelrj-google | 4db99c8 | 2023-11-17 19:32:27 | [diff] [blame] | 213 | |
| 214 | # Load each target specific config. |
| 215 | foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST) |
| 216 | if(LIBC_CMAKE_VERBOSE_LOGGING) |
| 217 | message(STATUS "Loading additional config: '${config_path}/config.json'") |
| 218 | endif() |
| 219 | load_libc_config(${config_path}/config.json ${cmd_line_conf}) |
| 220 | endforeach() |
Siva Chandra Reddy | 0f1507a | 2023-08-29 07:32:52 | [diff] [blame] | 221 | |
Joseph Huber | 47b7c91 | 2024-02-22 21:29:29 | [diff] [blame] | 222 | if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND LIBC_ENABLE_USE_BY_CLANG) |
Joseph Huber | 3368a92 | 2023-06-23 15:42:54 | [diff] [blame] | 223 | set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE}) |
| 224 | set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}) |
| 225 | set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}) |
| 226 | else() |
| 227 | if(NOT LIBC_ENABLE_USE_BY_CLANG) |
| 228 | set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include) |
| 229 | set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib) |
| 230 | elseif(LLVM_LIBRARY_OUTPUT_INTDIR) |
| 231 | set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include) |
| 232 | set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) |
| 233 | else() |
| 234 | set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include) |
| 235 | set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}) |
| 236 | endif() |
Joseph Huber | 47b7c91 | 2024-02-22 21:29:29 | [diff] [blame] | 237 | if(LIBC_TARGET_OS_IS_GPU) |
Joseph Huber | a30233f | 2024-03-06 04:03:20 | [diff] [blame] | 238 | if(LLVM_RUNTIMES_TARGET) |
| 239 | set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_RUNTIMES_TARGET}) |
| 240 | elseif(LIBC_TARGET_TRIPLE) |
| 241 | set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LIBC_TARGET_TRIPLE}) |
| 242 | else() |
| 243 | set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}) |
| 244 | endif() |
Joseph Huber | 47b7c91 | 2024-02-22 21:29:29 | [diff] [blame] | 245 | else() |
| 246 | set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}) |
| 247 | endif() |
Joseph Huber | 3368a92 | 2023-06-23 15:42:54 | [diff] [blame] | 248 | endif() |
| 249 | |
Joseph Huber | 049e142 | 2024-02-21 23:04:31 | [diff] [blame] | 250 | if(LIBC_TARGET_TRIPLE) |
| 251 | set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_TRIPLE}) |
Joseph Huber | cb807ff | 2024-03-02 01:28:45 | [diff] [blame] | 252 | elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR) |
Joseph Huber | 049e142 | 2024-02-21 23:04:31 | [diff] [blame] | 253 | set(LIBC_INSTALL_LIBRARY_DIR |
| 254 | lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE}) |
| 255 | else() |
| 256 | set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}) |
| 257 | endif() |
| 258 | |
Joseph Huber | 47b7c91 | 2024-02-22 21:29:29 | [diff] [blame] | 259 | if(LIBC_TARGET_OS_IS_GPU) |
Siva Chandra Reddy | 8d68195 | 2022-12-17 17:02:00 | [diff] [blame] | 260 | include(prepare_libc_gpu_build) |
Siva Chandra Reddy | 1e8960c | 2023-04-18 22:50:26 | [diff] [blame] | 261 | set(LIBC_ENABLE_UNITTESTS OFF) |
Joseph Huber | 55151e1 | 2022-11-22 20:45:38 | [diff] [blame] | 262 | endif() |
| 263 | |
Siva Chandra Reddy | 4f9c10e | 2022-12-17 00:04:23 | [diff] [blame] | 264 | include(LLVMLibCCheckMPFR) |
| 265 | |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 266 | if(LLVM_LIBC_CLANG_TIDY) |
| 267 | set(LLVM_LIBC_ENABLE_LINTING ON) |
Guillaume Chatelet | 0e97e84 | 2021-05-03 08:39:26 | [diff] [blame] | 268 | endif() |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 269 | |
Paula Toth | 741d3c2 | 2020-04-17 00:40:36 | [diff] [blame] | 270 | if(LLVM_LIBC_ENABLE_LINTING) |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 271 | if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang") |
| 272 | set(LLVM_LIBC_ENABLE_LINTING OFF) |
| 273 | message(WARNING "C++ compiler is not clang++, linting with be disabled.") |
Paula Toth | 741d3c2 | 2020-04-17 00:40:36 | [diff] [blame] | 274 | else() |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 275 | if (NOT LLVM_LIBC_CLANG_TIDY) |
| 276 | find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy) |
| 277 | endif() |
Paula Toth | 741d3c2 | 2020-04-17 00:40:36 | [diff] [blame] | 278 | |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 279 | if(LLVM_LIBC_CLANG_TIDY) |
| 280 | # Check clang-tidy major version. |
| 281 | execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version" |
Guillaume Chatelet | c21be63 | 2023-09-15 15:43:48 | [diff] [blame] | 282 | OUTPUT_VARIABLE CLANG_TIDY_OUTPUT |
| 283 | ERROR_VARIABLE CLANG_TIDY_ERROR |
| 284 | RESULT_VARIABLE CLANG_TIDY_RESULT) |
| 285 | |
| 286 | if(CLANG_TIDY_RESULT AND NOT CLANG_TIDY_RESULT EQUAL 0) |
| 287 | message(FATAL_ERROR "Failed to execute '${LLVM_LIBC_CLANG_TIDY} --version' |
| 288 | output : '${CLANG_TIDY_OUTPUT}' |
| 289 | error : '${CLANG_TIDY_ERROR}' |
| 290 | result : '${CLANG_TIDY_RESULT}' |
| 291 | ") |
| 292 | endif() |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 293 | string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}") |
| 294 | string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION |
Guillaume Chatelet | 56426c6 | 2023-09-06 08:27:56 | [diff] [blame] | 295 | "${CMAKE_CXX_COMPILER_VERSION}") |
| 296 | |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 297 | if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION) |
| 298 | set(LLVM_LIBC_ENABLE_LINTING OFF) |
| 299 | message(WARNING " |
| 300 | 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as |
| 301 | 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will |
| 302 | be disabled. |
| 303 | |
| 304 | The path to the clang-tidy binary can be set manually by passing |
| 305 | -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.") |
| 306 | endif() |
Siva Chandra Reddy | bfeef8b | 2023-02-24 08:27:29 | [diff] [blame] | 307 | add_custom_target(libc-lint) |
Tue Ly | 4816bfa8 | 2022-02-07 16:46:09 | [diff] [blame] | 308 | else() |
| 309 | message(FATAL_ERROR " |
| 310 | Linting is enabled but 'clang-tidy' is not found! |
| 311 | |
| 312 | The path to the clang-tidy binary can be set manually by passing |
| 313 | -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake. |
| 314 | |
| 315 | To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF |
| 316 | (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).") |
| 317 | endif() |
Paula Toth | 741d3c2 | 2020-04-17 00:40:36 | [diff] [blame] | 318 | endif() |
Paula Toth | 741d3c2 | 2020-04-17 00:40:36 | [diff] [blame] | 319 | endif() |
| 320 | |
Michael Jones | da06d17 | 2021-07-21 22:17:15 | [diff] [blame] | 321 | option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF) |
| 322 | if(LLVM_LIBC_INCLUDE_SCUDO) |
Alfred Persson Forsberg | fe9c3c7 | 2023-07-27 04:11:35 | [diff] [blame] | 323 | if (NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES)) |
| 324 | message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES") |
Michael Jones | da06d17 | 2021-07-21 22:17:15 | [diff] [blame] | 325 | endif() |
| 326 | endif() |
| 327 | |
Jeff Bailey | 4465c29 | 2022-01-29 00:27:34 | [diff] [blame] | 328 | option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS}) |
| 329 | |
Siva Chandra | 4380647 | 2019-10-04 17:30:54 | [diff] [blame] | 330 | include(CMakeParseArguments) |
Guillaume Chatelet | 04a309d | 2020-02-11 12:37:02 | [diff] [blame] | 331 | include(LLVMLibCCheckCpuFeatures) |
lntue | 3f906f5 | 2023-11-10 19:32:59 | [diff] [blame] | 332 | include(CheckCompilerFeatures) |
Tue Ly | 8000514 | 2022-05-08 17:45:40 | [diff] [blame] | 333 | include(LLVMLibCRules) |
Siva Chandra | 4380647 | 2019-10-04 17:30:54 | [diff] [blame] | 334 | |
michaelrj-google | 4db99c8 | 2023-11-17 19:32:27 | [diff] [blame] | 335 | set(TARGET_LLVMLIBC_ENTRYPOINTS "") |
| 336 | set(TARGET_LIBC_ENTRYPOINTS "") |
| 337 | set(TARGET_LIBM_ENTRYPOINTS "") |
| 338 | set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "") |
Siva Chandra Reddy | 58af0d5 | 2021-06-29 20:27:28 | [diff] [blame] | 339 | |
michaelrj-google | 4db99c8 | 2023-11-17 19:32:27 | [diff] [blame] | 340 | # Check entrypoints.txt |
| 341 | if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt") |
| 342 | include("${LIBC_CONFIG_PATH}/entrypoints.txt") |
| 343 | else() |
| 344 | message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.") |
Siva Chandra Reddy | 58af0d5 | 2021-06-29 20:27:28 | [diff] [blame] | 345 | endif() |
Siva Chandra Reddy | fd3295f | 2020-06-09 07:31:48 | [diff] [blame] | 346 | |
michaelrj-google | 4db99c8 | 2023-11-17 19:32:27 | [diff] [blame] | 347 | # Check headers.txt |
| 348 | if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt") |
| 349 | include("${LIBC_CONFIG_PATH}/headers.txt") |
| 350 | elseif(LLVM_LIBC_FULL_BUILD) |
| 351 | message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.") |
| 352 | endif() |
| 353 | |
| 354 | # Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list |
| 355 | if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt") |
| 356 | include("${LIBC_CONFIG_PATH}/exclude.txt") |
| 357 | endif() |
| 358 | |
| 359 | # #TODO: Set up support for premade configs adding their own exclude lists. |
| 360 | |
| 361 | foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS) |
| 362 | if(LIBC_CMAKE_VERBOSE_LOGGING) |
| 363 | message(STATUS "Removing entrypoint ${removed_entrypoint}") |
| 364 | endif() |
| 365 | list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint}) |
| 366 | list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint}) |
| 367 | list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint}) |
| 368 | endforeach() |
| 369 | |
Siva Chandra Reddy | fd3295f | 2020-06-09 07:31:48 | [diff] [blame] | 370 | set(TARGET_ENTRYPOINT_NAME_LIST "") |
Siva Chandra Reddy | 8d4ac53 | 2021-02-24 05:19:05 | [diff] [blame] | 371 | foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS) |
Siva Chandra Reddy | fd3295f | 2020-06-09 07:31:48 | [diff] [blame] | 372 | string(FIND ${entrypoint} "." last_dot_loc REVERSE) |
| 373 | if(${last_dot_loc} EQUAL -1) |
lntue | 0881d0f | 2024-02-05 15:44:19 | [diff] [blame] | 374 | message(FATAL_ERROR "Invalid entrypoint target name ${entrypoint}; Expected" |
| 375 | " a '.' (dot) in the name.") |
Siva Chandra Reddy | fd3295f | 2020-06-09 07:31:48 | [diff] [blame] | 376 | endif() |
| 377 | math(EXPR name_loc "${last_dot_loc} + 1") |
| 378 | string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name) |
| 379 | list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name}) |
| 380 | endforeach() |
| 381 | |
Michael Jones | f6bf282 | 2020-10-12 17:03:19 | [diff] [blame] | 382 | add_subdirectory(include) |
| 383 | add_subdirectory(config) |
lntue | 5748ad8 | 2024-04-05 22:21:16 | [diff] [blame] | 384 | add_subdirectory(hdr) |
Siva Chandra Reddy | e4767a6 | 2020-03-04 23:45:51 | [diff] [blame] | 385 | add_subdirectory(src) |
Siva Chandra Reddy | b47f9eb | 2019-11-05 19:40:26 | [diff] [blame] | 386 | add_subdirectory(utils) |
Siva Chandra Reddy | 5b24c08 | 2020-01-03 20:00:45 | [diff] [blame] | 387 | |
Siva Chandra Reddy | e9e788d | 2021-03-11 23:25:24 | [diff] [blame] | 388 | if(LLVM_LIBC_FULL_BUILD) |
Siva Chandra Reddy | 9a07780 | 2022-12-14 20:38:45 | [diff] [blame] | 389 | # The startup system can potentially depend on the library components so add |
| 390 | # it after the library implementation directories. |
| 391 | add_subdirectory(startup) |
Siva Chandra Reddy | e9e788d | 2021-03-11 23:25:24 | [diff] [blame] | 392 | endif() |
Siva Chandra Reddy | f6ccb4f | 2020-03-18 19:46:33 | [diff] [blame] | 393 | |
Siva Chandra Reddy | 5b24c08 | 2020-01-03 20:00:45 | [diff] [blame] | 394 | # The lib and test directories are added at the very end as tests |
| 395 | # and libraries potentially draw from the components present in all |
| 396 | # of the other directories. |
| 397 | add_subdirectory(lib) |
Joseph Huber | 39e9109 | 2023-03-16 16:42:57 | [diff] [blame] | 398 | if(LLVM_INCLUDE_TESTS) |
Alex Brachet | a1762f9 | 2020-03-23 05:50:16 | [diff] [blame] | 399 | add_subdirectory(test) |
| 400 | add_subdirectory(fuzzing) |
| 401 | endif() |
Siva Chandra Reddy | 438f7fc | 2020-06-15 20:54:53 | [diff] [blame] | 402 | |
Guillaume Chatelet | df838db | 2022-03-25 13:21:20 | [diff] [blame] | 403 | if(LIBC_INCLUDE_BENCHMARKS) |
| 404 | add_subdirectory(benchmarks) |
| 405 | endif() |
Jeff Bailey | 4465c29 | 2022-01-29 00:27:34 | [diff] [blame] | 406 | |
| 407 | if (LIBC_INCLUDE_DOCS) |
| 408 | add_subdirectory(docs) |
| 409 | endif() |