1 cmake_minimum_required(VERSION 3.13.4)
4 set(CMAKE_CXX_STANDARD 17)
6 # Use old version of target_sources command which converts the source
7 # file paths to full paths.
8 cmake_policy(SET CMP0076 OLD)
9 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
11 # The top-level sourse and binary directories.
12 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
13 set(LIBC_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
14 # The top-level directory in which libc is being built.
15 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
17 if(LLVM_LIBC_FULL_BUILD)
18 if(NOT LIBC_HDRGEN_EXE)
19 # We need to set up hdrgen first since other targets depend on it.
20 add_subdirectory(utils/LibcTableGenUtil)
21 add_subdirectory(utils/HdrGen)
23 message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
27 if(LLVM_ENABLE_RUNTIMES AND NOT LLVM_RUNTIMES_BUILD)
28 # When libc is build as part of the runtimes/bootstrap build's CMake run, we
29 # only need to build the host tools to build the libc. So, we just do enough
30 # to build libc-hdrgen and return.
34 option(LIBC_CMAKE_VERBOSE_LOGGING
35 "Log details warnings and notifications during CMake configuration." OFF)
36 # Path libc/scripts directory.
37 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
39 # Flags to pass down to the compiler while building the libc functions.
40 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
42 include(common_libc_tuners.cmake)
44 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
46 # Check --print-resource-dir to find the compiler resource dir if this flag
47 # is supported by the compiler.
49 OUTPUT_STRIP_TRAILING_WHITESPACE
50 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
51 RESULT_VARIABLE COMMAND_RETURN_CODE
52 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
54 # Retrieve the host compiler's resource dir.
55 if(COMMAND_RETURN_CODE EQUAL 0)
56 set(COMPILER_RESOURCE_DIR
57 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
59 message(STATUS "Set COMPILER_RESOURCE_DIR to "
60 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
62 set(COMPILER_RESOURCE_DIR OFF)
63 message(STATUS "COMPILER_RESOURCE_DIR not set
64 --print-resource-dir not supported by host compiler")
67 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
68 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
69 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
71 option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
72 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
74 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
75 include(LLVMLibCArchitectures)
77 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
78 include(prepare_libc_gpu_build)
81 include(LLVMLibCCheckMPFR)
83 if(LLVM_LIBC_CLANG_TIDY)
84 set(LLVM_LIBC_ENABLE_LINTING ON)
87 if(LLVM_LIBC_ENABLE_LINTING)
88 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
89 set(LLVM_LIBC_ENABLE_LINTING OFF)
90 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
92 if (NOT LLVM_LIBC_CLANG_TIDY)
93 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
96 if(LLVM_LIBC_CLANG_TIDY)
97 # Check clang-tidy major version.
98 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
99 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT)
100 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
101 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
102 "${CMAKE_CXX_COMPILER_VERSION}")
103 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
104 set(LLVM_LIBC_ENABLE_LINTING OFF)
106 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
107 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
110 The path to the clang-tidy binary can be set manually by passing
111 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
114 message(FATAL_ERROR "
115 Linting is enabled but 'clang-tidy' is not found!
117 The path to the clang-tidy binary can be set manually by passing
118 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
120 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
121 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
126 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
127 if(LLVM_LIBC_INCLUDE_SCUDO)
128 if (NOT "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
129 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS")
133 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
135 include(CMakeParseArguments)
136 include(LLVMLibCCheckCpuFeatures)
137 include(LLVMLibCRules)
139 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
140 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
141 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
142 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
144 message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
146 include(${entrypoint_file})
148 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
149 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
150 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
151 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
154 set(TARGET_ENTRYPOINT_NAME_LIST "")
155 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
156 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
157 if(${last_dot_loc} EQUAL -1)
158 message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
159 "(dot) in the name.")
161 math(EXPR name_loc "${last_dot_loc} + 1")
162 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
163 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
166 set(LIBC_INSTALL_DEPENDS)
167 if(LLVM_LIBC_FULL_BUILD)
168 set(LIBC_INSTALL_DEPENDS "install-libc-static-archives;install-libc-headers")
169 if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
170 # For now we will disable libc-startup installation for baremetal. The
171 # correct way to do it would be to make a hookable startup for baremetal
172 # and install it as part of the libc installation.
173 list(APPEND LIBC_INSTALL_DEPENDS "libc-startup")
176 set(LIBC_INSTALL_DEPENDS install-libc-static-archives)
179 add_subdirectory(include)
180 add_subdirectory(config)
181 add_subdirectory(src)
182 add_subdirectory(utils)
184 if(LLVM_LIBC_FULL_BUILD)
185 # The startup system can potentially depend on the library components so add
186 # it after the library implementation directories.
187 add_subdirectory(startup)
190 # The lib and test directories are added at the very end as tests
191 # and libraries potentially draw from the components present in all
192 # of the other directories.
193 # TODO: Add testing support for the libc GPU target.
194 add_subdirectory(lib)
195 if(LLVM_INCLUDE_TESTS AND NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
196 add_subdirectory(test)
197 add_subdirectory(fuzzing)
200 if(LIBC_INCLUDE_BENCHMARKS)
201 add_subdirectory(benchmarks)
204 if (LIBC_INCLUDE_DOCS)
205 add_subdirectory(docs)
209 if(LLVM_LIBC_FULL_BUILD)
210 add_llvm_install_targets(
213 COMPONENT libc-headers
217 add_llvm_install_targets(
219 DEPENDS ${LIBC_INSTALL_DEPENDS}