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
11 set(CMAKE_CXX_STANDARD 17)
13 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
15 # The top-level source directory.
16 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
17 # The top-level directory in which libc is being built.
18 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
20 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")
22 # Defining a global namespace to enclose all libc functions.
23 set(LIBC_NAMESPACE "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}"
24 CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
27 if(LLVM_LIBC_FULL_BUILD OR LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
28 if(NOT LIBC_HDRGEN_EXE)
29 # We need to set up hdrgen first since other targets depend on it.
30 add_subdirectory(utils/LibcTableGenUtil)
31 add_subdirectory(utils/HdrGen)
33 message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
37 option(LIBC_HDRGEN_ONLY "Only build the 'libc-hdrgen' executable" OFF)
38 if(("libc" IN_LIST LLVM_ENABLE_RUNTIMES AND NOT LLVM_RUNTIMES_BUILD) OR
40 # When libc is build as part of the runtimes/bootstrap build's CMake run, we
41 # only need to build the host tools to build the libc. So, we just do enough
42 # to build libc-hdrgen and return.
44 # Always make the RPC server availible to other projects for GPU mode.
45 if(LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
46 add_subdirectory(utils/gpu/server)
51 option(LIBC_CMAKE_VERBOSE_LOGGING
52 "Log details warnings and notifications during CMake configuration." OFF)
54 # Path libc/scripts directory.
55 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
57 if(NOT LIBC_NAMESPACE MATCHES "^__llvm_libc")
58 message(FATAL_ERROR "Invalid LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LIBC_NAMESPACE}'")
61 message(STATUS "Setting LIBC_NAMESPACE namespace to '${LIBC_NAMESPACE}'")
62 add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
64 # Flags to pass down to the compiler while building the libc functions.
65 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
67 include(common_libc_tuners.cmake)
69 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
71 # Check --print-resource-dir to find the compiler resource dir if this flag
72 # is supported by the compiler.
74 OUTPUT_STRIP_TRAILING_WHITESPACE
75 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
76 RESULT_VARIABLE COMMAND_RETURN_CODE
77 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
79 # Retrieve the host compiler's resource dir.
80 if(COMMAND_RETURN_CODE EQUAL 0)
81 set(COMPILER_RESOURCE_DIR
82 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
84 message(STATUS "Set COMPILER_RESOURCE_DIR to "
85 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
87 if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
88 message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
90 set(COMPILER_RESOURCE_DIR OFF)
91 message(STATUS "COMPILER_RESOURCE_DIR not set
92 --print-resource-dir not supported by host compiler")
96 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
97 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
98 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
100 option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
101 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
103 set(LIBC_ENABLE_UNITTESTS ON)
104 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
106 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
107 include(LLVMLibCArchitectures)
110 # Config loading happens in three steps:
111 # 1. Load the config file config/config.json and set up config vars.
112 # 2. Load config/${LIBC_TARGET_OS}/config.json if available and override
114 # 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is
115 # available and override vars as suitable.
116 # All the three steps will not override options already set from the
117 # CMake command line. That is, the CMake command line option values take
118 # precedence over the values in config.json files.
119 set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json)
120 read_libc_config(${main_config_file} global_config)
121 foreach(opt IN LISTS global_config)
122 string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0)
124 message(FATAL_ERROR ${json_error})
126 if(DEFINED ${opt_name})
127 # The option is already defined from the command line so we ignore it here.
128 # We still make note of it so that further config load can also ignore
130 message(STATUS "${opt_name}: ${${opt_name}} (from command line)")
131 list(APPEND cmd_line_conf ${opt_name})
135 string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name})
137 message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}")
139 string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value")
141 message(FATAL_ERROR ${json_error})
143 message(STATUS "${opt_name}: ${opt_value}")
144 set(${opt_name} ${opt_value})
146 generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
147 load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/config.json ${cmd_line_conf})
148 load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/config.json ${cmd_line_conf})
150 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
151 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
152 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/gpu-none-llvm)
153 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
154 elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND LIBC_ENABLE_USE_BY_CLANG)
155 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
156 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
157 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
159 if(NOT LIBC_ENABLE_USE_BY_CLANG)
160 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
161 set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
162 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
163 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include)
164 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
166 set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
167 set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
169 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
172 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
173 include(prepare_libc_gpu_build)
174 set(LIBC_ENABLE_UNITTESTS OFF)
177 include(LLVMLibCCheckMPFR)
179 if(LLVM_LIBC_CLANG_TIDY)
180 set(LLVM_LIBC_ENABLE_LINTING ON)
183 if(LLVM_LIBC_ENABLE_LINTING)
184 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
185 set(LLVM_LIBC_ENABLE_LINTING OFF)
186 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
188 if (NOT LLVM_LIBC_CLANG_TIDY)
189 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
192 if(LLVM_LIBC_CLANG_TIDY)
193 # Check clang-tidy major version.
194 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
195 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT)
196 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
197 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
198 "${CMAKE_CXX_COMPILER_VERSION}")
200 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
201 set(LLVM_LIBC_ENABLE_LINTING OFF)
203 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
204 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
207 The path to the clang-tidy binary can be set manually by passing
208 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
210 add_custom_target(libc-lint)
212 message(FATAL_ERROR "
213 Linting is enabled but 'clang-tidy' is not found!
215 The path to the clang-tidy binary can be set manually by passing
216 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
218 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
219 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
224 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
225 if(LLVM_LIBC_INCLUDE_SCUDO)
226 if (NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES))
227 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES")
231 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
233 include(CMakeParseArguments)
234 include(LLVMLibCCheckCpuFeatures)
235 include(LLVMLibCRules)
237 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
238 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
239 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
240 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
242 message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
244 include(${entrypoint_file})
246 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
247 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
248 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
249 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
252 set(TARGET_ENTRYPOINT_NAME_LIST "")
253 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
254 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
255 if(${last_dot_loc} EQUAL -1)
256 message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
257 "(dot) in the name.")
259 math(EXPR name_loc "${last_dot_loc} + 1")
260 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
261 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
264 set(LIBC_INSTALL_DEPENDS)
265 if(LLVM_LIBC_FULL_BUILD)
266 set(LIBC_INSTALL_DEPENDS "install-libc-static-archives;install-libc-headers")
267 if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
268 # For now we will disable libc-startup installation for baremetal. The
269 # correct way to do it would be to make a hookable startup for baremetal
270 # and install it as part of the libc installation.
271 list(APPEND LIBC_INSTALL_DEPENDS "libc-startup")
274 set(LIBC_INSTALL_DEPENDS install-libc-static-archives)
277 add_subdirectory(include)
278 add_subdirectory(config)
279 add_subdirectory(src)
280 add_subdirectory(utils)
282 if(LLVM_LIBC_FULL_BUILD)
283 # The startup system can potentially depend on the library components so add
284 # it after the library implementation directories.
285 add_subdirectory(startup)
288 # The lib and test directories are added at the very end as tests
289 # and libraries potentially draw from the components present in all
290 # of the other directories.
291 # TODO: Add testing support for the libc GPU target.
292 add_subdirectory(lib)
293 if(LLVM_INCLUDE_TESTS)
294 add_subdirectory(test)
295 add_subdirectory(fuzzing)
298 if(LIBC_INCLUDE_BENCHMARKS)
299 add_subdirectory(benchmarks)
302 if (LIBC_INCLUDE_DOCS)
303 add_subdirectory(docs)
307 if(LLVM_LIBC_FULL_BUILD)
308 add_llvm_install_targets(
311 COMPONENT libc-headers
315 add_llvm_install_targets(
317 DEPENDS ${LIBC_INSTALL_DEPENDS}