[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / CMakeLists.txt
blob4be92ba1380c9e2fdba1f4e611fd6534eab11454
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)
6 endif()
7 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
8   NO_POLICY_SCOPE)
10 # Default to C++17
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 if(LLVM_LIBC_FULL_BUILD OR LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
23   if(NOT LIBC_HDRGEN_EXE)
24     # We need to set up hdrgen first since other targets depend on it.
25     add_subdirectory(utils/LibcTableGenUtil)
26     add_subdirectory(utils/HdrGen)
27   else()
28     message(STATUS "Will use ${LIBC_HDRGEN_EXE} for libc header generation.")
29   endif()
30 endif()
32 option(LIBC_HDRGEN_ONLY "Only build the 'libc-hdrgen' executable" OFF)
33 if(("libc" IN_LIST LLVM_ENABLE_RUNTIMES AND NOT LLVM_RUNTIMES_BUILD) OR 
34    LIBC_HDRGEN_ONLY)
35   # When libc is build as part of the runtimes/bootstrap build's CMake run, we
36   # only need to build the host tools to build the libc. So, we just do enough
37   # to build libc-hdrgen and return.
39   # Always make the RPC server availible to other projects for GPU mode.
40   if(LIBC_GPU_BUILD OR LIBC_GPU_ARCHITECTURES)
41     add_subdirectory(utils/gpu/server)
42   endif()
43   return()
44 endif()
46 option(LIBC_CMAKE_VERBOSE_LOGGING
47        "Log details warnings and notifications during CMake configuration." OFF)
48 # Path libc/scripts directory.
49 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
51 # Flags to pass down to the compiler while building the libc functions.
52 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
54 include(common_libc_tuners.cmake)
56 list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
58 # Check --print-resource-dir to find the compiler resource dir if this flag
59 # is supported by the compiler.
60 execute_process(
61   OUTPUT_STRIP_TRAILING_WHITESPACE
62   COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
63   RESULT_VARIABLE COMMAND_RETURN_CODE
64   OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
66 # Retrieve the host compiler's resource dir.
67 if(COMMAND_RETURN_CODE EQUAL 0)
68   set(COMPILER_RESOURCE_DIR
69     "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
70   )
71   message(STATUS "Set COMPILER_RESOURCE_DIR to "
72                  "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
73 else()
74   set(COMPILER_RESOURCE_DIR OFF)
75   message(STATUS "COMPILER_RESOURCE_DIR not set
76                   --print-resource-dir not supported by host compiler")
77 endif()
79 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
80 option(LLVM_LIBC_IMPLEMENTATION_DEFINED_TEST_BEHAVIOR "Build LLVM libc tests assuming our implementation-defined behavior" ON)
81 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
83 option(LIBC_GPU_BUILD "Build libc for the GPU. All CPU build options will be ignored." OFF)
84 set(LIBC_TARGET_TRIPLE "" CACHE STRING "The target triple for the libc build.")
86 set(LIBC_ENABLE_UNITTESTS ON)
87 set(LIBC_ENABLE_HERMETIC_TESTS ${LLVM_LIBC_FULL_BUILD})
89 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
90 include(LLVMLibCArchitectures)
92 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
93   set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
94   set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/gpu-none-llvm)
95   set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
96 elseif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND LIBC_ENABLE_USE_BY_CLANG)
97   set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE})
98   set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
99   set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
100 else()
101   if(NOT LIBC_ENABLE_USE_BY_CLANG)
102     set(LIBC_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
103     set(LIBC_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
104   elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
105     set(LIBC_INCLUDE_DIR ${LLVM_BINARY_DIR}/include)
106     set(LIBC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
107   else()
108     set(LIBC_INCLUDE_DIR ${CMAKE_BINARY_DIR}/include)
109     set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
110   endif()
111   set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
112 endif()
114 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
115   include(prepare_libc_gpu_build)
116   set(LIBC_ENABLE_UNITTESTS OFF)
117 endif()
119 include(LLVMLibCCheckMPFR)
121 if(LLVM_LIBC_CLANG_TIDY)
122   set(LLVM_LIBC_ENABLE_LINTING ON)
123 endif()
125 if(LLVM_LIBC_ENABLE_LINTING)
126   if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
127     set(LLVM_LIBC_ENABLE_LINTING OFF)
128     message(WARNING "C++ compiler is not clang++, linting with be disabled.")
129   else()
130     if (NOT LLVM_LIBC_CLANG_TIDY)
131       find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
132     endif()
134     if(LLVM_LIBC_CLANG_TIDY)
135       # Check clang-tidy major version.
136       execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
137                       OUTPUT_VARIABLE CLANG_TIDY_OUTPUT)
138       string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
139       string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
140              "${CMAKE_CXX_COMPILER_VERSION}")
141       if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
142         set(LLVM_LIBC_ENABLE_LINTING OFF)
143         message(WARNING "
144           'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
145           'clang' (version ${CLANG_MAJOR_VERSION}).  Linting will
146           be disabled.
148           The path to the clang-tidy binary can be set manually by passing
149           -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
150       endif()
151       add_custom_target(libc-lint)
152     else()
153       message(FATAL_ERROR "
154         Linting is enabled but 'clang-tidy' is not found!
156         The path to the clang-tidy binary can be set manually by passing
157         -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
159         To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
160         (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
161     endif()
162   endif()
163 endif()
165 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
166 if(LLVM_LIBC_INCLUDE_SCUDO)
167   if (NOT "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
168     message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS")
169   endif()
170 endif()
172 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
174 include(CMakeParseArguments)
175 include(LLVMLibCCheckCpuFeatures)
176 include(LLVMLibCRules)
178 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
179   set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
180 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
181   set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
182 else()
183   message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
184 endif()
185 include(${entrypoint_file})
187 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
188   include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
189 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
190   include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
191 endif()
193 set(TARGET_ENTRYPOINT_NAME_LIST "")
194 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
195   string(FIND ${entrypoint} "." last_dot_loc REVERSE)
196   if(${last_dot_loc} EQUAL -1)
197     message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
198                   "(dot) in the name.")
199   endif()
200   math(EXPR name_loc "${last_dot_loc} + 1")
201   string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
202   list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
203 endforeach()
205 set(LIBC_INSTALL_DEPENDS)
206 if(LLVM_LIBC_FULL_BUILD)
207   set(LIBC_INSTALL_DEPENDS "install-libc-static-archives;install-libc-headers")
208   if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
209     # For now we will disable libc-startup installation for baremetal. The
210     # correct way to do it would be to make a hookable startup for baremetal
211     # and install it as part of the libc installation.
212     list(APPEND LIBC_INSTALL_DEPENDS "libc-startup")
213   endif()
214 else()
215   set(LIBC_INSTALL_DEPENDS install-libc-static-archives)
216 endif()
218 add_subdirectory(include)
219 add_subdirectory(config)
220 add_subdirectory(src)
221 add_subdirectory(utils)
223 if(LLVM_LIBC_FULL_BUILD)
224   # The startup system can potentially depend on the library components so add
225   # it after the library implementation directories.
226   add_subdirectory(startup)
227 endif()
229 # The lib and test directories are added at the very end as tests
230 # and libraries potentially draw from the components present in all
231 # of the other directories.
232 # TODO: Add testing support for the libc GPU target.
233 add_subdirectory(lib)
234 if(LLVM_INCLUDE_TESTS)
235   add_subdirectory(test)
236   add_subdirectory(fuzzing)
237 endif()
239 if(LIBC_INCLUDE_BENCHMARKS)
240   add_subdirectory(benchmarks)
241 endif()
243 if (LIBC_INCLUDE_DOCS)
244   add_subdirectory(docs)
245 endif()
248 if(LLVM_LIBC_FULL_BUILD)
249   add_llvm_install_targets(
250     install-libc-headers
251     DEPENDS libc-headers
252     COMPONENT libc-headers
253   )
254 endif()
256 add_llvm_install_targets(
257   install-libc
258   DEPENDS ${LIBC_INSTALL_DEPENDS}
259   COMPONENT libc