]> git.tdb.fi Git - builder.git/commitdiff
Deal with static build flags for binary packages
authorMikko Rasa <tdb@tdb.fi>
Mon, 10 Nov 2014 18:50:35 +0000 (20:50 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 10 Nov 2014 18:50:35 +0000 (20:50 +0200)
Some libraries have internal dependencies to other libraries, which are
automatically pulled in with dynamic linking, but need to be manually
specified to the linker when linking statically.

source/binary.cpp
source/binarypackage.cpp
source/binarypackage.h
source/packagemanager.cpp

index 448292cf051a5899412ba89329888053312276cc..55e517a6190ffbb36075616593b3f71110e152bf 100644 (file)
@@ -61,8 +61,7 @@ void Binary::find_dependencies()
                        static_binfo.keep_symbols.insert(static_binfo.keep_symbols.end(), binfo.keep_symbols.begin(), binfo.keep_symbols.end());
                }
 
-               list<Target *>::iterator insert_pos = j;
-               ++insert_pos;
+               list<Target *> libs_to_process;
                for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
                {
                        if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
@@ -71,16 +70,27 @@ void Binary::find_dependencies()
                        BuildInfo::LibraryMode libmode = component->get_build_info().get_libmode_for(*i);
                        Target *lib = builder.get_vfs().find_library(*i, binfo.libpath, libmode);
                        if(lib)
-                       {
-                               Target *real = lib->get_real_target();
-                               if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(real))
-                                       queue.insert(insert_pos, stlib);
-                               else
-                                       dep_libs.push_back(lib);
-                       }
+                               libs_to_process.push_back(lib);
                        else if(missing_libs.insert(*i).second)
                                problems.push_back(format("Required library %s not found", *i));
                }
+
+               if(tgt!=this)
+               {
+                       const Target::Dependencies &tdeps = tgt->get_transitive_dependencies();
+                       libs_to_process.insert(libs_to_process.end(), tdeps.begin(), tdeps.end());
+               }
+
+               list<Target *>::iterator insert_pos = j;
+               ++insert_pos;
+               for(list<Target *>::const_iterator i=libs_to_process.begin(); i!=libs_to_process.end(); ++i)
+               {
+                       Target *real = (*i)->get_real_target();
+                       if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(real))
+                               queue.insert(insert_pos, stlib);
+                       else
+                               dep_libs.push_back(*i);
+               }
        }
 
        queue.pop_front();
index 94667c989dede480f3b36afbddb04108f84881cb..679fb636132127ab39195a101e2ce93204277d2a 100644 (file)
@@ -1,7 +1,9 @@
+#include <algorithm>
 #include <msp/io/print.h>
 #include <msp/strings/utils.h>
 #include "binarypackage.h"
 #include "builder.h"
+#include "filetarget.h"
 
 using namespace std;
 using namespace Msp;
@@ -91,15 +93,45 @@ void BinaryPackage::do_prepare()
                for(BuildInfo::PathList::iterator i=export_binfo.libpath.begin(); i!=export_binfo.libpath.end(); ++i)
                        *i = base_path/ *i;
        }
+
+       if(!static_binfo.libs.empty())
+       {
+               BuildInfo::PathList combined_libpath = static_binfo.libpath;
+               combined_libpath.insert(combined_libpath.end(), export_binfo.libpath.begin(), export_binfo.libpath.end());
+
+               Target::Dependencies static_dep_libs;
+               for(BuildInfo::WordList::const_iterator i=static_binfo.libs.begin(); i!=static_binfo.libs.end(); ++i)
+                       if(Target *lib = builder.get_vfs().find_library(*i, combined_libpath, BuildInfo::STATIC, system))
+                               static_dep_libs.push_back(lib);
+
+               for(BuildInfo::WordList::const_iterator i=export_binfo.libs.begin(); i!=export_binfo.libs.end(); ++i)
+                       if(Target *lib = builder.get_vfs().find_library(*i, export_binfo.libpath, BuildInfo::FORCE_STATIC, system))
+                       {
+                               for(Target::Dependencies::const_iterator j=static_dep_libs.begin(); j!=static_dep_libs.end(); ++j)
+                                       lib->add_transitive_dependency(**j);
+                       }
+       }
 }
 
