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" OFF)
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=...)")
116 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
118 # Check --print-resource-dir to find the compiler resource dir if this flag
119 # is supported by the compiler.
121 OUTPUT_STRIP_TRAILING_WHITESPACE
122 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
123 RESULT_VARIABLE COMMAND_RETURN_CODE
124 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
126 # Retrieve the host compiler's resource dir.
127 if(COMMAND_RETURN_CODE EQUAL 0)
128 set(COMPILER_RESOURCE_DIR
129 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
131 message(STATUS "Set COMPILER_RESOURCE_DIR to "
132 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
134 if (LIBC_TARGET_OS_IS_GPU)
135 message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
137 set(COMPILER_RESOURCE_DIR OFF)
138 message(STATUS "COMPILER_RESOURCE_DIR not set
139 --print-resource-dir not supported by host compiler")
143 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
144 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
145 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
147 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
149 option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF)
151 set(LIBC_ENABLE_UNITTESTS ON)
152 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
154 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
155 include(LLVMLibCArchitectures)
157 set(LIBC_CONFIG_JSON_FILE_LIST "")
159 if(NOT LIBC_CONFIG_PATH)
160 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
161 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
162 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
163 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
164 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
165 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
168 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}")
171 if(NOT LIBC_CONFIG_PATH)
172 message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.")
173 elseif(LIBC_CMAKE_VERBOSE_LOGGING)
174 message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}")
177 # option(LIBC_ENABLE_WIDE_CHARACTERS
178 # "Whether to enable wide character functions on supported platforms. This may
179 # also set flags to enable or disable wide character support within other
180 # functions (e.g. printf)." ON)
182 #TODO: Add carve-out specific config files to the list here.
185 # Config loading happens in three steps:
186 # 1. Load the config file config/config.json and set up config vars.
187 # 2. Load config/${LIBC_TARGET_OS}/config.json if available and override
189 # 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is
190 # available and override vars as suitable.
191 # All the three steps will not override options already set from the
192 # CMake command line. That is, the CMake command line option values take
193 # precedence over the values in config.json files.
194 set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json)
195 read_libc_config(${main_config_file} global_config)
196 foreach(opt IN LISTS global_config)
197 string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0)
199 message(FATAL_ERROR ${json_error})
201 if(DEFINED ${opt_name})
202 # The option is already defined from the command line so we ignore it here.
203 # We still make note of it so that further config load can also ignore
205 message(STATUS "${opt_name}: ${${opt_name}} (from command line)")
206 list(APPEND cmd_line_conf ${opt_name})
210 string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name})
212 message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}")
214 string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value")
216 message(FATAL_ERROR ${json_error})
218 message(STATUS "${opt_name}: ${opt_value}")
219 set(${opt_name} ${opt_value})
221 generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
223 # Load each target specific config.
224 foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST)
225 if(LIBC_CMAKE_VERBOSE_LOGGING)
226 message(STATUS "Loading additional config: '${config_path}/config.json'")
228 load_libc_config(${config_path}/config.json ${cmd_line_conf})
231 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND LIBC_ENABLE_USE_BY_CLANG)
232 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
233 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
234 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
236 if(NOT LIBC_ENABLE_USE_BY_CLANG)
237 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
238 set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
239 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
240 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include)
241 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
243 set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
244 set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
246 if(LIBC_TARGET_OS_IS_GPU)
247 if(LLVM_RUNTIMES_TARGET)
248 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_RUNTIMES_TARGET})
249 elseif(LIBC_TARGET_TRIPLE)
250 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LIBC_TARGET_TRIPLE})
252 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
255 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
259 if(LIBC_TARGET_TRIPLE)
260 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBC_TARGET_TRIPLE})
261 elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
262 set(LIBC_INSTALL_LIBRARY_DIR
263 lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE})
265 set(LIBC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX})
268 if(LIBC_TARGET_OS_IS_GPU)
269 include(prepare_libc_gpu_build)
270 set(LIBC_ENABLE_UNITTESTS OFF)
273 include(LLVMLibCCheckMPFR)
275 if(LLVM_LIBC_CLANG_TIDY)
276 set(LLVM_LIBC_ENABLE_LINTING ON)
279 if(LLVM_LIBC_ENABLE_LINTING)
280 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
281 set(LLVM_LIBC_ENABLE_LINTING OFF)
282 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
284 if (NOT LLVM_LIBC_CLANG_TIDY)
285 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
288 if(LLVM_LIBC_CLANG_TIDY)
289 # Check clang-tidy major version.
290 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
291 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT
292 ERROR_VARIABLE CLANG_TIDY_ERROR
293 RESULT_VARIABLE CLANG_TIDY_RESULT)
295 if(CLANG_TIDY_RESULT AND NOT CLANG_TIDY_RESULT EQUAL 0)
296 message(FATAL_ERROR "Failed to execute '${LLVM_LIBC_CLANG_TIDY} --version'
297 output : '${CLANG_TIDY_OUTPUT}'
298 error : '${CLANG_TIDY_ERROR}'
299 result : '${CLANG_TIDY_RESULT}'
302 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
303 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
304 "${CMAKE_CXX_COMPILER_VERSION}")
306 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
307 set(LLVM_LIBC_ENABLE_LINTING OFF)
309 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
310 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
313 The path to the clang-tidy binary can be set manually by passing
314 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
316 add_custom_target(libc-lint)
318 message(FATAL_ERROR "
319 Linting is enabled but 'clang-tidy' is not found!
321 The path to the clang-tidy binary can be set manually by passing
322 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
324 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
325 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
330 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
331 if(LLVM_LIBC_INCLUDE_SCUDO)
332 if (NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES))
333 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES")
337 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
339 include(CMakeParseArguments)
340 include(LLVMLibCCheckCpuFeatures)
341 include(CheckCompilerFeatures)
342 include(LLVMLibCRules)
344 set(TARGET_LLVMLIBC_ENTRYPOINTS "")
345 set(TARGET_LIBC_ENTRYPOINTS "")
346 set(TARGET_LIBM_ENTRYPOINTS "")
347 set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "")
349 # Check entrypoints.txt
350 if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt")
351 include("${LIBC_CONFIG_PATH}/entrypoints.txt")
353 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.")
357 if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt")
358 include("${LIBC_CONFIG_PATH}/headers.txt")
359 elseif(LLVM_LIBC_FULL_BUILD)
360 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.")
363 # Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list
364 if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt")
365 include("${LIBC_CONFIG_PATH}/exclude.txt")
368 # #TODO: Set up support for premade configs adding their own exclude lists.
370 foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS)
371 if(LIBC_CMAKE_VERBOSE_LOGGING)
372 message(STATUS "Removing entrypoint ${removed_entrypoint}")
374 list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint})
375 list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint})
376 list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint})
379 set(TARGET_ENTRYPOINT_NAME_LIST "")
380 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
381 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
382 if(${last_dot_loc} EQUAL -1)
383 message(FATAL_ERROR "Invalid entrypoint target name ${entrypoint}; Expected"
384 " a '.' (dot) in the name.")
386 math(EXPR name_loc "${last_dot_loc} + 1")
387 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
388 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
391 add_subdirectory(include)
392 add_subdirectory(config)
393 add_subdirectory(hdr)
394 add_subdirectory(src)
395 add_subdirectory(utils)
397 if(LLVM_LIBC_FULL_BUILD)
398 # The startup system can potentially depend on the library components so add
399 # it after the library implementation directories.
400 add_subdirectory(startup)
403 # The lib and test directories are added at the very end as tests
404 # and libraries potentially draw from the components present in all
405 # of the other directories.
406 add_subdirectory(lib)
407 if(LLVM_INCLUDE_TESTS)
408 add_subdirectory(test)
409 add_subdirectory(fuzzing)
412 add_subdirectory(benchmarks)
414 if (LIBC_INCLUDE_DOCS)
415 add_subdirectory(docs)