1cmake_minimum_required(VERSION 3.20.0) 2set(LLVM_SUBPROJECT_TITLE "libc") 3 4# Include LLVM's cmake policies. 5if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS) 6 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) 7endif() 8include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake 9 NO_POLICY_SCOPE) 10 11if (LIBC_CMAKE_VERBOSE_LOGGING) 12 get_directory_property(LIBC_OLD_PREPROCESSOR_DEFS COMPILE_DEFINITIONS) 13 foreach(OLD_DEF ${LIBC_OLD_PREPROCESSOR_DEFS}) 14 message(STATUS "Undefining ${OLD_DEF}") 15 endforeach() 16endif() 17set_directory_properties(PROPERTIES 18 # `llvm-project/llvm/CMakeLists.txt` adds the following directive 19 # `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})` We 20 # undo it to be able to precisely control what is getting included. 21 INCLUDE_DIRECTORIES "" 22 # `llvm/cmake/modules/HandleLLVMOptions.cmake` uses `add_compile_definitions` 23 # to set a few preprocessor defines which we do not want. 24 COMPILE_DEFINITIONS "" 25) 26if (CMAKE_BUILD_TYPE STREQUAL "Debug") 27 add_definitions("-D_DEBUG") 28endif() 29 30# Default to C++17 31set(CMAKE_CXX_STANDARD 17) 32 33list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") 34 35# The top-level source directory. 36set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 37# The top-level directory in which libc is being built. 38set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}) 39 40set(LIBC_ENABLE_USE_BY_CLANG OFF CACHE BOOL "Whether or not to place libc in a build directory findable by a just built clang") 41 42set(LIBC_KERNEL_HEADERS "/usr/include" CACHE STRING "Path to Linux kernel headers") 43 44# Defining a global namespace to enclose all libc functions. 45set(default_namespace "__llvm_libc") 46if(LLVM_VERSION_MAJOR) 47 set(default_namespace "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}") 48endif() 49set(LIBC_NAMESPACE ${default_namespace} 50 CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'." 51) 52 53# We will build the GPU utilities if we are not doing a runtimes build. 54option(LIBC_BUILD_GPU_LOADER "Always build the GPU loader utilities" OFF) 55if(LIBC_BUILD_GPU_LOADER OR NOT LLVM_RUNTIMES_BUILD) 56 foreach(_name ${LLVM_RUNTIME_TARGETS}) 57 if("libc" IN_LIST RUNTIMES_${_name}_LLVM_ENABLE_RUNTIMES) 58 if("${_name}" STREQUAL "amdgcn-amd-amdhsa" OR "${_name}" STREQUAL "nvptx64-nvidia-cuda") 59 set(LIBC_NEED_LOADER_UTILS TRUE) 60 endif() 61 endif() 62 endforeach() 63 if("${LIBC_TARGET_TRIPLE}" STREQUAL "amdgcn-amd-amdhsa" OR 64 "${LIBC_TARGET_TRIPLE}" STREQUAL "nvptx64-nvidia-cuda") 65 set(LIBC_NEED_LOADER_UTILS TRUE) 66 endif() 67 if(LIBC_NEED_LOADER_UTILS) 68 add_subdirectory(utils/gpu) 69 return() 70 endif() 71endif() 72 73add_subdirectory(newhdrgen) 74 75option(LIBC_CMAKE_VERBOSE_LOGGING 76 "Log details warnings and notifications during CMake configuration." OFF) 77 78# Path libc/scripts directory. 79set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts") 80 81if(NOT LIBC_NAMESPACE MATCHES "^__llvm_libc") 82 message(FATAL_ERROR "Invalid LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LIBC_NAMESPACE}'") 83endif() 84 85message(STATUS "Setting LIBC_NAMESPACE namespace to '${LIBC_NAMESPACE}'") 86add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE}) 87 88# Flags to pass down to the compiler while building the libc functions. 89set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)") 90set(LIBC_TEST_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Common compile options for all the tests.") 91 92list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS}) 93 94# Check --print-resource-dir to find the compiler resource dir if this flag 95# is supported by the compiler. 96execute_process( 97 OUTPUT_STRIP_TRAILING_WHITESPACE 98 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir 99 RESULT_VARIABLE COMMAND_RETURN_CODE 100 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR 101) 102# Retrieve the host compiler's resource dir. 103if(COMMAND_RETURN_CODE EQUAL 0) 104 set(COMPILER_RESOURCE_DIR 105 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir" 106 ) 107 message(STATUS "Set COMPILER_RESOURCE_DIR to " 108 "${COMPILER_RESOURCE_DIR} using --print-resource-dir") 109else() 110 # Try with GCC option: -print-search-dirs, which will output in the form: 111 # install: <path> 112 # programs: ........ 113 # So we try to capture the <path> after "install: " in the first line of the 114 # output. 115 execute_process( 116 OUTPUT_STRIP_TRAILING_WHITESPACE 117 COMMAND ${CMAKE_CXX_COMPILER} -print-search-dirs 118 RESULT_VARIABLE COMMAND_RETURN_CODE 119 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR 120 ) 121 if(COMMAND_RETURN_CODE EQUAL 0) 122 string(REPLACE " " ";" COMPILER_RESOURCE_DIR ${COMPILER_RESOURCE_DIR}) 123 string(REPLACE "\n" ";" COMPILER_RESOURCE_DIR "${COMPILER_RESOURCE_DIR}") 124 list(GET COMPILER_RESOURCE_DIR 1 COMPILER_RESOURCE_DIR) 125 message(STATUS "Set COMPILER_RESOURCE_DIR to " 126 "${COMPILER_RESOURCE_DIR} using --print-search-dirs") 127else() 128 if (LIBC_TARGET_OS_IS_GPU) 129 message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds") 130 else() 131 set(COMPILER_RESOURCE_DIR OFF) 132 message(STATUS "COMPILER_RESOURCE_DIR not set 133 --print-resource-dir not supported by host compiler") 134 endif() 135 endif() 136endif() 137 138option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF) 139option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON) 140option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF) 141 142set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.") 143 144option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF) 145 146set(LIBC_ENABLE_UNITTESTS ON) 147set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD}) 148 149# Defines LIBC_TARGET_ARCHITECTURE and associated macros. 150include(LLVMLibCArchitectures) 151 152set(LIBC_CONFIG_JSON_FILE_LIST "") 153 154if(NOT LIBC_CONFIG_PATH) 155 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}") 156 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}") 157 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}") 158 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}") 159 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}") 160 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}") 161 endif() 162else() 163 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}") 164endif() 165 166if(NOT LIBC_CONFIG_PATH) 167 message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.") 168elseif(LIBC_CMAKE_VERBOSE_LOGGING) 169 message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}") 170endif() 171 172# option(LIBC_ENABLE_WIDE_CHARACTERS 173# "Whether to enable wide character functions on supported platforms. This may 174# also set flags to enable or disable wide character support within other 175# functions (e.g. printf)." ON) 176 177#TODO: Add carve-out specific config files to the list here. 178 179include(LibcConfig) 180# Config loading happens in three steps: 181# 1. Load the config file config/config.json and set up config vars. 182# 2. Load config/${LIBC_TARGET_OS}/config.json if available and override 183# vars as suitable. 184# 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is 185# available and override vars as suitable. 186# All the three steps will not override options already set from the 187# CMake command line. That is, the CMake command line option values take 188# precedence over the values in config.json files. 189set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json) 190read_libc_config(${main_config_file} global_config) 191foreach(opt IN LISTS global_config) 192 string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0) 193 if(json_error) 194 message(FATAL_ERROR ${json_error}) 195 endif() 196 if(DEFINED ${opt_name}) 197 # The option is already defined from the command line so we ignore it here. 198 # We still make note of it so that further config load can also ignore 199 # this option. 200 message(STATUS "${opt_name}: ${${opt_name}} (from command line)") 201 list(APPEND cmd_line_conf ${opt_name}) 202 continue() 203 endif() 204 205 string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name}) 206 if(json_error) 207 message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}") 208 endif() 209 string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value") 210 if(json_error) 211 message(FATAL_ERROR ${json_error}) 212 endif() 213 message(STATUS "${opt_name}: ${opt_value}") 214 set(${opt_name} ${opt_value}) 215endforeach() 216generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst) 217 218# Load each target specific config. 219foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST) 220 if(LIBC_CMAKE_VERBOSE_LOGGING) 221 message(STATUS "Loading additional config: '${config_path}/config.json'") 222 endif() 223 load_libc_config(${config_path}/config.json ${cmd_line_conf}) 224endforeach() 225 226if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR) 227 set(LIBC_TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE}) 228 if(LIBC_LIBDIR_SUBDIR) 229 string(APPEND LIBC_TARGET_SUBDIR /${LIBC_LIBDIR_SUBDIR}) 230 endif() 231endif() 232 233if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND (LIBC_ENABLE_USE_BY_CLANG OR LIBC_TARGET_OS_IS_GPU)) 234 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE}) 235 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}) 236 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LIBC_TARGET_SUBDIR}) 237else() 238 if(NOT LIBC_ENABLE_USE_BY_CLANG) 239 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include) 240 set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib) 241 elseif(LLVM_LIBRARY_OUTPUT_INTDIR) 242 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include) 243 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) 244 else() 245 set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include) 246 set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}) 247 endif() 248 if(LIBC_TARGET_OS_IS_GPU) 249 if(LIBC_TARGET_TRIPLE) 250 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LIBC_TARGET_TRIPLE}) 251 else() 252 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}) 253 endif() 254 else() 255 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}) 256 endif() 257endif() 258 259if(LIBC_TARGET_TRIPLE) 260 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_TRIPLE}) 261elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR) 262 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_SUBDIR}) 263else() 264 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}) 265endif() 266 267if(LIBC_TARGET_OS_IS_GPU) 268 include(prepare_libc_gpu_build) 269 set(LIBC_ENABLE_UNITTESTS OFF) 270endif() 271 272include(LLVMLibCCheckMPFR) 273 274if(LLVM_LIBC_CLANG_TIDY) 275 set(LLVM_LIBC_ENABLE_LINTING ON) 276endif() 277 278if(LLVM_LIBC_ENABLE_LINTING) 279 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 280 set(LLVM_LIBC_ENABLE_LINTING OFF) 281 message(WARNING "C++ compiler is not clang++, linting with be disabled.") 282 else() 283 if (NOT LLVM_LIBC_CLANG_TIDY) 284 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy) 285 endif() 286 287 if(LLVM_LIBC_CLANG_TIDY) 288 # Check clang-tidy major version. 289 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version" 290 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT 291 ERROR_VARIABLE CLANG_TIDY_ERROR 292 RESULT_VARIABLE CLANG_TIDY_RESULT) 293 294 if(CLANG_TIDY_RESULT AND NOT CLANG_TIDY_RESULT EQUAL 0) 295 message(FATAL_ERROR "Failed to execute '${LLVM_LIBC_CLANG_TIDY} --version' 296 output : '${CLANG_TIDY_OUTPUT}' 297 error : '${CLANG_TIDY_ERROR}' 298 result : '${CLANG_TIDY_RESULT}' 299 ") 300 endif() 301 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}") 302 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION 303 "${CMAKE_CXX_COMPILER_VERSION}") 304 305 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION) 306 set(LLVM_LIBC_ENABLE_LINTING OFF) 307 message(WARNING " 308 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as 309 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will 310 be disabled. 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 endif() 315 add_custom_target(libc-lint) 316 else() 317 message(FATAL_ERROR " 318 Linting is enabled but 'clang-tidy' is not found! 319 320 The path to the clang-tidy binary can be set manually by passing 321 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake. 322 323 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF 324 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).") 325 endif() 326 endif() 327endif() 328 329option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF) 330if(LLVM_LIBC_INCLUDE_SCUDO) 331 if (NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES)) 332 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES") 333 endif() 334endif() 335 336option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS}) 337 338include(CMakeParseArguments) 339include(LLVMLibCCheckCpuFeatures) 340include(CheckCompilerFeatures) 341include(LLVMLibCRules) 342 343set(TARGET_LLVMLIBC_ENTRYPOINTS "") 344set(TARGET_LIBC_ENTRYPOINTS "") 345set(TARGET_LIBM_ENTRYPOINTS "") 346set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "") 347 348# Check entrypoints.txt 349if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt") 350 include("${LIBC_CONFIG_PATH}/entrypoints.txt") 351else() 352 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.") 353endif() 354 355# Check headers.txt 356if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt") 357 include("${LIBC_CONFIG_PATH}/headers.txt") 358elseif(LLVM_LIBC_FULL_BUILD) 359 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.") 360endif() 361 362# Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list 363if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt") 364 include("${LIBC_CONFIG_PATH}/exclude.txt") 365endif() 366 367# #TODO: Set up support for premade configs adding their own exclude lists. 368 369foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS) 370 if(LIBC_CMAKE_VERBOSE_LOGGING) 371 message(STATUS "Removing entrypoint ${removed_entrypoint}") 372 endif() 373 list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint}) 374 list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint}) 375 list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint}) 376endforeach() 377 378set(TARGET_ENTRYPOINT_NAME_LIST "") 379foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS) 380 string(FIND ${entrypoint} "." last_dot_loc REVERSE) 381 if(${last_dot_loc} EQUAL -1) 382 message(FATAL_ERROR "Invalid entrypoint target name ${entrypoint}; Expected" 383 " a '.' (dot) in the name.") 384 endif() 385 math(EXPR name_loc "${last_dot_loc} + 1") 386 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name) 387 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name}) 388endforeach() 389 390if(MSVC AND NOT MSYS) 391 set(libc_opt_high_flag "/O2") 392else() 393 set(libc_opt_high_flag "-O3") 394endif() 395 396add_subdirectory(include) 397add_subdirectory(config) 398add_subdirectory(hdr) 399add_subdirectory(src) 400add_subdirectory(utils) 401 402if(LLVM_LIBC_FULL_BUILD) 403 # The startup system can potentially depend on the library components so add 404 # it after the library implementation directories. 405 add_subdirectory(startup) 406endif() 407 408# The lib and test directories are added at the very end as tests 409# and libraries potentially draw from the components present in all 410# of the other directories. 411add_subdirectory(lib) 412if(LLVM_INCLUDE_TESTS) 413 add_subdirectory(test) 414 add_subdirectory(fuzzing) 415endif() 416 417add_subdirectory(benchmarks) 418 419if (LIBC_INCLUDE_DOCS) 420 add_subdirectory(docs) 421endif() 422