]> git.tdb.fi Git - builder.git/commitdiff
Use -rpath (or -rpath-link) when linking
authorMikko Rasa <tdb@tdb.fi>
Thu, 30 Aug 2018 11:01:33 +0000 (14:01 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 30 Aug 2018 11:01:33 +0000 (14:01 +0300)
It avoids the need to use LD_LIBRARY_PATH when running programs linked
against custom libraries.  The default build types have been amended so
that debug builds use an absolute rpath.  This allows them to be run from
the source tree.  Release builds use a relative rpath to make them more
easily relocatable in the filesystem.

builderrc
source/buildinfo.cpp
source/buildinfo.h
source/gnulinker.cpp

index d84a15bf970bfaa244afcc9c436abec4b87c05b2..207c2d6ab0e1e9c6444c2158840549bcde5cd7b7 100644 (file)
--- a/builderrc
+++ b/builderrc
@@ -21,6 +21,7 @@ build_type "debug"
                define "DEBUG" "1";
                warning_level 3;
                fatal_warnings true;
+               runtime_path_mode ABSOLUTE;
        };
 };
 
@@ -33,6 +34,7 @@ build_type "optimized_debug"
                optimize 2;
                warning_level 3;
                fatal_warnings true;
+               runtime_path_mode ABSOLUTE;
        };
 };
 
@@ -43,6 +45,7 @@ build_type "release"
                optimize 3;
                strip true;
                warning_level 1;
+               runtime_path_mode RELATIVE;
        };
 };
 
index 114a368ecc96feabec39807621d1dc0d1a363e85..e2df34c06e3ec91cb56500f97c3bed1b53b23d02 100644 (file)
@@ -28,6 +28,7 @@ void unique(list<T> &l)
 
 BuildInfo::BuildInfo():
        libmode(DYNAMIC),
+       rpath_mode(NO_RPATH),
        threads(false),
        debug(false),
        optimize(0),
@@ -64,6 +65,7 @@ void BuildInfo::update_from(const BuildInfo &bi, UpdateLevel level)
                sysroot = bi.sysroot;
                local_incpath.insert(local_incpath.begin(), bi.local_incpath.begin(), bi.local_incpath.end());
                libmode = bi.libmode;
+               rpath_mode = bi.rpath_mode;
                for(LibModeMap::const_iterator i=bi.libmodes.begin(); i!=bi.libmodes.end(); ++i)
                        libmodes[i->first] = i->second;
                keep_symbols.insert(keep_symbols.end(), bi.keep_symbols.begin(), bi.keep_symbols.end());
@@ -95,6 +97,7 @@ BuildInfo::Loader::Loader(BuildInfo &bi):
        add("libmode",  &Loader::libmode_for_lib);
        add("local_incpath", &Loader::local_incpath);
        add("optimize", &BuildInfo::optimize);
+       add("runtime_path_mode", &BuildInfo::rpath_mode);
        add("standard", &Loader::standard);
        add("strip",    &BuildInfo::strip);
        add("sysroot",  &Loader::sysroot);
@@ -162,3 +165,16 @@ void operator>>(const LexicalConverter &conv, BuildInfo::LibraryMode &libmode)
        else
                throw lexical_error(format("Conversion of '%s' to LibraryMode", conv.get()));
 }
+
+
+void operator>>(const LexicalConverter &conv, BuildInfo::RuntimePathMode &rpath_mode)
+{
+       if(conv.get()=="NONE")
+               rpath_mode = BuildInfo::NO_RPATH;
+       else if(conv.get()=="RELATIVE")
+               rpath_mode = BuildInfo::RELATIVE;
+       else if(conv.get()=="ABSOLUTE")
+               rpath_mode = BuildInfo::ABSOLUTE;
+       else
+               throw lexical_error(format("Conversion of '%s' to RuntimePathMode", conv.get()));
+}
index 5528748fc55c82bfde6b14d09e3ca9bc3313e3bc..ccfa2b630bc9f47a03e471c846e8bc7e2e3c8838 100644 (file)
@@ -21,6 +21,13 @@ public:
                FORCE_DYNAMIC  //< Only accept dynamic libraries
        };
 
+       enum RuntimePathMode
+       {
+               NO_RPATH,  //< Do not record rpath in binaries
+               RELATIVE,  //< Record relative rpath in binaries
+               ABSOLUTE   //< Record absolute rpath in binaries
+       };
+
        class Loader: public Msp::DataFile::ObjectLoader<BuildInfo>
        {
        public:
@@ -83,6 +90,7 @@ public:
        PathList libpath;
        WordList libs;
        Tracked<LibraryMode> libmode;
+       Tracked<RuntimePathMode> rpath_mode;
        LibModeMap libmodes;
        WordList keep_symbols;
        StandardMap standards;
index faa62024e250aee1414caab7f52aed7ed794ebe8..a132acecf5b9f0f85bb1220c2edb08098f0a4829 100644 (file)
@@ -271,6 +271,16 @@ Task *GnuLinker::Linker::run(const Target &target) const
        if(!sysroot.empty())
                argv.push_back("--sysroot="+sysroot.str());
 
+       FS::Path lib_dir = builder.get_prefix()/"lib";
+       if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
+               argv.push_back("-Wl,-rpath,"+lib_dir.str());
+       else
+       {
+               if(binfo.rpath_mode==BuildInfo::RELATIVE)
+                       argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
+               argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
+       }
+
        for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
                argv.push_back("-L"+i->str());
        if(binfo.strip)