Skip to content

Commit 7235595

Browse files
committed
Merge pull request #1108 from tinysun212/porting-to-cygwin
Porting to cygwin
2 parents c978092 + 8c829ee commit 7235595

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+641
-27
lines changed

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,19 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
455455

456456
set(SWIFT_PRIMARY_VARIANT_SDK_default "FREEBSD")
457457
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
458+
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
459+
configure_sdk_unix(CYGWIN "Cygwin" "windows" "cygwin" "x86_64" "x86_64-unknown-windows-cygnus")
460+
461+
# set(CMAKE_EXECUTABLE_FORMAT "ELF")
462+
463+
set(SWIFT_HOST_VARIANT "windows" CACHE STRING
464+
"Deployment OS for Swift host tools (the compiler) [windows].")
465+
466+
set(SWIFT_HOST_VARIANT_SDK "CYGWIN")
467+
set(SWIFT_HOST_VARIANT_ARCH "x86_64")
468+
469+
set(SWIFT_PRIMARY_VARIANT_SDK_default "CYGWIN")
470+
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
458471
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
459472
# Set defaults.
460473

cmake/modules/AddSwift.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ function(_add_variant_link_flags
182182
list(APPEND result "-lpthread" "-ldl")
183183
elseif("${sdk}" STREQUAL "FREEBSD")
184184
list(APPEND result "-lpthread")
185+
elseif("${sdk}" STREQUAL "CYGWIN")
186+
# No extra libraries required.
185187
else()
186188
list(APPEND result "-lobjc")
187189
endif()
@@ -967,6 +969,10 @@ function(_add_swift_library_single target name)
967969
set_target_properties("${target}"
968970
PROPERTIES
969971
INSTALL_RPATH "$ORIGIN:/usr/lib/swift/linux")
972+
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Cygwin")
973+
set_target_properties("${target}"
974+
PROPERTIES
975+
INSTALL_RPATH "$ORIGIN:/usr/lib/swift/windows")
970976
endif()
971977

972978
set_target_properties("${target}" PROPERTIES BUILD_WITH_INSTALL_RPATH YES)

include/swift/AST/IRGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class IRGenOptions {
6464
/// The compilation directory for the debug info.
6565
std::string DebugCompilationDir;
6666

67+
/// The DWARF version of debug info.
68+
unsigned DWARFVersion;
69+
6770
/// The command line string that is to be stored in the DWARF debug info.
6871
std::string DWARFDebugFlags;
6972

include/swift/Basic/Dwarf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
namespace swift {
2323
/// The DWARF version emitted by the Swift compiler.
24-
const unsigned DWARFVersion = 3;
24+
const unsigned GenericDWARFVersion = 3;
25+
const unsigned CygwinDWARFVersion = 4;
26+
2527
static const char MachOASTSegmentName[] = "__SWIFT";
2628
static const char MachOASTSectionName[] = "__ast";
2729
static const char ELFASTSectionName[] = ".swift_ast";

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ namespace swift {
181181
} else if (Target.isWatchOS()) {
182182
Target.getOSVersion(major, minor, revision);
183183
} else if (Target.isOSLinux() || Target.isOSFreeBSD() ||
184+
Target.isOSWindows() ||
184185
Target.getTriple().empty())
185186
{
186187
major = minor = revision = 0;

include/swift/Basic/Lazy.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ namespace swift {
2727
using OnceToken_t = dispatch_once_t;
2828
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
2929
::dispatch_once_f(&TOKEN, CONTEXT, FUNC)
30+
#elif defined(__CYGWIN__)
31+
// _swift_once_f() is declared in Private.h.
32+
// This prototype is copied instead including the header file.
33+
void _swift_once_f(uintptr_t *predicate, void *context,
34+
void (*function)(void *));
35+
using OnceToken_t = unsigned long;
36+
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
37+
_swift_once_f(&TOKEN, CONTEXT, FUNC)
3038
#else
3139
using OnceToken_t = std::once_flag;
3240
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \

include/swift/Runtime/HeapObject.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ struct TwoWordPair {
8787
// in registers, so cram the result into an unsigned long long.
8888
// Use an enum class with implicit conversions so we don't dirty C callers
8989
// too much.
90-
#if __arm__ || __i386__
90+
#if __arm__ || __i386__ || defined(__CYGWIN__)
91+
#if defined(__CYGWIN__)
92+
enum class Return : unsigned __int128 {};
93+
#else
9194
enum class Return : unsigned long long {};
95+
#endif
9296

9397
operator Return() const {
9498
union {

include/swift/Runtime/Once.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ namespace swift {
2727
// On OS X and iOS, swift_once_t matches dispatch_once_t.
2828
typedef long swift_once_t;
2929

30+
#elif defined(__CYGWIN__)
31+
32+
// On Cygwin, std::once_flag can not be used because it is larger than the
33+
// platform word.
34+
typedef uintptr_t swift_once_t;
3035
#else
3136

3237
// On other platforms swift_once_t is std::once_flag

lib/Basic/Demangle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/StringRef.h"
2424
#include <functional>
2525
#include <vector>
26+
#include <cstdio>
2627
#include <cstdlib>
2728

2829
using namespace swift;

lib/Basic/LangOptions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ static const StringRef SupportedConditionalCompilationOSs[] = {
2929
"watchOS",
3030
"iOS",
3131
"Linux",
32-
"FreeBSD"
32+
"FreeBSD",
33+
"Windows"
3334
};
3435

3536
static const StringRef SupportedConditionalCompilationArches[] = {
@@ -108,6 +109,8 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
108109
addPlatformConditionValue("os", "Linux");
109110
else if (triple.isOSFreeBSD())
110111
addPlatformConditionValue("os", "FreeBSD");
112+
else if (triple.isOSWindows())
113+
addPlatformConditionValue("os", "Windows");
111114
else {
112115
UnsupportedOS = true;
113116
}

lib/Basic/Platform.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
6666

6767
if (triple.isOSFreeBSD())
6868
return "freebsd";
69+
70+
if (triple.isOSWindows())
71+
return "windows";
6972

7073
return "";
7174
}

lib/Basic/Remangle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/Strings.h"
2525
#include "llvm/ADT/StringRef.h"
2626
#include <vector>
27+
#include <cstdio>
2728
#include <cstdlib>
2829
#include <unordered_map>
2930

lib/Basic/TaskQueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using namespace swift;
2323
using namespace swift::sys;
2424

2525
// Include the correct TaskQueue implementation.
26-
#if LLVM_ON_UNIX
26+
#if LLVM_ON_UNIX && !defined(__CYGWIN__)
2727
#include "Unix/TaskQueue.inc"
2828
#else
2929
#include "Default/TaskQueue.inc"

lib/Driver/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,9 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const {
20292029
case llvm::Triple::FreeBSD:
20302030
TC = new toolchains::GenericUnix(*this, Target);
20312031
break;
2032+
case llvm::Triple::Win32:
2033+
TC = new toolchains::Windows(*this, Target);
2034+
break;
20322035
default:
20332036
TC = nullptr;
20342037
}

lib/Driver/ToolChains.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,3 +1241,121 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
12411241
return {"clang++", Arguments};
12421242
}
12431243

1244+
ToolChain::InvocationInfo
1245+
toolchains::Windows::constructInvocation(const InterpretJobAction &job,
1246+
const JobContext &context) const {
1247+
InvocationInfo II = ToolChain::constructInvocation(job, context);
1248+
1249+
SmallString<128> runtimeLibraryPath;
1250+
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, *this);
1251+
1252+
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "LD_LIBRARY_PATH",
1253+
":", options::OPT_L, context.Args,
1254+
runtimeLibraryPath);
1255+
return II;
1256+
}
1257+
1258+
ToolChain::InvocationInfo
1259+
toolchains::Windows::constructInvocation(const AutolinkExtractJobAction &job,
1260+
const JobContext &context) const {
1261+
assert(context.Output.getPrimaryOutputType() == types::TY_AutolinkFile);
1262+
1263+
ArgStringList Arguments;
1264+
addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
1265+
addInputsOfType(Arguments, context.InputActions, types::TY_Object);
1266+
1267+
Arguments.push_back("-o");
1268+
Arguments.push_back(
1269+
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
1270+
1271+
return {"swift-autolink-extract", Arguments};
1272+
}
1273+
1274+
ToolChain::InvocationInfo
1275+
toolchains::Windows::constructInvocation(const LinkJobAction &job,
1276+
const JobContext &context) const {
1277+
const Driver &D = getDriver();
1278+
1279+
assert(context.Output.getPrimaryOutputType() == types::TY_Image &&
1280+
"Invalid linker output type.");
1281+
1282+
ArgStringList Arguments;
1283+
1284+
switch (job.getKind()) {
1285+
case LinkKind::None:
1286+
llvm_unreachable("invalid link kind");
1287+
case LinkKind::Executable:
1288+
// Default case, nothing extra needed
1289+
break;
1290+
case LinkKind::DynamicLibrary:
1291+
Arguments.push_back("-shared");
1292+
break;
1293+
}
1294+
1295+
addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
1296+
addInputsOfType(Arguments, context.InputActions, types::TY_Object);
1297+
1298+
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
1299+
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
1300+
context.Args.AddAllArgs(Arguments, options::OPT_F);
1301+
1302+
if (!context.OI.SDKPath.empty()) {
1303+
Arguments.push_back("--sysroot");
1304+
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
1305+
}
1306+
1307+
// Add the runtime library link path, which is platform-specific and found
1308+
// relative to the compiler.
1309+
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
1310+
// library link path and the standard library module import path don't
1311+
// need to be the same.
1312+
llvm::SmallString<128> RuntimeLibPath;
1313+
1314+
if (const Arg *A = context.Args.getLastArg(options::OPT_resource_dir)) {
1315+
RuntimeLibPath = A->getValue();
1316+
} else {
1317+
RuntimeLibPath = D.getSwiftProgramPath();
1318+
llvm::sys::path::remove_filename(RuntimeLibPath); // remove /swift
1319+
llvm::sys::path::remove_filename(RuntimeLibPath); // remove /bin
1320+
llvm::sys::path::append(RuntimeLibPath, "lib", "swift");
1321+
}
1322+
llvm::sys::path::append(RuntimeLibPath,
1323+
getPlatformNameForTriple(getTriple()));
1324+
Arguments.push_back("-L");
1325+
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
1326+
1327+
if (context.Args.hasArg(options::OPT_profile_generate)) {
1328+
SmallString<128> LibProfile(RuntimeLibPath);
1329+
llvm::sys::path::remove_filename(LibProfile); // remove platform name
1330+
llvm::sys::path::append(LibProfile, "clang", CLANG_VERSION_STRING);
1331+
1332+
llvm::sys::path::append(LibProfile, "lib", getTriple().getOSName(),
1333+
Twine("libclang_rt.profile-") +
1334+
getTriple().getArchName() + ".a");
1335+
Arguments.push_back(context.Args.MakeArgString(LibProfile));
1336+
}
1337+
1338+
// FIXME: We probably shouldn't be adding an rpath here unless we know ahead
1339+
// of time the standard library won't be copied.
1340+
Arguments.push_back("-Xlinker");
1341+
Arguments.push_back("-rpath");
1342+
Arguments.push_back("-Xlinker");
1343+
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
1344+
1345+
// Always add the stdlib
1346+
Arguments.push_back("-lswiftCore");
1347+
1348+
// Add any autolinking scripts to the arguments
1349+
for (const Job *Cmd : context.Inputs) {
1350+
auto &OutputInfo = Cmd->getOutput();
1351+
if (OutputInfo.getPrimaryOutputType() == types::TY_AutolinkFile)
1352+
Arguments.push_back(context.Args.MakeArgString(
1353+
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
1354+
}
1355+
1356+
// This should be the last option, for convenience in checking output.
1357+
Arguments.push_back("-o");
1358+
Arguments.push_back(context.Output.getPrimaryOutputFilename().c_str());
1359+
1360+
return {"clang++", Arguments};
1361+
}

lib/Driver/ToolChains.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
5050
~GenericUnix() = default;
5151
};
5252

53+
class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
54+
protected:
55+
InvocationInfo constructInvocation(const InterpretJobAction &job,
56+
const JobContext &context) const override;
57+
InvocationInfo constructInvocation(const AutolinkExtractJobAction &job,
58+
const JobContext &context) const override;
59+
InvocationInfo constructInvocation(const LinkJobAction &job,
60+
const JobContext &context) const override;
61+
62+
public:
63+
Windows(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
64+
~Windows() = default;
65+
};
66+
5367
} // end namespace toolchains
5468
} // end namespace driver
5569
} // end namespace swift

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
835835
}
836836

837837
if (UnsupportedOS) {
838-
auto TargetArgOS = TargetComponents.size() > 2 ? TargetComponents.back() : "";
838+
auto TargetArgOS = TargetComponents.size() > 2 ? TargetComponents[2] : "";
839839
Diags.diagnose(SourceLoc(), diag::error_unsupported_target_os, TargetArgOS);
840840
}
841841

lib/IDE/ReconstructType.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
#include "swift/IDE/Utils.h"
5757

58+
#include <cstdio>
59+
5860
typedef const std::string ConstString;
5961
typedef void Log;
6062
typedef swift::ASTContext SwiftASTContext;

lib/IRGen/GenDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,9 @@ llvm::Constant *IRGenModule::emitProtocolConformances() {
20272027
case llvm::Triple::ELF:
20282028
sectionName = ".swift2_protocol_conformances";
20292029
break;
2030+
case llvm::Triple::COFF:
2031+
sectionName = ".sw2prtc";
2032+
break;
20302033
default:
20312034
llvm_unreachable("Don't know how to emit protocol conformances for "
20322035
"the selected object format.");
@@ -2108,6 +2111,9 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
21082111
case llvm::Triple::ELF:
21092112
sectionName = ".swift2_type_metadata";
21102113
break;
2114+
case llvm::Triple::COFF:
2115+
sectionName = ".sw2tymd";
2116+
break;
21112117
default:
21122118
llvm_unreachable("Don't know how to emit type metadata table for "
21132119
"the selected object format.");

lib/IRGen/IRGenModule.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static clang::CodeGenerator *createClangCodeGenerator(ASTContext &Context,
9797
}
9898
if (Opts.DebugInfoKind != IRGenDebugInfoKind::None) {
9999
CGO.DebugCompilationDir = Opts.DebugCompilationDir;
100-
CGO.DwarfVersion = swift::DWARFVersion;
100+
CGO.DwarfVersion = Opts.DWARFVersion;
101101
CGO.DwarfDebugFlags = Opts.DWARFDebugFlags;
102102
}
103103

@@ -751,6 +751,7 @@ void IRGenModule::emitAutolinkInfo() {
751751
}
752752
break;
753753
}
754+
case llvm::Triple::COFF:
754755
case llvm::Triple::ELF: {
755756
// Merge the entries into null-separated string.
756757
llvm::SmallString<64> EntriesString;
@@ -840,6 +841,7 @@ void IRGenModule::finalize() {
840841
ModuleHash->setSection("__LLVM,__swift_modhash");
841842
break;
842843
case llvm::Triple::ELF:
844+
case llvm::Triple::COFF:
843845
ModuleHash->setSection(".swift_modhash");
844846
break;
845847
default:

lib/SIL/Projection.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,12 +1472,12 @@ replaceValueUsesWithLeafUses(SILBuilder &Builder, SILLocation Loc,
14721472

14731473
DEBUG(llvm::dbgs() << " Current Worklist:\n");
14741474
#ifndef NDEBUG
1475-
for (auto *_N : Worklist) {
1476-
DEBUG(llvm::dbgs() << " Type: " << _N->getType()
1475+
for (auto *_work : Worklist) {
1476+
DEBUG(llvm::dbgs() << " Type: " << _work->getType()
14771477
<< "; Complete: "
1478-
<< (AggBuilderMap.isComplete(_N)? "yes" : "no")
1478+
<< (AggBuilderMap.isComplete(_work)? "yes" : "no")
14791479
<< "; Invalidated: "
1480-
<< (AggBuilderMap.isInvalidated(_N)? "yes" : "no") << "\n");
1480+
<< (AggBuilderMap.isInvalidated(_work)? "yes" : "no") << "\n");
14811481
}
14821482
#endif
14831483

0 commit comments

Comments
 (0)