Skip to content

Porting to cygwin #1108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,19 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")

set(SWIFT_PRIMARY_VARIANT_SDK_default "FREEBSD")
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
configure_sdk_unix(CYGWIN "Cygwin" "windows" "cygwin" "x86_64" "x86_64-unknown-windows-cygnus")

# set(CMAKE_EXECUTABLE_FORMAT "ELF")

set(SWIFT_HOST_VARIANT "windows" CACHE STRING
"Deployment OS for Swift host tools (the compiler) [windows].")

set(SWIFT_HOST_VARIANT_SDK "CYGWIN")
set(SWIFT_HOST_VARIANT_ARCH "x86_64")

set(SWIFT_PRIMARY_VARIANT_SDK_default "CYGWIN")
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
# Set defaults.

Expand Down
6 changes: 6 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ function(_add_variant_link_flags
list(APPEND result "-lpthread" "-ldl")
elseif("${sdk}" STREQUAL "FREEBSD")
list(APPEND result "-lpthread")
elseif("${sdk}" STREQUAL "CYGWIN")
# No extra libraries required.
else()
list(APPEND result "-lobjc")
endif()
Expand Down Expand Up @@ -967,6 +969,10 @@ function(_add_swift_library_single target name)
set_target_properties("${target}"
PROPERTIES
INSTALL_RPATH "$ORIGIN:/usr/lib/swift/linux")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Cygwin")
set_target_properties("${target}"
PROPERTIES
INSTALL_RPATH "$ORIGIN:/usr/lib/swift/windows")
endif()

set_target_properties("${target}" PROPERTIES BUILD_WITH_INSTALL_RPATH YES)
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class IRGenOptions {
/// The compilation directory for the debug info.
std::string DebugCompilationDir;

/// The DWARF version of debug info.
unsigned DWARFVersion;

/// The command line string that is to be stored in the DWARF debug info.
std::string DWARFDebugFlags;

Expand Down
4 changes: 3 additions & 1 deletion include/swift/Basic/Dwarf.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

namespace swift {
/// The DWARF version emitted by the Swift compiler.
const unsigned DWARFVersion = 3;
const unsigned GenericDWARFVersion = 3;
const unsigned CygwinDWARFVersion = 4;

static const char MachOASTSegmentName[] = "__SWIFT";
static const char MachOASTSectionName[] = "__ast";
static const char ELFASTSectionName[] = ".swift_ast";
Expand Down
1 change: 1 addition & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ namespace swift {
} else if (Target.isWatchOS()) {
Target.getOSVersion(major, minor, revision);
} else if (Target.isOSLinux() || Target.isOSFreeBSD() ||
Target.isOSWindows() ||
Target.getTriple().empty())
{
major = minor = revision = 0;
Expand Down
8 changes: 8 additions & 0 deletions include/swift/Basic/Lazy.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ namespace swift {
using OnceToken_t = dispatch_once_t;
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
::dispatch_once_f(&TOKEN, CONTEXT, FUNC)
#elif defined(__CYGWIN__)
// _swift_once_f() is declared in Private.h.
// This prototype is copied instead including the header file.
void _swift_once_f(uintptr_t *predicate, void *context,
void (*function)(void *));
using OnceToken_t = unsigned long;
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
_swift_once_f(&TOKEN, CONTEXT, FUNC)
#else
using OnceToken_t = std::once_flag;
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
Expand Down
6 changes: 5 additions & 1 deletion include/swift/Runtime/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ struct TwoWordPair {
// in registers, so cram the result into an unsigned long long.
// Use an enum class with implicit conversions so we don't dirty C callers
// too much.
#if __arm__ || __i386__
#if __arm__ || __i386__ || defined(__CYGWIN__)
#if defined(__CYGWIN__)
enum class Return : unsigned __int128 {};
#else
enum class Return : unsigned long long {};
#endif

operator Return() const {
union {
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Runtime/Once.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ namespace swift {
// On OS X and iOS, swift_once_t matches dispatch_once_t.
typedef long swift_once_t;

#elif defined(__CYGWIN__)

// On Cygwin, std::once_flag can not be used because it is larger than the
// platform word.
typedef uintptr_t swift_once_t;
#else

// On other platforms swift_once_t is std::once_flag
Expand Down
1 change: 1 addition & 0 deletions lib/Basic/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "llvm/ADT/StringRef.h"
#include <functional>
#include <vector>
#include <cstdio>
#include <cstdlib>

using namespace swift;
Expand Down
5 changes: 4 additions & 1 deletion lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ static const StringRef SupportedConditionalCompilationOSs[] = {
"watchOS",
"iOS",
"Linux",
"FreeBSD"
"FreeBSD",
"Windows"
};

static const StringRef SupportedConditionalCompilationArches[] = {
Expand Down Expand Up @@ -108,6 +109,8 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
addPlatformConditionValue("os", "Linux");
else if (triple.isOSFreeBSD())
addPlatformConditionValue("os", "FreeBSD");
else if (triple.isOSWindows())
addPlatformConditionValue("os", "Windows");
else {
UnsupportedOS = true;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Basic/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {

if (triple.isOSFreeBSD())
return "freebsd";

if (triple.isOSWindows())
return "windows";

return "";
}
1 change: 1 addition & 0 deletions lib/Basic/Remangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "swift/Strings.h"
#include "llvm/ADT/StringRef.h"
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <unordered_map>

Expand Down
2 changes: 1 addition & 1 deletion lib/Basic/TaskQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using namespace swift;
using namespace swift::sys;

// Include the correct TaskQueue implementation.
#if LLVM_ON_UNIX
#if LLVM_ON_UNIX && !defined(__CYGWIN__)
#include "Unix/TaskQueue.inc"
#else
#include "Default/TaskQueue.inc"
Expand Down
3 changes: 3 additions & 0 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,9 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const {
case llvm::Triple::FreeBSD:
TC = new toolchains::GenericUnix(*this, Target);
break;
case llvm::Triple::Win32:
TC = new toolchains::Windows(*this, Target);
break;
default:
TC = nullptr;
}
Expand Down
118 changes: 118 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,3 +1241,121 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
return {"clang++", Arguments};
}

ToolChain::InvocationInfo
toolchains::Windows::constructInvocation(const InterpretJobAction &job,
const JobContext &context) const {
InvocationInfo II = ToolChain::constructInvocation(job, context);

SmallString<128> runtimeLibraryPath;
getRuntimeLibraryPath(runtimeLibraryPath, context.Args, *this);

addPathEnvironmentVariableIfNeeded(II.ExtraEnvironment, "LD_LIBRARY_PATH",
":", options::OPT_L, context.Args,
runtimeLibraryPath);
return II;
}

ToolChain::InvocationInfo
toolchains::Windows::constructInvocation(const AutolinkExtractJobAction &job,
const JobContext &context) const {
assert(context.Output.getPrimaryOutputType() == types::TY_AutolinkFile);

ArgStringList Arguments;
addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
addInputsOfType(Arguments, context.InputActions, types::TY_Object);

Arguments.push_back("-o");
Arguments.push_back(
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));

return {"swift-autolink-extract", Arguments};
}

ToolChain::InvocationInfo
toolchains::Windows::constructInvocation(const LinkJobAction &job,
const JobContext &context) const {
const Driver &D = getDriver();

assert(context.Output.getPrimaryOutputType() == types::TY_Image &&
"Invalid linker output type.");

ArgStringList Arguments;

switch (job.getKind()) {
case LinkKind::None:
llvm_unreachable("invalid link kind");
case LinkKind::Executable:
// Default case, nothing extra needed
break;
case LinkKind::DynamicLibrary:
Arguments.push_back("-shared");
break;
}

addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
addInputsOfType(Arguments, context.InputActions, types::TY_Object);

context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
context.Args.AddAllArgs(Arguments, options::OPT_F);

if (!context.OI.SDKPath.empty()) {
Arguments.push_back("--sysroot");
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
}

// Add the runtime library link path, which is platform-specific and found
// relative to the compiler.
// FIXME: Duplicated from CompilerInvocation, but in theory the runtime
// library link path and the standard library module import path don't
// need to be the same.
llvm::SmallString<128> RuntimeLibPath;

if (const Arg *A = context.Args.getLastArg(options::OPT_resource_dir)) {
RuntimeLibPath = A->getValue();
} else {
RuntimeLibPath = D.getSwiftProgramPath();
llvm::sys::path::remove_filename(RuntimeLibPath); // remove /swift
llvm::sys::path::remove_filename(RuntimeLibPath); // remove /bin
llvm::sys::path::append(RuntimeLibPath, "lib", "swift");
}
llvm::sys::path::append(RuntimeLibPath,
getPlatformNameForTriple(getTriple()));
Arguments.push_back("-L");
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));

if (context.Args.hasArg(options::OPT_profile_generate)) {
SmallString<128> LibProfile(RuntimeLibPath);
llvm::sys::path::remove_filename(LibProfile); // remove platform name
llvm::sys::path::append(LibProfile, "clang", CLANG_VERSION_STRING);

llvm::sys::path::append(LibProfile, "lib", getTriple().getOSName(),
Twine("libclang_rt.profile-") +
getTriple().getArchName() + ".a");
Arguments.push_back(context.Args.MakeArgString(LibProfile));
}

// FIXME: We probably shouldn't be adding an rpath here unless we know ahead
// of time the standard library won't be copied.
Arguments.push_back("-Xlinker");
Arguments.push_back("-rpath");
Arguments.push_back("-Xlinker");
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));

// Always add the stdlib
Arguments.push_back("-lswiftCore");

// Add any autolinking scripts to the arguments
for (const Job *Cmd : context.Inputs) {
auto &OutputInfo = Cmd->getOutput();
if (OutputInfo.getPrimaryOutputType() == types::TY_AutolinkFile)
Arguments.push_back(context.Args.MakeArgString(
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
}

// This should be the last option, for convenience in checking output.
Arguments.push_back("-o");
Arguments.push_back(context.Output.getPrimaryOutputFilename().c_str());

return {"clang++", Arguments};
}
14 changes: 14 additions & 0 deletions lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
~GenericUnix() = default;
};

class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
protected:
InvocationInfo constructInvocation(const InterpretJobAction &job,
const JobContext &context) const override;
InvocationInfo constructInvocation(const AutolinkExtractJobAction &job,
const JobContext &context) const override;
InvocationInfo constructInvocation(const LinkJobAction &job,
const JobContext &context) const override;

public:
Windows(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
~Windows() = default;
};

} // end namespace toolchains
} // end namespace driver
} // end namespace swift
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
}

if (UnsupportedOS) {
auto TargetArgOS = TargetComponents.size() > 2 ? TargetComponents.back() : "";
auto TargetArgOS = TargetComponents.size() > 2 ? TargetComponents[2] : "";
Diags.diagnose(SourceLoc(), diag::error_unsupported_target_os, TargetArgOS);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/IDE/ReconstructType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

#include "swift/IDE/Utils.h"

#include <cstdio>

typedef const std::string ConstString;
typedef void Log;
typedef swift::ASTContext SwiftASTContext;
Expand Down
6 changes: 6 additions & 0 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,9 @@ llvm::Constant *IRGenModule::emitProtocolConformances() {
case llvm::Triple::ELF:
sectionName = ".swift2_protocol_conformances";
break;
case llvm::Triple::COFF:
sectionName = ".sw2prtc";
break;
default:
llvm_unreachable("Don't know how to emit protocol conformances for "
"the selected object format.");
Expand Down Expand Up @@ -2108,6 +2111,9 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords() {
case llvm::Triple::ELF:
sectionName = ".swift2_type_metadata";
break;
case llvm::Triple::COFF:
sectionName = ".sw2tymd";
break;
default:
llvm_unreachable("Don't know how to emit type metadata table for "
"the selected object format.");
Expand Down
4 changes: 3 additions & 1 deletion lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static clang::CodeGenerator *createClangCodeGenerator(ASTContext &Context,
}
if (Opts.DebugInfoKind != IRGenDebugInfoKind::None) {
CGO.DebugCompilationDir = Opts.DebugCompilationDir;
CGO.DwarfVersion = swift::DWARFVersion;
CGO.DwarfVersion = Opts.DWARFVersion;
CGO.DwarfDebugFlags = Opts.DWARFDebugFlags;
}

Expand Down Expand Up @@ -751,6 +751,7 @@ void IRGenModule::emitAutolinkInfo() {
}
break;
}
case llvm::Triple::COFF:
case llvm::Triple::ELF: {
// Merge the entries into null-separated string.
llvm::SmallString<64> EntriesString;
Expand Down Expand Up @@ -840,6 +841,7 @@ void IRGenModule::finalize() {
ModuleHash->setSection("__LLVM,__swift_modhash");
break;
case llvm::Triple::ELF:
case llvm::Triple::COFF:
ModuleHash->setSection(".swift_modhash");
break;
default:
Expand Down
8 changes: 4 additions & 4 deletions lib/SIL/Projection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,12 +1472,12 @@ replaceValueUsesWithLeafUses(SILBuilder &Builder, SILLocation Loc,

DEBUG(llvm::dbgs() << " Current Worklist:\n");
#ifndef NDEBUG
for (auto *_N : Worklist) {
DEBUG(llvm::dbgs() << " Type: " << _N->getType()
for (auto *_work : Worklist) {
DEBUG(llvm::dbgs() << " Type: " << _work->getType()
<< "; Complete: "
<< (AggBuilderMap.isComplete(_N)? "yes" : "no")
<< (AggBuilderMap.isComplete(_work)? "yes" : "no")
<< "; Invalidated: "
<< (AggBuilderMap.isInvalidated(_N)? "yes" : "no") << "\n");
<< (AggBuilderMap.isInvalidated(_work)? "yes" : "no") << "\n");
}
#endif

Expand Down
Loading