1 cmake_minimum_required(VERSION 3.20.0)
2 set(LLVM_SUBPROJECT_TITLE "libc")
4 include(CheckCXXCompilerFlag)
6 # Include LLVM's cmake policies.
7 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
8 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
10 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
13 if (LIBC_CMAKE_VERBOSE_LOGGING)
14 get_directory_property(LIBC_OLD_PREPROCESSOR_DEFS COMPILE_DEFINITIONS)
15 foreach(OLD_DEF ${LIBC_OLD_PREPROCESSOR_DEFS})
16 message(STATUS "Undefining ${OLD_DEF}")
19 set_directory_properties(PROPERTIES
20 # `llvm-project/llvm/CMakeLists.txt` adds the following directive
21 # `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})` We
22 # undo it to be able to precisely control what is getting included.
23 INCLUDE_DIRECTORIES ""
24 # `llvm/cmake/modules/HandleLLVMOptions.cmake` uses `add_compile_definitions`
25 # to set a few preprocessor defines which we do not want.
26 COMPILE_DEFINITIONS ""
28 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
29 add_definitions("-D_DEBUG")
33 set(CMAKE_CXX_STANDARD 17)
35 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
37 # The top-level source directory.
38 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
39 # The top-level directory in which libc is being built.
40 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
42 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")
44 set(LIBC_KERNEL_HEADERS "/usr/include" CACHE STRING "Path to Linux kernel headers")
46 # Defining a global namespace to enclose all libc functions.
47 set(default_namespace "__llvm_libc")
48 if(LLVM_VERSION_MAJOR)
49 set(default_namespace "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}")
51 set(LIBC_NAMESPACE ${default_namespace}
52 CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
56 add_subdirectory(newhdrgen)
58 if(NOT LIBC_USE_NEW_HEADER_GEN)
59 if(LLVM_LIBC_FULL_BUILD OR LLVM_LIBC_GPU_BUILD)
60 if(NOT LIBC_HDRGEN_EXE)
61 # We need to set up hdrgen first since other targets depend on it.
62 add_subdirectory(utils/LibcTableGenUtil)
63 add_subdirectory(utils/HdrGen)
64 # Calling add_tablegen sets variables like LIBC_TABLEGEN_EXE in
65 # PARENT_SCOPE which get lost until saved in the cache.
66 set(LIBC_TABLEGEN_EXE "${LIBC_TABLEGEN_EXE}" CACHE INTERNAL "")
67 set(LIBC_TABLEGEN_TARGET "${LIBC_TABLEGEN_TARGET}" CACHE INTERNAL "")
69 message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
73 # We will build the GPU utilities if we are not doing a runtimes build.
74 option(LIBC_BUILD_GPU_LOADER "Always build the GPU loader utilities" OFF)
75 if(LIBC_BUILD_GPU_LOADER OR (LLVM_LIBC_GPU_BUILD AND NOT LLVM_RUNTIMES_BUILD))
76 add_subdirectory(utils/gpu)
79 option(LIBC_USE_NEW_HEADER_GEN "Generate header files using new headergen instead of the old one" ON)
81 set(NEED_LIBC_HDRGEN FALSE)
82 if(NOT LLVM_RUNTIMES_BUILD)
83 if("libc" IN_LIST LLVM_ENABLE_RUNTIMES)
84 set(NEED_LIBC_HDRGEN TRUE)
86 foreach(_name ${LLVM_RUNTIME_TARGETS})
87 if("libc" IN_LIST RUNTIMES_${_name}_LLVM_ENABLE_RUNTIMES)
88 set(NEED_LIBC_HDRGEN TRUE)
94 option(LIBC_HDRGEN_ONLY "Only build the 'libc-hdrgen' executable" OFF)
95 if(LIBC_HDRGEN_ONLY OR NEED_LIBC_HDRGEN)
96 # When libc is build as part of the runtimes/bootstrap build's CMake run, we
97 # only need to build the host tools to build the libc. So, we just do enough
98 # to build libc-hdrgen and return.
101 unset(NEED_LIBC_HDRGEN)
103 option(LIBC_CMAKE_VERBOSE_LOGGING
104 "Log details warnings and notifications during CMake configuration." OFF)
106 # Path libc/scripts directory.
107 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
109 if(NOT LIBC_NAMESPACE MATCHES "^__llvm_libc")
110 message(FATAL_ERROR "Invalid LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LIBC_NAMESPACE}'")
113 message(STATUS "Setting LIBC_NAMESPACE namespace to '${LIBC_NAMESPACE}'")
114 add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
116 # Flags to pass down to the compiler while building the libc functions.
117 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
118 set(LIBC_TEST_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Common compile options for all the tests.")
120 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
122 # Check --print-resource-dir to find the compiler resource dir if this flag
123 # is supported by the compiler.
125 OUTPUT_STRIP_TRAILING_WHITESPACE
126 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
127 RESULT_VARIABLE COMMAND_RETURN_CODE
128 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
130 # Retrieve the host compiler's resource dir.
131 if(COMMAND_RETURN_CODE EQUAL 0)
132 set(COMPILER_RESOURCE_DIR
133 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
135 message(STATUS "Set COMPILER_RESOURCE_DIR to "
136 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
138 # Try with GCC option: -print-search-dirs, which will output in the form:
141 # So we try to capture the <path> after "install: " in the first line of the
144 OUTPUT_STRIP_TRAILING_WHITESPACE
145 COMMAND ${CMAKE_CXX_COMPILER} -print-search-dirs
146 RESULT_VARIABLE COMMAND_RETURN_CODE
147 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
149 if(COMMAND_RETURN_CODE EQUAL 0)
150 string(REPLACE " " ";" COMPILER_RESOURCE_DIR ${COMPILER_RESOURCE_DIR})
151 string(REPLACE "\n" ";" COMPILER_RESOURCE_DIR "${COMPILER_RESOURCE_DIR}")
152 list(GET COMPILER_RESOURCE_DIR 1 COMPILER_RESOURCE_DIR)
153 message(STATUS "Set COMPILER_RESOURCE_DIR to "
154 "${COMPILER_RESOURCE_DIR} using --print-search-dirs")
156 if (LIBC_TARGET_OS_IS_GPU)
157 message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
159 set(COMPILER_RESOURCE_DIR OFF)
160 message(STATUS "COMPILER_RESOURCE_DIR not set
161 --print-resource-dir not supported by host compiler")
166 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
167 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
168 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
170 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
172 option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF)
174 set(LIBC_ENABLE_UNITTESTS ON)
175 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
177 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
178 include(LLVMLibCArchitectures)
180 set(LIBC_CONFIG_JSON_FILE_LIST "")
182 if(NOT LIBC_CONFIG_PATH)
183 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
184 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
185 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
186 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
187 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
188 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
191 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}")
194 if(NOT LIBC_CONFIG_PATH)
195 message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.")
196 elseif(LIBC_CMAKE_VERBOSE_LOGGING)
197 message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}")
200 # option(LIBC_ENABLE_WIDE_CHARACTERS
201 # "Whether to enable wide character functions on supported platforms. This may
202 # also set flags to enable or disable wide character support within other
203 # functions (e.g. printf)." ON)
205 #TODO: Add carve-out specific config files to the list here.
208 # Config loading happens in three steps:
209 # 1. Load the config file config/config.json and set up config vars.
210 # 2. Load config/${LIBC_TARGET_OS}/config.json if available and override
212 # 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is
213 # available and override vars as suitable.
214 # All the three steps will not override options already set from the
215 # CMake command line. That is, the CMake command line option values take
216 # precedence over the values in config.json files.
217 set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json)
218 read_libc_config(${main_config_file} global_config)
219 foreach(opt IN LISTS global_config)
220 string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0)
222 message(FATAL_ERROR ${json_error})
224 if(DEFINED ${opt_name})
225 # The option is already defined from the command line so we ignore it here.
226 # We still make note of it so that further config load can also ignore
228 message(STATUS "${opt_name}: ${${opt_name}} (from command line)")
229 list(APPEND cmd_line_conf ${opt_name})
233 string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name})
235 message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}")
237 string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value")
239 message(FATAL_ERROR ${json_error})
241 message(STATUS "${opt_name}: ${opt_value}")
242 set(${opt_name} ${opt_value})
244 generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
246 # Load each target specific config.
247 foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST)
248 if(LIBC_CMAKE_VERBOSE_LOGGING)
249 message(STATUS "Loading additional config: '${config_path}/config.json'")
251 load_libc_config(${config_path}/config.json ${cmd_line_conf})
254 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
255 set(LIBC_TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE})
256 if(LIBC_LIBDIR_SUBDIR)
257 string(APPEND LIBC_TARGET_SUBDIR /${LIBC_LIBDIR_SUBDIR})
261 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND (LIBC_ENABLE_USE_BY_CLANG OR LIBC_TARGET_OS_IS_GPU))
262 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
263 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
264 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LIBC_TARGET_SUBDIR})
266 if(NOT LIBC_ENABLE_USE_BY_CLANG)
267 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
268 set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
269 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
270 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include)
271 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
273 set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
274 set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
276 if(LIBC_TARGET_OS_IS_GPU)
277 if(LIBC_TARGET_TRIPLE)
278 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LIBC_TARGET_TRIPLE})
280 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
283 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
287 if(LIBC_TARGET_TRIPLE)
288 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_TRIPLE})
289 elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
290 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_SUBDIR})
292 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX})
295 if(LIBC_TARGET_OS_IS_GPU)
296 include(prepare_libc_gpu_build)
297 set(LIBC_ENABLE_UNITTESTS OFF)
300 include(LLVMLibCCheckMPFR)
302 if(LLVM_LIBC_CLANG_TIDY)
303 set(LLVM_LIBC_ENABLE_LINTING ON)
306 if(LLVM_LIBC_ENABLE_LINTING)
307 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
308 set(LLVM_LIBC_ENABLE_LINTING OFF)
309 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
311 if (NOT LLVM_LIBC_CLANG_TIDY)
312 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
315 if(LLVM_LIBC_CLANG_TIDY)
316 # Check clang-tidy major version.
317 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
318 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT
319 ERROR_VARIABLE CLANG_TIDY_ERROR
320 RESULT_VARIABLE CLANG_TIDY_RESULT)
322 if(CLANG_TIDY_RESULT AND NOT CLANG_TIDY_RESULT EQUAL 0)
323 message(FATAL_ERROR "Failed to execute '${LLVM_LIBC_CLANG_TIDY} --version'
324 output : '${CLANG_TIDY_OUTPUT}'
325 error : '${CLANG_TIDY_ERROR}'
326 result : '${CLANG_TIDY_RESULT}'
329 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
330 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
331 "${CMAKE_CXX_COMPILER_VERSION}")
333 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
334 set(LLVM_LIBC_ENABLE_LINTING OFF)
336 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
337 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
340 The path to the clang-tidy binary can be set manually by passing
341 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
343 add_custom_target(libc-lint)
345 message(FATAL_ERROR "
346 Linting is enabled but 'clang-tidy' is not found!
348 The path to the clang-tidy binary can be set manually by passing
349 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
351 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
352 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
357 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
358 if(LLVM_LIBC_INCLUDE_SCUDO)
359 if (LLVM_LIBC_COMPILER_RT_PATH)
360 add_subdirectory(${LLVM_LIBC_COMPILER_RT_PATH} ${CMAKE_CURRENT_BINARY_DIR}/compiler-rt)
361 elseif(NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES))
362 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES")
366 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
368 include(CMakeParseArguments)
369 include(LLVMLibCCheckCpuFeatures)
370 include(CheckCompilerFeatures)
371 include(LLVMLibCRules)
373 set(TARGET_LLVMLIBC_ENTRYPOINTS "")
374 set(TARGET_LIBC_ENTRYPOINTS "")
375 set(TARGET_LIBM_ENTRYPOINTS "")
376 set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "")
378 # Check entrypoints.txt
379 if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt")
380 include("${LIBC_CONFIG_PATH}/entrypoints.txt")
382 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.")
386 if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt")
387 include("${LIBC_CONFIG_PATH}/headers.txt")
388 elseif(LLVM_LIBC_FULL_BUILD)
389 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.")
392 # Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list
393 if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt")
394 include("${LIBC_CONFIG_PATH}/exclude.txt")
397 # #TODO: Set up support for premade configs adding their own exclude lists.
399 foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS)
400 if(LIBC_CMAKE_VERBOSE_LOGGING)
401 message(STATUS "Removing entrypoint ${removed_entrypoint}")
403 list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint})
404 list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint})
405 list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint})
408 set(TARGET_ENTRYPOINT_NAME_LIST "")
409 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
410 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
411 if(${last_dot_loc} EQUAL -1)
412 message(FATAL_ERROR "Invalid entrypoint target name ${entrypoint}; Expected"
413 " a '.' (dot) in the name.")
415 math(EXPR name_loc "${last_dot_loc} + 1")
416 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
417 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
420 add_subdirectory(include)
421 add_subdirectory(config)
422 add_subdirectory(hdr)
423 add_subdirectory(src)
424 add_subdirectory(utils)
426 if(LLVM_LIBC_FULL_BUILD)
427 # The startup system can potentially depend on the library components so add
428 # it after the library implementation directories.
429 add_subdirectory(startup)
432 # The lib and test directories are added at the very end as tests
433 # and libraries potentially draw from the components present in all
434 # of the other directories.
435 add_subdirectory(lib)
436 if(LLVM_INCLUDE_TESTS)
437 add_subdirectory(test)
438 add_subdirectory(fuzzing)
441 add_subdirectory(benchmarks)
443 if (LIBC_INCLUDE_DOCS)
444 add_subdirectory(docs)