Skip to content

Commit b2303de

Browse files
[compiler-rt] Add support for arm64 macOS
Summary: Allow compiler-rt to build for arm64 support on macOS. Reviewers: arphaman, doug.gregor, dexonsmith Reviewed By: arphaman, dexonsmith Subscribers: dberris, mgorny, kristof.beyls, jkorous, ributzka, danielkiss, #sanitizers, llvm-commits Tags: #sanitizers Differential Revision: https://reviews.llvm.org/D82610
1 parent 8808574 commit b2303de

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

compiler-rt/cmake/Modules/CompilerRTDarwinUtils.cmake

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,46 @@ function(darwin_test_archs os valid_archs)
169169
CACHE STRING "List of valid architectures for platform ${os}." FORCE)
170170
endfunction()
171171

172-
# This function checks the host cpusubtype to see if it is post-haswell. Haswell
173-
# and later machines can run x86_64h binaries. Haswell is cpusubtype 8.
172+
# This function checks the host cputype/cpusubtype to filter supported
173+
# architecture for the host OS. This is used to determine which tests are
174+
# available for the host.
174175
function(darwin_filter_host_archs input output)
175176
list_intersect(tmp_var DARWIN_osx_ARCHS ${input})
176177
execute_process(
177-
COMMAND sysctl hw.cpusubtype
178-
OUTPUT_VARIABLE SUBTYPE)
179-
180-
string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
181-
SUBTYPE_MATCHED "${SUBTYPE}")
182-
set(HASWELL_SUPPORTED Off)
183-
if(SUBTYPE_MATCHED)
184-
if(${CMAKE_MATCH_1} GREATER 7)
185-
set(HASWELL_SUPPORTED On)
178+
COMMAND sysctl hw.cputype
179+
OUTPUT_VARIABLE CPUTYPE)
180+
string(REGEX MATCH "hw.cputype: ([0-9]*)"
181+
CPUTYPE_MATCHED "${CPUTYPE}")
182+
set(ARM_HOST Off)
183+
if(CPUTYPE_MATCHED)
184+
# ARM cputype is (0x01000000 | 12) and X86(_64) is always 7.
185+
if(${CMAKE_MATCH_1} GREATER 11)
186+
set(ARM_HOST On)
186187
endif()
187188
endif()
188-
if(NOT HASWELL_SUPPORTED)
189-
list(REMOVE_ITEM tmp_var x86_64h)
189+
190+
if(ARM_HOST)
191+
list(REMOVE_ITEM tmp_var i386)
192+
else()
193+
list(REMOVE_ITEM tmp_var arm64)
194+
list(REMOVE_ITEM tmp_var arm64e)
195+
execute_process(
196+
COMMAND sysctl hw.cpusubtype
197+
OUTPUT_VARIABLE SUBTYPE)
198+
string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
199+
SUBTYPE_MATCHED "${SUBTYPE}")
200+
201+
set(HASWELL_SUPPORTED Off)
202+
if(SUBTYPE_MATCHED)
203+
if(${CMAKE_MATCH_1} GREATER 7)
204+
set(HASWELL_SUPPORTED On)
205+
endif()
206+
endif()
207+
if(NOT HASWELL_SUPPORTED)
208+
list(REMOVE_ITEM tmp_var x86_64h)
209+
endif()
190210
endif()
211+
191212
set(${output} ${tmp_var} PARENT_SCOPE)
192213
endfunction()
193214

compiler-rt/cmake/builtin-config-ix.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,34 @@ if(APPLE)
6464
find_darwin_sdk_dir(DARWIN_tvossim_SYSROOT appletvsimulator)
6565
find_darwin_sdk_dir(DARWIN_tvos_SYSROOT appletvos)
6666

