Skip to content

Commit 831facd

Browse files
kateinoigakukunMaxDesiatov
authored andcommitted
[Frontend] Add -public-autolink-library option
Foundation imports CoreFoundation with @_implementationOnly, so CoreFoundation's modulemap won't be read, and the dependent libraries of CoreFoundation will not be automatically linked when using static linking. For example, CoreFoundation depends on libicui18n and it's modulemap has `link "icui18n"` statement. If Foundation imports CoreFoundation with @_implementationOnly as a private dependency, the toolchain doesn't have CoreFoundation's modulemap and Foundation's swiftmodule doesn't import CoreFoundation. So the swiftc can't know that libicui18n is required. This new option will add LINK_LIBRARY entry in swiftmodule to specify dependent libraries (in the example case, Foundation's swiftmodule should have LINK_LIBRARY entry of libicui18n)
1 parent b001b0b commit 831facd

File tree

8 files changed

+30
-4
lines changed

8 files changed

+30
-4
lines changed

include/swift/AST/IRGenOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ class IRGenOptions {
204204
/// The libraries and frameworks specified on the command line.
205205
SmallVector<LinkLibrary, 4> LinkLibraries;
206206

207+
/// The public dependent libraries specified on the command line.
208+
std::vector<std::string> PublicLinkLibraries;
209+
207210
/// If non-empty, the (unmangled) name of a dummy symbol to emit that can be
208211
/// used to force-load this module.
209212
std::string ForceLoadSymbolName;

include/swift/Option/FrontendOptions.td

+3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def no_serialize_debugging_options :
160160
def autolink_library : Separate<["-"], "autolink-library">,
161161
HelpText<"Add dependent library">, Flags<[FrontendOption]>;
162162

163+
def public_autolink_library : Separate<["-"], "public-autolink-library">,
164+
HelpText<"Add public dependent library">, Flags<[FrontendOption]>;
165+
163166
def disable_typo_correction : Flag<["-"], "disable-typo-correction">,
164167
HelpText<"Disable typo correction">;
165168

include/swift/Serialization/SerializationOptions.h

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ namespace swift {
131131
uint64_t getSize() const { return Size; }
132132
};
133133
ArrayRef<FileDependency> Dependencies;
134+
ArrayRef<std::string> PublicDependentLibraries;
134135

135136
bool AutolinkForceLoad = false;
136137
bool SerializeAllSIL = false;

lib/Frontend/CompilerInvocation.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,10 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
17591759
for (const auto &Lib : Args.getAllArgValues(options::OPT_autolink_library))
17601760
Opts.LinkLibraries.push_back(LinkLibrary(Lib, LibraryKind::Library));
17611761

1762+
for (const auto &Lib : Args.getAllArgValues(options::OPT_public_autolink_library)) {
1763+
Opts.PublicLinkLibraries.push_back(Lib);
1764+
}
1765+
17621766
if (const Arg *A = Args.getLastArg(OPT_type_info_dump_filter_EQ)) {
17631767
StringRef mode(A->getValue());
17641768
if (mode == "all")

lib/Frontend/Frontend.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
156156
serializationOpts.ModuleLinkName = opts.ModuleLinkName;
157157
serializationOpts.UserModuleVersion = opts.UserModuleVersion;
158158
serializationOpts.ExtraClangOptions = getClangImporterOptions().ExtraArgs;
159-
159+
serializationOpts.PublicDependentLibraries =
160+
getIRGenOptions().PublicLinkLibraries;
161+
160162
if (opts.EmitSymbolGraph) {
161163
if (!opts.SymbolGraphOutputDir.empty()) {
162164
serializationOpts.SymbolGraphOutputDir = opts.SymbolGraphOutputDir;

lib/Serialization/Serialization.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,10 @@ void Serializer::writeInputBlock(const SerializationOptions &options) {
12171217
LinkLibrary.emit(ScratchRecord, serialization::LibraryKind::Library,
12181218
options.AutolinkForceLoad, options.ModuleLinkName);
12191219
}
1220+
for (auto dependentLib : options.PublicDependentLibraries) {
1221+
LinkLibrary.emit(ScratchRecord, serialization::LibraryKind::Library,
1222+
options.AutolinkForceLoad, dependentLib);
1223+
}
12201224
}
12211225

12221226
/// Translate AST default argument kind to the Serialization enum values, which

test/IRGen/autolink_elf.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %swift -disable-legacy-type-info -target x86_64-unknown-linux-gnu -emit-module -parse-stdlib -o %t -module-name Empty -module-link-name swiftEmpty %S/../Inputs/empty.swift
2+
// RUN: %swift -disable-legacy-type-info -target x86_64-unknown-linux-gnu -emit-module -parse-stdlib -o %t -module-name Empty -module-link-name swiftEmpty -public-autolink-library anotherLib %S/../Inputs/empty.swift
33
// RUN: %swift -disable-legacy-type-info -target x86_64-unknown-linux-gnu %s -I %t -parse-stdlib -disable-objc-interop -module-name main -emit-ir -o - | %FileCheck %s
44

55
// REQUIRES: CODEGENERATOR=X86
@@ -9,6 +9,6 @@ import Empty
99
// Check that on ELF targets autolinking information is emitted and marked
1010
// as used.
1111

12-
// CHECK-DAG: @_swift1_autolink_entries = private constant [13 x i8] c"-lswiftEmpty\00", section ".swift1_autolink_entries", align 8
13-
// CHECK-DAG: @llvm.used = appending global [{{.*}} x i8*] [{{.*}}i8* getelementptr inbounds ([13 x i8], [13 x i8]* @_swift1_autolink_entries, i32 0, i32 0){{.*}}], section "llvm.metadata", align 8
12+
// CHECK-DAG: @_swift1_autolink_entries = private constant [26 x i8] c"-lswiftEmpty\00-lanotherLib\00", section ".swift1_autolink_entries", align 8
13+
// CHECK-DAG: @llvm.used = appending global [{{.*}} x i8*] [{{.*}}i8* getelementptr inbounds ([26 x i8], [26 x i8]* @_swift1_autolink_entries, i32 0, i32 0){{.*}}], section "llvm.metadata", align 8
1414

test/Serialization/autolinking.swift

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
// RUN: %target-swift-frontend -runtime-compatibility-version none -emit-ir -parse-stdlib -module-name someModule -module-link-name module %S/../Inputs/empty.swift -autolink-force-load | %FileCheck --check-prefix=FORCE-LOAD %s
2323
// RUN: %target-swift-frontend -runtime-compatibility-version none -emit-ir -parse-stdlib -module-name someModule -module-link-name 0module %S/../Inputs/empty.swift -autolink-force-load | %FileCheck --check-prefix=FORCE-LOAD-HEX %s
2424

25+
// RUN: %target-swift-frontend -emit-module -parse-stdlib -o %t -module-name someModule -module-link-name module %S/../Inputs/empty.swift -public-autolink-library anotherLib
26+
// RUN: %target-swift-frontend -disable-autolinking-runtime-compatibility-dynamic-replacements -runtime-compatibility-version none -emit-ir -lmagic %s -I %t > %t/public-autolink.txt
27+
// RUN: %FileCheck %s < %t/public-autolink.txt
28+
// RUN: %FileCheck -check-prefix=PUBLIC-DEP %s < %t/public-autolink.txt
29+
2530
// Linux uses a different autolinking mechanism, based on
2631
// swift-autolink-extract. This file tests the Darwin mechanism.
2732
// UNSUPPORTED: autolink-extract
@@ -51,3 +56,7 @@ import someModule
5156
// FORCE-LOAD-CLIENT-macho: declare extern_weak {{(dllimport )?}}void @"_swift_FORCE_LOAD_$_module"()
5257
// FORCE-LOAD-CLIENT-COFF: declare extern {{(dllimport )?}}void @"_swift_FORCE_LOAD_$_module"()
5358

59+
// PUBLIC-DEP: !llvm.linker.options = !{
60+
// PUBLIC-DEP-DAG: !{{[0-9]+}} = !{!{{"-lmagic"|"/DEFAULTLIB:magic.lib"}}}
61+
// PUBLIC-DEP-DAG: !{{[0-9]+}} = !{!{{"-lmodule"|"/DEFAULTLIB:module.lib"}}}
62+
// PUBLIC-DEP-DAG: !{{[0-9]+}} = !{!{{"-lanotherLib"|"/DEFAULTLIB:anotherLib.lib"}}}

0 commit comments

Comments
 (0)