1 cmake_minimum_required(VERSION 3.20.0)
3 # Include LLVM's cmake policies.
4 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
5 set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
7 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
10 if (LIBC_CMAKE_VERBOSE_LOGGING)
11 get_directory_property(LIBC_OLD_PREPROCESSOR_DEFS COMPILE_DEFINITIONS)
12 foreach(OLD_DEF ${LIBC_OLD_PREPROCESSOR_DEFS})
13 message(STATUS "Undefining ${OLD_DEF}")
16 set_directory_properties(PROPERTIES
17 # `llvm-project/llvm/CMakeLists.txt` adds the following directive
18 # `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})` We
19 # undo it to be able to precisely control what is getting included.
20 INCLUDE_DIRECTORIES ""
21 # `llvm/cmake/modules/HandleLLVMOptions.cmake` uses `add_compile_definitions`
22 # to set a few preprocessor defines which we do not want.
23 COMPILE_DEFINITIONS ""
25 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
26 add_definitions("-D_DEBUG")
30 set(CMAKE_CXX_STANDARD 17)
32 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
34 # The top-level source directory.
35 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
36 # The top-level directory in which libc is being built.
37 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
39 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")
41 # Defining a global namespace to enclose all libc functions.
42 set(LIBC_NAMESPACE "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}"
43 CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
46 if(LLVM_LIBC_FULL_BUILD OR LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
47 if(NOT LIBC_HDRGEN_EXE)
48 # We need to set up hdrgen first since other targets depend on it.
49 add_subdirectory(utils/LibcTableGenUtil)
50 add_subdirectory(utils/HdrGen)
51 # Calling add_tablegen sets variables like LIBC_TABLEGEN_EXE in
52 # PARENT_SCOPE which get lost until saved in the cache.
53 set(LIBC_TABLEGEN_EXE "${LIBC_TABLEGEN_EXE}" CACHE INTERNAL "")
54 set(LIBC_TABLEGEN_TARGET "${LIBC_TABLEGEN_TARGET}" CACHE INTERNAL "")
56 message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
60 option(LIBC_HDRGEN_ONLY "Only build the 'libc-hdrgen' executable" OFF)
61 if(("libc" IN_LIST LLVM_ENABLE_RUNTIMES AND NOT LLVM_RUNTIMES_BUILD) OR
63 # When libc is build as part of the runtimes/bootstrap build's CMake run, we
64 # only need to build the host tools to build the libc. So, we just do enough
65 # to build libc-hdrgen and return.
67 # Always make the RPC server availible to other projects for GPU mode.
68 if(LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
69 add_subdirectory(utils/gpu/server)
74 option(LIBC_CMAKE_VERBOSE_LOGGING
75 "Log details warnings and notifications during CMake configuration." OFF)
77 # Path libc/scripts directory.
78 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
80 if(NOT LIBC_NAMESPACE MATCHES "^__llvm_libc")
81 message(FATAL_ERROR "Invalid LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LIBC_NAMESPACE}'")
84 message(STATUS "Setting LIBC_NAMESPACE namespace to '${LIBC_NAMESPACE}'")
85 add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
87 # Flags to pass down to the compiler while building the libc functions.
88 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
90 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
92 # Check --print-resource-dir to find the compiler resource dir if this flag
93 # is supported by the compiler.
95 OUTPUT_STRIP_TRAILING_WHITESPACE
96 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
97 RESULT_VARIABLE COMMAND_RETURN_CODE
98 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
100 # Retrieve the host compiler's resource dir.
101 if(COMMAND_RETURN_CODE EQUAL 0)
102 set(COMPILER_RESOURCE_DIR
103 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
105 message(STATUS "Set COMPILER_RESOURCE_DIR to "
106 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
108 if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
109 message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
111 set(COMPILER_RESOURCE_DIR OFF)
112 message(STATUS "COMPILER_RESOURCE_DIR not set
113 --print-resource-dir not supported by host compiler")
117 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
118 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
119 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
121 option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
122 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
124 option(LIBC_CONFIG_PATH "The path to user provided folder that configures the build for the target system." OFF)
126 set(LIBC_ENABLE_UNITTESTS ON)
127 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
129 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
130 include(LLVMLibCArchitectures)
132 set(LIBC_CONFIG_JSON_FILE_LIST "")
134 if(NOT LIBC_CONFIG_PATH)
135 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
136 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
137 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
138 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}")
139 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
140 set(LIBC_CONFIG_PATH "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}")
143 list(APPEND LIBC_CONFIG_JSON_FILE_LIST "${LIBC_CONFIG_PATH}")
146 if(NOT LIBC_CONFIG_PATH)
147 message(FATAL_ERROR "Configs for the platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' do not exist and LIBC_CONFIG_PATH is not set.")
148 elseif(LIBC_CMAKE_VERBOSE_LOGGING)
149 message(STATUS "Path for config files is: ${LIBC_CONFIG_PATH}")
152 # option(LIBC_ENABLE_WIDE_CHARACTERS
153 # "Whether to enable wide character functions on supported platforms. This may
154 # also set flags to enable or disable wide character support within other
155 # functions (e.g. printf)." ON)
157 #TODO: Add carve-out specific config files to the list here.
160 # Config loading happens in three steps:
161 # 1. Load the config file config/config.json and set up config vars.
162 # 2. Load config/${LIBC_TARGET_OS}/config.json if available and override
164 # 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is
165 # available and override vars as suitable.
166 # All the three steps will not override options already set from the
167 # CMake command line. That is, the CMake command line option values take
168 # precedence over the values in config.json files.
169 set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json)
170 read_libc_config(${main_config_file} global_config)
171 foreach(opt IN LISTS global_config)
172 string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0)
174 message(FATAL_ERROR ${json_error})
176 if(DEFINED ${opt_name})
177 # The option is already defined from the command line so we ignore it here.
178 # We still make note of it so that further config load can also ignore
180 message(STATUS "${opt_name}: ${${opt_name}} (from command line)")
181 list(APPEND cmd_line_conf ${opt_name})
185 string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name})
187 message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}")
189 string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value")
191 message(FATAL_ERROR ${json_error})
193 message(STATUS "${opt_name}: ${opt_value}")
194 set(${opt_name} ${opt_value})
196 generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
198 # Load each target specific config.
199 foreach(config_path IN LISTS LIBC_CONFIG_JSON_FILE_LIST)
200 if(LIBC_CMAKE_VERBOSE_LOGGING)
201 message(STATUS "Loading additional config: '${config_path}/config.json'")
203 load_libc_config(${config_path}/config.json ${cmd_line_conf})
206 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
207 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
208 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/gpu-none-llvm)
209 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
210 elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND LIBC_ENABLE_USE_BY_CLANG)
211 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
212 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
213 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
215 if(NOT LIBC_ENABLE_USE_BY_CLANG)
216 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
217 set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
218 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
219 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include)
220 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
222 set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
223 set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
225 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
228 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
229 include(prepare_libc_gpu_build)
230 set(LIBC_ENABLE_UNITTESTS OFF)
233 include(LLVMLibCCheckMPFR)
235 if(LLVM_LIBC_CLANG_TIDY)
236 set(LLVM_LIBC_ENABLE_LINTING ON)
239 if(LLVM_LIBC_ENABLE_LINTING)
240 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
241 set(LLVM_LIBC_ENABLE_LINTING OFF)
242 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
244 if (NOT LLVM_LIBC_CLANG_TIDY)
245 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
248 if(LLVM_LIBC_CLANG_TIDY)
249 # Check clang-tidy major version.
250 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
251 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT
252 ERROR_VARIABLE CLANG_TIDY_ERROR
253 RESULT_VARIABLE CLANG_TIDY_RESULT)
255 if(CLANG_TIDY_RESULT AND NOT CLANG_TIDY_RESULT EQUAL 0)
256 message(FATAL_ERROR "Failed to execute '${LLVM_LIBC_CLANG_TIDY} --version'
257 output : '${CLANG_TIDY_OUTPUT}'
258 error : '${CLANG_TIDY_ERROR}'
259 result : '${CLANG_TIDY_RESULT}'
262 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
263 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
264 "${CMAKE_CXX_COMPILER_VERSION}")
266 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
267 set(LLVM_LIBC_ENABLE_LINTING OFF)
269 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
270 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
273 The path to the clang-tidy binary can be set manually by passing
274 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
276 add_custom_target(libc-lint)
278 message(FATAL_ERROR "
279 Linting is enabled but 'clang-tidy' is not found!
281 The path to the clang-tidy binary can be set manually by passing
282 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
284 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
285 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
290 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
291 if(LLVM_LIBC_INCLUDE_SCUDO)
292 if (NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES))
293 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES")
297 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
299 include(CMakeParseArguments)
300 include(LLVMLibCCheckCpuFeatures)
301 include(CheckCompilerFeatures)
302 include(LLVMLibCRules)
304 set(TARGET_LLVMLIBC_ENTRYPOINTS "")
305 set(TARGET_LIBC_ENTRYPOINTS "")
306 set(TARGET_LIBM_ENTRYPOINTS "")
307 set(TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS "")
309 # Check entrypoints.txt
310 if(EXISTS "${LIBC_CONFIG_PATH}/entrypoints.txt")
311 include("${LIBC_CONFIG_PATH}/entrypoints.txt")
313 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/entrypoints.txt file not found.")
317 if(EXISTS "${LIBC_CONFIG_PATH}/headers.txt")
318 include("${LIBC_CONFIG_PATH}/headers.txt")
319 elseif(LLVM_LIBC_FULL_BUILD)
320 message(FATAL_ERROR "${LIBC_CONFIG_PATH}/headers.txt file not found and fullbuild requested.")
323 # Check exclude.txt that appends to LIBC_EXCLUDE_ENTRYPOINTS list
324 if(EXISTS "${LIBC_CONFIG_PATH}/exclude.txt")
325 include("${LIBC_CONFIG_PATH}/exclude.txt")
328 # #TODO: Set up support for premade configs adding their own exclude lists.
330 foreach(removed_entrypoint IN LISTS TARGET_LLVMLIBC_REMOVED_ENTRYPOINTS)
331 if(LIBC_CMAKE_VERBOSE_LOGGING)
332 message(STATUS "Removing entrypoint ${removed_entrypoint}")
334 list(REMOVE_ITEM TARGET_LLVMLIBC_ENTRYPOINTS ${removed_entrypoint})
335 list(REMOVE_ITEM TARGET_LIBC_ENTRYPOINTS ${removed_entrypoint})
336 list(REMOVE_ITEM TARGET_LIBM_ENTRYPOINTS ${removed_entrypoint})
339 set(TARGET_ENTRYPOINT_NAME_LIST "")
340 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
341 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
342 if(${last_dot_loc} EQUAL -1)
343 message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
344 "(dot) in the name.")
346 math(EXPR name_loc "${last_dot_loc} + 1")
347 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
348 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
351 set(LIBC_INSTALL_DEPENDS)
352 if(LLVM_LIBC_FULL_BUILD)
353 set(LIBC_INSTALL_DEPENDS "install-libc-static-archives;install-libc-headers")
354 if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
355 # For now we will disable libc-startup installation for baremetal. The
356 # correct way to do it would be to make a hookable startup for baremetal
357 # and install it as part of the libc installation.
358 list(APPEND LIBC_INSTALL_DEPENDS "libc-startup")
361 set(LIBC_INSTALL_DEPENDS install-libc-static-archives)
364 add_subdirectory(include)
365 add_subdirectory(config)
366 add_subdirectory(src)
367 add_subdirectory(utils)
369 if(LLVM_LIBC_FULL_BUILD)
370 # The startup system can potentially depend on the library components so add
371 # it after the library implementation directories.
372 add_subdirectory(startup)
375 # The lib and test directories are added at the very end as tests
376 # and libraries potentially draw from the components present in all
377 # of the other directories.
378 add_subdirectory(lib)
379 if(LLVM_INCLUDE_TESTS)
380 add_subdirectory(test)
381 add_subdirectory(fuzzing)
384 if(LIBC_INCLUDE_BENCHMARKS)
385 add_subdirectory(benchmarks)
388 if (LIBC_INCLUDE_DOCS)
389 add_subdirectory(docs)
393 if(LLVM_LIBC_FULL_BUILD)
394 add_llvm_install_targets(
397 COMPONENT libc-headers
401 add_llvm_install_targets(
403 DEPENDS ${LIBC_INSTALL_DEPENDS}