Index: include/clang/Basic/DiagnosticDriverKinds.td =================================================================== --- include/clang/Basic/DiagnosticDriverKinds.td +++ include/clang/Basic/DiagnosticDriverKinds.td @@ -195,6 +195,7 @@ "precompiled header '%0' was ignored because '%1' is not first '-include'">; def warn_missing_sysroot : Warning<"no such sysroot directory: '%0'">, InGroup>; +def warn_incompatible_sdk : Warning<"using SDK for '%0' but deploying to '%1'">; def warn_debug_compression_unavailable : Warning<"cannot compress debug sections (zlib not installed)">, InGroup>; def warn_drv_enabling_rtti_with_exceptions : Warning< Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -1087,6 +1087,8 @@ def Wlarger_than_EQ : Joined<["-"], "Wlarger-than=">, Group; def Wlarger_than_ : Joined<["-"], "Wlarger-than-">, Alias; def Wframe_larger_than_EQ : Joined<["-"], "Wframe-larger-than=">, Group, Flags<[DriverOption]>; +def Wincompatible_sdk : Flag<["-"], "Wincompatible-sdk">, Group, + Flags<[DriverOption]>; def : Flag<["-"], "fterminated-vtables">, Alias; def fthreadsafe_statics : Flag<["-"], "fthreadsafe-statics">, Group; Index: lib/Driver/ToolChains.h =================================================================== --- lib/Driver/ToolChains.h +++ lib/Driver/ToolChains.h @@ -496,6 +496,7 @@ return TargetVersion < VersionTuple(V0, V1, V2); } + StringRef getPlatformFamily() const; StringRef getOSLibraryNameSuffix() const; public: Index: lib/Driver/ToolChains.cpp =================================================================== --- lib/Driver/ToolChains.cpp +++ lib/Driver/ToolChains.cpp @@ -329,6 +329,23 @@ } } +StringRef Darwin::getPlatformFamily() const { + switch(TargetPlatform) { + case DarwinPlatformKind::MacOS: + return "MacOSX"; + case DarwinPlatformKind::IPhoneOS: + case DarwinPlatformKind::IPhoneOSSimulator: + return "iPhone"; + case DarwinPlatformKind::TvOS: + case DarwinPlatformKind::TvOSSimulator: + return "AppleTV"; + case DarwinPlatformKind::WatchOS: + case DarwinPlatformKind::WatchOSSimulator: + return "Watch"; + } + llvm_unreachable("Unsupported platform"); +} + StringRef Darwin::getOSLibraryNameSuffix() const { switch(TargetPlatform) { case DarwinPlatformKind::MacOS: @@ -721,6 +738,21 @@ Platform = WatchOSSimulator; setTarget(Platform, Major, Minor, Micro); + + if(Args.hasArg(options::OPT_Wincompatible_sdk)) { + if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) { + StringRef isysroot = A->getValue(); + // Assume SDK has path: SOME_PATH/SDKs/PlatformXX.YY.sdk + size_t BeginSDK = isysroot.rfind("SDKs/"); + size_t EndSDK = isysroot.rfind(".sdk"); + if (BeginSDK != StringRef::npos && EndSDK != StringRef::npos) { + StringRef SDK = isysroot.slice(BeginSDK + 5, EndSDK); + if(!SDK.startswith(getPlatformFamily())) + getDriver().Diag(diag::warn_incompatible_sdk) << SDK + << getPlatformFamily(); + } + } + } } void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args, Index: test/Driver/incompatible_sdk.c =================================================================== --- /dev/null +++ test/Driver/incompatible_sdk.c @@ -0,0 +1,10 @@ +// RUN: %clang -Wincompatible-sdk -isysroot SDKs/MacOSX10.9.sdk -mios-version-min=9.0 -S -o - %s 2>&1 | FileCheck -check-prefix CHECK-OSX-IOS %s +// RUN: %clang -Wincompatible-sdk -isysroot SDKs/iPhoneOS9.2.sdk -mwatchos-version-min=2.0 -S -o - %s 2>&1 | FileCheck -check-prefix CHECK-IOS-WATCHOS %s +// RUN: %clang -Wincompatible-sdk -isysroot SDKs/iPhoneOS9.2.sdk -mtvos-version-min=9.0 -S -o - %s 2>&1 | FileCheck -check-prefix CHECK-IOS-TVOS %s +// RUN: %clang -Wincompatible-sdk -isysroot SDKs/iPhoneSimulator9.2.sdk -mios-version-min=9.0 -S -o - %s 2>&1 | FileCheck -check-prefix CHECK-IOS-IOSSIM %s + +int main() { return 0; } +// CHECK-OSX-IOS: warning: using SDK for 'MacOSX10.9' but deploying to 'iPhone' +// CHECK-IOS-WATCHOS: warning: using SDK for 'iPhoneOS9.2' but deploying to 'Watch' +// CHECK-IOS-TVOS: warning: using SDK for 'iPhoneOS9.2' but deploying to 'AppleTV' +// CHECK-IOS-IOSSIM-NOT: warning: using SDK for '{{.*}}' but deploying to '{{.*}}'