From 0dee00f28e623830e816c4002c8004c86055df85 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 1 Apr 2015 11:08:13 +0200 Subject: Implement initial C++ configuration support --- build/cxx/module | 19 ++++++++ build/cxx/module.cxx | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++ build/cxx/rule.cxx | 134 +++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 261 insertions(+), 26 deletions(-) create mode 100644 build/cxx/module create mode 100644 build/cxx/module.cxx (limited to 'build/cxx') diff --git a/build/cxx/module b/build/cxx/module new file mode 100644 index 0000000..65cdcc6 --- /dev/null +++ b/build/cxx/module @@ -0,0 +1,19 @@ +// file : build/cxx/module -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef BUILD_CXX_MODULE +#define BUILD_CXX_MODULE + +#include + +namespace build +{ + namespace cxx + { + void + init (scope&, scope&, const location&); + } +} + +#endif // BUILD_CXX_MODULE diff --git a/build/cxx/module.cxx b/build/cxx/module.cxx new file mode 100644 index 0000000..eb7ca40 --- /dev/null +++ b/build/cxx/module.cxx @@ -0,0 +1,134 @@ +// file : build/cxx/module.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include + +#include +#include + +#include +#include +#include +#include + +using namespace std; + +namespace build +{ + namespace cxx + { + void + init (scope& root, scope& base, const location& l) + { + //@@ TODO: avoid multiple inits (generally, for modules). + // + + tracer trace ("cxx::init"); + + //@@ Should it be this way? + // + if (&root != &base) + fail (l) << "cxx module must be initialized in project root scope"; + + //@@ TODO: need to register target types, rules here instead of main(). + + const path& out_root (root.path ()); + level4 ([&]{trace << "for " << out_root << '/';}); + + // Configure. + // + + // config.cxx + // + for (bool f (true); f; f = false) + { + auto val (root["config.cxx"]); + + string v; + + if (val) + { + if (&val.scope () != global_scope) + break; // A value from config.build. + + v = val.as (); + } + else + v = "g++"; // Default. + + // Test it by trying to execute. + // + const char* args[] = {v.c_str (), "-dumpversion", nullptr}; + + if (verb >= 1) + print_process (args); + else + text << "test " << v; + + string ver; + try + { + process pr (args, false, false, true); + + __gnu_cxx::stdio_filebuf fb (pr.in_ofd, ios_base::in); + istream is (&fb); + + bool r (getline (is, ver)); + + if (!pr.wait ()) + throw failed (); + + if (!r) + fail << "unexpected output from " << v; + } + catch (const process_error& e) + { + error << "unable to execute " << v << ": " << e.what (); + + if (e.child ()) + exit (1); + + throw failed (); + } + + text << "toolchain version " << ver; + + // Set on the project root. + // + root.variables["config.cxx"] = move (v); + } + + // config.cxx.{p,c,l}options + // config.cxx.libs + // + // These are optional so all we need to do is "import" them + // into the root scope if they were specified on the command + // line. + // + if (auto val = root["config.cxx.poptions"]) + { + if (&val.scope () == global_scope) + root.variables["config.cxx.poptions"] = val; + } + + if (auto val = root["config.cxx.coptions"]) + { + if (&val.scope () == global_scope) + root.variables["config.cxx.coptions"] = val; + } + + if (auto val = root["config.cxx.loptions"]) + { + if (&val.scope () == global_scope) + root.variables["config.cxx.loptions"] = val; + } + + if (auto val = root["config.cxx.libs"]) + { + if (&val.scope () == global_scope) + root.variables["config.cxx.libs"] = val; + } + } + } +} diff --git a/build/cxx/rule.cxx b/build/cxx/rule.cxx index 549d987..8d7f3c3 100644 --- a/build/cxx/rule.cxx +++ b/build/cxx/rule.cxx @@ -9,6 +9,7 @@ #include // size_t #include // exit #include // move() +#include #include @@ -25,6 +26,36 @@ namespace build { namespace cxx { + static void + append_options (vector& args, scope& s, const char* var) + { + if (auto val = s[var]) + { + for (const name& n: val.as ().data) + { + if (!n.type.empty () || !n.dir.empty ()) + fail << "expected option instead of " << n << + info << "in variable " << var; + + args.push_back (n.value.c_str ()); + } + } + } + + static void + append_std (vector& args, scope& s, string& opt) + { + if (auto val = s["cxx.std"]) + { + const string& v (val.as ()); + + // @@ Need to translate 11 to 0x for older versions. + // + opt = "-std=c++" + v; + args.push_back (opt.c_str ()); + } + } + // compile // void* compile:: @@ -151,19 +182,39 @@ namespace build { tracer trace ("cxx::compile::inject_prerequisites"); + scope& ts (scopes.find (o.path ())); + const string& cxx (ts["config.cxx"].as ()); + + vector args {cxx.c_str ()}; + + append_options (args, ts, "config.cxx.poptions"); + append_options (args, ts, "cxx.poptions"); + + // @@ Some C++ options (e.g., -std, -m) affect the preprocessor. + // Or maybe they are not C++ options? Common options? + // + append_options (args, ts, "config.cxx.coptions"); + + string std; // Storage. + append_std (args, ts, std); + + append_options (args, ts, "cxx.coptions"); + + args.push_back ("-MM"); // @@ Change to -M + args.push_back ("-MG"); // Treat missing headers as generated. + args.push_back ("-MQ"); // Quoted target name. + args.push_back ("*"); // Old versions can't handle empty target name. + // We are using absolute source file path in order to get - // absolute paths in the result. @@ We will also have to - // use absolute -I paths to guarantee that. - // - const char* args[] = { - "g++-4.9", - "-std=c++14", - "-I", ds["src_root"].as ().string ().c_str (), - "-MM", //@@ -M - "-MG", // Treat missing headers as generated. - "-MQ", "*", // Quoted target (older version can't handle empty name). - s.path ().string ().c_str (), - nullptr}; + // absolute paths in the result. Any relative paths in the + // result are non-existent generated headers. + // + // @@ We will also have to use absolute -I paths to guarantee + // that. + // + args.push_back (s.path ().string ().c_str ()); + + args.push_back (nullptr); if (verb >= 2) print_process (args); @@ -172,7 +223,7 @@ namespace build try { - process pr (args, false, false, true); + process pr (args.data (), false, false, true); __gnu_cxx::stdio_filebuf fb (pr.in_ofd, ios_base::in); istream is (&fb); @@ -285,15 +336,28 @@ namespace build path ro (relative (o.path ())); path rs (relative (s->path ())); - const char* args[] = { - "g++-4.9", - "-std=c++14", - "-g", - "-I", o.prerequisites[0].get ().scope["src_root"].as ().string ().c_str (), - "-c", - "-o", ro.string ().c_str (), - rs.string ().c_str (), - nullptr}; + scope& ts (scopes.find (o.path ())); + const string& cxx (ts["config.cxx"].as ()); + + vector args {cxx.c_str ()}; + + append_options (args, ts, "config.cxx.poptions"); + append_options (args, ts, "cxx.poptions"); + + append_options (args, ts, "config.cxx.coptions"); + + string std; // Storage. + append_std (args, ts, std); + + append_options (args, ts, "cxx.coptions"); + + args.push_back ("-o"); + args.push_back (ro.string ().c_str ()); + + args.push_back ("-c"); + args.push_back (rs.string ().c_str ()); + + args.push_back (nullptr); if (verb >= 1) print_process (args); @@ -302,7 +366,7 @@ namespace build try { - process pr (args); + process pr (args.data ()); if (!pr.wait ()) throw failed (); @@ -509,9 +573,10 @@ namespace build prerequisite* cp1 (nullptr); for (prerequisite& p: ot.prerequisites) { - // Ignore some known target types (headers). + // Ignore some known target types (fsdir, headers). // - if (p.type.id == typeid (h) || + if (p.type.id == typeid (fsdir) || + p.type.id == typeid (h) || (cp.type.id == typeid (cxx) && (p.type.id == typeid (hxx) || p.type.id == typeid (ixx) || p.type.id == typeid (txx)))) @@ -583,10 +648,24 @@ namespace build path re (relative (e.path ())); vector ro; - vector args {"g++-4.9", "-std=c++14", "-g", "-o"}; + scope& ts (scopes.find (e.path ())); + const string& cxx (ts["config.cxx"].as ()); + + vector args {cxx.c_str ()}; + append_options (args, ts, "config.cxx.coptions"); + + string std; // Storage. + append_std (args, ts, std); + + append_options (args, ts, "cxx.coptions"); + + args.push_back ("-o"); args.push_back (re.string ().c_str ()); + append_options (args, ts, "config.cxx.loptions"); + append_options (args, ts, "cxx.loptions"); + for (const prerequisite& p: t.prerequisites) { if (const obj* o = dynamic_cast (p.target)) @@ -596,6 +675,9 @@ namespace build } } + append_options (args, ts, "config.cxx.libs"); + append_options (args, ts, "cxx.libs"); + args.push_back (nullptr); if (verb >= 1) -- cgit v1.1