Skip to content

[Toolchains] Cygwin toolchain inherits from Unix #1908

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 1 commit into from
Apr 6, 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
2 changes: 1 addition & 1 deletion lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const {
TC = new toolchains::GenericUnix(*this, Target);
break;
case llvm::Triple::Win32:
TC = new toolchains::Windows(*this, Target);
TC = new toolchains::Cygwin(*this, Target);
break;
default:
TC = nullptr;
Expand Down
243 changes: 82 additions & 161 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,11 +1190,52 @@ getSectionMagicArch(const llvm::Triple &Triple) {
}
}

std::string toolchains::GenericUnix::getDefaultLinker() const {
switch(getTriple().getArch()) {
case llvm::Triple::arm:
case llvm::Triple::armeb:
case llvm::Triple::thumb:
case llvm::Triple::thumbeb:
// BFD linker has issues wrt relocation of the protocol conformance
// section on these targets, it also generates COPY relocations for
// final executables, as such, unless specified, we default to gold
// linker.
return "gold";
default:
// Otherwise, use the default BFD linker.
return "";
}
}

bool toolchains::GenericUnix::shouldProvideRPathToLinker() const {
return true;
}

bool toolchains::GenericUnix::shouldSpecifyTargetTripleToLinker() const {
return true;
}

std::string toolchains::GenericUnix::getPreInputObjectPath(
StringRef RuntimeLibraryPath) const {
// On Linux and FreeBSD (really, ELF binaries) we need to add objects
// to provide markers and size for the metadata sections.
SmallString<128> PreInputObjectPath = RuntimeLibraryPath;
llvm::sys::path::append(PreInputObjectPath, getSectionMagicArch(getTriple()));
llvm::sys::path::append(PreInputObjectPath, "swift_begin.o");
return PreInputObjectPath.str();
}

std::string toolchains::GenericUnix::getPostInputObjectPath(
StringRef RuntimeLibraryPath) const {
SmallString<128> PostInputObjectPath = RuntimeLibraryPath;
llvm::sys::path::append(PostInputObjectPath, getSectionMagicArch(getTriple()));
llvm::sys::path::append(PostInputObjectPath, "swift_end.o");
return PostInputObjectPath.str();
}

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

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

Expand All @@ -1212,52 +1253,38 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
}

// Select the linker to use
StringRef Linker;

std::string Linker;
if (const Arg *A = context.Args.getLastArg(options::OPT_use_ld)) {
Linker = A->getValue();
} else {
switch(getTriple().getArch()) {
default:
break;
case llvm::Triple::arm:
case llvm::Triple::armeb:
case llvm::Triple::thumb:
case llvm::Triple::thumbeb:
// BFD linker has issues wrt relocation of the protocol conformance
// section on these targets, it also generates COPY relocations for
// final executables, as such, unless specified, we default to gold
// linker.
Linker = "gold";
}
Linker = getDefaultLinker();
}

if (!Linker.empty()) {
Arguments.push_back(context.Args.MakeArgString("-fuse-ld=" + Linker));
}

// Explicitly pass the target to the linker
if (shouldSpecifyTargetTripleToLinker()) {
Arguments.push_back(context.Args.MakeArgString("--target=" + getTriple().str()));
}

// 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");
getRuntimeLibraryPath(RuntimeLibPath, context.Args, *this);
if (shouldProvideRPathToLinker()) {
// 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));
}
llvm::sys::path::append(RuntimeLibPath,
getPlatformNameForTriple(getTriple()));

// On Linux and FreeBSD (really, ELF binaries) we need to add objects
// to provide markers and size for the metadata sections.
Arguments.push_back(context.Args.MakeArgString(
Twine(RuntimeLibPath) + "/" + getSectionMagicArch(getTriple()) + "/swift_begin.o"));
auto PreInputObjectPath = getPreInputObjectPath(RuntimeLibPath);
if (!PreInputObjectPath.empty()) {
Arguments.push_back(context.Args.MakeArgString(PreInputObjectPath));
}
addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
addInputsOfType(Arguments, context.InputActions, types::TY_Object);

Expand All @@ -1273,9 +1300,6 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Arguments.push_back("-L");
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));

// Explicitly pass the target to the linker
Arguments.push_back(context.Args.MakeArgString("--target=" + getTriple().str()));

if (context.Args.hasArg(options::OPT_profile_generate)) {
SmallString<128> LibProfile(RuntimeLibPath);
llvm::sys::path::remove_filename(LibProfile); // remove platform name
Expand All @@ -1288,13 +1312,6 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
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");

Expand All @@ -1306,10 +1323,12 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
}

// It is important that swift_end.o be the last object on the link line
// therefore, it is included just before the output filename.
Arguments.push_back(context.Args.MakeArgString(
Twine(RuntimeLibPath) + "/" + getSectionMagicArch(getTriple()) + "/swift_end.o"));
// Just before the output option, allow GenericUnix toolchains to add
// additional inputs.
auto PostInputObjectPath = getPostInputObjectPath(RuntimeLibPath);
if (!PostInputObjectPath.empty()) {
Arguments.push_back(context.Args.MakeArgString(PostInputObjectPath));
}

// This should be the last option, for convenience in checking output.
Arguments.push_back("-o");
Expand All @@ -1318,121 +1337,23 @@ 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;
std::string toolchains::Cygwin::getDefaultLinker() const {
// Cygwin uses the default BFD linker, even on ARM.
return "";
}

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};
bool toolchains::Cygwin::shouldSpecifyTargetTripleToLinker() const {
return false;
}

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());
std::string toolchains::Cygwin::getPreInputObjectPath(
StringRef RuntimeLibraryPath) const {
// Cygwin does not add "begin" and "end" objects.
return "";
}

return {"clang++", Arguments};
std::string toolchains::Cygwin::getPostInputObjectPath(
StringRef RuntimeLibraryPath) const {
// Cygwin does not add "begin" and "end" objects.
return "";
}
33 changes: 24 additions & 9 deletions lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
const JobContext &context) const override;
InvocationInfo constructInvocation(const AutolinkExtractJobAction &job,
const JobContext &context) const override;

virtual std::string getDefaultLinker() const;

virtual bool shouldProvideRPathToLinker() const;

virtual bool shouldSpecifyTargetTripleToLinker() const;

virtual std::string
getPreInputObjectPath(StringRef RuntimeLibraryPath) const;

virtual std::string
getPostInputObjectPath(StringRef RuntimeLibraryPath) const;

InvocationInfo constructInvocation(const LinkJobAction &job,
const JobContext &context) const override;

Expand All @@ -50,18 +63,20 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
~GenericUnix() = default;
};

class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
class LLVM_LIBRARY_VISIBILITY Cygwin : public GenericUnix {
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;
std::string getDefaultLinker() const override;

bool shouldSpecifyTargetTripleToLinker() const override;

std::string getPreInputObjectPath(
StringRef RuntimeLibraryPath) const override;

std::string getPostInputObjectPath(
StringRef RuntimeLibraryPath) const override;
public:
Windows(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
~Windows() = default;
Cygwin(const Driver &D, const llvm::Triple &Triple) : GenericUnix(D, Triple) {}
~Cygwin() = default;
};

} // end namespace toolchains
Expand Down