Skip to content
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

Fix for #3374 #3475

Merged
merged 2 commits into from
Aug 15, 2020
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
9 changes: 8 additions & 1 deletion driver/linker-gcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,14 @@ void ArgsBuilder::build(llvm::StringRef outputPath,
}
}

addDefaultPlatformLibs();
const auto explicitPlatformLibs = getExplicitPlatformLibs();
if (explicitPlatformLibs.hasValue()) {
for (const auto &name : explicitPlatformLibs.getValue()) {
args.push_back("-l" + name);
}
} else {
addDefaultPlatformLibs();
}

addTargetFlags();
}
Expand Down
35 changes: 18 additions & 17 deletions driver/linker-msvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,24 @@ int linkObjToBinaryMSVC(llvm::StringRef outputPath,
addSwitch(ls);
}

// default platform libs
// TODO check which libaries are necessary
args.push_back("kernel32.lib");
args.push_back("user32.lib");
args.push_back("gdi32.lib");
args.push_back("winspool.lib");
args.push_back("shell32.lib"); // required for dmain2.d
args.push_back("ole32.lib");
args.push_back("oleaut32.lib");
args.push_back("uuid.lib");
args.push_back("comdlg32.lib");
args.push_back("advapi32.lib");

// these get pulled in by druntime (rt/msvc.c); include explicitly for
// -betterC convenience (issue #3035)
args.push_back("oldnames.lib");
args.push_back("legacy_stdio_definitions.lib");
auto explicitPlatformLibs = getExplicitPlatformLibs();
if (explicitPlatformLibs.hasValue()) {
for (auto &lib : explicitPlatformLibs.getValue()) {
args.push_back(llvm::sys::path::has_extension(lib) ? std::move(lib)
: lib + ".lib");
}
} else {
// default platform libs
// TODO check which libaries are necessary
args.insert(args.end(),
{"kernel32.lib", "user32.lib", "gdi32.lib", "winspool.lib",
"shell32.lib", // required for dmain2.d
"ole32.lib", "oleaut32.lib", "uuid.lib", "comdlg32.lib",
"advapi32.lib",
// these get pulled in by druntime (rt/msvc.c); include
// explicitly for -betterC convenience (issue #3035)
"oldnames.lib", "legacy_stdio_definitions.lib"});
}

Logger::println("Linking with: ");
Stream logstr = Logger::cout();
Expand Down
63 changes: 46 additions & 17 deletions driver/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ static cl::opt<bool> linkInternally("link-internally", cl::ZeroOrMore,
constexpr bool linkInternally = false;
#endif

static cl::opt<std::string> platformLib(
"platformlib", cl::ZeroOrMore, cl::value_desc("lib1,lib2,..."),
cl::desc("Platform libraries to link with (overrides previous)"),
cl::cat(opts::linkingCategory));

static cl::opt<bool> noDefaultLib(
"nodefaultlib", cl::ZeroOrMore, cl::Hidden,
cl::desc("Don't add a default library for linking implicitly"));
Expand Down Expand Up @@ -136,6 +141,26 @@ static std::string getOutputName() {

//////////////////////////////////////////////////////////////////////////////

static std::vector<std::string>
parseLibNames(llvm::StringRef commaSeparatedList, llvm::StringRef suffix = {}) {
std::vector<std::string> result;

std::stringstream list(commaSeparatedList);
while (list.good()) {
std::string lib;
std::getline(list, lib, ',');
if (lib.empty()) {
continue;
}

result.push_back(suffix.empty() ? std::move(lib) : (lib + suffix).str());
}

return result;
}

//////////////////////////////////////////////////////////////////////////////

static std::vector<std::string> getDefaultLibNames() {
std::vector<std::string> result;

Expand All @@ -144,31 +169,35 @@ static std::vector<std::string> getDefaultLibNames() {
"overrides the existing list instead of appending to "
"it. Please use the latter instead.");
} else if (!global.params.betterC) {
const bool addDebugSuffix =
(linkDefaultLibDebug && debugLib.getNumOccurrences() == 0);
const bool addSharedSuffix = linkAgainstSharedDefaultLibs();

// Parse comma-separated default library list.
std::stringstream libNames(
linkDefaultLibDebug && !addDebugSuffix ? debugLib : defaultLib);
while (libNames.good()) {
std::string lib;
std::getline(libNames, lib, ',');
if (lib.empty()) {
continue;
}

result.push_back((llvm::Twine(lib) + (addDebugSuffix ? "-debug" : "") +
(addSharedSuffix ? "-shared" : ""))
.str());
llvm::StringRef list = defaultLib;
std::string suffix;

if (linkDefaultLibDebug) {
if (debugLib.getNumOccurrences() == 0)
suffix = "-debug";
else
list = debugLib;
}
if (linkAgainstSharedDefaultLibs()) {
suffix += "-shared";
}

result = parseLibNames(list, suffix);
}

return result;
}

//////////////////////////////////////////////////////////////////////////////

llvm::Optional<std::vector<std::string>> getExplicitPlatformLibs() {
if (platformLib.getNumOccurrences() > 0)
return parseLibNames(platformLib);
return llvm::None;
}

//////////////////////////////////////////////////////////////////////////////

bool useInternalLLDForLinking() {
return linkInternally
#if LDC_WITH_LLD
Expand Down
5 changes: 5 additions & 0 deletions driver/linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ llvm::cl::boolOrDefault linkFullyStatic();
*/
bool linkAgainstSharedDefaultLibs();

/**
* Returns the -platformlib library names, if specified.
*/
llvm::Optional<std::vector<std::string>> getExplicitPlatformLibs();

/**
* Returns the value of -mscrtlib.
*/
Expand Down
27 changes: 27 additions & 0 deletions tests/linking/platformlib.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Make sure -platformlib overrides the default platform libraries list.


// RUN: %ldc %s -platformlib= -gcc=echo -linker=echo | FileCheck --check-prefix=EMPTY %s

// EMPTY-NOT: -lrt
// EMPTY-NOT: -ldl
// EMPTY-NOT: -lpthread
// EMPTY-NOT: -lm

// EMPTY-NOT: kernel32
// EMPTY-NOT: user32
// EMPTY-NOT: gdi32
// EMPTY-NOT: winspool
// EMPTY-NOT: shell32
// EMPTY-NOT: ole32
// EMPTY-NOT: oleaut32
// EMPTY-NOT: uuid
// EMPTY-NOT: comdlg32
// EMPTY-NOT: advapi32
// EMPTY-NOT: oldnames
// EMPTY-NOT: legacy_stdio_definitions


// RUN: %ldc %s -platformlib=myPlatformLib1,myPlatformLib2 -gcc=echo -linker=echo | FileCheck --check-prefix=CUSTOM %s

// CUSTOM: {{(-lmyPlatformLib1 -lmyPlatformLib2)|(myPlatformLib1.lib myPlatformLib2.lib)}}