From 3f10e86873306e443159d3b8296eb024461e5c2f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 5 Aug 2021 12:02:34 +0200 Subject: Add resolved library cache, use in cc::process_libraries() --- libbuild2/cc/common.cxx | 69 +++++++++++++++++++++++++++++++++++++++++++------ libbuild2/cc/common.hxx | 14 +++++++++- 2 files changed, 74 insertions(+), 9 deletions(-) (limited to 'libbuild2') diff --git a/libbuild2/cc/common.cxx b/libbuild2/cc/common.cxx index 3130afa..79e53bb 100644 --- a/libbuild2/cc/common.cxx +++ b/libbuild2/cc/common.cxx @@ -92,13 +92,20 @@ namespace build2 bool com, // cc. or x. bool exp)>& proc_opt, // *.export. bool self /*= false*/, // Call proc_lib on l? + library_cache* cache, small_vector* chain) const { + library_cache cache_storage; + if (cache == nullptr) + cache = &cache_storage; + small_vector chain_storage; if (chain == nullptr) { chain = &chain_storage; - chain->push_back (nullptr); + + if (proc_lib) + chain->push_back (nullptr); } // See what type of library this is (C, C++, etc). Use it do decide @@ -284,7 +291,8 @@ namespace build2 process_libraries (a, bs, *li, *sysd, *f, la, pt.data, - proc_impl, proc_lib, proc_opt, true, chain); + proc_impl, proc_lib, proc_opt, true, + cache, chain); } } } @@ -379,7 +387,7 @@ namespace build2 return make_pair (n, s); }; - auto proc_int = [&l, chain, + auto proc_int = [&l, cache, chain, &proc_impl, &proc_lib, &proc_lib_name, &proc_opt, &sysd, &usrd, &find_sysd, &find_linfo, &sense_fragment, @@ -429,7 +437,8 @@ namespace build2 n, (n.pair ? (++i)->dir : dir_path ()), *li, - *sysd, usrd)); + *sysd, usrd, + cache)); if (proc_lib) { @@ -471,7 +480,8 @@ namespace build2 // process_libraries (a, bs, *li, *sysd, t, t.is_a () || t.is_a (), 0, - proc_impl, proc_lib, proc_opt, true, chain); + proc_impl, proc_lib, proc_opt, true, + cache, chain); } ++i; @@ -580,14 +590,52 @@ namespace build2 const dir_path& out, optional li, const dir_paths& sysd, - optional& usrd) const + optional& usrd, + library_cache* cache) const { + bool q (cn.qualified ()); + auto lo (li ? optional (li->order) : nullopt); + + // If this is an absolute and normalized unqualified name (which means + // the scope does not factor into the result), then first check the + // cache. + // + // Note that normally we will have a handful of libraries repeated a + // large number of times (see Boost for an extreme example of this). + // + // Note also that for non-utility libraries we know that only the link + // order from linfo is used. + // + if (cache != nullptr) + { + if (!q && + (cn.dir.absolute () && cn.dir.normalized ()) && + (out.empty () || (out.absolute () && out.normalized ()))) + { + auto i (find_if (cache->begin (), cache->end (), + [lo, &cn, &out] (const library_cache_entry& e) + { + const target& t (e.lib); + return (e.lo == lo && + e.value == cn.value && + e.type == cn.type && + t.dir == cn.dir && + t.out == out); + })); + + if (i != cache->end ()) + return i->lib; + } + else + cache = nullptr; // Do not cache. + } + if (cn.type != "lib" && cn.type != "liba" && cn.type != "libs") fail << "target name " << cn << " is not a library"; const target* xt (nullptr); - if (!cn.qualified ()) + if (!q) { // Search for an existing target with this name "as if" it was a // prerequisite. @@ -624,7 +672,12 @@ namespace build2 xt = link_member (*l, a, *li); // Pick lib*{e,a,s}{}. } - return xt->as (); + auto& t (xt->as ()); + + if (cache != nullptr) + cache->push_back (library_cache_entry {lo, cn.type, cn.value, t}); + + return t; } // Note that pk's scope should not be NULL (even if dir is absolute). diff --git a/libbuild2/cc/common.hxx b/libbuild2/cc/common.hxx index 176c30e..a62b91b 100644 --- a/libbuild2/cc/common.hxx +++ b/libbuild2/cc/common.hxx @@ -272,6 +272,16 @@ namespace build2 // Library handling. // public: + struct library_cache_entry + { + optional lo; + string type; // name::type + string value; // name::value + reference_wrapper lib; + }; + + using library_cache = small_vector; + void process_libraries ( action, @@ -287,6 +297,7 @@ namespace build2 lflags, bool)>&, const function&, bool = false, + library_cache* = nullptr, small_vector* = nullptr) const; const target* @@ -321,7 +332,8 @@ namespace build2 const dir_path&, optional, const dir_paths&, - optional&) const; + optional&, + library_cache* = nullptr) const; template static ulock -- cgit v1.1