Skip to content

Commit b8dd577

Browse files
committed
[swiftc] Fixed for Cygwin
Fixed for the difference of Cygwin with other Windows variants (MSVC, Itanium, MinGW). - The platform name is renamed to "cygwin" from "windows" which is used for searching the standard libraries. - The consideration for DLL storage class (DllExport/DllImport) is not required for Cygwin and MinGW. There is no problem when linking in these environment. - Cygwin should use large memory model as default.(This may be changed if someone ports to 32bit) - Cygwin and MinGW should use the autolink feature in the sameway of Linux due to the linker's limit.
1 parent 5a24741 commit b8dd577

File tree

14 files changed

+74
-43
lines changed

14 files changed

+74
-43
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,10 @@ elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "FREEBSD")
589589
elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "CYGWIN")
590590

591591
# set(CMAKE_EXECUTABLE_FORMAT "ELF")
592-
set(SWIFT_HOST_VARIANT "windows" CACHE STRING
593-
"Deployment OS for Swift host tools (the compiler) [windows].")
592+
set(SWIFT_HOST_VARIANT "cygwin" CACHE STRING
593+
"Deployment OS for Swift host tools (the compiler) [cygwin].")
594594

595-
configure_sdk_unix(CYGWIN "Cygwin" "windows" "cygwin" "windows" "x86_64-unknown-windows-cygnus" "/")
595+
configure_sdk_unix(CYGWIN "Cygwin" "cygwin" "cygwin" "x86_64" "x86_64-unknown-windows-cygnus" "/")
596596
set(SWIFT_PRIMARY_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
597597
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
598598

include/swift/Runtime/Concurrent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <stdint.h>
1818
#include "llvm/Support/Allocator.h"
1919

20-
#if defined(__FreeBSD__)
20+
#if defined(__FreeBSD__) || defined(__CYGWIN__)
2121
#include <stdio.h>
2222
#endif
2323

lib/Basic/Platform.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,17 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
119119
case llvm::Triple::FreeBSD:
120120
return "freebsd";
121121
case llvm::Triple::Win32:
122-
return "windows";
122+
switch (triple.getEnvironment()) {
123+
case llvm::Triple::Cygnus:
124+
return "cygwin";
125+
case llvm::Triple::GNU:
126+
return "mingw";
127+
case llvm::Triple::MSVC:
128+
case llvm::Triple::Itanium:
129+
return "windows";
130+
default:
131+
llvm_unreachable("unsupported Windows environment");
132+
}
123133
case llvm::Triple::PS4:
124134
return "ps4";
125135
}

