mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Add libSwiftScan entry-point to query target info.
This provides the library with functionality to answer `-print-target-info` queries in place of calls to `swift-frontend`.
This commit is contained in:
@@ -49,6 +49,7 @@
|
||||
#include "swift/Basic/Statistic.h"
|
||||
#include "swift/Basic/UUID.h"
|
||||
#include "swift/Basic/Version.h"
|
||||
#include "swift/Basic/TargetInfo.h"
|
||||
#include "swift/Option/Options.h"
|
||||
#include "swift/Frontend/Frontend.h"
|
||||
#include "swift/Frontend/AccumulatingDiagnosticConsumer.h"
|
||||
@@ -1810,141 +1811,6 @@ createJSONFixItDiagnosticConsumerIfNeeded(
|
||||
});
|
||||
}
|
||||
|
||||
/// Print information about a
|
||||
static void printCompatibilityLibrary(
|
||||
llvm::VersionTuple runtimeVersion, llvm::VersionTuple maxVersion,
|
||||
StringRef filter, StringRef libraryName, bool &printedAny,
|
||||
llvm::raw_ostream &out) {
|
||||
if (runtimeVersion > maxVersion)
|
||||
return;
|
||||
|
||||
if (printedAny) {
|
||||
out << ",";
|
||||
}
|
||||
|
||||
out << "\n";
|
||||
out << " {\n";
|
||||
|
||||
out << " \"libraryName\": \"";
|
||||
out.write_escaped(libraryName);
|
||||
out << "\",\n";
|
||||
|
||||
out << " \"filter\": \"";
|
||||
out.write_escaped(filter);
|
||||
out << "\"\n";
|
||||
out << " }";
|
||||
|
||||
printedAny = true;
|
||||
}
|
||||
|
||||
/// Print information about the target triple in JSON.
|
||||
static void printTripleInfo(const llvm::Triple &triple,
|
||||
llvm::Optional<llvm::VersionTuple> runtimeVersion,
|
||||
llvm::raw_ostream &out) {
|
||||
out << "{\n";
|
||||
|
||||
out << " \"triple\": \"";
|
||||
out.write_escaped(triple.getTriple());
|
||||
out << "\",\n";
|
||||
|
||||
out << " \"unversionedTriple\": \"";
|
||||
out.write_escaped(getUnversionedTriple(triple).getTriple());
|
||||
out << "\",\n";
|
||||
|
||||
out << " \"moduleTriple\": \"";
|
||||
out.write_escaped(getTargetSpecificModuleTriple(triple).getTriple());
|
||||
out << "\",\n";
|
||||
|
||||
if (runtimeVersion) {
|
||||
out << " \"swiftRuntimeCompatibilityVersion\": \"";
|
||||
out.write_escaped(runtimeVersion->getAsString());
|
||||
out << "\",\n";
|
||||
|
||||
// Compatibility libraries that need to be linked.
|
||||
out << " \"compatibilityLibraries\": [";
|
||||
bool printedAnyCompatibilityLibrary = false;
|
||||
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \
|
||||
printCompatibilityLibrary( \
|
||||
*runtimeVersion, llvm::VersionTuple Version, #Filter, LibraryName, \
|
||||
printedAnyCompatibilityLibrary, out);
|
||||
#include "swift/Frontend/BackDeploymentLibs.def"
|
||||
|
||||
if (printedAnyCompatibilityLibrary) {
|
||||
out << "\n ";
|
||||
}
|
||||
out << " ],\n";
|
||||
} else {
|
||||
out << " \"compatibilityLibraries\": [ ],\n";
|
||||
}
|
||||
|
||||
out << " \"librariesRequireRPath\": "
|
||||
<< (tripleRequiresRPathForSwiftLibrariesInOS(triple) ? "true" : "false")
|
||||
<< "\n";
|
||||
|
||||
out << " }";
|
||||
|
||||
}
|
||||
|
||||
/// Print information about the selected target in JSON.
|
||||
static void printTargetInfo(const CompilerInvocation &invocation,
|
||||
llvm::raw_ostream &out) {
|
||||
out << "{\n";
|
||||
|
||||
// Compiler version, as produced by --version.
|
||||
out << " \"compilerVersion\": \"";
|
||||
out.write_escaped(version::getSwiftFullVersion(
|
||||
version::Version::getCurrentLanguageVersion()));
|
||||
out << "\",\n";
|
||||
|
||||
// Target triple and target variant triple.
|
||||
auto runtimeVersion =
|
||||
invocation.getIRGenOptions().AutolinkRuntimeCompatibilityLibraryVersion;
|
||||
auto &langOpts = invocation.getLangOptions();
|
||||
out << " \"target\": ";
|
||||
printTripleInfo(langOpts.Target, runtimeVersion, out);
|
||||
out << ",\n";
|
||||
|
||||
if (auto &variant = langOpts.TargetVariant) {
|
||||
out << " \"targetVariant\": ";
|
||||
printTripleInfo(*variant, runtimeVersion, out);
|
||||
out << ",\n";
|
||||
}
|
||||
|
||||
// Various paths.
|
||||
auto &searchOpts = invocation.getSearchPathOptions();
|
||||
out << " \"paths\": {\n";
|
||||
|
||||
if (!searchOpts.SDKPath.empty()) {
|
||||
out << " \"sdkPath\": \"";
|
||||
out.write_escaped(searchOpts.SDKPath);
|
||||
out << "\",\n";
|
||||
}
|
||||
|
||||
auto outputPaths = [&](StringRef name, const std::vector<std::string> &paths){
|
||||
out << " \"" << name << "\": [\n";
|
||||
llvm::interleave(paths, [&out](const std::string &path) {
|
||||
out << " \"";
|
||||
out.write_escaped(path);
|
||||
out << "\"";
|
||||
}, [&out] {
|
||||
out << ",\n";
|
||||
});
|
||||
out << "\n ],\n";
|
||||
};
|
||||
|
||||
outputPaths("runtimeLibraryPaths", searchOpts.RuntimeLibraryPaths);
|
||||
outputPaths("runtimeLibraryImportPaths",
|
||||
searchOpts.RuntimeLibraryImportPaths);
|
||||
|
||||
out << " \"runtimeResourcePath\": \"";
|
||||
out.write_escaped(searchOpts.RuntimeResourcePath);
|
||||
out << "\"\n";
|
||||
|
||||
out << " }\n";
|
||||
|
||||
out << "}\n";
|
||||
}
|
||||
|
||||
/// A PrettyStackTraceEntry to print frontend information useful for debugging.
|
||||
class PrettyStackTraceFrontend : public llvm::PrettyStackTraceEntry {
|
||||
const LangOptions &LangOpts;
|
||||
@@ -2115,7 +1981,7 @@ int swift::performFrontend(ArrayRef<const char *> Args,
|
||||
}
|
||||
|
||||
if (Invocation.getFrontendOptions().PrintTargetInfo) {
|
||||
printTargetInfo(Invocation, llvm::outs());
|
||||
swift::targetinfo::printTargetInfo(Invocation, llvm::outs());
|
||||
return finishDiagProcessing(0, /*verifierEnabled*/ false);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user