Skip to content

Commit b91cc20

Browse files
committed
Only set rpath when linking against shared default libs
1 parent 70f3a1a commit b91cc20

14 files changed

+94
-40
lines changed

driver/cl_options.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,6 @@ cl::opt<std::string>
289289
"'-deps' alone prints module dependencies "
290290
"(imports/file/version/debug/lib)"));
291291

292-
cl::opt<cl::boolOrDefault>
293-
staticFlag("static", llvm::cl::ZeroOrMore,
294-
llvm::cl::desc("Create a statically linked binary, including "
295-
"all system dependencies"),
296-
cl::cat(linkingCategory));
297-
298292
cl::opt<bool> m32bits("m32", cl::desc("32 bit target"), cl::ZeroOrMore);
299293

300294
cl::opt<bool> m64bits("m64", cl::desc("64 bit target"), cl::ZeroOrMore);

driver/cl_options.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ extern cl::opt<std::string> moduleDeps;
6868
extern cl::opt<std::string> cacheDir;
6969
extern cl::list<std::string> linkerSwitches;
7070
extern cl::list<std::string> ccSwitches;
71-
extern cl::opt<cl::boolOrDefault> staticFlag;
7271

7372
extern cl::opt<bool> m32bits;
7473
extern cl::opt<bool> m64bits;

driver/configfile.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static llvm::cl::opt<std::string>
3535
clConf("conf", llvm::cl::desc("Use configuration file <filename>"),
3636
llvm::cl::value_desc("filename"), llvm::cl::ZeroOrMore);
3737

38+
ConfigFile ConfigFile::instance;
39+
3840
#if _WIN32
3941
std::string getUserHomeDirectory() {
4042
char buff[MAX_PATH];

driver/configfile.d

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ ArraySetting findArraySetting(GroupSetting section, string name)
4141
return null;
4242
}
4343

44+
ScalarSetting findScalarSetting(GroupSetting section, string name)
45+
{
46+
if (!section) return null;
47+
foreach (c; section.children)
48+
{
49+
if (c.type == Setting.Type.scalar && c.name == name)
50+
return cast(ScalarSetting) c;
51+
}
52+
return null;
53+
}
4454

