aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/config/operation.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2019-11-04 13:34:52 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2019-11-04 15:15:30 +0200
commitb39ce46b80ef5cccc592398e0a74ba8d02742ead (patch)
treea7ccf1a628ea8cf6a5804d4ad6c0b704fade5807 /libbuild2/config/operation.cxx
parent41a31b0a61464fd506166887f621100364e67276 (diff)
Add $config.export() function
This is similar to the config.export variable functionality except it can be called from within buildfiles. Note that this function can only be used during configure unless the config module creation was forced for other meta-operations with config.module=true in bootstrap.build.
Diffstat (limited to 'libbuild2/config/operation.cxx')
-rw-r--r--libbuild2/config/operation.cxx78
1 files changed, 48 insertions, 30 deletions
diff --git a/libbuild2/config/operation.cxx b/libbuild2/config/operation.cxx
index 9050645..c9b5ef6 100644
--- a/libbuild2/config/operation.cxx
+++ b/libbuild2/config/operation.cxx
@@ -4,8 +4,6 @@
#include <libbuild2/config/operation.hxx>
-#include <set>
-
#include <libbuild2/file.hxx>
#include <libbuild2/spec.hxx>
#include <libbuild2/scope.hxx>
@@ -90,26 +88,23 @@ namespace build2
// If inherit is false, then don't rely on inheritance from outer scopes
// (used for config.export).
//
- static void
+ void
save_config (const scope& rs,
- const path& f,
+ ostream& os,
+ const string& name,
bool inherit,
const project_set& projects)
{
context& ctx (rs.ctx);
- const module& mod (*rs.lookup_module<const module> (module::name));
+ const module* mod (rs.lookup_module<const module> (module::name));
- const string& df (f.string () != "-" ? f.string () : "<stdout>");
-
- if (verb)
- text << (verb >= 2 ? "cat >" : "save ") << df;
+ if (mod == nullptr)
+ fail << "no configuration information available during this meta-"
+ << "operation";
try
{
- ofdstream ofs;
- ostream& os (open_file_or_stdout (f, ofs));
-
os << "# Created automatically by the config module, but feel " <<
"free to edit." << endl
<< "#" << endl;
@@ -132,7 +127,7 @@ namespace build2
//
names storage;
- for (auto p: mod.saved_modules.order)
+ for (auto p: mod->saved_modules.order)
{
const string& sname (p.second->first);
const saved_variables& svars (p.second->second);
@@ -170,7 +165,8 @@ namespace build2
// still valid and we should probably continue using them but we
// definitely want to move them to our config.build since they
// will be dropped from the amalgamation's config.build on the
- // next reconfigure. Let's also warn the user just in case.
+ // next reconfigure. Let's also warn the user just in case,
+ // unless there is no module and thus we couldn't really check.
//
// There is also another case that falls under this now that
// overrides are by default amalgamation-wide rather than just
@@ -178,13 +174,13 @@ namespace build2
// subproject but the override is now set on the outer project's
// root.
//
- bool found (false);
+ bool found (false), checked (true);
const scope* r (&rs);
while ((r = r->parent_scope ()->root_scope ()) != nullptr)
{
if (l.belongs (*r))
{
- // Find the config module.
+ // Find the config module (might not be there).
//
if (auto* m = r->lookup_module<const module> (module::name))
{
@@ -215,6 +211,8 @@ namespace build2
found = false;
}
}
+ else
+ checked = false;
break;
}
@@ -229,32 +227,30 @@ namespace build2
}
else
{
- location loc (&f);
-
// If this value is not defined in a project's root scope,
// then something is broken.
//
if (r == nullptr)
- fail (loc) << "inherited variable " << var << " value "
- << "is not from a root scope";
+ fail << name << ": inherited variable " << var << " value "
+ << "is not from a root scope";
// If none of the outer project's configurations use this
- // value, then we warn and save as our own. One special case
- // where we don't want to warn the user is if the variable is
- // overriden.
+ // value, then we warn (unless we couldn't check) and save as
+ // our own. One special case where we don't want to warn the
+ // user is if the variable is overriden.
//
- if (org.first == ovr.first)
+ if (checked && org.first == ovr.first)
{
diag_record dr;
- dr << warn (loc) << "saving previously inherited variable "
- << var;
+ dr << warn << name << ": saving previously inherited "
+ << "variable " << var;
- dr << info (loc) << "because project " << *r
- << " no longer uses it in its configuration";
+ dr << info << "because project " << *r << " no longer uses "
+ << "it in its configuration";
if (verb >= 2)
{
- dr << info (loc) << "variable value: ";
+ dr << info << "variable value: ";
if (*l)
{
@@ -322,12 +318,34 @@ namespace build2
os << n << " = [null]" << endl;
}
}
+ }
+ catch (const io_error& e)
+ {
+ fail << "unable to write " << name << ": " << e;
+ }
+ }
+ static void
+ save_config (const scope& rs,
+ const path& f,
+ bool inherit,
+ const project_set& projects)
+ {
+ const string& fs (f.string () != "-" ? f.string () : "<stdout>");
+
+ if (verb)
+ text << (verb >= 2 ? "cat >" : "save ") << fs;
+
+ try
+ {
+ ofdstream ofs;
+ ostream& os ();
+ save_config (rs, open_file_or_stdout (f, ofs), fs, inherit, projects);
ofs.close ();
}
catch (const io_error& e)
{
- fail << "unable to write " << df << ": " << e;
+ fail << "unable to write " << fs << ": " << e;
}
}