Skip to content

Commit 5f25625

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 395e967 commit 5f25625

14 files changed

+62
-33
lines changed

CMakeLists.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ if(POLICY CMP0051)
44
cmake_policy(SET CMP0051 NEW)
55
endif()
66

7+
if(POLICY CMP0054)
8+
cmake_policy(SET CMP0054 OLD)
9+
endif()
10+
711
# Add path for custom CMake modules.
812
list(APPEND CMAKE_MODULE_PATH
913
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
@@ -610,10 +614,10 @@ elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "FREEBSD")
610614
elseif("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "CYGWIN")
611615

612616
# set(CMAKE_EXECUTABLE_FORMAT "ELF")
613-
set(SWIFT_HOST_VARIANT "windows" CACHE STRING
614-
"Deployment OS for Swift host tools (the compiler) [windows].")
617+
set(SWIFT_HOST_VARIANT "cygwin" CACHE STRING
618+
"Deployment OS for Swift host tools (the compiler) [cygwin].")
615619

616-
configure_sdk_unix(CYGWIN "Cygwin" "windows" "cygwin" "windows" "x86_64-unknown-windows-cygnus" "/")
620+
configure_sdk_unix(CYGWIN "Cygwin" "cygwin" "cygwin" "x86_64" "x86_64-unknown-windows-cygnus" "/")
617621
set(SWIFT_PRIMARY_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
618622
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
619623

include/swift/Runtime/Concurrent.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <atomic>
1616
#include <stdint.h>
1717

18-
#if defined(__FreeBSD__)
18+
#if defined(__FreeBSD__) || defined(__CYGWIN__)
1919
#include <stdio.h>
2020
#endif
2121

lib/Basic/Platform.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,16 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
118118
case llvm::Triple::FreeBSD:
119119
return "freebsd";
120120
case llvm::Triple::Win32:
121-
return "windows";
121+
switch (triple.getEnvironment()) {
122+
case llvm::Triple::Cygnus:
123+
return "cygwin";
124+
case llvm::Triple::GNU:
125+
return "mingw";
126+
case llvm::Triple::MSVC:
127+
return "windows";
128+
default:
129+
llvm_unreachable("unsupported Windows environment");
130+
}
122131
case llvm::Triple::PS4:
123132
return "ps4";
124133
}

lib/IRGen/GenDecl.cpp

+6-6
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
}
@@ -1239,7 +1239,7 @@ getIRLinkage(IRGenModule &IGM, SILLinkage linkage, bool isFragile,
12391239

12401240
const auto ObjFormat = IGM.TargetInfo.OutputObjectFormat;
12411241
bool IsELFObject = ObjFormat == llvm::Triple::ELF;
1242-
bool IsCOFFObject = ObjFormat == llvm::Triple::COFF;
1242+
bool UseDLLStorage = IGM.useDllStorage();
12431243

12441244
// Use protected visibility for public symbols we define on ELF. ld.so
12451245
// doesn't support relative relocations at load time, which interferes with
@@ -1249,11 +1249,11 @@ getIRLinkage(IRGenModule &IGM, SILLinkage linkage, bool isFragile,
12491249
IsELFObject ? llvm::GlobalValue::ProtectedVisibility
12501250
: llvm::GlobalValue::DefaultVisibility;
12511251
llvm::GlobalValue::DLLStorageClassTypes ExportedStorage =
1252-
IsCOFFObject ? llvm::GlobalValue::DLLExportStorageClass
1253-
: llvm::GlobalValue::DefaultStorageClass;
1252+
UseDLLStorage ? llvm::GlobalValue::DLLExportStorageClass
1253+
: llvm::GlobalValue::DefaultStorageClass;
12541254
llvm::GlobalValue::DLLStorageClassTypes ImportedStorage =
1255-
IsCOFFObject ? llvm::GlobalValue::DLLImportStorageClass
1256-
: llvm::GlobalValue::DefaultStorageClass;
1255+
UseDLLStorage ? llvm::GlobalValue::DLLImportStorageClass
1256+
: llvm::GlobalValue::DefaultStorageClass;
12571257

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

lib/IRGen/GenFunc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ void irgen::emitBlockHeader(IRGenFunction &IGF,
14991499
auto NSConcreteStackBlock =
15001500
IGF.IGM.getModule()->getOrInsertGlobal("_NSConcreteStackBlock",
15011501
IGF.IGM.ObjCClassStructTy);
1502-
if (IGF.IGM.Triple.isOSBinFormatCOFF())
1502+
if (IGF.IGM.useDllStorage())
15031503
cast<llvm::GlobalVariable>(NSConcreteStackBlock)
15041504
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
15051505

lib/IRGen/GenMeta.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3982,7 +3982,7 @@ static void emitObjCClassSymbol(IRGenModule &IGM,
39823982
metadata->getLinkage(),
39833983
classSymbol.str(), metadata,
39843984
IGM.getModule());
3985-
if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::COFF)
3985+
if (IGM.useDllStorage())
39863986
alias->setDLLStorageClass(metadata->getDLLStorageClass());
39873987
}
39883988

lib/IRGen/IRGen.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,13 @@ static createTargetMachine(IRGenOptions &Opts, ASTContext &Ctx) {
490490
}
491491

492492
// Create a target machine.
493-
llvm::TargetMachine *TargetMachine
494-
= Target->createTargetMachine(Triple.str(), CPU,
495-
targetFeatures, TargetOpts, Reloc::PIC_,
496-
CodeModel::Default, OptLevel);
493+
auto cmodel = CodeModel::Default;
494+
if (Triple.isWindowsCygwinEnvironment())
495+
cmodel = CodeModel::Large;
496+
497+
llvm::TargetMachine *TargetMachine =
498+
Target->createTargetMachine(Triple.str(), CPU, targetFeatures, TargetOpts,
499+
Reloc::PIC_, cmodel, OptLevel);
497500
if (!TargetMachine) {
498501
Ctx.Diags.diagnose(SourceLoc(), diag::no_llvm_target,
499502
Triple.str(), "no LLVM target machine");

lib/IRGen/IRGenModule.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module,
452452
fn->setCallingConv(cc);
453453

454454
if (llvm::Triple(Module.getTargetTriple()).isOSBinFormatCOFF() &&
455+
!llvm::Triple(Module.getTargetTriple()).isOSCygMing() &&
455456
(fn->getLinkage() == llvm::GlobalValue::ExternalLinkage ||
456457
fn->getLinkage() == llvm::GlobalValue::AvailableExternallyLinkage))
457458
fn->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
@@ -531,7 +532,8 @@ llvm::Constant *swift::getWrapperFn(llvm::Module &Module,
531532
auto *globalFnPtr =
532533
new llvm::GlobalVariable(Module, fnPtrTy, false,
533534
llvm::GlobalValue::ExternalLinkage, 0, symbol);
534-
if (llvm::Triple(Module.getTargetTriple()).isOSBinFormatCOFF())
535+
if (llvm::Triple(Module.getTargetTriple()).isOSBinFormatCOFF() &&
536+
!llvm::Triple(Module.getTargetTriple()).isOSCygMing())
535537
globalFnPtr->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
536538

537539
// Forward all arguments.
@@ -649,7 +651,7 @@ llvm::Constant *IRGenModule::getEmptyTupleMetadata() {
649651

650652
EmptyTupleMetadata =
651653
Module.getOrInsertGlobal("_TMT_", FullTypeMetadataStructTy);
652-
if (Triple.isOSBinFormatCOFF())
654+
if (useDllStorage())
653655
cast<llvm::GlobalVariable>(EmptyTupleMetadata)
654656
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
655657
return EmptyTupleMetadata;
@@ -663,7 +665,7 @@ llvm::Constant *IRGenModule::getObjCEmptyCachePtr() {
663665
// struct objc_cache _objc_empty_cache;
664666
ObjCEmptyCachePtr = Module.getOrInsertGlobal("_objc_empty_cache",
665667
OpaquePtrTy->getElementType());
666-
if (Triple.isOSBinFormatCOFF())
668+
if (useDllStorage())
667669
cast<llvm::GlobalVariable>(ObjCEmptyCachePtr)
668670
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
669671
} else {
@@ -696,7 +698,7 @@ Address IRGenModule::getAddrOfObjCISAMask() {
696698
assert(TargetInfo.hasISAMasking());
697699
if (!ObjCISAMaskPtr) {
698700
ObjCISAMaskPtr = Module.getOrInsertGlobal("swift_isaMask", IntPtrTy);
699-
if (Triple.isOSBinFormatCOFF())
701+
if (useDllStorage())
700702
cast<llvm::GlobalVariable>(ObjCISAMaskPtr)
701703
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
702704
}
@@ -862,7 +864,7 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
862864
llvm::SmallString<64> buf;
863865
encodeForceLoadSymbolName(buf, linkLib.getName());
864866
auto symbolAddr = Module.getOrInsertGlobal(buf.str(), Int1Ty);
865-
if (Triple.isOSBinFormatCOFF())
867+
if (useDllStorage())
866868
cast<llvm::GlobalVariable>(symbolAddr)
867869
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
868870

@@ -927,9 +929,9 @@ void IRGenModule::emitAutolinkInfo() {
927929
}),
928930
AutolinkEntries.end());
929931

930-
if (TargetInfo.OutputObjectFormat == llvm::Triple::COFF ||
931-
TargetInfo.OutputObjectFormat == llvm::Triple::MachO ||
932-
Triple.isPS4()) {
932+
if ((TargetInfo.OutputObjectFormat == llvm::Triple::COFF &&
933+
!Triple.isOSCygMing()) ||
934+
TargetInfo.OutputObjectFormat == llvm::Triple::MachO || Triple.isPS4()) {
933935
llvm::LLVMContext &ctx = Module.getContext();
934936

935937
if (!LinkerOptions) {
@@ -946,8 +948,9 @@ void IRGenModule::emitAutolinkInfo() {
946948
assert(FoundOldEntry && "Could not replace old linker options entry?");
947949
}
948950
} else {
949-
assert(TargetInfo.OutputObjectFormat == llvm::Triple::ELF &&
950-
"expected ELF output format");
951+
assert((TargetInfo.OutputObjectFormat == llvm::Triple::ELF ||
952+
Triple.isOSCygMing()) &&
953+
"expected ELF output format or COFF format for Cygwin/MinGW");
951954

952955
// Merge the entries into null-separated string.
953956
llvm::SmallString<64> EntriesString;
@@ -980,7 +983,7 @@ void IRGenModule::emitAutolinkInfo() {
980983
llvm::GlobalValue::CommonLinkage,
981984
llvm::Constant::getNullValue(Int1Ty),
982985
buf.str());
983-
if (Triple.isOSBinFormatCOFF())
986+
if (useDllStorage())
984987
symbol->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
985988
}
986989
}

lib/IRGen/IRGenModule.h

+4
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ class IRGenModule {
520520
void fatal_unimplemented(SourceLoc, StringRef Message);
521521
void error(SourceLoc loc, const Twine &message);
522522

523+
bool useDllStorage() {
524+
return Triple.isOSBinFormatCOFF() && !Triple.isOSCygMing();
525+
}
526+
523527
private:
524528
Size PtrSize;
525529
llvm::Type *FixedBufferTy; /// [N x i8], where N == 3 * sizeof(void*)

lib/IRGen/TypeLayoutVerifier.cpp

+1-1
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.Triple.isOSBinFormatCOFF() && !IGF.IGM.Triple.isOSCygMing())
4747
if (auto *F = dyn_cast<llvm::Function>(verifierFn))
4848
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4949

lib/LLVMPasses/ARCEntryPointBuilder.h

+2-1
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();

lib/LLVMPasses/LLVMStackPromotion.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ bool SwiftStackPromotion::runOnFunction(Function &F) {
216216
{MetaDataTy, HeapObjTy},
217217
false);
218218
initFunc = M->getOrInsertFunction("swift_initStackObject", NewFTy);
219-
if (llvm::Triple(M->getTargetTriple()).isOSBinFormatCOFF())
219+
if (llvm::Triple(M->getTargetTriple()).isOSBinFormatCOFF() &&
220+
!llvm::Triple(M->getTargetTriple()).isOSCygMing())
220221
if (auto *F = dyn_cast<llvm::Function>(initFunc))
221222
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
222223
}
@@ -238,7 +239,8 @@ bool SwiftStackPromotion::runOnFunction(Function &F) {
238239
if (!allocFunc) {
239240
allocFunc = M->getOrInsertFunction("swift_bufferAllocate",
240241
Callee->getFunctionType());
241-
if (llvm::Triple(M->getTargetTriple()).isOSBinFormatCOFF())
242+
if (llvm::Triple(M->getTargetTriple()).isOSBinFormatCOFF() &&
243+
!llvm::Triple(M->getTargetTriple()).isOSCygMing())
242244
if (auto *F = dyn_cast<llvm::Function>(allocFunc))
243245
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
244246
}

lib/Markup/Markup.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "swift/AST/Comment.h"
1616
#include "swift/Markup/LineList.h"
1717
#include "swift/Markup/Markup.h"
18+
#if defined(__CYGWIN__)
19+
#define CMARK_STATIC_DEFINE
20+
#endif
1821
#include "cmark.h"
1922

2023
using namespace swift;

utils/build-script-impl

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ function set_build_options_for_host() {
459459
SWIFT_HOST_VARIANT_ARCH="x86_64"
460460
;;
461461
cygwin-x86_64)
462-
SWIFT_HOST_VARIANT="windows"
462+
SWIFT_HOST_VARIANT="cygwin"
463463
SWIFT_HOST_VARIANT_SDK="CYGWIN"
464464
SWIFT_HOST_VARIANT_ARCH="x86_64"
465465
;;

0 commit comments

Comments
 (0)