4555
string replace(string str, string pattern, string replacement)
4656
{
@@ -95,6 +105,7 @@ private:
95105
const(char)* pathcstr;
96106
Array!(const(char)*) switches;
97107
Array!(const(char)*) postSwitches;
108+
const(char)* rpathcstr;
98109

99110
bool readConfig(const(char)* cfPath, const(char)* sectionName, const(char)* binDir)
100111
{
@@ -159,6 +170,18 @@ private:
159170
applyArray(this.switches, switches);
160171
applyArray(this.postSwitches, postSwitches);
161172

173+
ScalarSetting findScalar(string name)
174+
{
175+
auto r = findScalarSetting(section, name);
176+
if (!r)
177+
r = findScalarSetting(defaultSection, name);
178+
return r;
179+
}
180+
181+
auto rpath = findScalar("rpath");
182+
if (rpath)
183+
this.rpathcstr = (rpath.val.replace("%%ldcbinarypath%%", dBinDir) ~ '\0').ptr;
184+
162185
return true;
163186
}
164187
catch (Exception ex)

driver/configfile.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
class ConfigFile {
2323
public:
24+
static ConfigFile instance;
25+
2426
bool read(const char *explicitConfFile, const char *section);
2527

26-
std::string path() { return std::string(pathcstr); }
28+
std::string path() { return pathcstr; }
2729

2830
void extendCommandLine(llvm::SmallVectorImpl<const char *> &args);
31+
std::string rpath() { return rpathcstr; }
2932

3033
private:
3134
bool locate(std::string &pathstr);
@@ -36,6 +39,7 @@ class ConfigFile {
3639
const char *pathcstr = nullptr;
3740
Array<const char *> switches;
3841
Array<const char *> postSwitches;
42+
const char *rpathcstr = nullptr;
3943
};
4044

4145
#endif // LDC_DRIVER_CONFIGFILE_H

driver/linker-gcc.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#include "driver/cl_options.h"
1212
#include "driver/cl_options_instrumentation.h"
1313
#include "driver/cl_options_sanitizers.h"
14+
#include "driver/configfile.h"
1415
#include "driver/exe_path.h"
1516
#include "driver/ldc-version.h"
17+
#include "driver/linker.h"
1618
#include "driver/tool.h"
1719
#include "gen/irstate.h"
1820
#include "gen/logger.h"
@@ -476,11 +478,18 @@ void ArgsBuilder::build(llvm::StringRef outputPath,
476478
addLinker();
477479
addUserSwitches();
478480

479-
// libs added via pragma(lib, libname)
481+
// default libs and libs added via pragma(lib, libname)
480482
for (auto ls : global.params.linkswitches) {
481483
args.push_back(ls);
482484
}
483485

486+
// -rpath if linking against shared default libs
487+
if (willLinkAgainstSharedDefaultLibs()) {
488+
const std::string rpath = ConfigFile::instance.rpath();
489+
if (!rpath.empty())
490+
addLdFlag("-rpath", rpath);
491+
}
492+
484493
if (global.params.targetTriple->getOS() == llvm::Triple::Linux) {
485494
// Make sure we don't do --gc-sections when generating a profile-
486495
// instrumented binary. The runtime relies on magic sections, which

driver/linker.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@
1818
#include "llvm/Support/FileSystem.h"
1919
#include "llvm/Support/SourceMgr.h"
2020

21+
namespace cl = llvm::cl;
22+
2123
//////////////////////////////////////////////////////////////////////////////
2224

2325
#if LDC_WITH_LLD
24-
static llvm::cl::opt<bool>
25-
useInternalLinker("link-internally", llvm::cl::ZeroOrMore,
26-
llvm::cl::desc("Use internal LLD for linking"),
27-
llvm::cl::cat(opts::linkingCategory));
26+
static cl::opt<bool> useInternalLinker("link-internally", cl::ZeroOrMore,
27+
cl::desc("Use internal LLD for linking"),
28+
cl::cat(opts::linkingCategory));
2829
#else
2930
constexpr bool useInternalLinker = false;
3031
#endif
3132

33+
static cl::opt<cl::boolOrDefault>
34+
staticFlag("static", cl::ZeroOrMore,
35+
cl::desc("Create a statically linked binary, including "
36+
"all system dependencies"),
37+
cl::cat(opts::linkingCategory));
38+
39+
static cl::opt<cl::boolOrDefault> linkDefaultLibShared(
40+
"link-defaultlib-shared", cl::ZeroOrMore,
41+
cl::desc("Link with shared versions of default libraries"),
42+
cl::cat(opts::linkingCategory));
43+
3244
//////////////////////////////////////////////////////////////////////////////
3345

3446
// linker-gcc.cpp
3547
int linkObjToBinaryGcc(llvm::StringRef outputPath, bool useInternalLinker,
36-
llvm::cl::boolOrDefault fullyStaticFlag);
48+
cl::boolOrDefault fullyStaticFlag);
3749

3850
// linker-msvc.cpp
3951
int linkObjToBinaryMSVC(llvm::StringRef outputPath, bool useInternalLinker,
40-
llvm::cl::boolOrDefault fullyStaticFlag);
52+
cl::boolOrDefault fullyStaticFlag);
4153

4254
//////////////////////////////////////////////////////////////////////////////
4355

@@ -87,6 +99,16 @@ static std::string getOutputName() {
8799

88100
//////////////////////////////////////////////////////////////////////////////
89101

102+
bool willLinkAgainstSharedDefaultLibs() {
103+
// -static enforces static default libs.
104+
// Default to shared default libs for DLLs.
105+
return staticFlag != cl::BOU_TRUE &&
106+
(linkDefaultLibShared == cl::BOU_TRUE ||
107+
(linkDefaultLibShared == cl::BOU_UNSET && global.params.dll));
108+
}
109+
110+
//////////////////////////////////////////////////////////////////////////////
111+
90112
/// Insert an LLVM bitcode file into the module
91113
static void insertBitcodeIntoModule(const char *bcFile, llvm::Module &M,
92114
llvm::LLVMContext &Context) {
@@ -130,10 +152,10 @@ int linkObjToBinary() {
130152
createDirectoryForFileOrFail(gExePath);
131153

132154
if (global.params.targetTriple->isWindowsMSVCEnvironment()) {
133-
return linkObjToBinaryMSVC(gExePath, useInternalLinker, opts::staticFlag);
155+
return linkObjToBinaryMSVC(gExePath, useInternalLinker, staticFlag);
134156
}
135157

136-
return linkObjToBinaryGcc(gExePath, useInternalLinker, opts::staticFlag);
158+
return linkObjToBinaryGcc(gExePath, useInternalLinker, staticFlag);
137159
}
138160

139161
//////////////////////////////////////////////////////////////////////////////

driver/linker.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class LLVMContext;
2222

2323
template <typename TYPE> struct Array;
2424

25+
/**
26+
* Indicates whether the command-line options select shared druntime/Phobos for
27+
* linking.
28+
*/
29+
bool willLinkAgainstSharedDefaultLibs();
30+
2531
/**
2632
* Inserts bitcode files passed on the commandline into a module.
2733
*/

driver/main.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,6 @@ static cl::alias _linkDebugLib("link-debuglib", cl::Hidden,
119119
cl::desc("Alias for -link-defaultlib-debug"),
120120
cl::cat(linkingCategory));
121121

122-
static cl::opt<bool> linkDefaultLibShared(
123-
"link-defaultlib-shared", cl::ZeroOrMore,
124-
cl::desc("Link with shared versions of default libraries"),
125-
cl::cat(linkingCategory));
126-
127122
// This function exits the program.
128123
void printVersion(llvm::raw_ostream &OS) {
129124
OS << "LDC - the LLVM D compiler (" << global.ldc_version << "):\n";
@@ -325,7 +320,7 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles,
325320
expandResponseFiles(allocator, allArguments);
326321

327322
// read config file
328-
ConfigFile cfg_file;
323+
ConfigFile &cfg_file = ConfigFile::instance;
329324
const char *explicitConfFile = tryGetExplicitConfFile(allArguments);
330325
const std::string cfg_triple = tryGetExplicitTriple(allArguments).getTriple();
331326
// just ignore errors for now, they are still printed
@@ -502,13 +497,7 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles,
502497
} else if (!global.params.betterC) {
503498
const bool addDebugSuffix =
504499
(linkDefaultLibDebug && debugLib.getNumOccurrences() == 0);
505-
506-
// -static enforces static default libs.
507-
// Default to shared default libs for DLLs.
508-
const bool addSharedSuffix =
509-
staticFlag != cl::BOU_TRUE &&
510-
(linkDefaultLibShared ||
511-
(linkDefaultLibShared.getNumOccurrences() == 0 && global.params.dll));
500+
const bool addSharedSuffix = willLinkAgainstSharedDefaultLibs();
512501

513502
// Parse comma-separated default library list.
514503
std::stringstream libNames(

ldc2.conf.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ default:
77
// default switches injected before all explicit command-line switches
88
switches = [
99
"-I@RUNTIME_DIR@/src",
10-
"-I@JITRT_DIR@/d",@SHARED_LIBS_RPATH@
10+
"-I@JITRT_DIR@/d",
1111
"-defaultlib=druntime-ldc"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
1212
];
1313
// default switches appended after all explicit command-line switches
1414
post-switches = [
1515
"-L-L@CMAKE_BINARY_DIR@/lib@LIB_SUFFIX@",@MULTILIB_ADDITIONAL_PATH@
1616
];
17+
// default rpath when linking against the shared default libs
18+
rpath = "@SHARED_LIBS_RPATH@";
1719
};

ldc2_install.conf.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ default:
77
// default switches injected before all explicit command-line switches
88
switches = [
99
"-I@INCLUDE_INSTALL_DIR@/ldc",
10-
"-I@INCLUDE_INSTALL_DIR@",@SHARED_LIBS_INSTALL_RPATH@
10+
"-I@INCLUDE_INSTALL_DIR@",
1111
"-defaultlib=phobos2-ldc,druntime-ldc"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
1212
];
1313
// default switches appended after all explicit command-line switches
1414
post-switches = [
1515
"-L-L@CMAKE_INSTALL_LIBDIR@",@MULTILIB_ADDITIONAL_INSTALL_PATH@
1616
];
17+
// default rpath when linking against the shared default libs
18+
rpath = "@SHARED_LIBS_INSTALL_RPATH@";
1719
};

ldc2_phobos.conf.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ default:
88
switches = [
99
"-I@RUNTIME_DIR@/src",
1010
"-I@JITRT_DIR@/d",
11-
"-I@PHOBOS2_DIR@",@SHARED_LIBS_RPATH@
11+
"-I@PHOBOS2_DIR@",
1212
"-defaultlib=phobos2-ldc,druntime-ldc"@ADDITIONAL_DEFAULT_LDC_SWITCHES@
1313
];
1414
// default switches appended after all explicit command-line switches
1515
post-switches = [
1616
"-L-L@CMAKE_BINARY_DIR@/lib@LIB_SUFFIX@",@MULTILIB_ADDITIONAL_PATH@
1717
];
18+
// default rpath when linking against the shared default libs
19+
rpath = "@SHARED_LIBS_RPATH@";
1820
};

runtime/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,14 @@ if(MULTILIB AND NOT "${TARGET_SYSTEM}" MATCHES "APPLE")
251251
set(MULTILIB_ADDITIONAL_INSTALL_PATH "\n \"-L-L${CMAKE_INSTALL_PREFIX}/lib${MULTILIB_SUFFIX}\",\n \"-L--no-warn-search-mismatch\",")
252252
endif()
253253

254-
# Set the -rpath linker option when shared runtime libraries are built.
255-
if(NOT ${BUILD_SHARED_LIBS} STREQUAL "OFF")
254+
# Default -rpath linker option when linking against shared libraries.
255+
if(SHARED_LIBS_SUPPORTED)
256256
if(MULTILIB AND NOT "${TARGET_SYSTEM}" MATCHES "APPLE")
257-
set(SHARED_LIBS_RPATH "\n \"-L-rpath\", \"-L${CMAKE_BINARY_DIR}/lib${LIB_SUFFIX}:${CMAKE_BINARY_DIR}/lib${MULTILIB_SUFFIX}\",")
258-
set(SHARED_LIBS_INSTALL_RPATH "\n \"-L-rpath\", \"-L${CMAKE_INSTALL_LIBDIR}:${CMAKE_INSTALL_PREFIX}/lib${MULTILIB_SUFFIX}\",")
257+
set(SHARED_LIBS_RPATH "${CMAKE_BINARY_DIR}/lib${LIB_SUFFIX}:${CMAKE_BINARY_DIR}/lib${MULTILIB_SUFFIX}")
258+
set(SHARED_LIBS_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}:${CMAKE_INSTALL_PREFIX}/lib${MULTILIB_SUFFIX}")
259259
else()
260-
set(SHARED_LIBS_RPATH "\n \"-L-rpath\", \"-L${CMAKE_BINARY_DIR}/lib${LIB_SUFFIX}\",")
261-
set(SHARED_LIBS_INSTALL_RPATH "\n \"-L-rpath\", \"-L${CMAKE_INSTALL_LIBDIR}\",")
260+
set(SHARED_LIBS_RPATH "${CMAKE_BINARY_DIR}/lib${LIB_SUFFIX}")
261+
set(SHARED_LIBS_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
262262
endif()
263263
endif()
264264

runtime/druntime

0 commit comments

Comments
 (0)