diff --git a/gen/cl_options.cpp b/gen/cl_options.cpp index 6e0f6e93..1eceadae 100644 --- a/gen/cl_options.cpp +++ b/gen/cl_options.cpp @@ -110,6 +110,12 @@ cl::opt objectDir("od", cl::Prefix, cl::desc("Write object files to directory ")); +cl::opt soname("soname", + cl::value_desc("soname"), + cl::Hidden, + cl::Prefix, + cl::desc("Use as output shared library soname")); + // Output format options cl::opt output_bc("output-bc", diff --git a/gen/cl_options.h b/gen/cl_options.h index 3eb8f5c1..fb6bfeb6 100644 --- a/gen/cl_options.h +++ b/gen/cl_options.h @@ -28,6 +28,7 @@ namespace opts { extern cl::opt dontWriteObj; extern cl::opt objectFile; extern cl::opt objectDir; + extern cl::opt soname; extern cl::opt output_bc; extern cl::opt output_ll; extern cl::opt output_s; diff --git a/gen/linker.cpp b/gen/linker.cpp index ce880239..3bf0e8f4 100644 --- a/gen/linker.cpp +++ b/gen/linker.cpp @@ -278,8 +278,6 @@ int linkObjToBinary(bool sharedLib) } } - - args.push_back("-o"); args.push_back(output.c_str()); @@ -315,14 +313,17 @@ int linkObjToBinary(bool sharedLib) } // default libs + bool addSoname = false; switch(global.params.os) { case OSLinux: + addSoname = true; args.push_back("-lrt"); // fallthrough case OSMacOSX: args.push_back("-ldl"); // fallthrough case OSFreeBSD: + addSoname = true; args.push_back("-lpthread"); args.push_back("-lm"); break; @@ -345,6 +346,16 @@ int linkObjToBinary(bool sharedLib) // Assume 32-bit? args.push_back("-m32"); + OutBuffer buf; + if (opts::createSharedLib && addSoname) { + std::string soname = opts::soname.getNumOccurrences() == 0 ? output : opts::soname; + if (!soname.empty()) { + buf.writestring("-Wl,-soname,"); + buf.writestring(soname.c_str()); + args.push_back(buf.toChars()); + } + } + // print link command? if (!quiet || global.params.verbose) { diff --git a/gen/main.cpp b/gen/main.cpp index 056187e8..bacdbca3 100644 --- a/gen/main.cpp +++ b/gen/main.cpp @@ -419,6 +419,11 @@ int main(int argc, char** argv) } } + if (soname.getNumOccurrences() > 0 && !createSharedLib) { + error("-soname can be used only when building a shared library"); + fatal(); + } + // create a proper target Ir ir;