Skip to content

Commit 7b5cf4a

Browse files
authored
Merge pull request swiftlang#3841 from tinysun212/pr-swiftc-cygwin-2
[swiftc] Fixed for Cygwin
2 parents baedff6 + b8dd577 commit 7b5cf4a

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
@@ -641,10 +641,10 @@ elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "FREEBSD")
641641
elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "CYGWIN")
642642

643643
# set(CMAKE_EXECUTABLE_FORMAT "ELF")
644-
set(SWIFT_HOST_VARIANT "windows" CACHE STRING
645-
"Deployment OS for Swift host tools (the compiler) [windows].")
644+
set(SWIFT_HOST_VARIANT "cygwin" CACHE STRING
645+
"Deployment OS for Swift host tools (the compiler) [cygwin].")
646646

647-
configure_sdk_unix(CYGWIN "Cygwin" "windows" "cygwin" "windows" "x86_64-unknown-windows-cygnus" "/")
647+
configure_sdk_unix(CYGWIN "Cygwin" "cygwin" "cygwin" "x86_64" "x86_64-unknown-windows-cygnus" "/")
648648
set(SWIFT_PRIMARY_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
649649
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
650650

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
@@ -947,7 +947,7 @@ void IRGenModule::emitVTableStubs() {
947947
// For each eliminated method symbol create an alias to the stub.
948948
auto *alias = llvm::GlobalAlias::create(llvm::GlobalValue::ExternalLinkage,
949949
F.getName(), stub);
950-
if (Triple.isOSBinFormatCOFF())
950+
if (useDllStorage())
951951
alias->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
952952
}
953953
}
@@ -1278,7 +1278,7 @@ getIRLinkage(IRGenModule &IGM, SILLinkage linkage, bool isFragile,
12781278

12791279
const auto ObjFormat = IGM.TargetInfo.OutputObjectFormat;
12801280
bool IsELFObject = ObjFormat == llvm::Triple::ELF;
1281-
bool IsCOFFObject = ObjFormat == llvm::Triple::COFF;
1281+
bool UseDLLStorage = IGM.useDllStorage();
12821282

12831283
// Use protected visibility for public symbols we define on ELF. ld.so
12841284
// doesn't support relative relocations at load time, which interferes with
@@ -1288,11 +1288,11 @@ getIRLinkage(IRGenModule &IGM, SILLinkage linkage, bool isFragile,
12881288
IsELFObject ? llvm::GlobalValue::ProtectedVisibility
12891289
: llvm::GlobalValue::DefaultVisibility;
12901290
llvm::GlobalValue::DLLStorageClassTypes ExportedStorage =
1291-
IsCOFFObject ? llvm::GlobalValue::DLLExportStorageClass
1292-
: llvm::GlobalValue::DefaultStorageClass;
1291+
UseDLLStorage ? llvm::GlobalValue::DLLExportStorageClass
1292+
: llvm::GlobalValue::DefaultStorageClass;
12931293
llvm::GlobalValue::DLLStorageClassTypes ImportedStorage =
1294-
IsCOFFObject ? llvm::GlobalValue::DLLImportStorageClass
1295-
: llvm::GlobalValue::DefaultStorageClass;
1294+
UseDLLStorage ? llvm::GlobalValue::DLLImportStorageClass
1295+
: llvm::GlobalValue::DefaultStorageClass;
12961296

12971297
if (isFragile) {
12981298
// 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
@@ -497,10 +497,13 @@ swift::createTargetMachine(IRGenOptions &Opts, ASTContext &Ctx) {
497497
}
498498

499499
// Create a target machine.
500-
llvm::TargetMachine *TargetMachine
501-
= Target->createTargetMachine(Triple.str(), CPU,
502-
targetFeatures, TargetOpts, Reloc::PIC_,
503-
CodeModel::Default, OptLevel);
500+
auto cmodel = CodeModel::Default;
501+
if (Triple.isWindowsCygwinEnvironment())
502+
cmodel = CodeModel::Large;
503+
504+
llvm::TargetMachine *TargetMachine =
505+
Target->createTargetMachine(Triple.str(), CPU, targetFeatures, TargetOpts,
506+
Reloc::PIC_, cmodel, OptLevel);
504507
if (!TargetMachine) {
505508
Ctx.Diags.diagnose(SourceLoc(), diag::no_llvm_target,
506509
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,
@@ -442,7 +447,7 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
442447
if (auto fn = dyn_cast<llvm::Function>(cache)) {
443448
fn->setCallingConv(cc);
444449

445-
if (llvm::Triple(Module.getTargetTriple()).isOSBinFormatCOFF() &&
450+
if (::useDllStorage(llvm::Triple(Module.getTargetTriple())) &&
446451
(fn->getLinkage() == llvm::GlobalValue::ExternalLinkage ||
447452
fn->getLinkage() == llvm::GlobalValue::AvailableExternallyLinkage))
448453
fn->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
@@ -522,7 +527,7 @@ llvm::Constant *swift::getWrapperFn(llvm::Module &Module,
522527
auto *globalFnPtr = new llvm::GlobalVariable(
523528
Module, fnPtrTy, false, llvm::GlobalValue::ExternalLinkage, nullptr,
524529
symbol);
525-
if (llvm::Triple(Module.getTargetTriple()).isOSBinFormatCOFF())
530+
if (::useDllStorage(llvm::Triple(Module.getTargetTriple())))
526531
globalFnPtr->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
527532

528533
// Forward all arguments.
@@ -641,7 +646,7 @@ llvm::Constant *IRGenModule::getEmptyTupleMetadata() {
641646
EmptyTupleMetadata = Module.getOrInsertGlobal(
642647
MANGLE_AS_STRING(METADATA_SYM(EMPTY_TUPLE_MANGLING)),
643648
FullTypeMetadataStructTy);
644-
if (Triple.isOSBinFormatCOFF())
649+
if (useDllStorage())
645650
cast<llvm::GlobalVariable>(EmptyTupleMetadata)
646651
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
647652
return EmptyTupleMetadata;
@@ -655,7 +660,7 @@ llvm::Constant *IRGenModule::getObjCEmptyCachePtr() {
655660
// struct objc_cache _objc_empty_cache;
656661
ObjCEmptyCachePtr = Module.getOrInsertGlobal("_objc_empty_cache",
657662
OpaquePtrTy->getElementType());
658-
if (Triple.isOSBinFormatCOFF())
663+
if (useDllStorage())
659664
cast<llvm::GlobalVariable>(ObjCEmptyCachePtr)
660665
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
661666
} else {
@@ -688,7 +693,7 @@ Address IRGenModule::getAddrOfObjCISAMask() {
688693
assert(TargetInfo.hasISAMasking());
689694
if (!ObjCISAMaskPtr) {
690695
ObjCISAMaskPtr = Module.getOrInsertGlobal("swift_isaMask", IntPtrTy);
691-
if (Triple.isOSBinFormatCOFF())
696+
if (useDllStorage())
692697
cast<llvm::GlobalVariable>(ObjCISAMaskPtr)
693698
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
694699
}
@@ -854,7 +859,7 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
854859
llvm::SmallString<64> buf;
855860
encodeForceLoadSymbolName(buf, linkLib.getName());
856861
auto symbolAddr = Module.getOrInsertGlobal(buf.str(), Int1Ty);
857-
if (Triple.isOSBinFormatCOFF())
862+
if (useDllStorage())
858863
cast<llvm::GlobalVariable>(symbolAddr)
859864
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
860865

@@ -919,9 +924,9 @@ void IRGenModule::emitAutolinkInfo() {
919924
}),
920925
AutolinkEntries.end());
921926

922-
if (TargetInfo.OutputObjectFormat == llvm::Triple::COFF ||
923-
TargetInfo.OutputObjectFormat == llvm::Triple::MachO ||
924-
Triple.isPS4()) {
927+
if ((TargetInfo.OutputObjectFormat == llvm::Triple::COFF &&
928+
!Triple.isOSCygMing()) ||
929+
TargetInfo.OutputObjectFormat == llvm::Triple::MachO || Triple.isPS4()) {
925930
llvm::LLVMContext &ctx = Module.getContext();
926931

927932
if (!LinkerOptions) {
@@ -938,8 +943,9 @@ void IRGenModule::emitAutolinkInfo() {
938943
assert(FoundOldEntry && "Could not replace old linker options entry?");
939944
}
940945
} else {
941-
assert(TargetInfo.OutputObjectFormat == llvm::Triple::ELF &&
942-
"expected ELF output format");
946+
assert((TargetInfo.OutputObjectFormat == llvm::Triple::ELF ||
947+
Triple.isOSCygMing()) &&
948+
"expected ELF output format or COFF format for Cygwin/MinGW");
943949

944950
// Merge the entries into null-separated string.
945951
llvm::SmallString<64> EntriesString;
@@ -972,7 +978,7 @@ void IRGenModule::emitAutolinkInfo() {
972978
llvm::GlobalValue::CommonLinkage,
973979
llvm::Constant::getNullValue(Int1Ty),
974980
buf.str());
975-
if (Triple.isOSBinFormatCOFF())
981+
if (useDllStorage())
976982
symbol->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
977983
}
978984
}
@@ -1075,6 +1081,8 @@ void IRGenModule::error(SourceLoc loc, const Twine &message) {
10751081
message.toStringRef(buffer));
10761082
}
10771083

1084+
bool IRGenModule::useDllStorage() { return ::useDllStorage(Triple); }
1085+
10781086
void IRGenerator::addGenModule(SourceFile *SF, IRGenModule *IGM) {
10791087
assert(GenModules.count(SF) == 0);
10801088
GenModules[SF] = IGM;

lib/IRGen/IRGenModule.h

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

545+
bool useDllStorage();
546+
545547
private:
546548
Size PtrSize;
547549
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)