]> git.tdb.fi Git - builder.git/commitdiff
Refactor file locating code in BinaryPackage into a separate function
authorMikko Rasa <tdb@tdb.fi>
Sat, 4 May 2024 08:09:15 +0000 (11:09 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 4 May 2024 08:09:15 +0000 (11:09 +0300)
source/lib/binarypackage.cpp
source/lib/binarypackage.h

index a366878be25302ae75d3650e4bac1ca35361a5d3..d9df0a1f2049b12ee8e9b783578aa936e6ddaad0 100644 (file)
@@ -58,26 +58,62 @@ void BinaryPackage::process_flags(const Flags &flags, BuildInfo &binfo)
 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) ||
+       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);
 
+       if(!locate_files())
+               return;
+
+       /* Add default entries to paths if they're empty and the package was found
+       in a non-system location */
+       if(!found_on_system && export_binfo.incpath.empty())
+               export_binfo.incpath.push_back(base_path/"include");
+       if(!found_on_system && export_binfo.libpath.empty())
+               export_binfo.libpath.push_back(base_path/"lib");
+
+       if(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, found_on_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);
+                               }
+       }
+}
+
+bool BinaryPackage::locate_files()
+{
        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()))
+       if(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;
        unsigned least_missing = numeric_limits<unsigned>::max();
        string missing_name;
        for(const FS::Path &b: bases)
        {
                FS::Path prefix = b;
-               system = prefix.empty();
+               bool system = prefix.empty();
                if(system)
                {
                        prefix = "/usr";
@@ -113,6 +149,7 @@ void BinaryPackage::do_prepare()
                if(!missing_count)
                {
                        base_path = prefix;
+                       found_on_system = system;
                        builder.get_logger().log("configure", "%s found in %s", name, ((system && use_pkgconfig) ? "system" : base_path.str()));
                        break;
                }
@@ -128,39 +165,10 @@ void BinaryPackage::do_prepare()
                string missing_descr = (least_missing>1 ? format("%s and %d other files", missing_name, least_missing-1) : missing_name);
                builder.get_logger().log("problems", "Cannot locate %s for %s", missing_descr, name);
                problems.push_back(format("Cannot locate %s", missing_descr));
-               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;
+               return false;
        }
 
-       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);
-                               }
-       }
+       return true;
 }
 
 
index b7d3294be7c25b6d9d38f6fd213af61d3e07141c..b690d83ebdfa5a263effaffda4494c0bf17161c0 100644 (file)
@@ -23,6 +23,8 @@ public:
        using Flags = std::vector<std::string>;
 
 private:
+       bool relative_paths = false;
+       bool found_on_system = false;
        Msp::FS::Path base_path;
        std::vector<std::string> headers;
        BuildInfo static_binfo;
@@ -36,6 +38,8 @@ public:
 private:
        static void process_flags(const Flags &, BuildInfo &);
        void do_prepare() override;
+
+       bool locate_files();
 };
 
 #endif