]> git.tdb.fi Git - builder.git/blobdiff - source/binarypackage.cpp
Refactor logger to do message formatting internally
[builder.git] / source / binarypackage.cpp
index bc3e5c929ec41af7d6a5b515f2ac2a17501c50b1..adfca1a273bd4ae86b1296e23225918000f0c6cd 100644 (file)
+#include <msp/core/algorithm.h>
 #include <msp/io/print.h>
 #include <msp/strings/utils.h>
 #include "binarypackage.h"
 #include "builder.h"
+#include "filetarget.h"
+#include "staticlibrary.h"
 
 using namespace std;
 using namespace Msp;
 
 BinaryPackage::BinaryPackage(Builder &b, const string &n):
-       Package(b, n),
-       need_path(false)
+       Package(b, n)
 {
        use_pkgconfig = false;
 }
 
-void BinaryPackage::set_path(const FS::Path &p)
+BinaryPackage *BinaryPackage::from_flags(Builder &builder, const string &name, const Flags &flags, const Flags &static_flags)
 {
-       path = builder.get_cwd()/p;
-}
+       BinaryPackage *pkg = new BinaryPackage(builder, name);
+       pkg->use_pkgconfig = true;
 
-void BinaryPackage::create_build_info()
-{
-       for(StringList::iterator i=export_binfo.incpath.begin(); i!=export_binfo.incpath.end(); ++i)
-               if((*i)[0]=='@')
-                       *i = (path/i->substr(1)).str();
+       process_flags(flags, pkg->export_binfo);
+
+       Flags exclusive_static_flags;
+       for(const string &f: static_flags)
+               if(!any_equals(flags, f))
+                       exclusive_static_flags.push_back(f);
+       process_flags(exclusive_static_flags, pkg->static_binfo);
 
-       for(StringList::iterator i=export_binfo.libpath.begin(); i!=export_binfo.libpath.end(); ++i)
-               if((*i)[0]=='@')
-                       *i = (path/i->substr(1)).str();
+       return pkg;
 }
 
-BinaryPackage *BinaryPackage::from_pkgconfig(Builder &builder, const string &name)
+void BinaryPackage::process_flags(const Flags &flags, BuildInfo &binfo)
 {
-       string info = builder.run_pkgconfig(name, "flags");
-
-       BinaryPackage *pkg = new BinaryPackage(builder, name);
-       pkg->use_pkgconfig = true;
-       BuildInfo &binfo = pkg->export_binfo;
-
-       vector<string> flags = split(info);
-       for(vector<string>::const_iterator i=flags.begin(); i!=flags.end(); ++i)
+       for(const string &f: flags)
        {
-               if(!i->compare(0, 2, "-I"))
-                       binfo.incpath.push_back(i->substr(2));
-               else if(!i->compare(0, 2, "-D"))
+               if(!f.compare(0, 2, "-I"))
+                       binfo.incpath.push_back(f.substr(2));
+               else if(!f.compare(0, 2, "-D"))
                {
-                       string::size_type equals = i->find('=');
+                       string::size_type equals = f.find('=');
                        if(equals!=string::npos)
-                               binfo.defines[i->substr(2, equals-2)] = i->substr(equals+1);
+                               binfo.defines[f.substr(2, equals-2)] = f.substr(equals+1);
                        else
-                               binfo.defines[i->substr(2)] = string();
+                               binfo.defines[f.substr(2)] = string();
                }
-               else if(!i->compare(0, 2, "-L"))
-                       binfo.libpath.push_back(i->substr(2));
-               else if(!i->compare(0, 2, "-l"))
-                       binfo.libs.push_back(i->substr(2));
-               else if(*i=="-pthread")
+               else if(!f.compare(0, 2, "-L"))
+                       binfo.libpath.push_back(f.substr(2));
+               else if(!f.compare(0, 2, "-l"))
+                       binfo.libs.push_back(f.substr(2));
+               else if(f=="-pthread")
                        binfo.threads = true;
        }
+}
 
