Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ class FrontendOptions {
/// 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 PrintTargetInfo = 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;
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -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_info : Flag<["-"], "print-target-info">,
Flags<[FrontendOption]>,
HelpText<"Print target information 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">;
Expand Down
25 changes: 25 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,31 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
std::end(commandArgs));
}

if (Args.hasArg(options::OPT_print_target_info)) {
SmallVector<const char *, 5> commandLine;
commandLine.push_back("-frontend");
commandLine.push_back("-print-target-info");
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;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ bool ArgsToFrontendOptionsConverter::convert(
Opts.IgnoreSwiftSourceInfo |= Args.hasArg(OPT_ignore_module_source_info);
computeHelpOptions();

if (Args.hasArg(OPT_print_target_info)) {
Opts.PrintTargetInfo = true;
}

if (const Arg *A = Args.getLastArg(OPT_verify_generic_signatures)) {
Opts.VerifyGenericSignaturesInModule = A->getValue();
}
Expand Down
38 changes: 38 additions & 0 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "swift/Basic/JSONSerialization.h"
#include "swift/Basic/LLVMContext.h"
#include "swift/Basic/LLVMInitialize.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
Expand Down Expand Up @@ -1863,6 +1864,38 @@ createJSONFixItDiagnosticConsumerIfNeeded(
});
}

/// Print information about the selected target in JSON.
static void printTargetInfo(CompilerInvocation &invocation,
llvm::raw_ostream &out) {
out << "{\n";

// Target information.
auto &langOpts = invocation.getLangOptions();
out << " \"target\": {\n";

out << " \"triple\": \"";
out.write_escaped(langOpts.Target.getTriple());
out << "\",\n";

out << " \"moduleTriple\": \"";
out.write_escaped(getTargetSpecificModuleTriple(langOpts.Target).getTriple());
out << "\",\n";

if (auto runtimeVersion = getSwiftRuntimeCompatibilityVersionForTarget(
langOpts.Target)) {
out << " \"swiftRuntimeCompatibilityVersion\": \"";
out.write_escaped(runtimeVersion->getAsString());
out << "\",\n";
}

out << " \"librariesRequireRPath\": "
<< (tripleRequiresRPathForSwiftInOS(langOpts.Target) ? "true" : "false")
<< "\n";

out << " }\n";

out << "}\n";
}

int swift::performFrontend(ArrayRef<const char *> Args,
const char *Argv0, void *MainAddr,
Expand Down Expand Up @@ -1987,6 +2020,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
return finishDiagProcessing(0);
}

if (Invocation.getFrontendOptions().PrintTargetInfo) {
printTargetInfo(Invocation, llvm::outs());
return finishDiagProcessing(0);
}

if (Invocation.getFrontendOptions().RequestedAction ==
FrontendOptions::ActionType::NoneAction) {
Instance->getDiags().diagnose(SourceLoc(),
Expand Down
18 changes: 18 additions & 0 deletions test/Driver/print_target_info.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %swift_driver -print-target-info -target arm64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS %s
// RUN: %target-swift-frontend -print-target-info -target arm64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS %s

// RUN: %swift_driver -print-target-info -target x86_64-unknown-linux | %FileCheck -check-prefix CHECK-LINUX %s
// RUN: %target-swift-frontend -print-target-info -target x86_64-unknown-linux | %FileCheck -check-prefix CHECK-LINUX %s

// CHECK-IOS: "target": {
// CHECK-IOS: "triple": "arm64-apple-ios12.0",
// CHECK-IOS: "moduleTriple": "arm64-apple-ios",
// CHECK-IOS: "swiftRuntimeCompatibilityVersion": "5.0",
// CHECK-IOS: "librariesRequireRPath": true
// CHECK-IOS: }

// CHECK-LINUX: "target": {
// CHECK-LINUX: "triple": "x86_64-unknown-linux",
// CHECK-LINUX: "moduleTriple": "x86_64-unknown-linux",
// CHECK-LINUX: "librariesRequireRPath": false
// CHECK-LINUX: }
10 changes: 10 additions & 0 deletions test/Driver/print_target_info_macos.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Test that -print-target-info infers the host OS when that host OS is macOS
//
// We could duplicate this test for other host platforms.

// RUN: %swift_driver -print-target-info | %FileCheck %s
// RUN: %target-swift-frontend -print-target-info | %FileCheck %s

// REQUIRES: OS=macosx

// CHECK: "triple": "x86_64-apple-macosx