Index: include/clang/Driver/ToolChain.h =================================================================== --- include/clang/Driver/ToolChain.h +++ include/clang/Driver/ToolChain.h @@ -93,7 +93,7 @@ private: const Driver &D; - const llvm::Triple Triple; + llvm::Triple Triple; const llvm::opt::ArgList &Args; // We need to initialize CachedRTTIArg before CachedRTTIMode const llvm::opt::Arg *const CachedRTTIArg; @@ -136,6 +136,8 @@ ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args); + void setTripleEnvironment(llvm::Triple::EnvironmentType Env); + virtual Tool *buildAssembler() const; virtual Tool *buildLinker() const; virtual Tool *getTool(Action::ActionClass AC) const; Index: lib/CodeGen/CGObjCMac.cpp =================================================================== --- lib/CodeGen/CGObjCMac.cpp +++ lib/CodeGen/CGObjCMac.cpp @@ -4885,10 +4885,7 @@ } // Indicate whether we're compiling this to run on a simulator. - const llvm::Triple &Triple = CGM.getTarget().getTriple(); - if ((Triple.isiOS() || Triple.isWatchOS()) && - (Triple.getArch() == llvm::Triple::x86 || - Triple.getArch() == llvm::Triple::x86_64)) + if (CGM.getTarget().getTriple().isSimulatorEnvironment()) Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated", eImageInfo_ImageIsSimulated); Index: lib/Driver/ToolChain.cpp =================================================================== --- lib/Driver/ToolChain.cpp +++ lib/Driver/ToolChain.cpp @@ -79,6 +79,12 @@ getFilePaths().push_back(CandidateLibPath); } +void ToolChain::setTripleEnvironment(llvm::Triple::EnvironmentType Env) { + Triple.setEnvironment(Env); + if (EffectiveTriple != llvm::Triple()) + EffectiveTriple.setEnvironment(Env); +} + ToolChain::~ToolChain() { } Index: lib/Driver/ToolChains/Darwin.h =================================================================== --- lib/Driver/ToolChains/Darwin.h +++ lib/Driver/ToolChains/Darwin.h @@ -317,7 +317,7 @@ // FIXME: Eliminate these ...Target functions and derive separate tool chains // for these targets and put version in constructor. void setTarget(DarwinPlatformKind Platform, unsigned Major, unsigned Minor, - unsigned Micro) const { + unsigned Micro, bool Simulator) const { // FIXME: For now, allow reinitialization as long as values don't // change. This will go away when we move away from argument translation. if (TargetInitialized && TargetPlatform == Platform && @@ -328,6 +328,8 @@ TargetInitialized = true; TargetPlatform = Platform; TargetVersion = VersionTuple(Major, Minor, Micro); + if (Simulator) + const_cast(this)->setTripleEnvironment(llvm::Triple::Simulator); } bool isTargetIPhoneOS() const { Index: lib/Driver/ToolChains/Darwin.cpp =================================================================== --- lib/Driver/ToolChains/Darwin.cpp +++ lib/Driver/ToolChains/Darwin.cpp @@ -1214,13 +1214,16 @@ if (iOSVersion) ExplicitIOSDeploymentTargetStr = iOSVersion->getAsString(Args); - // Add a macro to differentiate between m(iphone|tv|watch)os-version-min=X.Y and - // -m(iphone|tv|watch)simulator-version-min=X.Y. + bool Simulator; + // Differentiate between m(iphone|tv|watch)os-version-min=X.Y and + // -m(iphone|tv|watch)simulator-version-min=X.Y using the 'simulator' + // environment in the triple. if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ) || Args.hasArg(options::OPT_mtvos_simulator_version_min_EQ) || Args.hasArg(options::OPT_mwatchos_simulator_version_min_EQ)) - Args.append(Args.MakeSeparateArg(nullptr, Opts.getOption(options::OPT_D), - " __APPLE_EMBEDDED_SIMULATOR__=1")); + Simulator = true; + else + Simulator = false; if (OSXVersion && (iOSVersion || TvOSVersion || WatchOSVersion)) { getDriver().Diag(diag::err_drv_argument_not_allowed_with) @@ -1461,7 +1464,7 @@ getTriple().getArch() == llvm::Triple::x86_64)) Platform = WatchOSSimulator; - setTarget(Platform, Major, Minor, Micro); + setTarget(Platform, Major, Minor, Micro, Simulator); if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { StringRef SDK = getSDKName(A->getValue()); Index: lib/Frontend/InitPreprocessor.cpp =================================================================== --- lib/Frontend/InitPreprocessor.cpp +++ lib/Frontend/InitPreprocessor.cpp @@ -987,6 +987,11 @@ Builder.defineMacro("__nullable", "_Nullable"); } + // Add a macro to differentiate between regular iOS/tvOS/watchOS targets and + // the corresponding simulator targets. + if (TI.getTriple().isOSDarwin() && TI.getTriple().isSimulatorEnvironment()) + Builder.defineMacro("__APPLE_EMBEDDED_SIMULATOR__", "1"); + // OpenMP definition // OpenMP 2.2: // In implementations that support a preprocessor, the _OPENMP Index: test/Driver/darwin-version.c =================================================================== --- test/Driver/darwin-version.c +++ test/Driver/darwin-version.c @@ -41,7 +41,7 @@ // RUN: %clang -target x86_64-apple-darwin -mios-simulator-version-min=11.0 -c -### %s 2>&1 | \ // RUN: FileCheck --check-prefix=CHECK-VERSION-IOS10 %s -// CHECK-VERSION-IOS10: x86_64-apple-ios11.0.0 +// CHECK-VERSION-IOS10: x86_64-apple-ios11.0.0-simulator // RUN: %clang -target arm64-apple-ios11.1 -c -### %s 2>&1 | \ // RUN: FileCheck --check-prefix=CHECK-VERSION-IOS11 %s @@ -85,13 +85,13 @@ // CHECK-VERSION-TVOS83: "thumbv7-apple-tvos8.3.0" // RUN: %clang -target i386-apple-darwin -mtvos-simulator-version-min=8.3 -c %s -### 2>&1 | \ // RUN: FileCheck --check-prefix=CHECK-VERSION-TVSIM83 %s -// CHECK-VERSION-TVSIM83: "i386-apple-tvos8.3.0" +// CHECK-VERSION-TVSIM83: "i386-apple-tvos8.3.0-simulator" // RUN: %clang -target armv7k-apple-darwin -mwatchos-version-min=2.0 -c %s -### 2>&1 | \ // RUN: FileCheck --check-prefix=CHECK-VERSION-WATCHOS20 %s // CHECK-VERSION-WATCHOS20: "thumbv7k-apple-watchos2.0.0" // RUN: %clang -target i386-apple-darwin -mwatchos-simulator-version-min=2.0 -c %s -### 2>&1 | \ // RUN: FileCheck --check-prefix=CHECK-VERSION-WATCHSIM20 %s -// CHECK-VERSION-WATCHSIM20: "i386-apple-watchos2.0.0" +// CHECK-VERSION-WATCHSIM20: "i386-apple-watchos2.0.0-simulator" // Check environment variable gets interpreted correctly // RUN: env MACOSX_DEPLOYMENT_TARGET=10.5 IPHONEOS_DEPLOYMENT_TARGET=2.0 \