lib/IRGen/GenDecl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ void IRGenModule::emitVTableStubs() {
937937
// For each eliminated method symbol create an alias to the stub.
938938
auto *alias = llvm::GlobalAlias::create(llvm::GlobalValue::ExternalLinkage,
939939
F.getName(), stub);
940-
if (Triple.isOSBinFormatCOFF())
940+
if (useDllStorage())
941941
alias->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
942942
}
943943
}
@@ -1260,7 +1260,7 @@ getIRLinkage(IRGenModule &IGM, SILLinkage linkage, bool isFragile,
12601260

12611261
const auto ObjFormat = IGM.TargetInfo.OutputObjectFormat;
12621262
bool IsELFObject = ObjFormat == llvm::Triple::ELF;
1263-
bool IsCOFFObject = ObjFormat == llvm::Triple::COFF;
1263+
bool UseDLLStorage = IGM.useDllStorage();
12641264

12651265
// Use protected visibility for public symbols we define on ELF. ld.so
12661266
// doesn't support relative relocations at load time, which interferes with
@@ -1270,11 +1270,11 @@ getIRLinkage(IRGenModule &IGM, SILLinkage linkage, bool isFragile,
12701270
IsELFObject ? llvm::GlobalValue::ProtectedVisibility
12711271
: llvm::GlobalValue::DefaultVisibility;
12721272
llvm::GlobalValue::DLLStorageClassTypes ExportedStorage =
1273-
IsCOFFObject ? llvm::GlobalValue::DLLExportStorageClass
1274-
: llvm::GlobalValue::DefaultStorageClass;
1273+
UseDLLStorage ? llvm::GlobalValue::DLLExportStorageClass
1274+
: llvm::GlobalValue::DefaultStorageClass;
12751275
llvm::GlobalValue::DLLStorageClassTypes ImportedStorage =
1276-
IsCOFFObject ? llvm::GlobalValue::DLLImportStorageClass
1277-
: llvm::GlobalValue::DefaultStorageClass;
1276+
UseDLLStorage ? llvm::GlobalValue::DLLImportStorageClass
1277+
: llvm::GlobalValue::DefaultStorageClass;
12781278

12791279
if (isFragile) {
12801280
// Fragile functions/globals must be visible from outside, regardless of

lib/IRGen/GenFunc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ void irgen::emitBlockHeader(IRGenFunction &IGF,
15111511
auto NSConcreteStackBlock =
15121512
IGF.IGM.getModule()->getOrInsertGlobal("_NSConcreteStackBlock",
15131513
IGF.IGM.ObjCClassStructTy);
1514-
if (IGF.IGM.Triple.isOSBinFormatCOFF())
1514+
if (IGF.IGM.useDllStorage())
15151515
cast<llvm::GlobalVariable>(NSConcreteStackBlock)
15161516
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
15171517

lib/IRGen/GenMeta.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4035,7 +4035,7 @@ static void emitObjCClassSymbol(IRGenModule &IGM,
40354035
metadata->getLinkage(),
40364036
classSymbol.str(), metadata,
40374037
IGM.getModule());
4038-
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::COFF)
4038+
if (IGM.useDllStorage())
40394039
alias->setDLLStorageClass(metadata->getDLLStorageClass());
40404040
}
40414041

lib/IRGen/IRGen.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,13 @@ swift::createTargetMachine(IRGenOptions &Opts, ASTContext &Ctx) {
496496
}
497497

498498
// Create a target machine.
499-
llvm::TargetMachine *TargetMachine
500-
= Target->createTargetMachine(Triple.str(), CPU,
501-
targetFeatures, TargetOpts, Reloc::PIC_,
502-
CodeModel::Default, OptLevel);
499+
auto cmodel = CodeModel::Default;
500+
if (Triple.isWindowsCygwinEnvironment())
501+
cmodel = CodeModel::Large;
502+
503+
llvm::TargetMachine *TargetMachine =
504+
Target->createTargetMachine(Triple.str(), CPU, targetFeatures, TargetOpts,
505+
Reloc::PIC_, cmodel, OptLevel);
503506
if (!TargetMachine) {
504507
Ctx.Diags.diagnose(SourceLoc(), diag::no_llvm_target,
505508
Triple.str(), "no LLVM target machine");

lib/IRGen/IRGenModule.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ static clang::CodeGenerator *createClangCodeGenerator(ASTContext &Context,
117117
return ClangCodeGen;
118118
}
119119

120+
/// A helper for determining if the triple uses the DLL storage
121+
static bool useDllStorage(const llvm::Triple &Triple) {
122+
return Triple.isOSBinFormatCOFF() && !Triple.isOSCygMing();
123+
}
124+
120125
IRGenModule::IRGenModule(IRGenerator &irgen,
121126
std::unique_ptr<llvm::TargetMachine> &&target,
122127
SourceFile *SF, llvm::LLVMContext &LLVMContext,
@@ -439,7 +444,7 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
439444
if (auto fn = dyn_cast<llvm::Function>(cache)) {
440445
fn->setCallingConv(cc);
441446

442-
if (llvm::Triple(Module.getTargetTriple()).isOSBinFormatCOFF() &&
447+
if (::useDllStorage(llvm::Triple(Module.getTargetTriple())) &&
443448
(fn->getLinkage() == llvm::GlobalValue::ExternalLinkage ||
444449
fn->getLinkage() == llvm::GlobalValue::AvailableExternallyLinkage))
445450
fn->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
@@ -519,7 +524,7 @@ llvm::Constant *swift::getWrapperFn(llvm::Module &Module,
519524
auto *globalFnPtr = new llvm::GlobalVariable(
520525
Module, fnPtrTy, false, llvm::GlobalValue::ExternalLinkage, nullptr,
521526
symbol);
522-
if (llvm::Triple(Module.getTargetTriple()).isOSBinFormatCOFF())
527+
if (::useDllStorage(llvm::Triple(Module.getTargetTriple())))
523528
globalFnPtr->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
524529

525530
// Forward all arguments.
@@ -638,7 +643,7 @@ llvm::Constant *IRGenModule::getEmptyTupleMetadata() {
638643
EmptyTupleMetadata = Module.getOrInsertGlobal(
639644
MANGLE_AS_STRING(METADATA_SYM(EMPTY_TUPLE_MANGLING)),
640645
FullTypeMetadataStructTy);
641-
if (Triple.isOSBinFormatCOFF())
646+
if (useDllStorage())
642647
cast<llvm::GlobalVariable>(EmptyTupleMetadata)
643648
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
644649
return EmptyTupleMetadata;
@@ -652,7 +657,7 @@ llvm::Constant *IRGenModule::getObjCEmptyCachePtr() {
652657
// struct objc_cache _objc_empty_cache;
653658
ObjCEmptyCachePtr = Module.getOrInsertGlobal("_objc_empty_cache",
654659
OpaquePtrTy->getElementType());
655-
if (Triple.isOSBinFormatCOFF())
660+
if (useDllStorage())
656661
cast<llvm::GlobalVariable>(ObjCEmptyCachePtr)
657662
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
658663
} else {
@@ -685,7 +690,7 @@ Address IRGenModule::getAddrOfObjCISAMask() {
685690
assert(TargetInfo.hasISAMasking());
686691
if (!ObjCISAMaskPtr) {
687692
ObjCISAMaskPtr = Module.getOrInsertGlobal("swift_isaMask", IntPtrTy);
688-
if (Triple.isOSBinFormatCOFF())
693+
if (useDllStorage())
689694
cast<llvm::GlobalVariable>(ObjCISAMaskPtr)
690695
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
691696
}
@@ -851,7 +856,7 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
851856
llvm::SmallString<64> buf;
852857
encodeForceLoadSymbolName(buf, linkLib.getName());
853858
auto symbolAddr = Module.getOrInsertGlobal(buf.str(), Int1Ty);
854-
if (Triple.isOSBinFormatCOFF())
859+
if (useDllStorage())
855860
cast<llvm::GlobalVariable>(symbolAddr)
856861
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
857862

@@ -916,9 +921,9 @@ void IRGenModule::emitAutolinkInfo() {
916921
}),
917922
AutolinkEntries.end());
918923

919-
if (TargetInfo.OutputObjectFormat == llvm::Triple::COFF ||
920-
TargetInfo.OutputObjectFormat == llvm::Triple::MachO ||
921-
Triple.isPS4()) {
924+
if ((TargetInfo.OutputObjectFormat == llvm::Triple::COFF &&
925+
!Triple.isOSCygMing()) ||
926+
TargetInfo.OutputObjectFormat == llvm::Triple::MachO || Triple.isPS4()) {
922927
llvm::LLVMContext &ctx = Module.getContext();
923928

924929
if (!LinkerOptions) {
@@ -935,8 +940,9 @@ void IRGenModule::emitAutolinkInfo() {
935940
assert(FoundOldEntry && "Could not replace old linker options entry?");
936941
}
937942
} else {
938-
assert(TargetInfo.OutputObjectFormat == llvm::Triple::ELF &&
939-
"expected ELF output format");
943+
assert((TargetInfo.OutputObjectFormat == llvm::Triple::ELF ||
944+
Triple.isOSCygMing()) &&
945+
"expected ELF output format or COFF format for Cygwin/MinGW");
940946

941947
// Merge the entries into null-separated string.
942948
llvm::SmallString<64> EntriesString;
@@ -969,7 +975,7 @@ void IRGenModule::emitAutolinkInfo() {
969975
llvm::GlobalValue::CommonLinkage,
970976
llvm::Constant::getNullValue(Int1Ty),
971977
buf.str());
972-
if (Triple.isOSBinFormatCOFF())
978+
if (useDllStorage())
973979
symbol->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
974980
}
975981
}
@@ -1072,6 +1078,8 @@ void IRGenModule::error(SourceLoc loc, const Twine &message) {
10721078
message.toStringRef(buffer));
10731079
}
10741080

1081+
bool IRGenModule::useDllStorage() { return ::useDllStorage(Triple); }
1082+
10751083
void IRGenerator::addGenModule(SourceFile *SF, IRGenModule *IGM) {
10761084
assert(GenModules.count(SF) == 0);
10771085
GenModules[SF] = IGM;

lib/IRGen/IRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ class IRGenModule {
534534
void fatal_unimplemented(SourceLoc, StringRef Message);
535535
void error(SourceLoc loc, const Twine &message);
536536

537+
bool useDllStorage();
538+
537539
private:
538540
Size PtrSize;
539541
llvm::Type *FixedBufferTy; /// [N x i8], where N == 3 * sizeof(void*)

lib/IRGen/TypeLayoutVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ irgen::emitTypeLayoutVerifier(IRGenFunction &IGF,
4343
/*var arg*/ false);
4444
auto verifierFn = IGF.IGM.Module.getOrInsertFunction(
4545
"_swift_debug_verifyTypeLayoutAttribute", verifierFnTy);
46-
if (IGF.IGM.Triple.isOSBinFormatCOFF())
46+
if (IGF.IGM.useDllStorage())
4747
if (auto *F = dyn_cast<llvm::Function>(verifierFn))
4848
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4949

lib/LLVMPasses/ARCEntryPointBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ class ARCEntryPointBuilder {
272272
CheckUnowned = M.getOrInsertFunction("swift_checkUnowned", AttrList,
273273
Type::getVoidTy(M.getContext()),
274274
ObjectPtrTy, nullptr);
275-
if (llvm::Triple(M.getTargetTriple()).isOSBinFormatCOFF())
275+
if (llvm::Triple(M.getTargetTriple()).isOSBinFormatCOFF() &&
276+
!llvm::Triple(M.getTargetTriple()).isOSCygMing())
276277
if (auto *F = llvm::dyn_cast<llvm::Function>(CheckUnowned.get()))
277278
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
278279
return CheckUnowned.get();

test/IRGen/autolink-coff-x86.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
4+
// RUN: %swift -target x86_64--windows-gnu -parse-as-library -parse-stdlib -emit-module-path %t/module.swiftmodule -module-name module -module-link-name module %s
5+
// RUN: %swift -target x86_64--windows-gnu -parse-as-library -parse-stdlib -module-name autolink -I %t -D MAIN_MODULE -emit-ir -o - %s | %FileCheck %s -check-prefix CHECK-GNU-IR
6+
// RUN: %swift -target x86_64--windows-gnu -parse-as-library -parse-stdlib -module-name autolink -I %t -D MAIN_MODULE -S -o - %s | %FileCheck %s -check-prefix CHECK-GNU-ASM
7+
8+
// REQUIRES: CODEGENERATOR=X86
9+
10+
#if MAIN_MODULE
11+
import module
12+
#endif
13+
14+
// CHECK-GNU-IR: @_swift1_autolink_entries = private constant [9 x i8] c"-lmodule\00", section ".swift1_autolink_entries", align 8
15+
// CHECK-GNU-IR: @llvm.used = appending global [{{.*}} x i8*] [{{.*}}i8* getelementptr inbounds ([9 x i8], [9 x i8]* @_swift1_autolink_entries, i32 0, i32 0){{.*}}], section "llvm.metadata", align 8
16+
17+
// CHECK-GNU-ASM: .section .swift1_autolink_entries{{.*}}
18+
// CHECK-GNU-ASM: .asciz "-lmodule"

test/IRGen/autolink-coff.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// RUN: rm -rf %t
22
// RUN: mkdir -p %t
33

4-
// RUN: %swift -target thumbv7--windows-gnu -parse-as-library -parse-stdlib -emit-module-path %t/module.swiftmodule -module-name module -module-link-name module %s
5-
// RUN: %swift -target thumbv7--windows-gnu -parse-as-library -parse-stdlib -module-name autolink -I %t -D MAIN_MODULE -emit-ir -o - %s | %FileCheck %s -check-prefix CHECK-GNU-IR
6-
// RUN: %swift -target thumbv7--windows-gnu -parse-as-library -parse-stdlib -module-name autolink -I %t -D MAIN_MODULE -S -o - %s | %FileCheck %s -check-prefix CHECK-GNU-ASM
7-
84
// RUN: %swift -target thumbv7--windows-itanium -parse-as-library -parse-stdlib -emit-module-path %t/module.swiftmodule -module-name module -module-link-name module %s
95
// RUN: %swift -target thumbv7--windows-itanium -parse-as-library -parse-stdlib -module-name autolink -I %t -D MAIN_MODULE -emit-ir -o - %s | %FileCheck %s -check-prefix CHECK-MSVC-IR
106
// RUN: %swift -target thumbv7--windows-itanium -parse-as-library -parse-stdlib -module-name autolink -I %t -D MAIN_MODULE -S -o - %s | %FileCheck %s -check-prefix CHECK-MSVC-ASM
@@ -19,13 +15,6 @@
1915
import module
2016
#endif
2117

22-
// CHECK-GNU-IR: !{{[0-9]+}} = !{i32 {{[0-9]+}}, !"Linker Options", [[NODE:![0-9]+]]}
23-
// CHECK-GNU-IR: [[NODE]] = !{[[LIST:![0-9]+]]}
24-
// CHECK-GNU-IR: [[LIST]] = !{!"-lmodule"}
25-
26-
// CHECK-GNU-ASM: .section .drectve
27-
// CHECK-GNU-ASM: .ascii " -lmodule"
28-
2918
// CHECK-MSVC-IR: !{{[0-9]+}} = !{i32 {{[0-9]+}}, !"Linker Options", [[NODE:![0-9]+]]}
3019
// CHECK-MSVC-IR: [[NODE]] = !{[[LIST:![0-9]+]]}
3120
// CHECK-MSVC-IR: [[LIST]] = !{!"/DEFAULTLIB:module.lib"}

utils/build-script-impl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ function set_build_options_for_host() {
419419
USE_GOLD_LINKER=1
420420
;;
421421
cygwin-x86_64)
422-
SWIFT_HOST_VARIANT="windows"
422+
SWIFT_HOST_VARIANT="cygwin"
423423
SWIFT_HOST_VARIANT_SDK="CYGWIN"
424424
SWIFT_HOST_VARIANT_ARCH="x86_64"
425425
;;

0 commit comments

Comments
 (0)