Skip to content

Commit

Permalink
[clang][driver] set macOS as the target OS for -arch arm64 when clang
Browse files Browse the repository at this point in the history
is running on an Apple Silicon mac

This change allows users to use `-arch arm64` to build for mac when
running it on Apple Silicon mac without explicit `-target` option.

Differential Revision: https://reviews.llvm.org/D82428
  • Loading branch information
hyp committed Jun 24, 2020
1 parent 050ed97 commit 565603c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
12 changes: 10 additions & 2 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1672,8 +1672,16 @@ inferDeploymentTargetFromArch(DerivedArgList &Args, const Darwin &Toolchain,
llvm::Triple::OSType OSTy = llvm::Triple::UnknownOS;

StringRef MachOArchName = Toolchain.getMachOArchName(Args);
if (MachOArchName == "armv7" || MachOArchName == "armv7s" ||
MachOArchName == "arm64")
if (MachOArchName == "arm64") {
#if __arm64__
// A clang running on an Apple Silicon mac defaults
// to building for mac when building for arm64 rather than
// defaulting to iOS.
OSTy = llvm::Triple::MacOSX;
#else
OSTy = llvm::Triple::IOS;
#endif
} else if (MachOArchName == "armv7" || MachOArchName == "armv7s")
OSTy = llvm::Triple::IOS;
else if (MachOArchName == "armv7k" || MachOArchName == "arm64_32")
OSTy = llvm::Triple::WatchOS;
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Driver/apple-arm64-arch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: env SDKROOT="/" %clang -arch arm64 -c -### %s 2>&1 | \
// RUN: FileCheck %s
//
// XFAIL: apple-silicon-mac
//
// CHECK: "-triple" "arm64-apple-ios{{[0-9.]+}}"
6 changes: 6 additions & 0 deletions clang/test/Driver/apple-silicon-arch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: env SDKROOT="/" %clang -arch arm64 -c -### %s 2>&1 | \
// RUN: FileCheck %s
//
// REQUIRES: apple-silicon-mac
//
// CHECK: "-triple" "arm64-apple-macosx{{[0-9.]+}}"
4 changes: 4 additions & 0 deletions clang/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ def is_filesystem_case_insensitive():
if not re.match(r'.*-(cygwin)$', config.target_triple):
config.available_features.add('clang-driver')

# Tests that are specific to the Apple Silicon macOS.
if re.match(r'^arm64(e)?-apple-(macos|darwin)', config.target_triple):
config.available_features.add('apple-silicon-mac')

# [PR18856] Depends to remove opened file. On win32, a file could be removed
# only if all handles were closed.
if platform.system() not in ['Windows']:
Expand Down

0 comments on commit 565603c

Please sign in to comment.