Skip to content

Commit cee75bd

Browse files
authored
Merge pull request #32758 from DougGregor/enhanced-print-target-info
[Frontend] Improve -print-target-info
2 parents 063568e + 59b8c6e commit cee75bd

File tree

5 files changed

+154
-55
lines changed

5 files changed

+154
-55
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===------ BackDeploymentLibs.def - Backward Deployment Libraries --------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 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+
// Enumerates the backward deployment libraries that need to be linked
13+
// into Swift targets. Clients of this file must define the macro
14+
//
15+
// BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName)
16+
//
17+
// where:
18+
// Version is a maximum Swift version written like a tuple, e.g., (5, 1)
19+
// Filter is one of executable or all.
20+
// LibraryName is the name of the library, e.g., "swiftCompatibility51"
21+
//===----------------------------------------------------------------------===//
22+
23+
#ifndef BACK_DEPLOYMENT_LIB
24+
# error "Must define BACK_DEPLOYMENT_LIB(Version, Filter, Library)"
25+
#endif
26+
27+
BACK_DEPLOYMENT_LIB((5, 0), all, "swiftCompatibility50")
28+
BACK_DEPLOYMENT_LIB((5, 1), all, "swiftCompatibility51")
29+
BACK_DEPLOYMENT_LIB((5, 0), executable, "swiftCompatibilityDynamicReplacements")
30+
31+
#undef BACK_DEPLOYMENT_LIB

lib/Driver/DarwinToolChains.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ toolchains::Darwin::addSanitizerArgs(ArgStringList &Arguments,
330330
/*shared=*/false);
331331
}
332332

333+
namespace {
334+
335+
enum class BackDeployLibFilter {
336+
executable,
337+
all
338+
};
339+
340+
// Whether the given job matches the backward-deployment library filter.
341+
bool jobMatchesFilter(LinkKind jobKind, BackDeployLibFilter filter) {
342+
switch (filter) {
343+
case BackDeployLibFilter::executable:
344+
return jobKind == LinkKind::Executable;
345+
346+
case BackDeployLibFilter::all:
347+
return true;
348+
}
349+
}
350+
351+
}
352+
333353
void
334354
toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
335355
const DynamicLinkJobAction &job,
@@ -359,47 +379,31 @@ toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
359379
}
360380

361381
if (runtimeCompatibilityVersion) {
362-
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 0)) {
363-
// Swift 5.0 compatibility library
364-
SmallString<128> BackDeployLib;
365-
BackDeployLib.append(SharedResourceDirPath);
366-
llvm::sys::path::append(BackDeployLib, "libswiftCompatibility50.a");
367-
368-
if (llvm::sys::fs::exists(BackDeployLib)) {
369-
Arguments.push_back("-force_load");
370-
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
371-
}
372-
}
382+
auto addBackDeployLib = [&](llvm::VersionTuple version,
383+
BackDeployLibFilter filter,
384+
StringRef libraryName) {
385+
if (*runtimeCompatibilityVersion > version)
386+
return;
373387

374-
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 1)) {
375-
// Swift 5.1 compatibility library
388+
if (!jobMatchesFilter(job.getKind(), filter))
389+
return;
390+
376391
SmallString<128> BackDeployLib;
377392
BackDeployLib.append(SharedResourceDirPath);
378-
llvm::sys::path::append(BackDeployLib, "libswiftCompatibility51.a");
393+
llvm::sys::path::append(BackDeployLib, "lib" + libraryName + ".a");
379394

380395
if (llvm::sys::fs::exists(BackDeployLib)) {
381396
Arguments.push_back("-force_load");
382397
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
383398
}
384-
}
399+
};
400+
401+
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \
402+
addBackDeployLib( \
403+
llvm::VersionTuple Version, BackDeployLibFilter::Filter, LibraryName);
404+
#include "swift/Frontend/BackDeploymentLibs.def"
385405
}
386406