67+
# Get supported architecture from SDKSettings.
68+
function(sdk_has_arch_support sdk_path os arch has_support)
69+
execute_process(COMMAND
70+
/usr/libexec/PlistBuddy -c "Print :SupportedTargets:${os}:Archs" ${sdk_path}/SDKSettings.plist
71+
OUTPUT_VARIABLE SDK_SUPPORTED_ARCHS
72+
RESULT_VARIABLE PLIST_ERROR)
73+
if (PLIST_ERROR EQUAL 0 AND
74+
SDK_SUPPORTED_ARCHS MATCHES " ${arch}\n")
75+
message(STATUS "Found ${arch} support in ${sdk_path}/SDKSettings.plist")
76+
set("${has_support}" On PARENT_SCOPE)
77+
else()
78+
message(STATUS "No ${arch} support in ${sdk_path}/SDKSettings.plist")
79+
set("${has_support}" Off PARENT_SCOPE)
80+
endif()
81+
endfunction()
82+
6783
set(DARWIN_EMBEDDED_PLATFORMS)
6884
set(DARWIN_osx_BUILTIN_MIN_VER 10.5)
6985
set(DARWIN_osx_BUILTIN_MIN_VER_FLAG
7086
-mmacosx-version-min=${DARWIN_osx_BUILTIN_MIN_VER})
7187
set(DARWIN_osx_BUILTIN_ALL_POSSIBLE_ARCHS ${X86} ${X86_64})
88+
# Add support for arm64 macOS if available in SDK.
89+
foreach(arch ${ARM64})
90+
sdk_has_arch_support(${DARWIN_osx_SYSROOT} macosx ${arch} MACOS_ARM_SUPPORT)
91+
if (MACOS_ARM_SUPPORT)
92+
list(APPEND DARWIN_osx_BUILTIN_ALL_POSSIBLE_ARCHS ${arch})
93+
endif()
94+
endforeach(arch)
7295

7396
if(COMPILER_RT_ENABLE_IOS)
7497
list(APPEND DARWIN_EMBEDDED_PLATFORMS ios)

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
#define SANITIZER_INTERCEPT_STATFS \
342342
(SI_FREEBSD || SI_MAC || SI_LINUX_NOT_ANDROID || SI_SOLARIS)
343343
#define SANITIZER_INTERCEPT_STATFS64 \
344-
((SI_MAC && !SI_IOS) || SI_LINUX_NOT_ANDROID)
344+
(((SI_MAC && !TARGET_CPU_ARM64) && !SI_IOS) || SI_LINUX_NOT_ANDROID)
345345
#define SANITIZER_INTERCEPT_STATVFS \
346346
(SI_FREEBSD || SI_NETBSD || SI_OPENBSD || SI_LINUX_NOT_ANDROID)
347347
#define SANITIZER_INTERCEPT_STATVFS64 SI_LINUX_NOT_ANDROID

compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ typedef struct user_fpregs elf_fpregset_t;
170170
namespace __sanitizer {
171171
unsigned struct_utsname_sz = sizeof(struct utsname);
172172
unsigned struct_stat_sz = sizeof(struct stat);
173-
#if !SANITIZER_IOS
173+
#if !SANITIZER_IOS && !(SANITIZER_MAC && TARGET_CPU_ARM64)
174174
unsigned struct_stat64_sz = sizeof(struct stat64);
175175
#endif // !SANITIZER_IOS
176176
unsigned struct_rusage_sz = sizeof(struct rusage);
@@ -197,7 +197,7 @@ namespace __sanitizer {
197197
unsigned struct_regex_sz = sizeof(regex_t);
198198
unsigned struct_regmatch_sz = sizeof(regmatch_t);
199199

200-
#if SANITIZER_MAC && !SANITIZER_IOS
200+
#if (SANITIZER_MAC && !TARGET_CPU_ARM64) && !SANITIZER_IOS
201201
unsigned struct_statfs64_sz = sizeof(struct statfs64);
202202
#endif // SANITIZER_MAC && !SANITIZER_IOS
203203

0 commit comments

Comments
 (0)