// file : libbuild2/cc/functions.cxx -*- C++ -*- // license : MIT; see accompanying LICENSE file #include #include #include #include #include #include #include #include namespace build2 { const target& to_target (const scope&, name&&, name&&); // libbuild2/functions-name.cxx namespace cc { using namespace bin; // Common thunk for $x.lib_*(, [, ...]) functions. // struct lib_data { const char* x; void (*f) (strings&, const vector_view&, const module&, const scope&, action, const file&, bool, linfo); }; static value lib_thunk (const scope* bs, vector_view vs, const function_overload& f) { const lib_data& d (*reinterpret_cast (&f.data)); if (bs == nullptr) fail << f.name << " called out of scope"; const scope* rs (bs->root_scope ()); if (rs == nullptr) fail << f.name << " called out of project"; if (bs->ctx.phase != run_phase::execute) fail << f.name << " can only be called during execution"; const module* m (rs->find_module (d.x)); if (m == nullptr) fail << f.name << " called without " << d.x << " module being loaded"; // We can assume these are present due to function's types signature. // names& ts_ns (vs[0].as ()); // names& ot_ns (vs[1].as ()); // linfo li; { string t (convert (move (ot_ns))); const target_type* tt (bs->find_target_type (t)); if (tt == nullptr) fail << "unknown target type '" << t << "'"; // Try both linker and compiler output types. // otype ot (link_type (*tt).type); switch (ot) { case otype::e: case otype::a: case otype::s: break; default: ot = compile_type (*tt); switch (ot) { case otype::e: case otype::a: case otype::s: break; default: fail << "target type " << t << " is not compiler/linker output"; } } li = link_info (*bs, ot); } // In a somewhat hackish way strip the outer operation to match how we // call the underlying functions in the compile/link rules. This should // be harmless since ad hoc recipes are always for the inner operation. // action a (rs->ctx.current_action ().inner_action ()); strings r; for (auto i (ts_ns.begin ()); i != ts_ns.end (); ++i) { name& n (*i), o; const target& t (to_target (*bs, move (n), move (n.pair ? *++i : o))); const file* f; bool la (false); if ((la = (f = t.is_a ())) || (la = (f = t.is_a ())) || ( (f = t.is_a ()))) { d.f (r, vs, *m, *bs, a, *f, la, li); } else fail << t << " is not a library target"; } return value (move (r)); } void compile_rule:: functions (function_family& f, const char* x) { // $.lib_poptions(, ) // // Return the preprocessor options that should be passed when compiling // sources that depend on the specified libraries. The second argument // is the output target type (obje, objs, etc). // // Note that this function can only be called during execution after all // the specified library targets have been matched. Normally it is used // in ad hoc recipes to implement custom compilation. // f[".lib_poptions"].insert ( &lib_thunk, lib_data { x, [] (strings& r, const vector_view&, const module& m, const scope& bs, action a, const file& l, bool la, linfo li) { m.append_lib_options (r, bs, a, l, la, li); }}); } void link_rule:: functions (function_family& f, const char* x) { // $.lib_libs(, [, [, ]]) // // Return the libraries (and any associated options) that should be // passed when linking targets that depend on the specified libraries. // The second argument is the output target type (exe, libs, etc). // // The following flags are supported: // // whole - link the specified libraries in the whole archive mode // // If the last argument is false, then do not return the specified // libraries themselves. // // Note that this function can only be called during execution after all // the specified library targets have been matched. Normally it is used // in ad hoc recipes to implement custom linking. // f[".lib_libs"].insert, optional> ( &lib_thunk, lib_data { x, [] (strings& r, const vector_view& vs, const module& m, const scope& bs, action a, const file& l, bool la, linfo li) { lflags lf (0); if (vs.size () > 2) { for (const name& f: vs[2].as ()) { string s (convert (name (f))); if (s == "whole") lf |= lflag_whole; else fail << "invalid flag '" << s << "'"; } } bool self (vs.size () > 3 ? convert (vs[3]) : true); m.append_libraries (r, bs, a, l, la, lf, li, self); }}); // $.lib_rpaths(, [, [, ]]) // // Return the rpath options that should be passed when linking targets // that depend on the specified libraries. The second argument is the // output target type (exe, libs, etc). // // If the third argument is true, then use rpath-link options rather // than rpath (which is what should normally be used when linking for // install, for example). // // If the last argument is false, then do not return the options for the // specified libraries themselves. // // Note that this function can only be called during execution after all // the specified library targets have been matched. Normally it is used // in ad hoc recipes to implement custom linking. // f[".lib_rpaths"].insert, optional> ( &lib_thunk, lib_data { x, [] (strings& r, const vector_view& vs, const module& m, const scope& bs, action a, const file& l, bool la, linfo li) { bool link (vs.size () > 2 ? convert (vs[2]) : false); bool self (vs.size () > 3 ? convert (vs[3]) : true); m.rpath_libraries (r, bs, a, l, la, li, link, self); }}); } } }