From 41a31b0a61464fd506166887f621100364e67276 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 4 Nov 2019 09:37:30 +0200 Subject: Add support for configuration exporting and importing The new config.export variable specifies the alternative file to write the configuration to as part of the configure meta-operation. For example: $ b configure: proj/ config.export=proj-config.build The config.export value "applies" only to the projects on whose root scope it is specified or if it is a global override (the latter is a bit iffy but we allow it, for example, to dump everything to stdout). This means that in order to save a subproject's configuration we will have to use a scope-specific override (since the default will apply to the outermost amalgamation). For example: $ b configure: subproj/ subproj/config.export=.../subproj-config.build This could be somewhat unnatural but then it will be the amalgamation whose configuration we normally want to export. The new config.import variable specifies additional configuration files to be loaded after the project's default config.build, if any. For example: $ b create: cfg/,cc config.import=my-config.build Similar to config.export, the config.import value "applies" only to the project on whose root scope it is specified or if it is a global override. This allows the use of the standard override "positioning" machinery (i.e., where the override applies) to decide where the extra configuration files are loaded. The resulting semantics is quite natural and consistent with command line variable overrides, for example: $ b config.import=.../config.build # outermost amalgamation $ b ./config.import=.../config.build # this project $ b !config.import=.../config.build # every project Both config.export and config.import recognize the special `-` file name as an instruction to write/read to/from stdout/stdin, respectively. For example: $ b configure: src-prj/ config.export=- | b configure: dst-prj/ config.import=- --- libbuild2/file.cxx | 95 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 24 deletions(-) (limited to 'libbuild2/file.cxx') diff --git a/libbuild2/file.cxx b/libbuild2/file.cxx index 603943c..b55d576 100644 --- a/libbuild2/file.cxx +++ b/libbuild2/file.cxx @@ -4,8 +4,6 @@ #include -#include // cin - #include #include #include @@ -161,27 +159,39 @@ namespace build2 } static void - source (scope& root, scope& base, const path& bf, bool boot) + source (scope& root, scope& base, lexer& l, bool boot) { tracer trace ("source"); + const path& bf (l.name ()); + try { - bool sin (bf.string () == "-"); - - ifdstream ifs; - - if (!sin) - ifs.open (bf); - else - cin.exceptions (ifdstream::failbit | ifdstream::badbit); - - istream& is (sin ? cin : ifs); - l5 ([&]{trace << "sourcing " << bf;}); parser p (root.ctx, boot); - p.parse_buildfile (is, bf, root, base); + p.parse_buildfile (l, root, base); + } + catch (const io_error& e) + { + fail << "unable to read buildfile " << bf << ": " << e; + } + } + + static void + source (scope& root, scope& base, istream& is, const path& bf, bool boot) + { + lexer l (is, bf); + source (root, base, l, boot); + } + + static void + source (scope& root, scope& base, const path& bf, bool boot) + { + try + { + ifdstream ifs; + return source (root, base, open_file_or_stdin (bf, ifs), bf, boot); } catch (const io_error& e) { @@ -195,6 +205,18 @@ namespace build2 source (root, base, bf, false); } + void + source (scope& root, scope& base, istream& is, const path& bf) + { + source (root, base, is, bf, false); + } + + void + source (scope& root, scope& base, lexer& l) + { + source (root, base, l, false); + } + bool source_once (scope& root, scope& base, const path& bf, scope& once) { @@ -206,7 +228,7 @@ namespace build2 return false; } - source (root, base, bf); + source (root, base, bf, false); return true; } @@ -498,18 +520,17 @@ namespace build2 } pair - extract_variable (context& ctx, const path& bf, const variable& var) + extract_variable (context& ctx, lexer& l, const variable& var) { + const path& bf (l.name ()); + try { - ifdstream ifs (bf); - - lexer lex (ifs, bf); - token t (lex.next ()); + token t (l.next ()); token_type tt; if (t.type != token_type::word || t.value != var.name || - ((tt = lex.next ().type) != token_type::assign && + ((tt = l.next ().type) != token_type::assign && tt != token_type::prepend && tt != token_type::append)) { @@ -518,7 +539,7 @@ namespace build2 parser p (ctx); temp_scope tmp (ctx.global_scope.rw ()); - p.parse_variable (lex, tmp, var, tt); + p.parse_variable (l, tmp, var, tt); value* v (tmp.vars.find_to_modify (var).first); assert (v != nullptr); @@ -533,6 +554,32 @@ namespace build2 } } + pair + extract_variable (context& ctx, + istream& is, + const path& bf, + const variable& var) + { + lexer l (is, bf); + return extract_variable (ctx, l, var); + } + + pair + extract_variable (context& ctx, const path& bf, const variable& var) + { + try + { + ifdstream ifs (bf); + return extract_variable (ctx, ifs, bf, var); + } + catch (const io_error& e) + { + fail << "unable to read buildfile " << bf << ": " << e << endf; + } + } + + + // Extract the project name from bootstrap.build. // static project_name @@ -745,7 +792,7 @@ namespace build2 // root scope multiple time. // if (rs.buildfiles.insert (f).second) - source (rs, rs, f, true); + source (rs, rs, f, true /* boot */); else l5 ([&]{trace << "skipping already sourced " << f;}); -- cgit v1.1