1 # ------------------------------------------------------------------------------
2 # Architecture and OS definitions.
4 # The correct target OS and architecture to build the libc for is deduced here.
5 # When possible, we also setup appropriate compile options for the target
7 # ------------------------------------------------------------------------------
10 # If the compiler is visual c++ or equivalent, we will assume a host build.
11 set(LIBC_TARGET_OS ${CMAKE_HOST_SYSTEM_NAME})
12 string(TOLOWER ${LIBC_TARGET_OS} LIBC_TARGET_OS)
13 set(LIBC_TARGET_ARCHITECTURE ${CMAKE_HOST_SYSTEM_PROCESSOR})
14 if(LIBC_TARGET_TRIPLE)
15 message(WARNING "libc build: Detected MSVC or equivalent compiler; "
16 "LIBC_TARGET_TRIPLE is ignored and a host build is assumed.")
21 # A helper function to get the architecture and system components from a target
23 function(get_arch_and_system_from_triple triple arch_var sys_var)
24 string(REPLACE "-" ";" triple_comps ${triple})
25 list(LENGTH triple_comps triple_size)
26 if(triple_size LESS "3")
29 math(EXPR system_index "${triple_size} - 2")
30 list(GET triple_comps 0 target_arch)
31 # The target_arch string can have sub-architecture suffixes which we want to
32 # remove. So, we regex-match the string and set target_arch to a cleaner
34 if(target_arch MATCHES "^mips")
35 set(target_arch "mips")
36 elseif(target_arch MATCHES "^arm")
37 # TODO(lntue): Shall we separate `arm64`? It is currently recognized as
39 set(target_arch "arm")
40 elseif(target_arch MATCHES "^aarch64")
41 set(target_arch "aarch64")
42 elseif(target_arch MATCHES "(x86_64)|(AMD64|amd64)|(^i.86$)")
43 set(target_arch "x86_64")
44 elseif(target_arch MATCHES "^(powerpc|ppc)")
45 set(target_arch "power")
46 elseif(target_arch MATCHES "^riscv32")
47 set(target_arch "riscv32")
48 elseif(target_arch MATCHES "^riscv64")
49 set(target_arch "riscv64")
50 elseif(target_arch MATCHES "^amdgcn")
51 set(target_arch "amdgpu")
52 elseif(target_arch MATCHES "^nvptx64")
53 set(target_arch "nvptx")
58 set(${arch_var} ${target_arch} PARENT_SCOPE)
59 list(GET triple_comps ${system_index} target_sys)
61 # Correcting OS name for Apple's systems.
62 if(target_sys STREQUAL "apple")
63 list(GET triple_comps 2 target_sys)
65 # Strip version from `darwin###`
66 if(target_sys MATCHES "^darwin")
67 set(target_sys "darwin")
70 # Setting OS name for GPU architectures.
71 list(GET triple_comps -1 gpu_target_sys)
72 if(gpu_target_sys MATCHES "^amdhsa" OR gpu_target_sys MATCHES "^cuda")
76 set(${sys_var} ${target_sys} PARENT_SCOPE)
77 endfunction(get_arch_and_system_from_triple)
79 execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version -v
80 RESULT_VARIABLE libc_compiler_info_result
81 OUTPUT_VARIABLE libc_compiler_info
82 ERROR_VARIABLE libc_compiler_info)
83 if(NOT (libc_compiler_info_result EQUAL "0"))
84 message(FATAL_ERROR "libc build: error querying compiler info from the "
85 "compiler: ${libc_compiler_info}")
87 string(REGEX MATCH "Target: [-_a-z0-9.]+[ \r\n]+"
88 libc_compiler_target_info ${libc_compiler_info})
89 if(NOT libc_compiler_target_info)
90 message(FATAL_ERROR "libc build: could not read compiler target info from:\n"
91 "${libc_compiler_info}")
93 string(STRIP ${libc_compiler_target_info} libc_compiler_target_info)
94 string(SUBSTRING ${libc_compiler_target_info} 8 -1 libc_compiler_triple)
95 get_arch_and_system_from_triple(${libc_compiler_triple}
96 compiler_arch compiler_sys)
99 "libc build: Invalid or unknown libc compiler target triple: "
100 "${libc_compiler_triple}")
103 set(LIBC_TARGET_ARCHITECTURE ${compiler_arch})
104 set(LIBC_TARGET_OS ${compiler_sys})
105 set(LIBC_CROSSBUILD FALSE)
107 # One should not set LLVM_RUNTIMES_TARGET and LIBC_TARGET_TRIPLE
108 if(LLVM_RUNTIMES_TARGET AND LIBC_TARGET_TRIPLE)
110 "libc build: Specify only LLVM_RUNTIMES_TARGET if you are doing a "
111 "runtimes/bootstrap build. If you are doing a standalone build, "
112 "specify only LIBC_TARGET_TRIPLE.")
115 set(explicit_target_triple)
116 if(LLVM_RUNTIMES_TARGET)
117 set(explicit_target_triple ${LLVM_RUNTIMES_TARGET})
118 elseif(LIBC_TARGET_TRIPLE)
119 set(explicit_target_triple ${LIBC_TARGET_TRIPLE})
122 # The libc's target architecture and OS are set to match the compiler's default
123 # target triple above. However, one can explicitly set LIBC_TARGET_TRIPLE or
124 # LLVM_RUNTIMES_TARGET (for runtimes/bootstrap build). If one of them is set,
125 # then we will use that target triple to deduce libc's target OS and
127 if(explicit_target_triple)
128 get_arch_and_system_from_triple(${explicit_target_triple} libc_arch libc_sys)
131 "libc build: Invalid or unknown triple: ${explicit_target_triple}")
133 set(LIBC_TARGET_ARCHITECTURE ${libc_arch})
134 set(LIBC_TARGET_OS ${libc_sys})
137 if((LIBC_TARGET_OS STREQUAL "unknown") OR (LIBC_TARGET_OS STREQUAL "none"))
138 # We treat "unknown" and "none" systems as baremetal targets.
139 set(LIBC_TARGET_OS "baremetal")
142 # Set up some convenient vars to make conditionals easy to use in other parts of
143 # the libc CMake infrastructure. Also, this is where we also check if the target
144 # architecture is currently supported.
145 if(LIBC_TARGET_ARCHITECTURE STREQUAL "arm")
146 set(LIBC_TARGET_ARCHITECTURE_IS_ARM TRUE)
147 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "aarch64")
148 set(LIBC_TARGET_ARCHITECTURE_IS_AARCH64 TRUE)
149 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "x86_64")
150 set(LIBC_TARGET_ARCHITECTURE_IS_X86 TRUE)
151 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "riscv64")
152 set(LIBC_TARGET_ARCHITECTURE_IS_RISCV64 TRUE)
153 set(LIBC_TARGET_ARCHITECTURE "riscv")
154 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "riscv32")
155 set(LIBC_TARGET_ARCHITECTURE_IS_RISCV32 TRUE)
156 set(LIBC_TARGET_ARCHITECTURE "riscv")
157 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "amdgpu")
158 set(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU TRUE)
159 elseif(LIBC_TARGET_ARCHITECTURE STREQUAL "nvptx")
160 set(LIBC_TARGET_ARCHITECTURE_IS_NVPTX TRUE)
163 "Unsupported libc target architecture ${LIBC_TARGET_ARCHITECTURE}")
166 if(LIBC_TARGET_OS STREQUAL "baremetal")
167 set(LIBC_TARGET_OS_IS_BAREMETAL TRUE)
168 elseif(LIBC_TARGET_OS STREQUAL "linux")
169 set(LIBC_TARGET_OS_IS_LINUX TRUE)
170 elseif(LIBC_TARGET_OS STREQUAL "poky" OR LIBC_TARGET_OS STREQUAL "suse" OR
171 LIBC_TARGET_OS STREQUAL "redhat")
172 # poky are custom Linux-base systems created by yocto. Since these are Linux
173 # images, we change the LIBC_TARGET_OS to linux. This define is used to
174 # include the right directories during compilation.
176 # openSUSE and redhat use different triple format which causes LIBC_TARGET_OS
177 # to be computed as "suse" or "redhat" instead of "linux".
178 set(LIBC_TARGET_OS_IS_LINUX TRUE)
179 set(LIBC_TARGET_OS "linux")
180 elseif(LIBC_TARGET_OS STREQUAL "darwin")
181 set(LIBC_TARGET_OS_IS_DARWIN TRUE)
182 elseif(LIBC_TARGET_OS STREQUAL "windows")
183 set(LIBC_TARGET_OS_IS_WINDOWS TRUE)
184 elseif(LIBC_TARGET_OS STREQUAL "gpu")
185 set(LIBC_TARGET_OS_IS_GPU TRUE)
188 "Unsupported libc target operating system ${LIBC_TARGET_OS}")
192 # If the compiler target triple is not the same as the triple specified by
193 # LIBC_TARGET_TRIPLE or LLVM_RUNTIMES_TARGET, we will add a --target option
194 # if the compiler is clang. If the compiler is GCC we just error out as there
195 # is no equivalent of an option like --target.
196 if(explicit_target_triple AND
197 (NOT (libc_compiler_triple STREQUAL explicit_target_triple)))
198 set(LIBC_CROSSBUILD TRUE)
199 if(CMAKE_COMPILER_IS_GNUCXX)
201 "GCC target triple (${libc_compiler_triple}) and the explicity "
202 "specified target triple (${explicit_target_triple}) do not match.")
205 LIBC_COMPILE_OPTIONS_DEFAULT "--target=${explicit_target_triple}")
210 "Building libc for ${LIBC_TARGET_ARCHITECTURE} on ${LIBC_TARGET_OS}")