Skip to content

Commit b1058c8

Browse files
authored
Fix the swiftCxxStdlib link with explicit module build on Windows (#85904)
We currently support static linking swiftCxxStdlib only on Windows. When C++ interop is enabled but swiftCxxStdlib isn't actually used or only when its underlying std module is used, we incorrectly emitted the dynamic version of the lib name and caused the link error. This change fixes it by always adding it to the dependency. Issue #85876
1 parent fe3200e commit b1058c8

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/DependencyScan/ModuleDependencyScanner.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,23 @@ ModuleDependencyScanner::getMainModuleDependencyInfo(ModuleDecl *mainModule) {
620620
break;
621621
}
622622

623+
if (ScanASTContext.LangOpts.EnableCXXInterop) {
624+
StringRef mainModuleName = mainModule->getName().str();
625+
if (mainModuleName != CXX_MODULE_NAME)
626+
mainDependencies.addModuleImport(CXX_MODULE_NAME, /* isExported */ false,
627+
AccessLevel::Public,
628+
&alreadyAddedModules);
629+
if (llvm::none_of(llvm::ArrayRef<StringRef>{CXX_MODULE_NAME,
630+
ScanASTContext.Id_CxxStdlib.str(), "std"},
631+
[mainModuleName](StringRef Name) {
632+
return mainModuleName == Name;
633+
}))
634+
mainDependencies.addModuleImport(ScanASTContext.Id_CxxStdlib.str(),
635+
/* isExported */ false,
636+
AccessLevel::Public,
637+
&alreadyAddedModules);
638+
}
639+
623640
// Add any implicit module names.
624641
for (const auto &import : importInfo.AdditionalUnloadedImports) {
625642
mainDependencies.addModuleImport(
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// REQUIRES: OS=windows-msvc
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: split-file %s %t
5+
// RUN: %target-swift-frontend -scan-dependencies -module-name NoCpp \
6+
// RUN: -module-cache-path %t/clang-module-cache -disable-objc-interop \
7+
// RUN: -cxx-interoperability-mode=default \
8+
// RUN: %t/NoCpp.swift -o %t/NoCpp-deps.json -I %t 2>&1
9+
// RUN: %target-swift-frontend -scan-dependencies -module-name Cpp \
10+
// RUN: -module-cache-path %t/clang-module-cache -disable-objc-interop \
11+
// RUN: -cxx-interoperability-mode=default \
12+
// RUN: %t/Cpp.swift -o %t/Cpp-deps.json -I %t 2>&1
13+
// RUN: %target-swift-frontend -scan-dependencies -module-name Crt \
14+
// RUN: -module-cache-path %t/clang-module-cache -disable-objc-interop \
15+
// RUN: -cxx-interoperability-mode=default \
16+
// RUN: %t/Crt.swift -o %t/Crt-deps.json -I %t 2>&1
17+
// RUN: %FileCheck %s --check-prefix=NOCPP < %t/NoCpp-deps.json
18+
// RUN: %FileCheck %s --check-prefix=CPP < %t/Cpp-deps.json
19+
// RUN: %FileCheck %s --check-prefix=CRT < %t/Crt-deps.json
20+
21+
// NOCPP: "mainModuleName": "NoCpp"
22+
// NOCPP: "linkName": "swiftCxxStdlib",
23+
// NOCPP-NEXT: "isStatic": true,
24+
25+
// CPP: "mainModuleName": "Cpp"
26+
// CPP: "linkName": "swiftCxxStdlib",
27+
// CPP-NEXT: "isStatic": true,
28+
29+
// CRT: "mainModuleName": "Crt"
30+
// CRT: "linkName": "swiftCxxStdlib",
31+
// CRT-NEXT: "isStatic": true,
32+
33+
//--- NoCpp.swift
34+
// Empty
35+
36+
//--- Cpp.swift
37+
import CxxStdlib
38+
39+
//--- Crt.swift
40+
import CRT
41+
42+
43+

0 commit comments

Comments
 (0)