-BinaryPackage *BinaryPackage::from_flags(Builder &builder, const string &name, const vector<string> &flags)
+BinaryPackage *BinaryPackage::from_flags(Builder &builder, const string &name, const Flags &flags, const Flags &static_flags)
 {
        BinaryPackage *pkg = new BinaryPackage(builder, name);
        pkg->use_pkgconfig = true;
-       BuildInfo &binfo = pkg->export_binfo;
 
-       for(vector<string>::const_iterator i=flags.begin(); i!=flags.end(); ++i)
+       process_flags(flags, pkg->export_binfo);
+
+       Flags exclusive_static_flags;
+       for(Flags::const_iterator i=static_flags.begin(); i!=static_flags.end(); ++i)
+               if(find(flags.begin(), flags.end(), *i)==flags.end())
+                       exclusive_static_flags.push_back(*i);
+       process_flags(exclusive_static_flags, pkg->static_binfo);
+
+       return pkg;
+}
+
+void BinaryPackage::process_flags(const Flags &flags, BuildInfo &binfo)
+{
+       for(Flags::const_iterator i=flags.begin(); i!=flags.end(); ++i)
        {
                if(!i->compare(0, 2, "-I"))
                        binfo.incpath.push_back(i->substr(2));
@@ -118,8 +150,6 @@ BinaryPackage *BinaryPackage::from_flags(Builder &builder, const string &name, c
                else if(*i=="-pthread")
                        binfo.threads = true;
        }
-
-       return pkg;
 }
 
 
index 13e2fd85ddc7a65397654400a9ece95913d308be..b8c29de772e45610d9078de5ea0225c8ce961349 100644 (file)
@@ -19,17 +19,23 @@ public:
                void header(const std::string &);
        };
 
+       typedef std::vector<std::string> Flags;
+
 private:
        typedef std::list<std::string> HeaderList;
 
        Msp::FS::Path base_path;
        HeaderList headers;
+       BuildInfo static_binfo;
 
 public:
        BinaryPackage(Builder &, const std::string &);
 
-       static BinaryPackage *from_flags(Builder &, const std::string &, const std::vector<std::string> &);
+       const BuildInfo &get_static_build_info() const { return static_binfo; }
+
+       static BinaryPackage *from_flags(Builder &, const std::string &, const Flags &, const Flags & = Flags());
 private:
+       static void process_flags(const Flags &, BuildInfo &);
        virtual void do_prepare();
 };
 
index ee07987d0186e86641cf43cf8aa11f801d3ea18f..2319c5a906022ffab072e09eb87531e888d08d95 100644 (file)
@@ -109,8 +109,10 @@ Package *PackageManager::find_package(const string &name)
        {
                // Package source not found - create a binary package
                string flags_str = run_pkgconfig(name, "flags");
-               vector<string> flags = split(flags_str);
-               Package *pkg = BinaryPackage::from_flags(builder, name, flags);
+               BinaryPackage::Flags flags = split(flags_str);
+               flags_str = run_pkgconfig(name, "staticflags");
+               BinaryPackage::Flags static_flags = split(flags_str);
+               Package *pkg = BinaryPackage::from_flags(builder, name, flags, static_flags);
                packages.insert(PackageMap::value_type(name, pkg));
                return pkg;
        }
@@ -150,10 +152,12 @@ string PackageManager::run_pkgconfig(const string &pkg, const string &what)
        argv.push_back("pkg-config");
        if(what=="cflags" || what=="libs")
                argv.push_back("--"+what);
-       else if(what=="flags")
+       else if(what=="flags" || what=="staticflags")
        {
                argv.push_back("--cflags");
                argv.push_back("--libs");
+               if(what=="staticflags")
+                       argv.push_back("--static");
        }
        else
                argv.push_back("--variable="+what);