[Driver] Switch -print-target-triple to -print-target-info and add more info.

Rather than only emitting the target triple, provide additional
information about that particular target, including the module triple
(i.e., what file names will be used for Swift modules for that
triple), the runtime compatibility version if there is one, and
whether linking with rpaths is required for the standard library and
other libraries shipped with Swift. Encode this as JSON so we can
extend it in the future. For now, it looks like this:

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

Which you can deserialize into a TargetInfo instance as defined below:

```
struct Target: Codable {
  /// The target triple.
  var triple: String
  /// The triple used for module file names.
  var moduleTriple: String
  /// If this platform provides the Swift runtime, the Swift language
  version
  /// with which that runtime is compatible.
  var swiftRuntimeCompatibilityVersion: String?
  /// Whether linking against the Swift libraries requires the use of
  rpaths.
  var librariesRequireRPath: Bool
}

struct TargetInfo: Codable {
  var target: Target
}
```

Implements rdar://problem/47095159.
This commit is contained in:
Doug Gregor
2019-12-06 22:17:29 -08:00
parent cfe25eabb0
commit 61c83d2415
9 changed files with 70 additions and 27 deletions

View File

@@ -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"
@@ -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,
@@ -1987,8 +2020,8 @@ int swift::performFrontend(ArrayRef<const char *> Args,
return finishDiagProcessing(0);
}
if (Invocation.getFrontendOptions().PrintTargetTriple) {
llvm::outs() << Invocation.getLangOptions().Target.getTriple() << "\n";
if (Invocation.getFrontendOptions().PrintTargetInfo) {
printTargetInfo(Invocation, llvm::outs());
return finishDiagProcessing(0);
}