[Driver/Frontend] Add -print-target-triple

Add a -print-target-triple command line option to the Swift frontend
and driver to allow other tools (e.g., SwiftPM) to query the host
triple as it is understood by the Swift compiler. This follows the
precedent set by Clang. Implements rdar://problem/57434967.
This commit is contained in:
Doug Gregor
2019-12-05 17:19:53 -08:00
parent 345d1f420e
commit cfe25eabb0
7 changed files with 59 additions and 0 deletions

View File

@@ -247,6 +247,10 @@ public:
/// Indicates whether full help (including "hidden" options) should be shown.
bool PrintHelpHidden = false;
/// Indicates that the frontend should print the target triple and then
/// exit.
bool PrintTargetTriple = false;
/// Should we sort SIL functions, vtables, witness tables, and global
/// variables by name when we print it out. This eases diffing of SIL files.
bool EmitSortedSIL = false;

View File

@@ -884,6 +884,9 @@ def target : Separate<["-"], "target">,
HelpText<"Generate code for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
def target_legacy_spelling : Joined<["--"], "target=">,
Flags<[FrontendOption]>, Alias<target>;
def print_target_triple : Flag<["-"], "print-target-triple">,
Flags<[FrontendOption]>,
HelpText<"Generate code for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
def target_cpu : Separate<["-"], "target-cpu">, Flags<[FrontendOption, ModuleInterfaceOption]>,
HelpText<"Generate code for a particular CPU variant">;

View File

@@ -2122,6 +2122,31 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
std::end(commandArgs));
}
if (Args.hasArg(options::OPT_print_target_triple)) {
SmallVector<const char *, 5> commandLine;
commandLine.push_back("-frontend");
commandLine.push_back("-print-target-triple");
if (const Arg *TargetArg = Args.getLastArg(options::OPT_target)) {
commandLine.push_back("-target");
commandLine.push_back(TargetArg->getValue());
}
std::string executable = getSwiftProgramPath();
sys::TaskQueue queue;
queue.addTask(executable.c_str(), commandLine);
queue.execute(nullptr,
[](sys::ProcessId PID, int returnCode,
StringRef output, StringRef errors,
sys::TaskProcessInformation ProcInfo,
void *unused) -> sys::TaskFinishedResponse {
llvm::outs() << output;
llvm::errs() << errors;
return sys::TaskFinishedResponse::ContinueExecution;
});
return false;
}
return true;
}

View File

@@ -104,6 +104,10 @@ bool ArgsToFrontendOptionsConverter::convert(
Opts.IgnoreSwiftSourceInfo |= Args.hasArg(OPT_ignore_module_source_info);
computeHelpOptions();
if (Args.hasArg(OPT_print_target_triple)) {
Opts.PrintTargetTriple = true;
}
if (const Arg *A = Args.getLastArg(OPT_verify_generic_signatures)) {
Opts.VerifyGenericSignaturesInModule = A->getValue();
}

View File

@@ -1987,6 +1987,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
return finishDiagProcessing(0);
}
if (Invocation.getFrontendOptions().PrintTargetTriple) {
llvm::outs() << Invocation.getLangOptions().Target.getTriple() << "\n";
return finishDiagProcessing(0);
}
if (Invocation.getFrontendOptions().RequestedAction ==
FrontendOptions::ActionType::NoneAction) {
Instance->getDiags().diagnose(SourceLoc(),

View File

@@ -0,0 +1,8 @@
// RUN: %swift_driver -print-target-triple -target arm64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS %s
// RUN: %target-swift-frontend -print-target-triple -target arm64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS %s
// RUN: %swift_driver -print-target-triple -target x86_64-unknown-linux | %FileCheck -check-prefix CHECK-LINUX %s
// RUN: %target-swift-frontend -print-target-triple -target x86_64-unknown-linux | %FileCheck -check-prefix CHECK-LINUX %s
// CHECK-IOS: arm64-apple-ios12.0
// CHECK-LINUX: x86_64-unknown-linux

View File

@@ -0,0 +1,10 @@
// Test that -print-target-triple infers the host OS when that host OS is macOS
//
// We could duplicate this test for other host platforms.
// RUN: %swift_driver -print-target-triple | %FileCheck %s
// RUN: %target-swift-frontend -print-target-triple | %FileCheck %s
// REQUIRES: OS=macosx
// CHECK: x86_64-apple-macosx