|
| 1 | +//===--- TargetInfo.cpp - Target information printing --------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/Basic/TargetInfo.h" |
| 14 | +#include "swift/Basic/Version.h" |
| 15 | +#include "swift/Basic/Platform.h" |
| 16 | +#include "swift/Frontend/Frontend.h" |
| 17 | + |
| 18 | +#include "llvm/Support/raw_ostream.h" |
| 19 | + |
| 20 | +using namespace swift; |
| 21 | + |
| 22 | +/// Print information about a |
| 23 | +static void printCompatibilityLibrary( |
| 24 | + llvm::VersionTuple runtimeVersion, llvm::VersionTuple maxVersion, |
| 25 | + StringRef filter, StringRef libraryName, bool &printedAny, |
| 26 | + llvm::raw_ostream &out) { |
| 27 | + if (runtimeVersion > maxVersion) |
| 28 | + return; |
| 29 | + |
| 30 | + if (printedAny) { |
| 31 | + out << ","; |
| 32 | + } |
| 33 | + |
| 34 | + out << "\n"; |
| 35 | + out << " {\n"; |
| 36 | + |
| 37 | + out << " \"libraryName\": \""; |
| 38 | + out.write_escaped(libraryName); |
| 39 | + out << "\",\n"; |
| 40 | + |
| 41 | + out << " \"filter\": \""; |
| 42 | + out.write_escaped(filter); |
| 43 | + out << "\"\n"; |
| 44 | + out << " }"; |
| 45 | + |
| 46 | + printedAny = true; |
| 47 | +} |
| 48 | + |
| 49 | +/// Print information about the selected target in JSON. |
| 50 | +void targetinfo::printTargetInfo(const CompilerInvocation &invocation, |
| 51 | + llvm::raw_ostream &out) { |
| 52 | + out << "{\n"; |
| 53 | + |
| 54 | + // Compiler version, as produced by --version. |
| 55 | + out << " \"compilerVersion\": \""; |
| 56 | + out.write_escaped(version::getSwiftFullVersion( |
| 57 | + version::Version::getCurrentLanguageVersion())); |
| 58 | + out << "\",\n"; |
| 59 | + |
| 60 | + // Target triple and target variant triple. |
| 61 | + auto runtimeVersion = |
| 62 | + invocation.getIRGenOptions().AutolinkRuntimeCompatibilityLibraryVersion; |
| 63 | + auto &langOpts = invocation.getLangOptions(); |
| 64 | + out << " \"target\": "; |
| 65 | + printTripleInfo(langOpts.Target, runtimeVersion, out); |
| 66 | + out << ",\n"; |
| 67 | + |
| 68 | + if (auto &variant = langOpts.TargetVariant) { |
| 69 | + out << " \"targetVariant\": "; |
| 70 | + printTripleInfo(*variant, runtimeVersion, out); |
| 71 | + out << ",\n"; |
| 72 | + } |
| 73 | + |
| 74 | + // Various paths. |
| 75 | + auto &searchOpts = invocation.getSearchPathOptions(); |
| 76 | + out << " \"paths\": {\n"; |
| 77 | + |
| 78 | + if (!searchOpts.SDKPath.empty()) { |
| 79 | + out << " \"sdkPath\": \""; |
| 80 | + out.write_escaped(searchOpts.SDKPath); |
| 81 | + out << "\",\n"; |
| 82 | + } |
| 83 | + |
| 84 | + auto outputPaths = [&](StringRef name, const std::vector<std::string> &paths){ |
| 85 | + out << " \"" << name << "\": [\n"; |
| 86 | + llvm::interleave(paths, [&out](const std::string &path) { |
| 87 | + out << " \""; |
| 88 | + out.write_escaped(path); |
| 89 | + out << "\""; |
| 90 | + }, [&out] { |
| 91 | + out << ",\n"; |
| 92 | + }); |
| 93 | + out << "\n ],\n"; |
| 94 | + }; |
| 95 | + |
| 96 | + outputPaths("runtimeLibraryPaths", searchOpts.RuntimeLibraryPaths); |
| 97 | + outputPaths("runtimeLibraryImportPaths", |
| 98 | + searchOpts.RuntimeLibraryImportPaths); |
| 99 | + |
| 100 | + out << " \"runtimeResourcePath\": \""; |
| 101 | + out.write_escaped(searchOpts.RuntimeResourcePath); |
| 102 | + out << "\"\n"; |
| 103 | + |
| 104 | + out << " }\n"; |
| 105 | + |
| 106 | + out << "}\n"; |
| 107 | +} |
| 108 | + |
| 109 | +// Print information about the target triple in JSON. |
| 110 | +void targetinfo::printTripleInfo(const llvm::Triple &triple, |
| 111 | + llvm::Optional<llvm::VersionTuple> runtimeVersion, |
| 112 | + llvm::raw_ostream &out) { |
| 113 | + out << "{\n"; |
| 114 | + |
| 115 | + out << " \"triple\": \""; |
| 116 | + out.write_escaped(triple.getTriple()); |
| 117 | + out << "\",\n"; |
| 118 | + |
| 119 | + out << " \"unversionedTriple\": \""; |
| 120 | + out.write_escaped(getUnversionedTriple(triple).getTriple()); |
| 121 | + out << "\",\n"; |
| 122 | + |
| 123 | + out << " \"moduleTriple\": \""; |
| 124 | + out.write_escaped(getTargetSpecificModuleTriple(triple).getTriple()); |
| 125 | + out << "\",\n"; |
| 126 | + |
| 127 | + if (runtimeVersion) { |
| 128 | + out << " \"swiftRuntimeCompatibilityVersion\": \""; |
| 129 | + out.write_escaped(runtimeVersion->getAsString()); |
| 130 | + out << "\",\n"; |
| 131 | + |
| 132 | + // Compatibility libraries that need to be linked. |
| 133 | + out << " \"compatibilityLibraries\": ["; |
| 134 | + bool printedAnyCompatibilityLibrary = false; |
| 135 | + #define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \ |
| 136 | + printCompatibilityLibrary( \ |
| 137 | + *runtimeVersion, llvm::VersionTuple Version, #Filter, LibraryName, \ |
| 138 | + printedAnyCompatibilityLibrary, out); |
| 139 | + #include "swift/Frontend/BackDeploymentLibs.def" |
| 140 | + |
| 141 | + if (printedAnyCompatibilityLibrary) { |
| 142 | + out << "\n "; |
| 143 | + } |
| 144 | + out << " ],\n"; |
| 145 | + } else { |
| 146 | + out << " \"compatibilityLibraries\": [ ],\n"; |
| 147 | + } |
| 148 | + |
| 149 | + out << " \"librariesRequireRPath\": " |
| 150 | + << (tripleRequiresRPathForSwiftLibrariesInOS(triple) ? "true" : "false") |
| 151 | + << "\n"; |
| 152 | + |
| 153 | + out << " }"; |
| 154 | +} |
0 commit comments