Skip to content

Commit 43b2b59

Browse files
committed
ClangImporter: import SwiftShims properly for static linking
When building against the static standard library, we should define `SWIFT_STATIC_STDLIB` to indicate to the shims that the declarations should be giving hidden visibility and default DLL storage. This is required to ensure that these symbols are known to be `dso_local` when compiling to avoid an unnecessary look up through the PLT/GOT or the indirection through the synthesized `__imp_` symbol and the IAT. This corrects a number of incorrect code generation cases on Windows with the static standard library.
1 parent 68524a8 commit 43b2b59

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ namespace swift {
321321
/// Flags for use by tests
322322
///
323323

324+
bool UseStaticStandardLibrary = false;
325+
324326
/// Enable Objective-C Runtime interop code generation and build
325327
/// configuration options.
326328
bool EnableObjCInterop = true;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ void importer::getNormalInvocationArguments(
682682
}
683683
}
684684

685+
if (LangOpts.UseStaticStandardLibrary)
686+
invocationArgStrs.push_back("-DSWIFT_STATIC_STDLIB");
687+
685688
// If we support SendingArgsAndResults, set the -D flag to signal that it
686689
// is supported.
687690
if (LangOpts.hasFeature(Feature::SendingArgsAndResults))

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
15711571
ModuleInterfaceOpts.PublicFlags.IgnorableFlags +=
15721572
" " + printFormalCxxInteropVersion(Opts);
15731573

1574+
Opts.UseStaticStandardLibrary = Args.hasArg(OPT_use_static_resource_dir);
15741575
Opts.EnableObjCInterop =
15751576
Args.hasFlag(OPT_enable_objc_interop, OPT_disable_objc_interop,
15761577
Target.isOSDarwin() && !Opts.hasFeature(Feature::Embedded));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %swift_frontend_plain -parse-stdlib -typecheck %s -dump-clang-diagnostics 2>&1 | %FileCheck %s -check-prefix CHECK-DYNAMIC
2+
// RUN: %swift_frontend_plain -parse-stdlib -use-static-resource-dir -typecheck %s -dump-clang-diagnostics 2>&1 | %FileCheck %s -check-prefix CHECK-STATIC
3+
4+
// CHECK-DYNAMIC-NOT: clang importer cc1 args: {{.*}} '-D' 'SWIFT_STATIC_STDLIB'
5+
// CHECK-STATIC: clang importer cc1 args: {{.*}} '-D' 'SWIFT_STATIC_STDLIB'

0 commit comments

Comments
 (0)