diff options
Diffstat (limited to 'libbuild2/bin/init.cxx')
-rw-r--r-- | libbuild2/bin/init.cxx | 384 |
1 files changed, 309 insertions, 75 deletions
diff --git a/libbuild2/bin/init.cxx b/libbuild2/bin/init.cxx index ff00e89..610082e 100644 --- a/libbuild2/bin/init.cxx +++ b/libbuild2/bin/init.cxx @@ -3,8 +3,6 @@ #include <libbuild2/bin/init.hxx> -#include <map> - #include <libbuild2/scope.hxx> #include <libbuild2/function.hxx> #include <libbuild2/variable.hxx> @@ -18,6 +16,7 @@ #include <libbuild2/install/utility.hxx> #include <libbuild2/bin/rule.hxx> +#include <libbuild2/bin/def-rule.hxx> #include <libbuild2/bin/guess.hxx> #include <libbuild2/bin/target.hxx> #include <libbuild2/bin/utility.hxx> @@ -32,6 +31,7 @@ namespace build2 static const obj_rule obj_; static const libul_rule libul_; static const lib_rule lib_; + static const def_rule def_; // Default config.bin.*.lib values. // @@ -41,24 +41,30 @@ namespace build2 bool vars_init (scope& rs, - scope&, - const location&, - bool first, + scope& bs, + const location& loc, + bool, bool, module_init_extra&) { tracer trace ("bin::vars_init"); l5 ([&]{trace << "for " << rs;}); - assert (first); + // We only support root loading (which means there can only be one). + // + if (rs != bs) + fail (loc) << "bin.vars module must be loaded in project root"; // Enter variables. // + // All the variables we enter are qualified so go straight for the + // public variable pool. + // + auto& vp (rs.var_pool (true /* public */)); + // Target is a string and not target_triplet because it can be // specified by the user. // - auto& vp (rs.var_pool ()); - vp.insert<string> ("config.bin.target"); vp.insert<string> ("config.bin.pattern"); @@ -76,6 +82,9 @@ namespace build2 // example, addition of rpaths for prerequisite libraries (see the cc // module for an example). Default is true. // + // Note also that a rule may need to make rpath relative if + // install.relocatable is true. + // vp.insert<dir_paths> ("config.bin.rpath"); vp.insert<bool> ("config.bin.rpath.auto"); @@ -104,12 +113,12 @@ namespace build2 // Link whole archive. Note: with target visibility. // // The lookup semantics is as follows: we first look for a prerequisite- - // specific value, then for a target-specific value in the library being - // linked, and then for target type/pattern-specific value starting from - // the scope of the target being linked-to. In that final lookup we do - // not look in the target being linked-to itself since that is used to - // indicate how this target should be linked to other targets. For - // example: + // specific value, then for a target-specific value in the prerequisite + // library, and then for target type/pattern-specific value starting + // from the scope of the target being linked. In that final lookup we do + // not look in the target being linked itself since that is used to + // indicate how this target should be used as a prerequisite of other + // targets. For example: // // exe{test}: liba{foo} // liba{foo}: libua{foo1 foo2} @@ -144,8 +153,70 @@ namespace build2 vp.insert<string> ("bin.lib.load_suffix"); vp.insert<string> ("bin.lib.load_suffix_pattern"); - vp.insert<map<string, string>> ("bin.lib.version"); - vp.insert<string> ("bin.lib.version_pattern"); + vp.insert<map<optional<string>, string>> ("bin.lib.version"); + vp.insert<string> ("bin.lib.version_pattern"); + + return true; + } + + bool + types_init (scope& rs, + scope& bs, + const location& loc, + bool, + bool, + module_init_extra&) + { + tracer trace ("bin::types_init"); + l5 ([&]{trace << "for " << rs;}); + + // We only support root loading (which means there can only be one). + // + if (rs != bs) + fail (loc) << "bin.types module must be loaded in project root"; + + // Register target types. + // + // Note that certain platform-specific and toolchain-specific types are + // registered in bin and bin.ld. + // + // Note also that it would make sense to configure their default + // "installability" here but that requires the knowledge of the platform + // in some cases. So we do it all in bin for now. One way to support + // both use-cases would be to detect if we are loaded after bin.guess + // and then decide whether to do it here or delay to bin. + // + // NOTE: remember to update the documentation if changing anything here! + // + rs.insert_target_type<obj> (); + rs.insert_target_type<obje> (); + rs.insert_target_type<obja> (); + rs.insert_target_type<objs> (); + + rs.insert_target_type<bmi> (); + rs.insert_target_type<bmie> (); + rs.insert_target_type<bmia> (); + rs.insert_target_type<bmis> (); + + rs.insert_target_type<hbmi> (); + rs.insert_target_type<hbmie> (); + rs.insert_target_type<hbmia> (); + rs.insert_target_type<hbmis> (); + + rs.insert_target_type<libul> (); + rs.insert_target_type<libue> (); + rs.insert_target_type<libua> (); + rs.insert_target_type<libus> (); + + rs.insert_target_type<lib> (); + rs.insert_target_type<liba> (); + rs.insert_target_type<libs> (); + + // Register the def{} target type. Note that we do it here since it is + // input and can be specified unconditionally (i.e., not only when + // building for Windows). + // + rs.insert_target_type<def> (); return true; } @@ -195,6 +266,8 @@ namespace build2 // const target_triplet* tgt (nullptr); { + // Note: go straight for the public variable pool. + // const variable& var (ctx.var_pool["config.bin.target"]); // We first see if the value was specified via the configuration @@ -231,9 +304,9 @@ namespace build2 // if (!hint && config_sub) { - s = run<string> (3, - *config_sub, - s.c_str (), + s = run<string> (ctx, + 3, + *config_sub, s.c_str (), [] (string& l, bool) {return move (l);}); l5 ([&]{trace << "config.sub target: '" << s << "'";}); } @@ -272,6 +345,8 @@ namespace build2 // const string* pat (nullptr); { + // Note: go straight for the public variable pool. + // const variable& var (ctx.var_pool["config.bin.pattern"]); // We first see if the value was specified via the configuration @@ -440,53 +515,22 @@ namespace build2 tracer trace ("bin::init"); l5 ([&]{trace << "for " << bs;}); - // Load bin.config. + // Load bin.{config,types}. // load_module (rs, rs, "bin.config", loc, extra.hints); + load_module (rs, rs, "bin.types", loc); // Cache some config values we will be needing below. // const target_triplet& tgt (cast<target_triplet> (rs["bin.target"])); - // Register target types and configure their default "installability". + // Configure target type default "installability". Also register + // additional platform-specific types. // bool install_loaded (cast_false<bool> (rs["install.loaded"])); { using namespace install; - if (first) - { - rs.insert_target_type<obj> (); - rs.insert_target_type<obje> (); - rs.insert_target_type<obja> (); - rs.insert_target_type<objs> (); - - rs.insert_target_type<bmi> (); - rs.insert_target_type<bmie> (); - rs.insert_target_type<bmia> (); - rs.insert_target_type<bmis> (); - - rs.insert_target_type<hbmi> (); - rs.insert_target_type<hbmie> (); - rs.insert_target_type<hbmia> (); - rs.insert_target_type<hbmis> (); - - rs.insert_target_type<libul> (); - rs.insert_target_type<libue> (); - rs.insert_target_type<libua> (); - rs.insert_target_type<libus> (); - - rs.insert_target_type<lib> (); - rs.insert_target_type<liba> (); - rs.insert_target_type<libs> (); - - // Register the def{} target type. Note that we do it here since it - // is input and can be specified unconditionally (i.e., not only - // when building for Windows). - // - rs.insert_target_type<def> (); - } - // Note: libu*{} members are not installable. // if (install_loaded) @@ -536,6 +580,8 @@ namespace build2 if (tgt.cpu == "wasm32" || tgt.cpu == "wasm64") { + // @@ TODO: shouldn't this be wrapped in if(first) somehow? + const target_type& wasm ( rs.derive_target_type( target_type { @@ -546,8 +592,8 @@ namespace build2 nullptr, /* default_extension */ &target_pattern_fix<wasm_ext>, &target_print_0_ext_verb, // Fixed extension, no use printing. - &file_search, - false /* see_through */})); + &target_search, // Note: don't look for an existing file. + target_type::flag::none})); if (install_loaded) { @@ -578,8 +624,6 @@ namespace build2 // Similar to alias. // - - //@@ outer r.insert<lib> (perform_id, 0, "bin.lib", lib_); r.insert<lib> (configure_id, 0, "bin.lib", lib_); @@ -600,6 +644,18 @@ namespace build2 if (rs.find_module ("dist")) { + // Note that without custom dist rules in setups along the follwing + // lines the source file will be unreachable by dist: + // + // lib{foo}: obj{foo} + // obja{foo}: cxx{foo} + // objs{foo}: cxx{foo} + // + r.insert<obj> (dist_id, 0, "bin.obj", obj_); + r.insert<bmi> (dist_id, 0, "bin.bmi", obj_); + r.insert<hbmi> (dist_id, 0, "bin.hbmi", obj_); + r.insert<libul> (dist_id, 0, "bin.libul", libul_); + r.insert<lib> (dist_id, 0, "bin.lib", lib_); } } @@ -626,7 +682,10 @@ namespace build2 // if (first) { - auto& vp (rs.var_pool ()); + // All the variables we enter are qualified so go straight for the + // public variable pool. + // + auto& vp (rs.var_pool (true /* public */)); vp.insert<path> ("config.bin.ar"); vp.insert<path> ("config.bin.ranlib"); @@ -684,7 +743,7 @@ namespace build2 nullptr, config::save_default_commented))); - const ar_info& ari (guess_ar (ar, ranlib, pat.paths)); + const ar_info& ari (guess_ar (rs.ctx, ar, ranlib, pat.paths)); // If this is a configuration with new values, then print the report // at verbosity level 2 and up (-v). @@ -723,8 +782,11 @@ namespace build2 } } - rs.assign<process_path_ex> ("bin.ar.path") = - process_path_ex (ari.ar_path, "ar", ari.ar_checksum); + rs.assign<process_path_ex> ("bin.ar.path") = process_path_ex ( + ari.ar_path, + "ar", + ari.ar_checksum, + hash_environment (ari.ar_environment)); rs.assign<string> ("bin.ar.id") = ari.ar_id; rs.assign<string> ("bin.ar.signature") = ari.ar_signature; rs.assign<string> ("bin.ar.checksum") = ari.ar_checksum; @@ -739,13 +801,20 @@ namespace build2 rs.assign<string> ("bin.ar.version.build") = v.build; } + config::save_environment (rs, ari.ar_environment); + if (ranlib != nullptr) { - rs.assign<process_path_ex> ("bin.ranlib.path") = - process_path_ex (ari.ranlib_path, "ranlib", ari.ranlib_checksum); + rs.assign<process_path_ex> ("bin.ranlib.path") = process_path_ex ( + ari.ranlib_path, + "ranlib", + ari.ranlib_checksum, + hash_environment (ari.ranlib_environment)); rs.assign<string> ("bin.ranlib.id") = ari.ranlib_id; rs.assign<string> ("bin.ranlib.signature") = ari.ranlib_signature; rs.assign<string> ("bin.ranlib.checksum") = ari.ranlib_checksum; + + config::save_environment (rs, ari.ranlib_environment); } } @@ -790,7 +859,10 @@ namespace build2 // if (first) { - auto& vp (rs.var_pool ()); + // All the variables we enter are qualified so go straight for the + // public variable pool. + // + auto& vp (rs.var_pool (true /* public */)); vp.insert<path> ("config.bin.ld"); } @@ -822,7 +894,7 @@ namespace build2 path (apply_pattern (ld_d, pat.pattern)), config::save_default_commented))); - const ld_info& ldi (guess_ld (ld, pat.paths)); + const ld_info& ldi (guess_ld (rs.ctx, ld, pat.paths)); // If this is a configuration with new values, then print the report // at verbosity level 2 and up (-v). @@ -854,8 +926,11 @@ namespace build2 << " checksum " << ldi.checksum; } - rs.assign<process_path_ex> ("bin.ld.path") = - process_path_ex (ldi.path, "ld", ldi.checksum); + rs.assign<process_path_ex> ("bin.ld.path") = process_path_ex ( + ldi.path, + "ld", + ldi.checksum, + hash_environment (ldi.environment)); rs.assign<string> ("bin.ld.id") = ldi.id; rs.assign<string> ("bin.ld.signature") = ldi.signature; rs.assign<string> ("bin.ld.checksum") = ldi.checksum; @@ -870,6 +945,8 @@ namespace build2 rs.assign<uint64_t> ("bin.ld.version.patch") = v.patch; rs.assign<string> ("bin.ld.version.build") = v.build; } + + config::save_environment (rs, ldi.environment); } return true; @@ -901,6 +978,8 @@ namespace build2 if (lid == "msvc") { + // @@ TODO: shouldn't this be wrapped in if(first) somehow? + const target_type& pdb ( rs.derive_target_type( target_type { @@ -911,8 +990,8 @@ namespace build2 nullptr, /* default_extension */ &target_pattern_fix<pdb_ext>, &target_print_0_ext_verb, // Fixed extension, no use printing. - &file_search, - false /* see_through */})); + &target_search, // Note: don't look for an existing file. + target_type::flag::none})); if (cast_false<bool> (rs["install.loaded"])) { @@ -943,7 +1022,10 @@ namespace build2 // if (first) { - auto& vp (rs.var_pool ()); + // All the variables we enter are qualified so go straight for the + // public variable pool. + // + auto& vp (rs.var_pool (true /* public */)); vp.insert<path> ("config.bin.rc"); } @@ -975,7 +1057,7 @@ namespace build2 path (apply_pattern (rc_d, pat.pattern)), config::save_default_commented))); - const rc_info& rci (guess_rc (rc, pat.paths)); + const rc_info& rci (guess_rc (rs.ctx, rc, pat.paths)); // If this is a configuration with new values, then print the report // at verbosity level 2 and up (-v). @@ -989,11 +1071,16 @@ namespace build2 << " checksum " << rci.checksum; } - rs.assign<process_path_ex> ("bin.rc.path") = - process_path_ex (rci.path, "rc", rci.checksum); + rs.assign<process_path_ex> ("bin.rc.path") = process_path_ex ( + rci.path, + "rc", + rci.checksum, + hash_environment (rci.environment)); rs.assign<string> ("bin.rc.id") = rci.id; rs.assign<string> ("bin.rc.signature") = rci.signature; rs.assign<string> ("bin.rc.checksum") = rci.checksum; + + config::save_environment (rs, rci.environment); } return true; @@ -1018,20 +1105,167 @@ namespace build2 return true; } + bool + nm_config_init (scope& rs, + scope& bs, + const location& loc, + bool first, + bool, + module_init_extra& extra) + { + tracer trace ("bin::nm_config_init"); + l5 ([&]{trace << "for " << bs;}); + + // Make sure bin.config is loaded. + // + load_module (rs, bs, "bin.config", loc, extra.hints); + + // Enter configuration variables. + // + if (first) + { + // All the variables we enter are qualified so go straight for the + // public variable pool. + // + auto& vp (rs.var_pool (true /* public */)); + + vp.insert<path> ("config.bin.nm"); + } + + // Configuration. + // + if (first) + { + using config::lookup_config; + + bool new_cfg (false); // Any new configuration values? + + // config.bin.nm + // + // Use the target to decide on the default nm name. Note that in case + // of win32-msvc this is insufficient and we fallback to the linker + // type (if available) to decide between dumpbin and llvm-nm (with + // fallback to dumpbin). + // + // Finally note that the dumpbin.exe functionality is available via + // link.exe /DUMP. + // + const string& tsys (cast<string> (rs["bin.target.system"])); + const char* nm_d (tsys == "win32-msvc" + ? (cast_empty<string> (rs["bin.ld.id"]) == "msvc-lld" + ? "llvm-nm" + : "dumpbin") + : "nm"); + + // This can be either a pattern or search path(s). + // + pattern_paths pat (lookup_pattern (rs)); + + const path& nm ( + cast<path> ( + lookup_config (new_cfg, + rs, + "config.bin.nm", + path (apply_pattern (nm_d, pat.pattern)), + config::save_default_commented))); + + const nm_info& nmi (guess_nm (rs.ctx, nm, pat.paths)); + + // If this is a configuration with new values, then print the report + // at verbosity level 2 and up (-v). + // + if (verb >= (new_cfg ? 2 : 3)) + { + text << "bin.nm " << project (rs) << '@' << rs << '\n' + << " nm " << nmi.path << '\n' + << " id " << nmi.id << '\n' + << " signature " << nmi.signature << '\n' + << " checksum " << nmi.checksum; + } + + rs.assign<process_path_ex> ("bin.nm.path") = process_path_ex ( + nmi.path, + "nm", + nmi.checksum, + hash_environment (nmi.environment)); + rs.assign<string> ("bin.nm.id") = nmi.id; + rs.assign<string> ("bin.nm.signature") = nmi.signature; + rs.assign<string> ("bin.nm.checksum") = nmi.checksum; + + config::save_environment (rs, nmi.environment); + } + + return true; + } + + bool + nm_init (scope& rs, + scope& bs, + const location& loc, + bool, + bool, + module_init_extra& extra) + { + tracer trace ("bin::nm_init"); + l5 ([&]{trace << "for " << bs;}); + + // Make sure the bin core and nm.config are loaded. + // + load_module (rs, bs, "bin", loc, extra.hints); + load_module (rs, bs, "bin.nm.config", loc, extra.hints); + + return true; + } + + bool + def_init (scope& rs, + scope& bs, + const location& loc, + bool, + bool, + module_init_extra& extra) + { + tracer trace ("bin::def_init"); + l5 ([&]{trace << "for " << bs;}); + + // Make sure the bin core is loaded (def{} target type). We also load + // nm.config unless we are using MSVC link.exe and can access dumpbin + // via its /DUMP option. + // + const string* lid (cast_null<string> (rs["bin.ld.id"])); + + load_module (rs, bs, "bin", loc, extra.hints); + + if (lid == nullptr || *lid != "msvc") + load_module (rs, bs, "bin.nm.config", loc, extra.hints); + + // Register the def{} rule. + // + bs.insert_rule<def> (perform_update_id, "bin.def", def_); + bs.insert_rule<def> (perform_clean_id, "bin.def", def_); + bs.insert_rule<def> (configure_update_id, "bin.def", def_); + + return true; + } + static const module_functions mod_functions[] = { // NOTE: don't forget to also update the documentation in init.hxx if // changing anything here. {"bin.vars", nullptr, vars_init}, + {"bin.types", nullptr, types_init}, {"bin.config", nullptr, config_init}, - {"bin", nullptr, init}, {"bin.ar.config", nullptr, ar_config_init}, {"bin.ar", nullptr, ar_init}, {"bin.ld.config", nullptr, ld_config_init}, {"bin.ld", nullptr, ld_init}, {"bin.rc.config", nullptr, rc_config_init}, {"bin.rc", nullptr, rc_init}, + {"bin.nm.config", nullptr, nm_config_init}, + {"bin.nm", nullptr, nm_init}, + {"bin.def", nullptr, def_init}, + {"bin", nullptr, init}, {nullptr, nullptr, nullptr} }; |