Skip to content

Commit 53b9eb4

Browse files
committed
Frontend: add -autolink-library= option to support Windows
All modules on Windows need to link against one of {libcmtd.lib, libcmt.lib, msvcrtd.lib, msvcrt.lib}. In addition to being the C library, it is the equivalent of crtbegin0.o on other targets. It is responsible for providing the entry point itself. Traditionally, cl will embed the linkage requirement into all objects based on the flags given -- one of {/MTd, /MT, /MDd, /MD}. clang emulates this via the `--dependent-lib=` option. Emulate that behaviour in the swift driver so that swift objects being compiled for Windows targets can auto-link to the required libraries.
1 parent 5cf6006 commit 53b9eb4

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ def Xlinker : Separate<["-"], "Xlinker">,
272272
Flags<[DoesNotAffectIncrementalBuild]>,
273273
HelpText<"Specifies an option which should be passed to the linker">;
274274

275+
def autolink_library : Joined<["-"], "autolink-library=">,
276+
HelpText<"Add dependent library">, Flags<[FrontendOption]>;
277+
275278
// Optimization levels
276279

277280
def O_Group : OptionGroup<"<optimization level options>">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,9 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
12851285
Opts.EnableReflectionNames = false;
12861286
}
12871287

1288+
for (const auto &Lib : Args.getAllArgValues(options::OPT_autolink_library))
1289+
Opts.LinkLibraries.push_back(LinkLibrary(Lib, LibraryKind::Library));
1290+
12881291
return false;
12891292
}
12901293

test/IRGen/dependent-library.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %swift -target i686-unknown-windows-msvc -emit-ir -parse-as-library -parse-stdlib -module-name dependent -autolink-library=oldnames -autolink-library=msvcrt %s -o - | FileCheck %s
2+
3+
// CHECK: !{i32 6, !"Linker Options", ![[options:[0-9]+]]}
4+
// CHECK: ![[options]] = !{![[oldnames:[0-9]+]], ![[msvcrtd:[0-9]+]]}
5+
// CHECK: ![[oldnames]] = !{!"/DEFAULTLIB:oldnames.lib"}
6+
// CHECK: ![[msvcrtd]] = !{!"/DEFAULTLIB:msvcrt.lib"}
7+

0 commit comments

Comments
 (0)