387-
if (job.getKind() == LinkKind::Executable) {
388-
if (runtimeCompatibilityVersion)
389-
if (*runtimeCompatibilityVersion <= llvm::VersionTuple(5, 0)) {
390-
// Swift 5.0 dynamic replacement compatibility library.
391-
SmallString<128> BackDeployLib;
392-
BackDeployLib.append(SharedResourceDirPath);
393-
llvm::sys::path::append(BackDeployLib,
394-
"libswiftCompatibilityDynamicReplacements.a");
395-
396-
if (llvm::sys::fs::exists(BackDeployLib)) {
397-
Arguments.push_back("-force_load");
398-
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
399-
}
400-
}
401-
}
402-
403407
// Add the runtime library link path, which is platform-specific and found
404408
// relative to the compiler.
405409
SmallVector<std::string, 4> RuntimeLibPaths;

lib/FrontendTool/FrontendTool.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,8 +1948,36 @@ createJSONFixItDiagnosticConsumerIfNeeded(
19481948
});
19491949
}
19501950

1951+
/// Print information about a
1952+
static void printCompatibilityLibrary(
1953+
llvm::VersionTuple runtimeVersion, llvm::VersionTuple maxVersion,
1954+
StringRef filter, StringRef libraryName, bool &printedAny,
1955+
llvm::raw_ostream &out) {
1956+
if (runtimeVersion > maxVersion)
1957+
return;
1958+
1959+
if (printedAny) {
1960+
out << ",";
1961+
}
1962+
1963+
out << "\n";
1964+
out << " {\n";
1965+
1966+
out << " \"libraryName\": \"";
1967+
out.write_escaped(libraryName);
1968+
out << "\",\n";
1969+
1970+
out << " \"filter\": \"";
1971+
out.write_escaped(filter);
1972+
out << "\"\n";
1973+
out << " }";
1974+
1975+
printedAny = true;
1976+
}
1977+
19511978
/// Print information about the target triple in JSON.
19521979
static void printTripleInfo(const llvm::Triple &triple,
1980+
llvm::Optional<llvm::VersionTuple> runtimeVersion,
19531981
llvm::raw_ostream &out) {
19541982
out << "{\n";
19551983

@@ -1965,11 +1993,26 @@ static void printTripleInfo(const llvm::Triple &triple,
19651993
out.write_escaped(getTargetSpecificModuleTriple(triple).getTriple());
19661994
out << "\",\n";
19671995

1968-
if (auto runtimeVersion = getSwiftRuntimeCompatibilityVersionForTarget(
1969-
triple)) {
1996+
if (runtimeVersion) {
19701997
out << " \"swiftRuntimeCompatibilityVersion\": \"";
19711998
out.write_escaped(runtimeVersion->getAsString());
19721999
out << "\",\n";
2000+
2001+
// Compatibility libraries that need to be linked.
2002+
out << " \"compatibilityLibraries\": [";
2003+
bool printedAnyCompatibilityLibrary = false;
2004+
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \
2005+
printCompatibilityLibrary( \
2006+
*runtimeVersion, llvm::VersionTuple Version, #Filter, LibraryName, \
2007+
printedAnyCompatibilityLibrary, out);
2008+
#include "swift/Frontend/BackDeploymentLibs.def"
2009+
2010+
if (printedAnyCompatibilityLibrary) {
2011+
out << "\n ";
2012+
}
2013+
out << " ],\n";
2014+
} else {
2015+
out << " \"compatibilityLibraries\": [ ],\n";
19732016
}
19742017

19752018
out << " \"librariesRequireRPath\": "
@@ -1985,15 +2028,23 @@ static void printTargetInfo(const CompilerInvocation &invocation,
19852028
llvm::raw_ostream &out) {
19862029
out << "{\n";
19872030

2031+
// Compiler version, as produced by --version.
2032+
out << " \"compilerVersion\": \"";
2033+
out.write_escaped(version::getSwiftFullVersion(
2034+
version::Version::getCurrentLanguageVersion()));
2035+
out << "\",\n";
2036+
19882037
// Target triple and target variant triple.
2038+
auto runtimeVersion =
2039+
invocation.getIRGenOptions().AutolinkRuntimeCompatibilityLibraryVersion;
19892040
auto &langOpts = invocation.getLangOptions();
19902041
out << " \"target\": ";
1991-
printTripleInfo(langOpts.Target, out);
2042+
printTripleInfo(langOpts.Target, runtimeVersion, out);
19922043
out << ",\n";
19932044

19942045
if (auto &variant = langOpts.TargetVariant) {
19952046
out << " \"targetVariant\": ";
1996-
printTripleInfo(*variant, out);
2047+
printTripleInfo(*variant, runtimeVersion, out);
19972048
out << ",\n";
19982049
}
19992050

lib/IRGen/GenDecl.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -471,28 +471,31 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
471471
// situations where it isn't useful, such as for dylibs, though this is
472472
// harmless aside from code size.
473473
if (!IRGen.Opts.UseJIT) {
474-
if (auto compatibilityVersion
475-
= IRGen.Opts.AutolinkRuntimeCompatibilityLibraryVersion) {
476-
if (*compatibilityVersion <= llvm::VersionTuple(5, 0)) {
477-
this->addLinkLibrary(LinkLibrary("swiftCompatibility50",
478-
LibraryKind::Library,
479-
/*forceLoad*/ true));
480-
}
481-
if (*compatibilityVersion <= llvm::VersionTuple(5, 1)) {
482-
this->addLinkLibrary(LinkLibrary("swiftCompatibility51",
483-
LibraryKind::Library,
484-
/*forceLoad*/ true));
474+
auto addBackDeployLib = [&](llvm::VersionTuple version,
475+
StringRef libraryName) {
476+
Optional<llvm::VersionTuple> compatibilityVersion;
477+
if (libraryName == "swiftCompatibilityDynamicReplacements") {
478+
compatibilityVersion = IRGen.Opts.
479+
AutolinkRuntimeCompatibilityDynamicReplacementLibraryVersion;
480+
} else {
481+
compatibilityVersion = IRGen.Opts.
482+
AutolinkRuntimeCompatibilityLibraryVersion;
485483
}
486-
}
487484

488-
if (auto compatibilityVersion =
489-
IRGen.Opts.AutolinkRuntimeCompatibilityDynamicReplacementLibraryVersion) {
490-
if (*compatibilityVersion <= llvm::VersionTuple(5, 0)) {
491-
this->addLinkLibrary(LinkLibrary("swiftCompatibilityDynamicReplacements",
492-
LibraryKind::Library,
493-
/*forceLoad*/ true));
494-
}
495-
}
485+
if (!compatibilityVersion)
486+
return;
487+
488+
if (*compatibilityVersion > version)
489+
return;
490+
491+
this->addLinkLibrary(LinkLibrary(libraryName,
492+
LibraryKind::Library,
493+
/*forceLoad*/ true));
494+
};
495+
496+
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \
497+
addBackDeployLib(llvm::VersionTuple Version, LibraryName);
498+
#include "swift/Frontend/BackDeploymentLibs.def"
496499
}
497500
}
498501

test/Driver/print_target_info.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@
99

1010
// RUN: %swift_driver -print-target-info -target x86_64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS-SIM %s
1111

12+
// CHECK-IOS: "compilerVersion": "{{.*}}Swift version
13+
1214
// CHECK-IOS: "target": {
1315
// CHECK-IOS: "triple": "arm64-apple-ios12.0",
1416
// CHECK-IOS: "unversionedTriple": "arm64-apple-ios",
1517
// CHECK-IOS: "moduleTriple": "arm64-apple-ios",
1618
// CHECK-IOS: "swiftRuntimeCompatibilityVersion": "5.0",
19+
// CHECK-IOS: "compatibilityLibraries": [
20+
// CHECK-IOS: "libraryName": "swiftCompatibility50",
21+
// CHECK-IOS: "libraryName": "swiftCompatibility51",
22+
// CHECK-IOS: "libraryName": "swiftCompatibilityDynamicReplacements"
23+
// CHECK-IOS: "filter": "executable"
24+
// CHECK-IOS: ],
1725
// CHECK-IOS: "librariesRequireRPath": true
1826
// CHECK-IOS: }
1927

@@ -28,6 +36,8 @@
2836
// CHECK-IOS: }
2937

3038

39+
// CHECK-LINUX: "compilerVersion": "{{.*}}Swift version
40+
3141
// CHECK-LINUX: "target": {
3242
// CHECK-LINUX: "triple": "x86_64-unknown-linux",
3343
// CHECK-LINUX: "moduleTriple": "x86_64-unknown-linux",

0 commit comments

Comments
 (0)