1 cmake_minimum_required(VERSION 3.13.4)
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 sourse and binary directories.
16 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
17 set(LIBC_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
18 # The top-level directory in which libc is being built.
19 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
21 if(LLVM_LIBC_FULL_BUILD OR LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
22 if(NOT LIBC_HDRGEN_EXE)
23 # We need to set up hdrgen first since other targets depend on it.
24 add_subdirectory(utils/LibcTableGenUtil)
25 add_subdirectory(utils/HdrGen)
27 message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
31 if(LLVM_ENABLE_RUNTIMES AND NOT LLVM_RUNTIMES_BUILD)
32 # When libc is build as part of the runtimes/bootstrap build's CMake run, we
33 # only need to build the host tools to build the libc. So, we just do enough
34 # to build libc-hdrgen and return.
38 option(LIBC_CMAKE_VERBOSE_LOGGING
39 "Log details warnings and notifications during CMake configuration." OFF)
40 # Path libc/scripts directory.
41 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
43 # Flags to pass down to the compiler while building the libc functions.
44 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
46 include(common_libc_tuners.cmake)
48 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
50 # Check --print-resource-dir to find the compiler resource dir if this flag
51 # is supported by the compiler.
53 OUTPUT_STRIP_TRAILING_WHITESPACE
54 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
55 RESULT_VARIABLE COMMAND_RETURN_CODE
56 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
58 # Retrieve the host compiler's resource dir.
59 if(COMMAND_RETURN_CODE EQUAL 0)
60 set(COMPILER_RESOURCE_DIR
61 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
63 message(STATUS "Set COMPILER_RESOURCE_DIR to "
64 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
66 set(COMPILER_RESOURCE_DIR OFF)
67 message(STATUS "COMPILER_RESOURCE_DIR not set
68 --print-resource-dir not supported by host compiler")
71 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
72 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
73 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
75 option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
76 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
78 set(LIBC_ENABLE_UNITTESTS ON)
79 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
81 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
82 include(LLVMLibCArchitectures)
84 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
85 include(prepare_libc_gpu_build)
86 set(LIBC_ENABLE_UNITTESTS OFF)
89 include(LLVMLibCCheckMPFR)
91 if(LLVM_LIBC_CLANG_TIDY)
92 set(LLVM_LIBC_ENABLE_LINTING ON)
95 if(LLVM_LIBC_ENABLE_LINTING)
96 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
97 set(LLVM_LIBC_ENABLE_LINTING OFF)
98 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
100 if (NOT LLVM_LIBC_CLANG_TIDY)
101 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
104 if(LLVM_LIBC_CLANG_TIDY)
105 # Check clang-tidy major version.
106 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
107 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT)
108 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
109 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
110 "${CMAKE_CXX_COMPILER_VERSION}")
111 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
112 set(LLVM_LIBC_ENABLE_LINTING OFF)
114 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
115 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
118 The path to the clang-tidy binary can be set manually by passing
119 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
121 add_custom_target(libc-lint)
123 message(FATAL_ERROR "
124 Linting is enabled but 'clang-tidy' is not found!
126 The path to the clang-tidy binary can be set manually by passing
127 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
129 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
130 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
135 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
136 if(LLVM_LIBC_INCLUDE_SCUDO)
137 if (NOT "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
138 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS")
142 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
144 include(CMakeParseArguments)
145 include(LLVMLibCCheckCpuFeatures)
146 include(LLVMLibCRules)
148 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
149 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
150 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
151 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
153 message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
155 include(${entrypoint_file})
157 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
158 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
159 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
160 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
163 set(TARGET_ENTRYPOINT_NAME_LIST "")
164 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
165 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
166 if(${last_dot_loc} EQUAL -1)
167 message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
168 "(dot) in the name.")
170 math(EXPR name_loc "${last_dot_loc} + 1")
171 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
172 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
175 set(LIBC_INSTALL_DEPENDS)
176 if(LLVM_LIBC_FULL_BUILD)
177 set(LIBC_INSTALL_DEPENDS "install-libc-static-archives;install-libc-headers")
178 if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
179 # For now we will disable libc-startup installation for baremetal. The
180 # correct way to do it would be to make a hookable startup for baremetal
181 # and install it as part of the libc installation.
182 list(APPEND LIBC_INSTALL_DEPENDS "libc-startup")
185 set(LIBC_INSTALL_DEPENDS install-libc-static-archives)
188 add_subdirectory(include)
189 add_subdirectory(config)
190 add_subdirectory(src)
191 add_subdirectory(utils)
193 if(LLVM_LIBC_FULL_BUILD)
194 # The startup system can potentially depend on the library components so add
195 # it after the library implementation directories.
196 add_subdirectory(startup)
199 # The lib and test directories are added at the very end as tests
200 # and libraries potentially draw from the components present in all
201 # of the other directories.
202 # TODO: Add testing support for the libc GPU target.
203 add_subdirectory(lib)
204 if(LLVM_INCLUDE_TESTS)
205 add_subdirectory(test)
206 add_subdirectory(fuzzing)
209 if(LIBC_INCLUDE_BENCHMARKS)
210 add_subdirectory(benchmarks)
213 if (LIBC_INCLUDE_DOCS)
214 add_subdirectory(docs)
218 if(LLVM_LIBC_FULL_BUILD)
219 add_llvm_install_targets(
222 COMPONENT libc-headers
226 add_llvm_install_targets(
228 DEPENDS ${LIBC_INSTALL_DEPENDS}