Skip to content

Commit ba8a669

Browse files
committed
Porting to Cygwin
1 parent 51ee08e commit ba8a669

37 files changed

+482
-29
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cmake_minimum_required(VERSION 2.8.12)
2+
cmake_policy(SET CMP0054 NEW)
23

34
# Add path for custom CMake modules.
45
list(APPEND CMAKE_MODULE_PATH
@@ -437,6 +438,19 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
437438

438439
set(SWIFT_PRIMARY_VARIANT_SDK_default "FREEBSD")
439440
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
441+
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
442+
configure_sdk_unix(CYGWIN "Cygwin" "windows" "cygwin" "x86_64" "x86_64-unknown-windows-cygnus")
443+
444+
# set(CMAKE_EXECUTABLE_FORMAT "ELF")
445+
446+
set(SWIFT_HOST_VARIANT "windows" CACHE STRING
447+
"Deployment OS for Swift host tools (the compiler) [windows].")
448+
449+
set(SWIFT_HOST_VARIANT_SDK "CYGWIN")
450+
set(SWIFT_HOST_VARIANT_ARCH "x86_64")
451+
452+
set(SWIFT_PRIMARY_VARIANT_SDK_default "CYGWIN")
453+
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
440454
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
441455
# Set defaults.
442456

cmake/modules/AddSwift.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ function(_add_variant_link_flags
176176
list(APPEND result "-lpthread" "-ldl" "-Wl,-Bsymbolic")
177177
elseif("${sdk}" STREQUAL "FREEBSD")
178178
list(APPEND result "-lpthread" "-Wl,-Bsymbolic")
179+
elseif("${sdk}" STREQUAL "CYGWIN")
180+
# No extra libraries required.
179181
else()
180182
list(APPEND result "-lobjc")
181183
endif()

include/swift/Basic/Dwarf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121

2222
namespace swift {
2323
/// The DWARF version emitted by the Swift compiler.
24+
#if defined(__CYGWIN__)
25+
const unsigned DWARFVersion = 4;
26+
#else
2427
const unsigned DWARFVersion = 3;
28+
#endif
2529
static const char MachOASTSegmentName[] = "__SWIFT";
2630
static const char MachOASTSectionName[] = "__ast";
2731
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
@@ -173,6 +173,7 @@ namespace swift {
173173
} else if (Target.isWatchOS()) {
174174
Target.getOSVersion(major, minor, revision);
175175
} else if (Target.isOSLinux() || Target.isOSFreeBSD() ||
176+
Target.isOSWindows() ||
176177
Target.getTriple().empty())
177178
{
178179
major = minor = revision = 0;

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 unsigned long 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 @@ const std::vector<std::string> LangOptions::SupportedOSBuildConfigArguments = {
2929
"watchOS",
3030
"iOS",
3131
"Linux",
32-
"FreeBSD"
32+
"FreeBSD",
33+
"Windows"
3334
};
3435

3536
const std::vector<std::string> LangOptions::SupportedArchBuildConfigArguments = {
@@ -103,6 +104,8 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
103104
addTargetConfigOption("os", "Linux");
104105
else if (triple.isOSFreeBSD())
105106
addTargetConfigOption("os", "FreeBSD");
107+
else if (triple.isOSWindows())
108+
addTargetConfigOption("os", "Windows");
106109
else {
107110
UnsupportedOS = true;
108111
}

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
@@ -2031,6 +2031,9 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const {
20312031
case llvm::Triple::FreeBSD:
20322032
TC = new toolchains::GenericUnix(*this, Target);
20332033
break;
2034+
case llvm::Triple::Win32:
2035+
TC = new toolchains::Windows(*this, Target);
2036+
break;
20342037
default:
20352038
TC = nullptr;
20362039
}

lib/Driver/ToolChains.cpp

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

1177+
ToolChain::InvocationInfo
1178+
toolchains::Windows::constructInvocation(const InterpretJobAction &job,
1179+
const JobContext &context) const {
1180+
InvocationInfo II = ToolChain::constructInvocation(job, context);
1181+
1182+
SmallString<128> runtimeLibraryPath;
1183+
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, *this);
1184+
1185+
addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "LD_LIBRARY_PATH",
1186+
":", options::OPT_L, context.Args,
1187+
runtimeLibraryPath);
1188+
return II;
1189+
}
1190+
1191+
ToolChain::InvocationInfo
1192+
toolchains::Windows::constructInvocation(const AutolinkExtractJobAction &job,
1193+
const JobContext &context) const {
1194+
assert(context.Output.getPrimaryOutputType() == types::TY_AutolinkFile);
1195+
1196+
ArgStringList Arguments;
1197+
addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
1198+
addInputsOfType(Arguments, context.InputActions, types::TY_Object);
1199+
1200+
Arguments.push_back("-o");
1201+
Arguments.push_back(
1202+
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
1203+
1204+
return {"swift-autolink-extract", Arguments};
1205+
}
1206+
1207+
ToolChain::InvocationInfo
1208+
toolchains::Windows::constructInvocation(const LinkJobAction &job,
1209+
const JobContext &context) const {
1210+
const Driver &D = getDriver();
1211+
1212+
assert(context.Output.getPrimaryOutputType() == types::TY_Image &&
1213+
"Invalid linker output type.");
1214+
1215+
ArgStringList Arguments;
1216+
1217+
switch (job.getKind()) {
1218+
case LinkKind::None:
1219+
llvm_unreachable("invalid link kind");
1220+
case LinkKind::Executable:
1221+
// Default case, nothing extra needed
1222+
break;
1223+
case LinkKind::DynamicLibrary:
1224+
Arguments.push_back("-shared");
1225+
break;
1226+
}
1227+
1228+
addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
1229+
addInputsOfType(Arguments, context.InputActions, types::TY_Object);
1230+
1231+
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
1232+
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
1233+
context.Args.AddAllArgs(Arguments, options::OPT_F);
1234+
1235+
if (!context.OI.SDKPath.empty()) {
1236+
Arguments.push_back("--sysroot");
1237+
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
1238+
}
1239+
1240+
// Add the runtime library link path, which is platform-specific and found
1241+
// relative to the compiler.
1242+
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
1243+
// library link path and the standard library module import path don't
1244+
// need to be the same.
1245+
llvm::SmallString<128> RuntimeLibPath;
1246+
1247+
if (const Arg *A = context.Args.getLastArg(options::OPT_resource_dir)) {
1248+
RuntimeLibPath = A->getValue();
1249+
} else {
1250+
RuntimeLibPath = D.getSwiftProgramPath();
1251+
llvm::sys::path::remove_filename(RuntimeLibPath); // remove /swift
1252+
llvm::sys::path::remove_filename(RuntimeLibPath); // remove /bin
1253+
llvm::sys::path::append(RuntimeLibPath, "lib", "swift");
1254+
}
1255+
llvm::sys::path::append(RuntimeLibPath,
1256+
getPlatformNameForTriple(getTriple()));
1257+
Arguments.push_back("-L");
1258+
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
1259+
1260+
if (context.Args.hasArg(options::OPT_profile_generate)) {
1261+
SmallString<128> LibProfile(RuntimeLibPath);
1262+
llvm::sys::path::remove_filename(LibProfile); // remove platform name
1263+
llvm::sys::path::append(LibProfile, "clang", CLANG_VERSION_STRING);
1264+
1265+
llvm::sys::path::append(LibProfile, "lib", getTriple().getOSName(),
1266+
Twine("libclang_rt.profile-") +
1267+
getTriple().getArchName() + ".a");
1268+
Arguments.push_back(context.Args.MakeArgString(LibProfile));
1269+
}
1270+
1271+
// FIXME: We probably shouldn't be adding an rpath here unless we know ahead
1272+
// of time the standard library won't be copied.
1273+
Arguments.push_back("-Xlinker");
1274+
Arguments.push_back("-rpath");
1275+
Arguments.push_back("-Xlinker");
1276+
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
1277+
1278+
// Always add the stdlib
1279+
Arguments.push_back("-lswiftCore");
1280+
1281+
// Add any autolinking scripts to the arguments
1282+
for (const Job *Cmd : context.Inputs) {
1283+
auto &OutputInfo = Cmd->getOutput();
1284+
if (OutputInfo.getPrimaryOutputType() == types::TY_AutolinkFile)
1285+
Arguments.push_back(context.Args.MakeArgString(
1286+
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
1287+
}
1288+
1289+
// This should be the last option, for convenience in checking output.
1290+
Arguments.push_back("-o");
1291+
Arguments.push_back(context.Output.getPrimaryOutputFilename().c_str());
1292+
1293+
return {"clang++", Arguments};
1294+
}

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
@@ -811,7 +811,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
811811
}
812812

813813
if (UnsupportedOS) {
814-
auto TargetArgOS = TargetComponents.size() > 2 ? TargetComponents.back() : "";
814+
auto TargetArgOS = TargetComponents.size() > 2 ? TargetComponents[2] : "";
815815
Diags.diagnose(SourceLoc(), diag::error_unsupported_target_os, TargetArgOS);
816816
}
817817

lib/IRGen/GenDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,9 @@ llvm::Constant *IRGenModule::emitProtocolConformances() {
19661966
case llvm::Triple::ELF:
19671967
sectionName = ".swift2_protocol_conformances";
19681968
break;
1969+
case llvm::Triple::COFF:
1970+
sectionName = ".sw2prtc";
1971+
break;
19691972
default:
19701973
llvm_unreachable("Don't know how to emit protocol conformances for "
19711974
"the selected object format.");

lib/IRGen/IRGenModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ void IRGenModule::emitAutolinkInfo() {
723723
}
724724
break;
725725
}
726+
case llvm::Triple::COFF:
726727
case llvm::Triple::ELF: {
727728
// Merge the entries into null-separated string.
728729
llvm::SmallString<64> EntriesString;

lib/SIL/Projection.cpp

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

21042104
DEBUG(llvm::dbgs() << " Current Worklist:\n");
21052105
#ifndef NDEBUG
2106-
for (auto *_N : Worklist) {
2107-
DEBUG(llvm::dbgs() << " Type: " << _N->getType()
2106+
for (auto *_work : Worklist) {
2107+
DEBUG(llvm::dbgs() << " Type: " << _work->getType()
21082108
<< "; Complete: "
2109-
<< (AggBuilderMap.isComplete(_N)? "yes" : "no")
2109+
<< (AggBuilderMap.isComplete(_work)? "yes" : "no")
21102110
<< "; Invalidated: "
2111-
<< (AggBuilderMap.isInvalidated(_N)? "yes" : "no") << "\n");
2111+
<< (AggBuilderMap.isInvalidated(_work)? "yes" : "no") << "\n");
21122112
}
21132113
#endif
21142114

lib/Sema/PlaygroundTransform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/Subsystems.h"
3030
#include "TypeChecker.h"
3131

32+
#include <cstdio>
3233
#include <forward_list>
3334
#include <random>
3435

stdlib/public/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ if(CXX_SUPPORTS_EXIT_TIME_DESTRUCTORS_WARNING)
1212
list(APPEND SWIFT_CORE_CXX_FLAGS "-Wexit-time-destructors")
1313
endif()
1414

15+
if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
16+
list(APPEND SWIFT_CORE_CXX_FLAGS "-mcmodel=large")
17+
endif()
18+
1519
if(SWIFT_BUILD_STDLIB)
1620
# These must be kept in dependency order so that any referenced targets
1721
# exist at the time we look for them in add_swift_*.

stdlib/public/core/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
165165
${BSD_LIBRARIES})
166166
endif()
167167

168+
if(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
169+
execute_process(COMMAND "cygpath" "-u" "$ENV{SYSTEMROOT}"
170+
OUTPUT_VARIABLE ENV_SYSTEMROOT)
171+
172+
list(APPEND swift_core_link_flags "${ENV_SYSTEMROOT}/system32/psapi.dll")
173+
endif()
174+
168175
option(SWIFT_CHECK_ESSENTIAL_STDLIB
169176
"Check core standard library layering by linking its essential subset"
170177
FALSE)

stdlib/public/core/Print.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public func print(
2828
separator: String = " ",
2929
terminator: String = "\n"
3030
) {
31+
#if os(Windows)
32+
// FIXME: This fix is for 'crash at hook(output.left)' in cygwin.
33+
// Proper fix is needed. see: https://bugs.swift.org/browse/SR-612
34+
let _playgroundPrintHook : ((String)->Void)? = nil
35+
#endif
3136
if let hook = _playgroundPrintHook {
3237
var output = _TeeStream(left: "", right: _Stdout())
3338
_print(

stdlib/public/runtime/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
3232
else()
3333
endif()
3434

35+
set(swift_runtime_port_sources)
36+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
37+
set(swift_runtime_port_sources
38+
CygwinPort.cpp)
39+
endif()
40+
3541
add_swift_library(swiftRuntime IS_STDLIB IS_STDLIB_CORE
3642
Casting.cpp
3743
Demangle.cpp
@@ -49,6 +55,7 @@ add_swift_library(swiftRuntime IS_STDLIB IS_STDLIB_CORE
4955
SwiftObject.cpp
5056
${swift_runtime_objc_sources}
5157
${swift_runtime_leaks_sources}
58+
${swift_runtime_port_sources}
5259
C_COMPILE_FLAGS ${swift_runtime_compile_flags}
5360
INSTALL_IN_COMPONENT stdlib)
5461

0 commit comments

Comments
 (0)