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 # For a runtimes build we need to manually include tablgen and LLVM directories.
18 if ("libc" IN_LIST LLVM_ENABLE_RUNTIMES)
20 set(LLVM_LIBC_INCLUDE_DIRS ${LLVM_MAIN_INCLUDE_DIR} ${LLVM_BINARY_DIR}/include)
23 # Path libc/scripts directory.
24 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
26 # Flags to pass down to the compiler while building the libc functions.
27 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
29 include(common_libc_tuners.cmake)
31 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
33 # Check --print-resource-dir to find the compiler resource dir if this flag
34 # is supported by the compiler.
36 OUTPUT_STRIP_TRAILING_WHITESPACE
37 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
38 RESULT_VARIABLE COMMAND_RETURN_CODE
39 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
41 # Retrieve the host compiler's resource dir.
42 if(COMMAND_RETURN_CODE EQUAL 0)
43 set(COMPILER_RESOURCE_DIR
44 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
46 message(STATUS "Set COMPILER_RESOURCE_DIR to "
47 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
49 set(COMPILER_RESOURCE_DIR OFF)
50 message(STATUS "COMPILER_RESOURCE_DIR not set
51 --print-resource-dir not supported by host compiler")
54 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
55 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
56 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
58 option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
59 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
61 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
62 include(LLVMLibCArchitectures)
64 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
65 include(prepare_libc_gpu_build)
68 include(LLVMLibCCheckMPFR)
70 if(LLVM_LIBC_CLANG_TIDY)
71 set(LLVM_LIBC_ENABLE_LINTING ON)
74 if(LLVM_LIBC_ENABLE_LINTING)
75 if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
76 set(LLVM_LIBC_ENABLE_LINTING OFF)
77 message(WARNING "C++ compiler is not clang++, linting with be disabled.")
79 if (NOT LLVM_LIBC_CLANG_TIDY)
80 find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
83 if(LLVM_LIBC_CLANG_TIDY)
84 # Check clang-tidy major version.
85 execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
86 OUTPUT_VARIABLE CLANG_TIDY_OUTPUT)
87 string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
88 string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
89 "${CMAKE_CXX_COMPILER_VERSION}")
90 if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
91 set(LLVM_LIBC_ENABLE_LINTING OFF)
93 'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
94 'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
97 The path to the clang-tidy binary can be set manually by passing
98 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
101 message(FATAL_ERROR "
102 Linting is enabled but 'clang-tidy' is not found!
104 The path to the clang-tidy binary can be set manually by passing
105 -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
107 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
108 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
113 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
114 if(LLVM_LIBC_INCLUDE_SCUDO)
115 if (NOT "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
116 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS")
120 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
122 include(CMakeParseArguments)
123 include(LLVMLibCCheckCpuFeatures)
124 include(LLVMLibCRules)
126 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
127 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
128 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
129 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
131 message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
133 include(${entrypoint_file})
135 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
136 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
137 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
138 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
141 set(TARGET_ENTRYPOINT_NAME_LIST "")
142 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
143 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
144 if(${last_dot_loc} EQUAL -1)
145 message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
146 "(dot) in the name.")
148 math(EXPR name_loc "${last_dot_loc} + 1")
149 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
150 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
153 if(LLVM_LIBC_FULL_BUILD)
154 # We need to set up hdrgen first since other targets depend on it.
155 add_subdirectory(utils/LibcTableGenUtil)
156 add_subdirectory(utils/HdrGen)
161 set(LIBC_INSTALL_DEPENDS)
162 set(LIBC_INSTALL_TARGET)
163 if(LLVM_LIBC_FULL_BUILD)
164 set(LIBC_TARGET libc)
165 set(LIBC_COMPONENT libc)
166 set(LIBC_INSTALL_DEPENDS "libc;install-libc-headers;libc-startup")
167 set(LIBC_INSTALL_TARGET install-libc)
168 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
169 set(LIBC_ARCHIVE_NAME cgpu)
171 set(LIBC_ARCHIVE_NAME c)
174 set(LIBC_TARGET llvmlibc)
175 set(LIBC_COMPONENT llvmlibc)
176 set(LIBC_INSTALL_DEPENDS llvmlibc)
177 set(LIBC_INSTALL_TARGET install-llvmlibc)
178 set(LIBC_ARCHIVE_NAME llvmlibc)
181 add_subdirectory(include)
182 add_subdirectory(config)
183 add_subdirectory(src)
184 add_subdirectory(utils)
186 if(LLVM_LIBC_FULL_BUILD)
187 # The startup system can potentially depend on the library components so add
188 # it after the library implementation directories.
189 add_subdirectory(startup)
192 # The lib and test directories are added at the very end as tests
193 # and libraries potentially draw from the components present in all
194 # of the other directories.
195 # TODO: Add testing support for the libc GPU target.
196 add_subdirectory(lib)
197 if(LLVM_INCLUDE_TESTS AND NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
198 add_subdirectory(test)
199 add_subdirectory(fuzzing)
202 if(LIBC_INCLUDE_BENCHMARKS)
203 add_subdirectory(benchmarks)
206 if (LIBC_INCLUDE_DOCS)
207 add_subdirectory(docs)
211 if(LLVM_LIBC_FULL_BUILD)
212 add_llvm_install_targets(
215 COMPONENT libc-headers
219 add_llvm_install_targets(
220 ${LIBC_INSTALL_TARGET}
221 DEPENDS ${LIBC_INSTALL_DEPENDS}
222 COMPONENT ${LIBC_COMPONENT}