From 2164f1544357c0bad38fd23885290a099a1beb3a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 26 Nov 2024 08:27:07 +0200 Subject: Sanitize as identifiers /.. --- libbuild2/cc/common.cxx | 9 +++++++-- libbuild2/file.cxx | 31 +++++++++++++++++++------------ 2 files changed, 26 insertions(+), 14 deletions(-) (limited to 'libbuild2') diff --git a/libbuild2/cc/common.cxx b/libbuild2/cc/common.cxx index ae89a85..c1e60d2 100644 --- a/libbuild2/cc/common.cxx +++ b/libbuild2/cc/common.cxx @@ -1017,7 +1017,10 @@ namespace build2 // auto lookup_import = [&rs, &act, - &name, + namev = + (p.proj + ? sanitize_identifier (name) + : string ()), projv = (p.proj ? p.proj->variable () @@ -1025,7 +1028,9 @@ namespace build2 { if (!projv.empty ()) { - string varn ("config.import." + projv + '.' + name + '.' + tt); + // Note: we know tt is liba or libs and need not to be sanitized. + // + string varn ("config.import." + projv + '.' + namev + '.' + tt); if (config::specified_config (rs, varn, true /* exact */)) { diff --git a/libbuild2/file.cxx b/libbuild2/file.cxx index a6b62c0..f755506 100644 --- a/libbuild2/file.cxx +++ b/libbuild2/file.cxx @@ -2111,14 +2111,17 @@ namespace build2 dr << info << "use config.import." << pv << " configuration variable to " << "specify its " << (qual != nullptr ? qual : "") << "project out_root"; - // Suggest ad hoc import but only if it's a path-based target (doing it - // for lib{} is very confusing). + // Suggest ad hoc import. Note that now we do it even for non-path-based + // targets since we have ad hoc import phase 2 logic for lib{}. // - if (tt != nullptr && tt->is_a ()) + if (tt != nullptr) { - string v (tt->is_a () && (pv == tn || pn == tn) + string nv (sanitize_identifier (tn)); + + string v (tt->is_a () && pv == nv ? "config." + pv - : "config.import." + pv + '.' + tn + '.' + tt->name); + : ("config.import." + pv + '.' + nv + '.' + + sanitize_identifier (tt->name))); dr << info << "or use " << v << " configuration variable to specify " << "its " << (qual != nullptr ? qual : "") << "path"; @@ -2266,16 +2269,15 @@ namespace build2 // recognize the special config. (tool importation; we could // also handle the case where is not the same as via // the config.. variable). For backwards-compatibility - // reasons, it takes precedence over config.import. + // reasons, it takes precedence over config.import. Note that both + // and must be sanitized to a valid identifier (think + // libs{build2-foo} and cli.cxx{}). // // Note also that phase 2 import may handle these imports in an ad hoc // manner (see cc::search_library() for an example). // // Note: see import phase 2 diagnostics if changing anything here. // - // @@ How will this work for snake-case targets, say libs{build2-foo}? - // As well as for dot-separated target types, say, cli.cxx{}? - // // @@ This duality has a nasty side-effect: if we have config. // configured, then specifying config..import has no effect // (see also a note below on priority just among these options). @@ -2333,6 +2335,8 @@ namespace build2 // const path* p (nullptr); + string valv (sanitize_identifier (string (tgt.value))); + if (tgt.typed ()) { bool e (tgt.type == "exe"); @@ -2341,15 +2345,18 @@ namespace build2 // overridable variable of type path. The config. we have to // type manually. // - if (e && (projv == tgt.value || proj == tgt.value)) + if (e && projv == valv) p = lookup (vp.insert ("config." + projv), e); if (p == nullptr) - p = lookup (vp.insert (n + '.' + tgt.value + '.' + tgt.type), e); + { + string ttv (sanitize_identifier (string (tgt.type))); + p = lookup (vp.insert (n + '.' + valv + '.' + ttv), e); + } } if (p == nullptr) - p = lookup (vp.insert (n + '.' + tgt.value), false); + p = lookup (vp.insert (n + '.' + valv), false); if (p != nullptr) { -- cgit v1.1