From b6671c9a5840077911cb23f5434c4a7309069e2f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 23 Jul 2018 16:09:40 +0200 Subject: Sanitize project name when forming config.import. variable Specifically, '-', '+', and '.' are replaced with '_' to form a "canonical" variable name. --- build2/file.cxx | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/build2/file.cxx b/build2/file.cxx index 3cb6980..1367b8c 100644 --- a/build2/file.cxx +++ b/build2/file.cxx @@ -1082,6 +1082,24 @@ namespace build2 return rs; } + // Convert project name to import variable name. + // + static inline string + import_name (const string& p) + { + // For simplicity let's assume project name is a package name and only + // sanitize characters that we don't like from what's valid there. + // + auto sanitize = [] (char c) + { + return (c == '-' || c == '+' || c == '.') ? '_' : c; + }; + + string r; + transform (p.begin (), p.end (), back_inserter (r), sanitize); + return r; + } + names import (scope& ibase, name target, const location& loc) { @@ -1119,7 +1137,7 @@ namespace build2 for (;;) // Break-out loop. { - string n ("config.import." + proj); + string n ("config.import." + import_name (proj)); // config.import. // @@ -1404,7 +1422,7 @@ namespace build2 tracer trace ("import"); assert (pk.proj); - const string& p (*pk.proj); + const string& proj (*pk.proj); // Target type-specific search. // @@ -1470,12 +1488,12 @@ namespace build2 diag_record dr; dr << fail << "unable to import target " << pk; - if (p.empty ()) + if (proj.empty ()) dr << info << "consider adding its installation location" << info << "or explicitly specify its project name"; else - dr << info << "use config.import." << p << " command line variable to " - << "specifying its project out_root"; + dr << info << "use config.import." << import_name (proj) + << " command line variable to specifying its project out_root"; dr << endf; } -- cgit v1.1