[docs] Improve documentation around CMAKE_BUILD_TYPE
[llvm-project.git] / libc / CMakeLists.txt
blobca9a81991fa988b73c68bc7ed6b67ff5b716a70f
1 cmake_minimum_required(VERSION 3.13.4)
3 # Default to C++17
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 # Path libc/scripts directory.
18 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
20 set(LIBC_TARGET_OS ${CMAKE_SYSTEM_NAME})
21 string(TOLOWER ${LIBC_TARGET_OS} LIBC_TARGET_OS)
23 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
24 include(LLVMLibCArchitectures)
25 include(LLVMLibCCheckMPFR)
27 # Flags to pass down to the compiler while building the libc functions.
28 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
30 # Check --print-resource-dir to find the compiler resource dir if this flag
31 # is supported by the compiler.
32 execute_process(
33   OUTPUT_STRIP_TRAILING_WHITESPACE
34   COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
35   RESULT_VARIABLE COMMAND_RETURN_CODE
36   OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
38 # Retrieve the host compiler's resource dir.
39 if(COMMAND_RETURN_CODE EQUAL 0)
40   set(COMPILER_RESOURCE_DIR
41     "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
42   )
43   message(STATUS "Set COMPILER_RESOURCE_DIR to "
44                  "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
45 else()
46   set(COMPILER_RESOURCE_DIR OFF)
47   message(STATUS "COMPILER_RESOURCE_DIR not set
48                   --print-resource-dir not supported by host compiler")
49 endif()
51 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
53 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
55 if(LLVM_LIBC_CLANG_TIDY)
56   set(LLVM_LIBC_ENABLE_LINTING ON)
57 endif()
59 if(LLVM_LIBC_ENABLE_LINTING)
60   if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
61     set(LLVM_LIBC_ENABLE_LINTING OFF)
62     message(WARNING "C++ compiler is not clang++, linting with be disabled.")
63   else()
64     if (NOT LLVM_LIBC_CLANG_TIDY)
65       find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
66     endif()
68     if(LLVM_LIBC_CLANG_TIDY)
69       # Check clang-tidy major version.
70       execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
71                       OUTPUT_VARIABLE CLANG_TIDY_OUTPUT)
72       string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
73       string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
74              "${CMAKE_CXX_COMPILER_VERSION}")
75       if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
76         set(LLVM_LIBC_ENABLE_LINTING OFF)
77         message(WARNING "
78           'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
79           'clang' (version ${CLANG_MAJOR_VERSION}).  Linting will
80           be disabled.
82           The path to the clang-tidy binary can be set manually by passing
83           -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
84       endif()
85     else()
86       message(FATAL_ERROR "
87         Linting is enabled but 'clang-tidy' is not found!
89         The path to the clang-tidy binary can be set manually by passing
90         -DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
92         To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
93         (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
94     endif()
95   endif()
96 endif()
98 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
99 if(LLVM_LIBC_INCLUDE_SCUDO)
100   if (NOT "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
101     message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS")
102   endif()
103 endif()
105 option(LIBC_INCLUDE_DOCS "Build the libc documentation." ${LLVM_INCLUDE_DOCS})
107 include(CMakeParseArguments)
108 include(LLVMLibCRules)
109 include(LLVMLibCCheckCpuFeatures)
111 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
112   set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
113 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
114   set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
115 else()
116   message(FATAL_ERROR "entrypoints.txt file for the target platform '${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}' not found.")
117 endif()
118 include(${entrypoint_file})
120 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
121   include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
122 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
123   include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
124 endif()
126 set(TARGET_ENTRYPOINT_NAME_LIST "")
127 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
128   string(FIND ${entrypoint} "." last_dot_loc REVERSE)
129   if(${last_dot_loc} EQUAL -1)
130     message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
131                   "(dot) in the name.")
132   endif()
133   math(EXPR name_loc "${last_dot_loc} + 1")
134   string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
135   list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
136 endforeach()
138 if(LLVM_LIBC_FULL_BUILD)
139   # We need to set up hdrgen first since other targets depend on it.
140   add_subdirectory(utils/LibcTableGenUtil)
141   add_subdirectory(utils/HdrGen)
142 endif()
144 add_subdirectory(include)
145 add_subdirectory(config)
146 add_subdirectory(src)
147 add_subdirectory(utils)
149 if(LLVM_LIBC_FULL_BUILD)
150   # The loader can potentially depend on the library components so add it
151   # after the library implementation directories.
152   add_subdirectory(loader)
153 endif()
155 # The lib and test directories are added at the very end as tests
156 # and libraries potentially draw from the components present in all
157 # of the other directories.
158 add_subdirectory(lib)
159 if(LLVM_INCLUDE_TESTS)
160   add_subdirectory(test)
161   add_subdirectory(fuzzing)
162 endif()
164 if(LIBC_INCLUDE_BENCHMARKS)
165   add_subdirectory(benchmarks)
166 endif()
168 if (LIBC_INCLUDE_DOCS)
169   add_subdirectory(docs)
170 endif()