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 # `llvm-project/llvm/CMakeLists.txt` adds the following directive
11 # `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})`
12 # We undo it to be able to precisely control what is getting included.
13 set_directory_properties(PROPERTIES INCLUDE_DIRECTORIES "")
16 set(CMAKE_CXX_STANDARD 17)
18 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
20 # The top-level source directory.
21 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
22 # The top-level directory in which libc is being built.
23 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
25 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")
27 # Defining a global namespace to enclose all libc functions.
28 set(LIBC_NAMESPACE "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}"
29 CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
32 if(LLVM_LIBC_FULL_BUILD OR LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
33 if(NOT LIBC_HDRGEN_EXE)
34 # We need to set up hdrgen first since other targets depend on it.
35 add_subdirectory(utils/LibcTableGenUtil)
36 add_subdirectory(utils/HdrGen)
38 message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
42 option(LIBC_HDRGEN_ONLY "Only build the 'libc-hdrgen' executable" OFF)
43 if(("libc" IN_LIST LLVM_ENABLE_RUNTIMES AND NOT LLVM_RUNTIMES_BUILD) OR
45 # When libc is build as part of the runtimes/bootstrap build's CMake run, we
46 # only need to build the host tools to build the libc. So, we just do enough
47 # to build libc-hdrgen and return.
49 # Always make the RPC server availible to other projects for GPU mode.
50 if(LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
51 add_subdirectory(utils/gpu/server)
56 option(LIBC_CMAKE_VERBOSE_LOGGING
57 "Log details warnings and notifications during CMake configuration." OFF)
59 # Path libc/scripts directory.
60 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
62 if(NOT LIBC_NAMESPACE MATCHES "^__llvm_libc")
63 message(FATAL_ERROR "Invalid LIBC_NAMESPACE. Must start with '__llvm_libc' was '${LIBC_NAMESPACE}'")
66 message(STATUS "Setting LIBC_NAMESPACE namespace to '${LIBC_NAMESPACE}'")
67 add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
69 # Flags to pass down to the compiler while building the libc functions.
70 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
72 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
74 # Check --print-resource-dir to find the compiler resource dir if this flag
75 # is supported by the compiler.
77 OUTPUT_STRIP_TRAILING_WHITESPACE
78 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
79 RESULT_VARIABLE COMMAND_RETURN_CODE
80 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
82 # Retrieve the host compiler's resource dir.
83 if(COMMAND_RETURN_CODE EQUAL 0)
84 set(COMPILER_RESOURCE_DIR
85 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
87 message(STATUS "Set COMPILER_RESOURCE_DIR to "
88 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
90 if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
91 message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
93 set(COMPILER_RESOURCE_DIR OFF)
94 message(STATUS "COMPILER_RESOURCE_DIR not set
95 --print-resource-dir not supported by host compiler")
99 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
100 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
101 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
103 option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
104 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
106 set(LIBC_ENABLE_UNITTESTS ON)
107 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
109 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
110 include(LLVMLibCArchitectures)
113 # Config loading happens in three steps:
114 # 1. Load the config file config/config.json and set up config vars.
115 # 2. Load config/${LIBC_TARGET_OS}/config.json if available and override
117 # 3. Load config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCH}/config.json is
118 # available and override vars as suitable.
119 # All the three steps will not override options already set from the
120 # CMake command line. That is, the CMake command line option values take
121 # precedence over the values in config.json files.
122 set(main_config_file ${LIBC_SOURCE_DIR}/config/config.json)
123 read_libc_config(${main_config_file} global_config)
124 foreach(opt IN LISTS global_config)
125 string(JSON opt_name ERROR_VARIABLE json_error MEMBER ${opt} 0)
127 message(FATAL_ERROR ${json_error})
129 if(DEFINED ${opt_name})
130 # The option is already defined from the command line so we ignore it here.
131 # We still make note of it so that further config load can also ignore
133 message(STATUS "${opt_name}: ${${opt_name}} (from command line)")
134 list(APPEND cmd_line_conf ${opt_name})
138 string(JSON opt_object ERROR_VARIABLE json_error GET ${opt} ${opt_name})
140 message(FATAL_ERROR "Error reading info of option '${opt_name}': ${json_error}")
142 string(JSON opt_value ERROR_VARIABLE json_error GET ${opt_object} "value")
144 message(FATAL_ERROR ${json_error})
146 message(STATUS "${opt_name}: ${opt_value}")
147 set(${opt_name} ${opt_value})
149 generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
150 load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/config.json ${cmd_line_conf})
151 load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/config.json ${cmd_line_conf})
153 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
154 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
155 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/gpu-none-llvm)
156 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
157 elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND LIBC_ENABLE_USE_BY_CLANG)
158 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
159 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
160 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
162 if(NOT LIBC_ENABLE_USE_BY_CLANG)
163 set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
164 set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
165 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
166 set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include)
167 set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
169 set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
170 set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
172 set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
175 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
176 include(prepare_libc_gpu_build)
177 set(LIBC_ENABLE_UNITTESTS OFF)
180 include(LLVMLibCCheckMPFR)
182 if(LLVM_LIBC_CLANG_TIDY)
183 set(LLVM_LIBC_ENABLE_LINTING ON)
186 if(LLVM_LIBC_ENABLE_LINTING)
187 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
188 set(LLVM_LIBC_ENABLE_LINTING OFF)
189 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
191 if (NOT LLVM_LIBC_CLANG_TIDY)
192 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
195 if(LLVM_LIBC_CLANG_TIDY)
196 # Check clang-tidy major version.
197 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
198 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT
199 ERROR_VARIABLE CLANG_TIDY_ERROR
200 RESULT_VARIABLE CLANG_TIDY_RESULT)
202 if(CLANG_TIDY_RESULT AND NOT CLANG_TIDY_RESULT EQUAL 0)
203 message(FATAL_ERROR "Failed to execute '${LLVM_LIBC_CLANG_TIDY} --version'
204 output : '${CLANG_TIDY_OUTPUT}'
205 error : '${CLANG_TIDY_ERROR}'
206 result : '${CLANG_TIDY_RESULT}'
209 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
210 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
211 "${CMAKE_CXX_COMPILER_VERSION}")
213 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
214 set(LLVM_LIBC_ENABLE_LINTING OFF)
216 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
217 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
220 The path to the clang-tidy binary can be set manually by passing
221 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
223 add_custom_target(libc-lint)
225 message(FATAL_ERROR "
226 Linting is enabled but 'clang-tidy' is not found!
228 The path to the clang-tidy binary can be set manually by passing
229 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
231 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
232 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
237 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
238 if(LLVM_LIBC_INCLUDE_SCUDO)
239 if (NOT ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS OR "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES))
240 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS or LLVM_ENABLE_RUNTIMES")
244 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
246 include(CMakeParseArguments)
247 include(LLVMLibCCheckCpuFeatures)
248 include(LLVMLibCRules)
250 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
251 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
252 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
253 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
255 message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
257 include(${entrypoint_file})
259 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
260 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
261 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
262 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
265 set(TARGET_ENTRYPOINT_NAME_LIST "")
266 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
267 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
268 if(${last_dot_loc} EQUAL -1)
269 message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
270 "(dot) in the name.")
272 math(EXPR name_loc "${last_dot_loc} + 1")
273 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
274 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
277 set(LIBC_INSTALL_DEPENDS)
278 if(LLVM_LIBC_FULL_BUILD)
279 set(LIBC_INSTALL_DEPENDS "install-libc-static-archives;install-libc-headers")
280 if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
281 # For now we will disable libc-startup installation for baremetal. The
282 # correct way to do it would be to make a hookable startup for baremetal
283 # and install it as part of the libc installation.
284 list(APPEND LIBC_INSTALL_DEPENDS "libc-startup")
287 set(LIBC_INSTALL_DEPENDS install-libc-static-archives)
290 add_subdirectory(include)
291 add_subdirectory(config)
292 add_subdirectory(src)
293 add_subdirectory(utils)
295 if(LLVM_LIBC_FULL_BUILD)
296 # The startup system can potentially depend on the library components so add
297 # it after the library implementation directories.
298 add_subdirectory(startup)
301 # The lib and test directories are added at the very end as tests
302 # and libraries potentially draw from the components present in all
303 # of the other directories.
304 add_subdirectory(lib)
305 if(LLVM_INCLUDE_TESTS)
306 add_subdirectory(test)
307 add_subdirectory(fuzzing)
310 if(LIBC_INCLUDE_BENCHMARKS)
311 add_subdirectory(benchmarks)
314 if (LIBC_INCLUDE_DOCS)
315 add_subdirectory(docs)
319 if(LLVM_LIBC_FULL_BUILD)
320 add_llvm_install_targets(
323 COMPONENT libc-headers
327 add_llvm_install_targets(
329 DEPENDS ${LIBC_INSTALL_DEPENDS}