fix: set rpath when linking using extra args

This commit is contained in:
youben11
2022-04-05 10:32:30 +01:00
committed by Ayoub Benaissa
parent ec8004cd3a
commit 2ded4b6f43
5 changed files with 38 additions and 17 deletions

View File

@@ -120,7 +120,9 @@ public:
LINKER_SHARED_OPT, AR, AR_STATIC_OPT, DOT_STATIC_LIB_EXT,
DOT_SHARED_LIB_EXT;
void addExtraObjectFilePath(std::string objectFilePath);
llvm::Expected<std::string> emit(std::string dotExt, std::string linker);
llvm::Expected<std::string>
emit(std::string dotExt, std::string linker,
llvm::Optional<std::vector<std::string>> extraArgs = {});
~Library();
private:

View File

@@ -11,8 +11,10 @@ namespace concretelang {
llvm::Error emitObject(llvm::Module &module, std::string objectPath);
llvm::Error emitLibrary(std::vector<std::string> objectsPath,
std::string libraryPath, std::string linker);
llvm::Error
emitLibrary(std::vector<std::string> objectsPath, std::string libraryPath,
std::string linker,
llvm::Optional<std::vector<std::string>> extraArgs = {});
} // namespace concretelang
} // namespace mlir

View File

@@ -37,7 +37,7 @@ DynamicModule::loadSharedLibrary(std::string path) {
libraryHandle = dlopen(
CompilerEngine::Library::getSharedLibraryPath(path).c_str(), RTLD_LAZY);
if (!libraryHandle) {
return StringError("Cannot open shared library") << dlerror();
return StringError("Cannot open shared library ") << dlerror();
}
return outcome::success();
}

View File

@@ -607,15 +607,12 @@ std::string ensureLibDotExt(std::string path, std::string dotExt) {
return path + dotExt;
}
llvm::Expected<std::string> CompilerEngine::Library::emit(std::string dotExt,
std::string linker) {
llvm::Expected<std::string> CompilerEngine::Library::emit(
std::string dotExt, std::string linker,
llvm::Optional<std::vector<std::string>> extraArgs) {
auto pathDotExt = ensureLibDotExt(libraryPath, dotExt);
auto objectsPathWithRuntimeLib = objectsPath;
if (!runtimeLibraryPath.empty()) {
objectsPathWithRuntimeLib.push_back(runtimeLibraryPath);
}
auto error = mlir::concretelang::emitLibrary(objectsPathWithRuntimeLib,
pathDotExt, linker);
auto error = mlir::concretelang::emitLibrary(objectsPath, pathDotExt, linker,
extraArgs);
if (error) {
return std::move(error);
}
@@ -623,7 +620,21 @@ llvm::Expected<std::string> CompilerEngine::Library::emit(std::string dotExt,
}
llvm::Expected<std::string> CompilerEngine::Library::emitShared() {
auto path = emit(DOT_SHARED_LIB_EXT, LINKER + LINKER_SHARED_OPT);
std::vector<std::string> extraArgs;
if (!runtimeLibraryPath.empty()) {
extraArgs.push_back(runtimeLibraryPath);
#ifndef __APPLE__ // LINUX
// Getting the parent dir should work on Linux and Mac
std::size_t rpathLastPos = runtimeLibraryPath.find_last_of("/");
if (rpathLastPos != std::string::npos) {
std::string rpath = runtimeLibraryPath.substr(0, rpathLastPos);
extraArgs.push_back("-rpath=" + rpath);
// Use RPATH instead of RUNPATH for transitive dependencies
extraArgs.push_back("--disable-new-dtags");
}
#endif
}
auto path = emit(DOT_SHARED_LIB_EXT, LINKER + LINKER_SHARED_OPT, extraArgs);
if (path) {
sharedLibraryPath = path.get();
}

View File

@@ -69,12 +69,17 @@ llvm::Error emitObject(llvm::Module &module, string objectPath) {
return llvm::Error::success();
}
string linkerCmd(vector<string> objectsPath, string libraryPath,
string linker) {
string linkerCmd(vector<string> objectsPath, string libraryPath, string linker,
llvm::Optional<vector<string>> extraArgs) {
string cmd = linker + libraryPath;
for (auto objectPath : objectsPath) {
cmd += " " + objectPath;
}
if (extraArgs.hasValue()) {
for (auto extraArg : extraArgs.getValue()) {
cmd += " " + extraArg;
}
}
cmd += " 2>&1"; // to keep stderr with popen
return cmd;
}
@@ -105,8 +110,9 @@ llvm::Error callCmd(string cmd) {
}
llvm::Error emitLibrary(vector<string> objectsPath, string libraryPath,
string linker) {
auto cmd = linkerCmd(objectsPath, libraryPath, linker);
string linker,
llvm::Optional<vector<string>> extraArgs) {
auto cmd = linkerCmd(objectsPath, libraryPath, linker, extraArgs);
return callCmd(cmd);
}