1include(BuiltinTests)
2include(CheckCSourceCompiles)
3
4# Make all the tests only check the compiler
5set(TEST_COMPILE_ONLY On)
6
7# Check host compiler support for certain flags
8builtin_check_c_compiler_flag(-fPIC                 COMPILER_RT_HAS_FPIC_FLAG)
9builtin_check_c_compiler_flag(-fPIE                 COMPILER_RT_HAS_FPIE_FLAG)
10builtin_check_c_compiler_flag(-fno-builtin          COMPILER_RT_HAS_FNO_BUILTIN_FLAG)
11builtin_check_c_compiler_flag(-std=c11              COMPILER_RT_HAS_STD_C11_FLAG)
12builtin_check_c_compiler_flag(-fvisibility=hidden   COMPILER_RT_HAS_VISIBILITY_HIDDEN_FLAG)
13builtin_check_c_compiler_flag(-ffreestanding        COMPILER_RT_HAS_FREESTANDING_FLAG)
14builtin_check_c_compiler_flag(-fxray-instrument     COMPILER_RT_HAS_XRAY_COMPILER_FLAG)
15
16builtin_check_c_compiler_source(COMPILER_RT_HAS_ATOMIC_KEYWORD
17"
18int foo(int x, int y) {
19 _Atomic int result = x * y;
20 return result;
21}
22")
23
24builtin_check_c_compiler_source(COMPILER_RT_HAS_FLOAT16
25"
26_Float16 foo(_Float16 x) {
27 return x;
28}
29"
30)
31
32builtin_check_c_compiler_source(COMPILER_RT_HAS_ASM_LSE
33"
34asm(\".arch armv8-a+lse\");
35asm(\"cas w0, w1, [x2]\");
36")
37
38set(ARM64 aarch64)
39set(ARM32 arm armhf armv6m armv7m armv7em armv7 armv7s armv7k armv8m.main armv8.1m.main)
40set(HEXAGON hexagon)
41set(X86 i386)
42set(X86_64 x86_64)
43set(MIPS32 mips mipsel)
44set(MIPS64 mips64 mips64el)
45set(PPC32 powerpc)
46set(PPC64 powerpc64 powerpc64le)
47set(RISCV32 riscv32)
48set(RISCV64 riscv64)
49set(SPARC sparc)
50set(SPARCV9 sparcv9)
51set(WASM32 wasm32)
52set(WASM64 wasm64)
53set(VE ve)
54
55if(APPLE)
56  set(ARM64 arm64 arm64e)
57  set(ARM32 armv7 armv7k armv7s)
58  set(X86_64 x86_64 x86_64h)
59endif()
60
61set(ALL_BUILTIN_SUPPORTED_ARCH
62  ${X86} ${X86_64} ${ARM32} ${ARM64}
63  ${HEXAGON} ${MIPS32} ${MIPS64} ${PPC32} ${PPC64}
64  ${RISCV32} ${RISCV64} ${SPARC} ${SPARCV9}
65  ${WASM32} ${WASM64} ${VE})
66
67include(CompilerRTUtils)
68include(CompilerRTDarwinUtils)
69
70if(APPLE)
71
72  find_darwin_sdk_dir(DARWIN_osx_SYSROOT macosx)
73  find_darwin_sdk_dir(DARWIN_iossim_SYSROOT iphonesimulator)
74  find_darwin_sdk_dir(DARWIN_ios_SYSROOT iphoneos)
75  find_darwin_sdk_dir(DARWIN_watchossim_SYSROOT watchsimulator)
76  find_darwin_sdk_dir(DARWIN_watchos_SYSROOT watchos)
77  find_darwin_sdk_dir(DARWIN_tvossim_SYSROOT appletvsimulator)
78  find_darwin_sdk_dir(DARWIN_tvos_SYSROOT appletvos)
79
80  # Get supported architecture from SDKSettings.
81  function(sdk_has_arch_support sdk_path os arch has_support)
82    execute_process(COMMAND
83        /usr/libexec/PlistBuddy -c "Print :SupportedTargets:${os}:Archs" ${sdk_path}/SDKSettings.plist
84      OUTPUT_VARIABLE SDK_SUPPORTED_ARCHS
85      RESULT_VARIABLE PLIST_ERROR)
86    if (PLIST_ERROR EQUAL 0 AND
87        SDK_SUPPORTED_ARCHS MATCHES " ${arch}\n")
88      message(STATUS "Found ${arch} support in ${sdk_path}/SDKSettings.plist")
89      set("${has_support}" On PARENT_SCOPE)
90    else()
91      message(STATUS "No ${arch} support in ${sdk_path}/SDKSettings.plist")
92      set("${has_support}" Off PARENT_SCOPE)
93    endif()
94  endfunction()
95
96  set(DARWIN_EMBEDDED_PLATFORMS)
97  set(DARWIN_osx_BUILTIN_MIN_VER 10.5)
98  set(DARWIN_osx_BUILTIN_MIN_VER_FLAG
99      -mmacosx-version-min=${DARWIN_osx_BUILTIN_MIN_VER})
100  set(DARWIN_osx_BUILTIN_ALL_POSSIBLE_ARCHS ${X86} ${X86_64})
101  # Add support for arm64 macOS if available in SDK.
102  foreach(arch ${ARM64})
103    sdk_has_arch_support(${DARWIN_osx_SYSROOT} macosx ${arch} MACOS_ARM_SUPPORT)
104    if (MACOS_ARM_SUPPORT)
105     list(APPEND DARWIN_osx_BUILTIN_ALL_POSSIBLE_ARCHS ${arch})
106    endif()
107  endforeach(arch)
108
109  if(COMPILER_RT_ENABLE_IOS)
110    list(APPEND DARWIN_EMBEDDED_PLATFORMS ios)
111    set(DARWIN_ios_MIN_VER_FLAG -miphoneos-version-min)
112    set(DARWIN_ios_BUILTIN_MIN_VER 6.0)
113    set(DARWIN_ios_BUILTIN_MIN_VER_FLAG
114      ${DARWIN_ios_MIN_VER_FLAG}=${DARWIN_ios_BUILTIN_MIN_VER})
115    set(DARWIN_ios_BUILTIN_ALL_POSSIBLE_ARCHS ${ARM64} ${ARM32})
116    set(DARWIN_iossim_BUILTIN_ALL_POSSIBLE_ARCHS ${X86} ${X86_64})
117  endif()
118  if(COMPILER_RT_ENABLE_WATCHOS)
119    list(APPEND DARWIN_EMBEDDED_PLATFORMS watchos)
120    set(DARWIN_watchos_MIN_VER_FLAG -mwatchos-version-min)
121    set(DARWIN_watchos_BUILTIN_MIN_VER 2.0)
122    set(DARWIN_watchos_BUILTIN_MIN_VER_FLAG
123      ${DARWIN_watchos_MIN_VER_FLAG}=${DARWIN_watchos_BUILTIN_MIN_VER})
124    set(DARWIN_watchos_BUILTIN_ALL_POSSIBLE_ARCHS armv7 armv7k arm64_32)
125    set(DARWIN_watchossim_BUILTIN_ALL_POSSIBLE_ARCHS ${X86})
126  endif()
127  if(COMPILER_RT_ENABLE_TVOS)
128    list(APPEND DARWIN_EMBEDDED_PLATFORMS tvos)
129    set(DARWIN_tvos_MIN_VER_FLAG -mtvos-version-min)
130    set(DARWIN_tvos_BUILTIN_MIN_VER 9.0)
131    set(DARWIN_tvos_BUILTIN_MIN_VER_FLAG
132      ${DARWIN_tvos_MIN_VER_FLAG}=${DARWIN_tvos_BUILTIN_MIN_VER})
133    set(DARWIN_tvos_BUILTIN_ALL_POSSIBLE_ARCHS armv7 arm64)
134    set(DARWIN_tvossim_BUILTIN_ALL_POSSIBLE_ARCHS ${X86} ${X86_64})
135  endif()
136
137  set(BUILTIN_SUPPORTED_OS osx)
138
139  # We're setting the flag manually for each target OS
140  set(CMAKE_OSX_DEPLOYMENT_TARGET "")
141
142  # NOTE: We deliberately avoid using `DARWIN_<os>_ARCHS` here because that is
143  # used by `config-ix.cmake` in the context of building the rest of
144  # compiler-rt where the global `${TEST_COMPILE_ONLY}` (used by
145  # `darwin_test_archs()`) has a different value.
146  darwin_test_archs(osx
147    DARWIN_osx_BUILTIN_ARCHS
148    ${DARWIN_osx_BUILTIN_ALL_POSSIBLE_ARCHS}
149  )
150  message(STATUS "OSX supported builtin arches: ${DARWIN_osx_BUILTIN_ARCHS}")
151  foreach(arch ${DARWIN_osx_BUILTIN_ARCHS})
152    list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
153    set(CAN_TARGET_${arch} 1)
154  endforeach()
155
156  foreach(platform ${DARWIN_EMBEDDED_PLATFORMS})
157    if(DARWIN_${platform}sim_SYSROOT)
158      set(DARWIN_${platform}sim_BUILTIN_MIN_VER
159        ${DARWIN_${platform}_BUILTIN_MIN_VER})
160      set(DARWIN_${platform}sim_BUILTIN_MIN_VER_FLAG
161        ${DARWIN_${platform}_BUILTIN_MIN_VER_FLAG})
162
163      set(DARWIN_${platform}sim_SKIP_CC_KEXT On)
164
165      darwin_test_archs(${platform}sim
166        DARWIN_${platform}sim_BUILTIN_ARCHS
167        ${DARWIN_${platform}sim_BUILTIN_ALL_POSSIBLE_ARCHS}
168      )
169      message(STATUS "${platform} Simulator supported builtin arches: ${DARWIN_${platform}sim_BUILTIN_ARCHS}")
170      if(DARWIN_${platform}sim_BUILTIN_ARCHS)
171        list(APPEND BUILTIN_SUPPORTED_OS ${platform}sim)
172      endif()
173      foreach(arch ${DARWIN_${platform}sim_BUILTIN_ARCHS})
174        list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
175        set(CAN_TARGET_${arch} 1)
176      endforeach()
177    endif()
178
179    if(DARWIN_${platform}_SYSROOT)
180      darwin_test_archs(${platform}
181        DARWIN_${platform}_BUILTIN_ARCHS
182        ${DARWIN_${platform}_BUILTIN_ALL_POSSIBLE_ARCHS}
183      )
184      message(STATUS "${platform} supported builtin arches: ${DARWIN_${platform}_BUILTIN_ARCHS}")
185      if(DARWIN_${platform}_BUILTIN_ARCHS)
186        list(APPEND BUILTIN_SUPPORTED_OS ${platform})
187      endif()
188      foreach(arch ${DARWIN_${platform}_BUILTIN_ARCHS})
189        list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
190        set(CAN_TARGET_${arch} 1)
191      endforeach()
192    endif()
193  endforeach()
194
195  list_intersect(BUILTIN_SUPPORTED_ARCH ALL_BUILTIN_SUPPORTED_ARCH COMPILER_RT_SUPPORTED_ARCH)
196
197else()
198  # If we're not building the builtins standalone, just rely on the  tests in
199  # config-ix.cmake to tell us what to build. Otherwise we need to do some leg
200  # work here...
201  if(COMPILER_RT_BUILTINS_STANDALONE_BUILD)
202    test_targets()
203  endif()
204  # Architectures supported by compiler-rt libraries.
205  filter_available_targets(BUILTIN_SUPPORTED_ARCH
206    ${ALL_BUILTIN_SUPPORTED_ARCH})
207endif()
208
209message(STATUS "Builtin supported architectures: ${BUILTIN_SUPPORTED_ARCH}")
210