1 cmake_minimum_required(VERSION 3.13.4)
3 # Use old version of target_sources command which converts the source
4 # file paths to full paths.
5 cmake_policy(SET CMP0076 OLD)
6 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
8 # The top-level sourse and binary directories.
9 set(LIBC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
10 set(LIBC_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
11 # The top-level directory in which libc is being built.
12 set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
14 # Path libc/scripts directory.
15 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
17 set(LIBC_TARGET_OS ${CMAKE_SYSTEM_NAME})
18 string(TOLOWER ${LIBC_TARGET_OS} LIBC_TARGET_OS)
20 # Defines LIBC_TARGET_ARCHITECTURE and associated macros.
21 include(LLVMLibCArchitectures)
22 include(LLVMLibCCheckMPFR)
24 # Flags to pass down to the compiler while building the libc functions.
25 set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
27 # Check --print-resource-dir to find the compiler resource dir if this flag
28 # is supported by the compiler.
30 OUTPUT_STRIP_TRAILING_WHITESPACE
31 COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
32 RESULT_VARIABLE COMMAND_RETURN_CODE
33 OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
35 # Retrieve the host compiler's resource dir.
36 if(COMMAND_RETURN_CODE EQUAL 0)
37 set(COMPILER_RESOURCE_DIR
38 "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
40 message(STATUS "Set COMPILER_RESOURCE_DIR to "
41 "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
43 set(COMPILER_RESOURCE_DIR OFF)
44 message(STATUS "COMPILER_RESOURCE_DIR not set
45 --print-resource-dir not supported by host compiler")
48 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
50 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
51 if(LLVM_LIBC_ENABLE_LINTING AND (NOT LLVM_LIBC_FULL_BUILD))
52 message(FATAL_ERROR "Cannot enable linting when full libc build is not enabled.")
54 if(LLVM_LIBC_ENABLE_LINTING)
55 if("clang-tools-extra" IN_LIST LLVM_ENABLE_PROJECTS
56 AND "clang" IN_LIST LLVM_ENABLE_PROJECTS)
57 add_custom_target(lint-libc)
58 file(COPY ${LIBC_SOURCE_DIR}/.clang-tidy DESTINATION ${LIBC_BUILD_DIR})
61 'clang' and 'clang-tools-extra' are required in LLVM_ENABLE_PROJECTS to
62 lint llvm-libc. The linting step performs important checks to help prevent
63 the introduction of subtle bugs, but it may increase build times.
65 To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
66 (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
68 elseif(LLVM_LIBC_FULL_BUILD)
70 Linting for libc is currently disabled.
72 This is not recommended, to enable set LLVM_LIBC_ENABLE_LINTING to ON
73 (pass -DLLVM_LIBC_ENABLE_LINTING=ON to cmake).")
76 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)
77 if(LLVM_LIBC_INCLUDE_SCUDO)
78 if (NOT "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
79 message(FATAL_ERROR "SCUDO cannot be included without adding compiler-rt to LLVM_ENABLE_PROJECTS")
83 include(CMakeParseArguments)
84 include(LLVMLibCRules)
85 include(LLVMLibCCheckCpuFeatures)
87 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
88 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/entrypoints.txt")
89 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
90 set(entrypoint_file "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/entrypoints.txt")
92 message(FATAL_ERROR "entrypoints.txt file for the target platform not found.")
94 include(${entrypoint_file})
96 if(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
97 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/headers.txt")
98 elseif(EXISTS "${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
99 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/headers.txt")
102 set(TARGET_ENTRYPOINT_NAME_LIST "")
103 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
104 string(FIND ${entrypoint} "." last_dot_loc REVERSE)
105 if(${last_dot_loc} EQUAL -1)
106 message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
107 "(dot) in the name.")
109 math(EXPR name_loc "${last_dot_loc} + 1")
110 string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
111 list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
114 if(LLVM_LIBC_FULL_BUILD)
115 # We need to set up hdrgen first since other targets depend on it.
116 add_subdirectory(utils/LibcTableGenUtil)
117 add_subdirectory(utils/HdrGen)
120 add_subdirectory(include)
121 add_subdirectory(config)
122 add_subdirectory(src)
123 add_subdirectory(utils)
125 if(LLVM_LIBC_FULL_BUILD)
126 # The loader can potentially depend on the library components so add it
127 # after the library implementation directories.
128 add_subdirectory(loader)
131 # The lib and test directories are added at the very end as tests
132 # and libraries potentially draw from the components present in all
133 # of the other directories.
134 add_subdirectory(lib)
135 if(LLVM_INCLUDE_TESTS)
136 add_subdirectory(test)
137 add_subdirectory(fuzzing)
140 add_subdirectory(benchmarks)