-       return pkg;
+void BinaryPackage::do_prepare()
+{
+       auto is_relative = [](const FS::Path &p){ return !p.is_absolute(); };
+       bool has_relative_paths = any_of(export_binfo.libpath.begin(), export_binfo.libpath.end(), is_relative) ||
+               any_of(export_binfo.incpath.begin(), export_binfo.incpath.end(), is_relative);
+
+       vector<FS::Path> bases;
+
+       /* If we have any relative paths that need resolving, or we have no paths at
+       all and are not using pkg-config, look for files in prefix */
+       if(has_relative_paths || (!use_pkgconfig && export_binfo.libpath.empty() && export_binfo.incpath.empty()))
+               bases.push_back(builder.get_prefix());
+
+       // Always look in system locations
+       bases.push_back(FS::Path());
+
+       bool system = false;
+       for(const FS::Path &b: bases)
+       {
+               FS::Path prefix = b;
+               system = prefix.empty();
+               if(system)
+               {
+                       prefix = "/usr";
+                       const Architecture &arch = builder.get_current_arch();
+                       if(arch.is_cross())
+                               prefix /= arch.get_cross_prefix();
+               }
+
+               VirtualFileSystem::SearchPath libpath = export_binfo.libpath;
+               if(!system && libpath.empty())
+                       libpath.push_back("lib");
+               for(FS::Path &p: libpath)
+                       p = prefix/p;
+
+               bool all_found = true;
+               for(const string &l: export_binfo.libs)
+                       all_found &= (builder.get_vfs().find_library(l, libpath, export_binfo.libmode, system)!=0);
+
+               VirtualFileSystem::SearchPath incpath = export_binfo.incpath;
+               if(!system && incpath.empty())
+                       incpath.push_back("include");
+               for(FS::Path &p: incpath)
+                       p = prefix/p;
+
+               for(const string &h: headers)
+                       all_found &= (builder.get_vfs().find_header(h, 0, incpath, system)!=0);
+
+               if(all_found)
+               {
+                       base_path = prefix;
+                       builder.get_logger().log("configure", "%s found in %s", name, ((system && use_pkgconfig) ? "system" : base_path.str()));
+                       break;
+               }
+       }
+
+       if(base_path.empty())
+       {
+               // TODO report which files were not found
+               builder.get_logger().log("problems", "Cannot locate files for %s", name);
+               problems.push_back("Cannot locate files");
+               return;
+       }
+
+       /* Add default entries to paths if they're empty and the package was found
+       in a non-system location */
+       if(!system && export_binfo.incpath.empty())
+               export_binfo.incpath.push_back(base_path/"include");
+       if(!system && export_binfo.libpath.empty())
+               export_binfo.libpath.push_back(base_path/"lib");
+
+       if(has_relative_paths)
+       {
+               for(FS::Path &p: export_binfo.incpath)
+                       p = base_path/p;
+               for(FS::Path &p: export_binfo.libpath)
+                       p = base_path/p;
+       }
+
+       if(!static_binfo.libs.empty())
+       {
+               VirtualFileSystem::SearchPath combined_libpath = static_binfo.libpath;
+               combined_libpath.insert(combined_libpath.end(), export_binfo.libpath.begin(), export_binfo.libpath.end());
+
+               for(const string &l: export_binfo.libs)
+                       if(Target *lib = builder.get_vfs().find_library(l, export_binfo.libpath, BuildInfo::FORCE_STATIC, system))
+                               if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(lib))
+                               {
+                                       for(const string &s: static_binfo.libs)
+                                               stlib->add_required_library(s);
+                                       for(const FS::Path &p: combined_libpath)
+                                               stlib->add_library_path(p);
+                               }
+       }
 }
 
 
 BinaryPackage::Loader::Loader(BinaryPackage &p):
-       Package::Loader(p)
+       DataFile::DerivedObjectLoader<BinaryPackage, Package::Loader>(p)
 {
-       add("need_path", &BinaryPackage::need_path);
        add("build_info", &Loader::build_info);
+       add("header",     &Loader::header);
 }
 
 void BinaryPackage::Loader::build_info()
 {
-       load_sub(static_cast<BinaryPackage &>(pkg).export_binfo);
+       load_sub(obj.export_binfo);
+}
+
+void BinaryPackage::Loader::header(const string &h)
+{
+       obj.headers.push_back(h);
 }