1 cmake_minimum_required(VERSION 3.20.0)
2 set(LLVM_SUBPROJECT_TITLE "libc")
4 # Include LLVM's cmake policies.
5 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
6 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
8 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
11 if (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}")
17 set_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 ""
26 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
27 add_definitions("-D_DEBUG")
31 set(CMAKE_CXX_STANDARD 17)
33 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
35 # The top-level source directory.
36 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
37 # The top-level directory in which libc is being built.
38 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
40 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")
42 set(LIBC_KERNEL_HEADERS "/usr/include" CACHE STRING "Path to Linux kernel headers")
44 # Defining a global namespace to enclose all libc functions.
45 set(default_namespace "__llvm_libc")
46 if(LLVM_VERSION_MAJOR)
47 set(default_namespace "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}")
49 set(LIBC_NAMESPACE ${default_namespace}
50 CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
54 add_subdirectory(newhdrgen)
57 if(LLVM_LIBC_FULL_BUILD OR LLVM_LIBC_GPU_BUILD)
58 if(NOT LIBC_HDRGEN_EXE)
59 # We need to set up hdrgen first since other targets depend on it.
60 add_subdirectory(utils/LibcTableGenUtil)
61 add_subdirectory(utils/HdrGen)
62 # Calling add_tablegen sets variables like LIBC_TABLEGEN_EXE in
63 # PARENT_SCOPE which get lost until saved in the cache.
64 set(LIBC_TABLEGEN_EXE "${LIBC_TABLEGEN_EXE}" CACHE INTERNAL "")
65 set(LIBC_TABLEGEN_TARGET "${LIBC_TABLEGEN_TARGET}" CACHE INTERNAL "")
67 message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
70 # We will build the GPU utilities if we are not doing a runtimes build.
71 option(LIBC_BUILD_GPU_LOADER "Always build the GPU loader utilities" OFF)
72 if(LIBC_BUILD_GPU_LOADER OR (LLVM_LIBC_GPU_BUILD AND NOT LLVM_RUNTIMES_BUILD))
73 add_subdirectory(utils/gpu)
76 option(LIBC_USE_NEW_HEADER_GEN "Generate header files using new headergen instead of the old one" ON)
78 set(NEED_LIBC_HDRGEN FALSE)
79 if(NOT LLVM_RUNTIMES_BUILD)
80 if("libc" IN_LIST LLVM_ENABLE_RUNTIMES)
81 set(NEED_LIBC_HDRGEN TRUE)
83 foreach(_name ${LLVM_RUNTIME_TARGETS})
84 if("libc" IN_LIST RUNTIMES_${_name}_LLVM_ENABLE_RUNTIMES)
85 set(NEED_LIBC_HDRGEN TRUE)
91 option(LIBC_HDRGEN_ONLY "Only build the 'libc-hdrgen' executable" OFF)
92 if(LIBC_HDRGEN_ONLY OR NEED_LIBC_HDRGEN)
93 # When libc is build as part of the runtimes/bootstrap build's CMake run, we
94 # only need to build the host tools to build the libc. So, we just do enough
95 # to build libc-hdrgen and return.
98 unset(NEED_LIBC_HDRGEN)
100 option(LIBC_CMAKE_VERBOSE_LOGGING
101 "Log details warnings and notifications during CMake configuration." OFF)
103 # Path libc/scripts directory.
104 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
106 if(NOT LIBC_NAMESPACE MATCHES "^__llvm_libc")
107 message(FATAL_ERROR "Invalid LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LIBC_NAMESPACE}'")
110 message(STATUS "Setting LIBC_NAMESPACE namespace to '${LIBC_NAMESPACE}'")
111 add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
113 # Flags to pass down to the compiler while building the libc functions.
114 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
115 set(LIBC_TEST_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Common compile options for all the tests.")
117 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
119 # Check --print-resource-dir to find the compiler resource dir if this flag
120 # is supported by the compiler.
122 OUTPUT_STRIP_TRAILING_WHITESPACE
123 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
124 RESULT_VARIABLE COMMAND_RETURN_CODE
125 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
127 # Retrieve the host compiler's resource dir.
128 if(COMMAND_RETURN_CODE EQUAL 0)
129 set(COMPILER_RESOURCE_DIR
130 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
132 message(STATUS "Set COMPILER_RESOURCE_DIR to "
133 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
135 # Try with GCC option: -print-search-dirs, which will output in the form:
138 # So we try to capture the <path> after "install: " in the first line of the
141 OUTPUT_STRIP_TRAILING_WHITESPACE
142 COMMAND ${CMAKE_CXX_COMPILER} -print-search-dirs
143 RESULT_VARIABLE COMMAND_RETURN_CODE
144 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
146 if(COMMAND_RETURN_CODE EQUAL 0)
147 string(REPLACE " " ";" COMPILER_RESOURCE_DIR ${COMPILER_RESOURCE_DIR})
148 string(REPLACE "\n" ";" COMPILER_RESOURCE_DIR "${COMPILER_RESOURCE_DIR}")
149 list(GET COMPILER_RESOURCE_DIR 1 COMPILER_RESOURCE_DIR)
150 message(STATUS "Set COMPILER_RESOURCE_DIR to "
151 "${COMPILER_RESOURCE_DIR} using --print-search-dirs")
153 if (LIBC_TARGET_OS_IS_GPU)
154 message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
156 set(COMPILER_RESOURCE_DIR OFF)
157 message(STATUS "COMPILER_RESOURCE_DIR not set
158 --print-resource-dir not supported by host compiler")
163 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
164 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
165 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
167 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
169 option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF)
171 set(LIBC_ENABLE_UNITTESTS ON)
172 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
174 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
175 include(LLVMLibCArchitectures)
177 set(LIBC_CONFIG_JSON_FILE_LIST "")
179 if(NOT LIBC_CONFIG_PATH)
180 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
181 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
182 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
183 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
184 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
185 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
188 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}")
191 if(NOT LIBC_CONFIG_PATH)
192 message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.")
193 elseif(LIBC_CMAKE_VERBOSE_LOGGING)
194 message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}")
197 # option(LIBC_ENABLE_WIDE_CHARACTERS
198 # "Whether to enable wide character functions on supported platforms. This may
199 # also set flags to enable or disable wide character support within other
200 # functions (e.g. printf)." ON)
202 #TODO: Add carve-out specific config files to the list here.
205 # Config loading happens in three steps:
206 # 1. Load the config file config/config.json and set up config vars.
207 # 2. Load config/${LIBC_TARGET_OS}/config.json if available and override
209 # 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is
210 # available and override vars as suitable.
211 # All the three steps will not override options already set from the
212 # CMake command line. That is, the CMake command line option values take
213 # precedence over the values in config.json files.
214 set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json)
215 read_libc_config(${main_config_file} global_config)
216 foreach(opt IN LISTS global_config)
217 string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0)
219 message(FATAL_ERROR ${json_error})
221 if(DEFINED ${opt_name})
222 # The option is already defined from the command line so we ignore it here.
223 # We still make note of it so that further config load can also ignore
225 message(STATUS "${opt_name}: ${${opt_name}} (from command line)")
226 list(APPEND cmd_line_conf ${opt_name})
230 string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name})
232 message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}")
234 string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value")
236 message(FATAL_ERROR ${json_error})
238 message(STATUS "${opt_name}: ${opt_value}")
239 set(${opt_name} ${opt_value})
241 generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
243 # Load each target specific config.
244 foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST)
245 if(LIBC_CMAKE_VERBOSE_LOGGING)
246 message(STATUS "Loading additional config: '${config_path}/config.json'")
248 load_libc_config(${config_path}/config.json ${cmd_line_conf})
251 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
252 set(LIBC_TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE})
253 if(LIBC_LIBDIR_SUBDIR)
254 string(APPEND LIBC_TARGET_SUBDIR /${LIBC_LIBDIR_SUBDIR})
258 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND (LIBC_ENABLE_USE_BY_CLANG OR LIBC_TARGET_OS_IS_GPU))
259 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
260 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
261 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LIBC_TARGET_SUBDIR})
263 if(NOT LIBC_ENABLE_USE_BY_CLANG)
264 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
265 set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
266 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
267 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include)
268 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
270 set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
271 set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
273 if(LIBC_TARGET_OS_IS_GPU)
274 if(LIBC_TARGET_TRIPLE)
275 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LIBC_TARGET_TRIPLE})
277 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
280 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
284 if(LIBC_TARGET_TRIPLE)
285 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_TRIPLE})
286 elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
287 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_SUBDIR})
289 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX})
292 if(LIBC_TARGET_OS_IS_GPU)
293 include(prepare_libc_gpu_build)
294 set(LIBC_ENABLE_UNITTESTS OFF)
297 include(LLVMLibCCheckMPFR)
299 if(LLVM_LIBC_CLANG_TIDY)
300 set(LLVM_LIBC_ENABLE_LINTING ON)
303 if(LLVM_LIBC_ENABLE_LINTING)
304 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
305 set(LLVM_LIBC_ENABLE_LINTING OFF)
306 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
308 if (NOT LLVM_LIBC_CLANG_TIDY)
309 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
312 if(LLVM_LIBC_CLANG_TIDY)
313 # Check clang-tidy major version.
314 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
315 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT
316 ERROR_VARIABLE CLANG_TIDY_ERROR
317 RESULT_VARIABLE CLANG_TIDY_RESULT)
319 if(CLANG_TIDY_RESULT AND NOT CLANG_TIDY_RESULT EQUAL 0)
320 message(FATAL_ERROR "Failed to execute '${LLVM_LIBC_CLANG_TIDY} --version'
321 output : '${CLANG_TIDY_OUTPUT}'
322 error : '${CLANG_TIDY_ERROR}'
323 result : '${CLANG_TIDY_RESULT}'
326 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
327 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
328 "${CMAKE_CXX_COMPILER_VERSION}")
330 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
331 set(LLVM_LIBC_ENABLE_LINTING OFF)
333 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
334 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
337 The path to the clang-tidy binary can be set manually by passing
338 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
340 add_custom_target(libc-lint)
342 message(FATAL_ERROR "
343 Linting is enabled but 'clang-tidy' is not found!
345 The path to the clang-tidy binary can be set manually by passing
346 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
348 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
349 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
354 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
355 if(LLVM_LIBC_INCLUDE_SCUDO)
356 if (NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES))
357 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES")
361 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
363 include(CMakeParseArguments)
364 include(LLVMLibCCheckCpuFeatures)
365 include(CheckCompilerFeatures)
366 include(LLVMLibCRules)
368 set(TARGET_LLVMLIBC_ENTRYPOINTS "")
369 set(TARGET_LIBC_ENTRYPOINTS "")
370 set(TARGET_LIBM_ENTRYPOINTS "")
371 set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "")
373 # Check entrypoints.txt
374 if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt")
375 include("${LIBC_CONFIG_PATH}/entrypoints.txt")
377 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.")
381 if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt")
382 include("${LIBC_CONFIG_PATH}/headers.txt")
383 elseif(LLVM_LIBC_FULL_BUILD)
384 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.")
387 # Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list
388 if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt")
389 include("${LIBC_CONFIG_PATH}/exclude.txt")
392 # #TODO: Set up support for premade configs adding their own exclude lists.
394 foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS)
395 if(LIBC_CMAKE_VERBOSE_LOGGING)
396 message(STATUS "Removing entrypoint ${removed_entrypoint}")
398 list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint})
399 list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint})
400 list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint})
403 set(TARGET_ENTRYPOINT_NAME_LIST "")
404 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
405 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
406 if(${last_dot_loc} EQUAL -1)
407 message(FATAL_ERROR "Invalid entrypoint target name ${entrypoint}; Expected"
408 " a '.' (dot) in the name.")
410 math(EXPR name_loc "${last_dot_loc} + 1")
411 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
412 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
415 add_subdirectory(include)
416 add_subdirectory(config)
417 add_subdirectory(hdr)
418 add_subdirectory(src)
419 add_subdirectory(utils)
421 if(LLVM_LIBC_FULL_BUILD)
422 # The startup system can potentially depend on the library components so add
423 # it after the library implementation directories.
424 add_subdirectory(startup)
427 # The lib and test directories are added at the very end as tests
428 # and libraries potentially draw from the components present in all
429 # of the other directories.
430 add_subdirectory(lib)
431 if(LLVM_INCLUDE_TESTS)
432 add_subdirectory(test)
433 add_subdirectory(fuzzing)
436 add_subdirectory(benchmarks)
438 if (LIBC_INCLUDE_DOCS)
439 add_subdirectory(docs)