Skip to content

Commit f818a1e

Browse files
committed
[Driver/IRGen] Put backward-deployment libraries into a table.
Describe the backward-deployment libraries via a preprocessor-driven table. Macro-metaprogramming the two places in the code base---the driver and IRGen---to use this tabble to determine which backward-compatibility libraries to link against.
1 parent 7bf676d commit f818a1e

File tree

3 files changed

+89
-51
lines changed

3 files changed

+89
-51
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/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

0 commit comments

Comments
 (0)