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
10 include(CheckCXXCompilerFlag)
12 if (LIBC_CMAKE_VERBOSE_LOGGING)
13 get_directory_property(LIBC_OLD_PREPROCESSOR_DEFS COMPILE_DEFINITIONS)
14 foreach(OLD_DEF ${LIBC_OLD_PREPROCESSOR_DEFS})
15 message(STATUS "Undefining ${OLD_DEF}")
18 set_directory_properties(PROPERTIES
19 # `llvm-project/llvm/CMakeLists.txt` adds the following directive
20 # `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})` We
21 # undo it to be able to precisely control what is getting included.
22 INCLUDE_DIRECTORIES ""
23 # `llvm/cmake/modules/HandleLLVMOptions.cmake` uses `add_compile_definitions`
24 # to set a few preprocessor defines which we do not want.
25 COMPILE_DEFINITIONS ""
27 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
28 add_definitions("-D_DEBUG")
32 set(CMAKE_CXX_STANDARD 17)
34 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
36 # The top-level source directory.
37 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
38 # The top-level directory in which libc is being built.
39 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
41 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")
43 set(LIBC_KERNEL_HEADERS "/usr/include" CACHE STRING "Path to Linux kernel headers")
45 # Defining a global namespace to enclose all libc functions.
46 set(default_namespace "__llvm_libc")
47 if(LLVM_VERSION_MAJOR)
48 set(default_namespace "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}")
50 set(LIBC_NAMESPACE ${default_namespace}
51 CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
54 # We will build the GPU utilities if we are not doing a runtimes build.
55 option(LIBC_BUILD_GPU_LOADER "Always build the GPU loader utilities" OFF)
56 if(LIBC_BUILD_GPU_LOADER OR ((NOT LLVM_RUNTIMES_BUILD) AND LLVM_LIBC_GPU_BUILD))
57 add_subdirectory(utils/gpu)
61 add_subdirectory(hdrgen)
63 option(LIBC_CMAKE_VERBOSE_LOGGING
64 "Log details warnings and notifications during CMake configuration." OFF)
66 # Path libc/scripts directory.
67 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
69 if(NOT LIBC_NAMESPACE MATCHES "^__llvm_libc")
70 message(FATAL_ERROR "Invalid LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LIBC_NAMESPACE}'")
73 message(STATUS "Setting LIBC_NAMESPACE namespace to '${LIBC_NAMESPACE}'")
74 add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
76 # Flags to pass down to the compiler while building the libc functions.
77 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
78 set(LIBC_TEST_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Common compile options for all the tests.")
80 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
82 # Check --print-resource-dir to find the compiler resource dir if this flag
83 # is supported by the compiler.
85 OUTPUT_STRIP_TRAILING_WHITESPACE
86 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
87 RESULT_VARIABLE COMMAND_RETURN_CODE
88 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
90 # Retrieve the host compiler's resource dir.
91 if(COMMAND_RETURN_CODE EQUAL 0)
92 set(COMPILER_RESOURCE_DIR
93 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
95 message(STATUS "Set COMPILER_RESOURCE_DIR to "
96 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
98 # Try with GCC option: -print-search-dirs, which will output in the form:
101 # So we try to capture the <path> after "install: " in the first line of the
104 OUTPUT_STRIP_TRAILING_WHITESPACE
105 COMMAND ${CMAKE_CXX_COMPILER} -print-search-dirs
106 RESULT_VARIABLE COMMAND_RETURN_CODE
107 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
109 if(COMMAND_RETURN_CODE EQUAL 0)
110 string(REPLACE " " ";" COMPILER_RESOURCE_DIR ${COMPILER_RESOURCE_DIR})
111 string(REPLACE "\n" ";" COMPILER_RESOURCE_DIR "${COMPILER_RESOURCE_DIR}")
112 list(GET COMPILER_RESOURCE_DIR 1 COMPILER_RESOURCE_DIR)
113 message(STATUS "Set COMPILER_RESOURCE_DIR to "
114 "${COMPILER_RESOURCE_DIR} using --print-search-dirs")
116 if (LIBC_TARGET_OS_IS_GPU)
117 message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
119 set(COMPILER_RESOURCE_DIR OFF)
120 message(STATUS "COMPILER_RESOURCE_DIR not set
121 --print-resource-dir not supported by host compiler")
126 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
127 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
128 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
130 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
132 option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF)
134 set(LIBC_ENABLE_UNITTESTS ON)
135 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
137 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
138 include(LLVMLibCArchitectures)
140 set(LIBC_CONFIG_JSON_FILE_LIST "")
142 if(NOT LIBC_CONFIG_PATH)
143 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
144 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
145 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
146 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
147 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
148 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
151 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}")
154 if(NOT LIBC_CONFIG_PATH)
155 message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.")
156 elseif(LIBC_CMAKE_VERBOSE_LOGGING)
157 message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}")
160 # option(LIBC_ENABLE_WIDE_CHARACTERS
161 # "Whether to enable wide character functions on supported platforms. This may
162 # also set flags to enable or disable wide character support within other
163 # functions (e.g. printf)." ON)
165 #TODO: Add carve-out specific config files to the list here.
168 # Config loading happens in three steps:
169 # 1. Load the config file config/config.json and set up config vars.
170 # 2. Load config/${LIBC_TARGET_OS}/config.json if available and override
172 # 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is
173 # available and override vars as suitable.
174 # All the three steps will not override options already set from the
175 # CMake command line. That is, the CMake command line option values take
176 # precedence over the values in config.json files.
177 set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json)
178 read_libc_config(${main_config_file} global_config)
179 foreach(opt IN LISTS global_config)
180 string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0)
182 message(FATAL_ERROR ${json_error})
184 if(DEFINED ${opt_name})
185 # The option is already defined from the command line so we ignore it here.
186 # We still make note of it so that further config load can also ignore
188 message(STATUS "${opt_name}: ${${opt_name}} (from command line)")
189 list(APPEND cmd_line_conf ${opt_name})
193 string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name})
195 message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}")
197 string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value")
199 message(FATAL_ERROR ${json_error})
201 message(STATUS "${opt_name}: ${opt_value}")
202 set(${opt_name} ${opt_value})
204 generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
206 # Load each target specific config.
207 foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST)
208 if(LIBC_CMAKE_VERBOSE_LOGGING)
209 message(STATUS "Loading additional config: '${config_path}/config.json'")
211 load_libc_config(${config_path}/config.json ${cmd_line_conf})
214 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
215 set(LIBC_TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE})
216 if(LIBC_LIBDIR_SUBDIR)
217 string(APPEND LIBC_TARGET_SUBDIR /${LIBC_LIBDIR_SUBDIR})
221 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND (LIBC_ENABLE_USE_BY_CLANG OR LIBC_TARGET_OS_IS_GPU))
222 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
223 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
224 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LIBC_TARGET_SUBDIR})
226 if(NOT LIBC_ENABLE_USE_BY_CLANG)
227 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
228 set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
229 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
230 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include)
231 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
233 set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
234 set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
236 if(LIBC_TARGET_OS_IS_GPU)
237 if(LIBC_TARGET_TRIPLE)
238 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LIBC_TARGET_TRIPLE})
240 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
243 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
247 if(LIBC_TARGET_TRIPLE)
248 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_TRIPLE})
249 elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
250 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_SUBDIR})
252 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX})
255 if(LIBC_TARGET_OS_IS_GPU)
256 include(prepare_libc_gpu_build)
257 set(LIBC_ENABLE_UNITTESTS OFF)
260 include(LLVMLibCCheckMPFR)
262 if(LLVM_LIBC_CLANG_TIDY)
263 set(LLVM_LIBC_ENABLE_LINTING ON)
266 if(LLVM_LIBC_ENABLE_LINTING)
267 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
268 set(LLVM_LIBC_ENABLE_LINTING OFF)
269 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
271 if (NOT LLVM_LIBC_CLANG_TIDY)
272 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
275 if(LLVM_LIBC_CLANG_TIDY)
276 # Check clang-tidy major version.
277 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
278 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT
279 ERROR_VARIABLE CLANG_TIDY_ERROR
280 RESULT_VARIABLE CLANG_TIDY_RESULT)
282 if(CLANG_TIDY_RESULT AND NOT CLANG_TIDY_RESULT EQUAL 0)
283 message(FATAL_ERROR "Failed to execute '${LLVM_LIBC_CLANG_TIDY} --version'
284 output : '${CLANG_TIDY_OUTPUT}'
285 error : '${CLANG_TIDY_ERROR}'
286 result : '${CLANG_TIDY_RESULT}'
289 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
290 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
291 "${CMAKE_CXX_COMPILER_VERSION}")
293 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
294 set(LLVM_LIBC_ENABLE_LINTING OFF)
296 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
297 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
300 The path to the clang-tidy binary can be set manually by passing
301 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
303 add_custom_target(libc-lint)
305 message(FATAL_ERROR "
306 Linting is enabled but 'clang-tidy' is not found!
308 The path to the clang-tidy binary can be set manually by passing
309 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
311 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
312 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
317 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
318 if(LLVM_LIBC_INCLUDE_SCUDO)
319 if (NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES))
320 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES")
324 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
326 include(CMakeParseArguments)
327 include(LLVMLibCCheckCpuFeatures)
328 include(CheckCompilerFeatures)
329 include(LLVMLibCRules)
331 set(TARGET_LLVMLIBC_ENTRYPOINTS "")
332 set(TARGET_LIBC_ENTRYPOINTS "")
333 set(TARGET_LIBM_ENTRYPOINTS "")
334 set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "")
336 # Check entrypoints.txt
337 if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt")
338 include("${LIBC_CONFIG_PATH}/entrypoints.txt")
340 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.")
344 if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt")
345 include("${LIBC_CONFIG_PATH}/headers.txt")
346 elseif(LLVM_LIBC_FULL_BUILD)
347 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.")
350 # Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list
351 if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt")
352 include("${LIBC_CONFIG_PATH}/exclude.txt")
355 # #TODO: Set up support for premade configs adding their own exclude lists.
357 foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS)
358 if(LIBC_CMAKE_VERBOSE_LOGGING)
359 message(STATUS "Removing entrypoint ${removed_entrypoint}")
361 list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint})
362 list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint})
363 list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint})
366 set(TARGET_ENTRYPOINT_NAME_LIST "")
367 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
368 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
369 if(${last_dot_loc} EQUAL -1)
370 message(FATAL_ERROR "Invalid entrypoint target name ${entrypoint}; Expected"
371 " a '.' (dot) in the name.")
373 math(EXPR name_loc "${last_dot_loc} + 1")
374 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
375 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
378 if(MSVC AND NOT MSYS)
379 set(libc_opt_high_flag "/O2")
381 set(libc_opt_high_flag "-O3")
384 add_subdirectory(include)
385 add_subdirectory(config)
386 add_subdirectory(hdr)
387 add_subdirectory(src)
388 add_subdirectory(utils)
390 if(LLVM_LIBC_FULL_BUILD)
391 # The startup system can potentially depend on the library components so add
392 # it after the library implementation directories.
393 add_subdirectory(startup)
396 # The lib and test directories are added at the very end as tests
397 # and libraries potentially draw from the components present in all
398 # of the other directories.
399 add_subdirectory(lib)
400 if(LLVM_INCLUDE_TESTS)
401 add_subdirectory(test)
402 add_subdirectory(fuzzing)
405 add_subdirectory(benchmarks)
407 if (LIBC_INCLUDE_DOCS)
408 add_subdirectory(docs)