Skip to content

[Frontend] Add -public-autolink-library option #3353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ class IRGenOptions {
/// The libraries and frameworks specified on the command line.
SmallVector<LinkLibrary, 4> LinkLibraries;

/// The public dependent libraries specified on the command line.
std::vector<std::string> PublicLinkLibraries;

/// If non-empty, the (unmangled) name of a dummy symbol to emit that can be
/// used to force-load this module.
std::string ForceLoadSymbolName;
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ def no_serialize_debugging_options :
def autolink_library : Separate<["-"], "autolink-library">,
HelpText<"Add dependent library">, Flags<[FrontendOption]>;

def public_autolink_library : Separate<["-"], "public-autolink-library">,
HelpText<"Add public dependent library">, Flags<[FrontendOption]>;

def disable_typo_correction : Flag<["-"], "disable-typo-correction">,
HelpText<"Disable typo correction">;

Expand Down
1 change: 1 addition & 0 deletions include/swift/Serialization/SerializationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ namespace swift {
uint64_t getSize() const { return Size; }
};
ArrayRef<FileDependency> Dependencies;
ArrayRef<std::string> PublicDependentLibraries;

bool AutolinkForceLoad = false;
bool SerializeAllSIL = false;
Expand Down
4 changes: 4 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,10 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
for (const auto &Lib : Args.getAllArgValues(options::OPT_autolink_library))
Opts.LinkLibraries.push_back(LinkLibrary(Lib, LibraryKind::Library));

for (const auto &Lib : Args.getAllArgValues(options::OPT_public_autolink_library)) {
Opts.PublicLinkLibraries.push_back(Lib);
}

if (const Arg *A = Args.getLastArg(OPT_type_info_dump_filter_EQ)) {
StringRef mode(A->getValue());
if (mode == "all")
Expand Down
4 changes: 3 additions & 1 deletion lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
serializationOpts.ModuleLinkName = opts.ModuleLinkName;
serializationOpts.UserModuleVersion = opts.UserModuleVersion;
serializationOpts.ExtraClangOptions = getClangImporterOptions().ExtraArgs;

serializationOpts.PublicDependentLibraries =
getIRGenOptions().PublicLinkLibraries;

if (opts.EmitSymbolGraph) {
if (!opts.SymbolGraphOutputDir.empty()) {
serializationOpts.SymbolGraphOutputDir = opts.SymbolGraphOutputDir;
Expand Down
4 changes: 4 additions & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,10 @@ void Serializer::writeInputBlock(const SerializationOptions &options) {
LinkLibrary.emit(ScratchRecord, serialization::LibraryKind::Library,
options.AutolinkForceLoad, options.ModuleLinkName);
}
for (auto dependentLib : options.PublicDependentLibraries) {
LinkLibrary.emit(ScratchRecord, serialization::LibraryKind::Library,
options.AutolinkForceLoad, dependentLib);
}
}

/// Translate AST default argument kind to the Serialization enum values, which
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/autolink_elf.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %empty-directory(%t)
// 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
// 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
// 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

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

// CHECK-DAG: @_swift1_autolink_entries = private constant [13 x i8] c"-lswiftEmpty\00", section ".swift1_autolink_entries", align 8
// 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
// CHECK-DAG: @_swift1_autolink_entries = private constant [26 x i8] c"-lswiftEmpty\00-lanotherLib\00", section ".swift1_autolink_entries", align 8
// 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

9 changes: 9 additions & 0 deletions test/Serialization/autolinking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
// 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
// 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

// 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
// RUN: %target-swift-frontend -disable-autolinking-runtime-compatibility-dynamic-replacements -runtime-compatibility-version none -emit-ir -lmagic %s -I %t > %t/public-autolink.txt
// RUN: %FileCheck %s < %t/public-autolink.txt
// RUN: %FileCheck -check-prefix=PUBLIC-DEP %s < %t/public-autolink.txt

// Linux uses a different autolinking mechanism, based on
// swift-autolink-extract. This file tests the Darwin mechanism.
// UNSUPPORTED: autolink-extract
Expand Down Expand Up @@ -55,3 +60,7 @@ import someModule
// FORCE-LOAD-CLIENT-macho: declare extern_weak {{(dllimport )?}}void @"_swift_FORCE_LOAD_$_module"()
// FORCE-LOAD-CLIENT-COFF: declare extern {{(dllimport )?}}void @"_swift_FORCE_LOAD_$_module"()

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