Merge branch 'addmainunit4-altlink-sharedstmt-dieref-dwz3-inlinebug-testcategory...
[lldb.git] / libc / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.13.4)
2
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")
7
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})
13
14 # Path libc/scripts directory.
15 set(LIBC_BUILD_SCRIPTS_DIR "${LIBC_SOURCE_DIR}/utils/build_scripts")
16
17 set(LIBC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE PATH "Define libc destination prefix.")
18
19 set(LIBC_TARGET_OS ${CMAKE_SYSTEM_NAME})
20 string(TOLOWER ${LIBC_TARGET_OS} LIBC_TARGET_OS)
21
22 set(LIBC_TARGET_MACHINE ${CMAKE_SYSTEM_PROCESSOR})
23
24 # Check --print-resource-dir to find the compiler resource dir if this flag
25 # is supported by the compiler.
26 execute_process(
27   OUTPUT_STRIP_TRAILING_WHITESPACE
28   COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir
29   RESULT_VARIABLE COMMAND_RETURN_CODE
30   OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
31 )
32 # Retrieve the host compiler's resource dir.
33 if(COMMAND_RETURN_CODE EQUAL 0)
34   set(COMPILER_RESOURCE_DIR
35     "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir"
36   )
37   message(STATUS "Set COMPILER_RESOURCE_DIR to "
38                  "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
39 else()
40   set(COMPILER_RESOURCE_DIR OFF)
41   message(STATUS "COMPILER_RESOURCE_DIR not set
42                   --print-resource-dir not supported by host compiler")
43 endif()
44
45 option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
46 if(LLVM_LIBC_ENABLE_LINTING)
47   if("clang-tools-extra" IN_LIST LLVM_ENABLE_PROJECTS
48              AND "clang" IN_LIST LLVM_ENABLE_PROJECTS)
49     add_custom_target(lint-libc)
50   else()
51     message(FATAL_ERROR "
52       'clang' and 'clang-tools-extra' are required in LLVM_ENABLE_PROJECTS to
53       lint llvm-libc. The linting step performs important checks to help prevent
54       the introduction of subtle bugs, but it may increase build times.
55
56       To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
57       (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
58   endif()
59 else()
60   message(WARNING "
61     Linting for libc is currently disabled.
62
63     This is not recommended, to enable set LLVM_LIBC_ENABLE_LINTING to ON
64     (pass -DLLVM_LIBC_ENABLE_LINTING=ON to cmake).")
65 endif()
66
67 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
68
69 include(CMakeParseArguments)
70 include(LLVMLibCRules)
71 include(LLVMLibCCheckCpuFeatures)
72
73 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_MACHINE}/entrypoints.txt")
74 include("${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_MACHINE}/headers.txt")
75
76 set(TARGET_ENTRYPOINT_NAME_LIST "")
77 foreach(entrypoint IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
78   string(FIND ${entrypoint} "." last_dot_loc REVERSE)
79   if(${last_dot_loc} EQUAL -1)
80     message(FATAL "Invalid entrypoint target name ${entrypoint}; Expected a '.' "
81                   "(dot) in the name.")
82   endif()
83   math(EXPR name_loc "${last_dot_loc} + 1")
84   string(SUBSTRING ${entrypoint} ${name_loc} -1 entrypoint_name)
85   list(APPEND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name})
86 endforeach()
87
88 if(LLVM_LIBC_FULL_BUILD)
89   # We need to set up hdrgen first since other targets depend on it.
90   add_subdirectory(utils/LibcTableGenUtil)
91   add_subdirectory(utils/HdrGen)
92 endif()
93
94 add_subdirectory(include)
95 add_subdirectory(config)
96 add_subdirectory(src)
97 add_subdirectory(utils)
98
99 if(LLVM_LIBC_FULL_BUILD)
100   # The loader can potentially depend on the library components so add it
101   # after the library implementation directories.
102   add_subdirectory(loader)
103 endif()
104
105 # The lib and test directories are added at the very end as tests
106 # and libraries potentially draw from the components present in all
107 # of the other directories.
108 add_subdirectory(lib)
109 if(LLVM_INCLUDE_TESTS)
110   add_subdirectory(test)
111   add_subdirectory(fuzzing)
112 endif()
113
114 add_subdirectory(benchmarks)