aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/utility.ixx
diff options
context:
space:
mode:
Diffstat (limited to 'libbuild2/utility.ixx')
-rw-r--r--libbuild2/utility.ixx259
1 files changed, 247 insertions, 12 deletions
diff --git a/libbuild2/utility.ixx b/libbuild2/utility.ixx
index 9a77fae..58ea8db 100644
--- a/libbuild2/utility.ixx
+++ b/libbuild2/utility.ixx
@@ -6,42 +6,195 @@
namespace build2
{
inline bool
- run_wait (cstrings& args, process& pr, const location& loc)
+ run_wait (const cstrings& args, process& pr, const location& loc)
{
return run_wait (args.data (), pr, loc);
}
- // Note: currently this function is also used in a run() implementations.
+ // Note: these functions are also used in the run() implementations.
//
LIBBUILD2_SYMEXPORT bool
- run_finish_impl (const char*[],
+ run_finish_impl (const char* const*,
process&,
- bool error,
+ bool fail,
const string&,
- const location& = location ());
+ uint16_t,
+ bool = false,
+ const location& = {});
+
+ LIBBUILD2_SYMEXPORT bool
+ run_finish_impl (diag_buffer&,
+ const char* const*,
+ process&,
+ bool fail,
+ uint16_t,
+ bool = false,
+ const location& = {});
inline void
- run_finish (const char* args[],
+ run_finish (const char* const* args,
process& pr,
- const string& l,
+ uint16_t v,
+ bool on,
+ const location& loc)
+ {
+ run_finish_impl (args, pr, true /* fail */, string (), v, on, loc);
+ }
+
+ inline void
+ run_finish (const cstrings& args,
+ process& pr,
+ uint16_t v,
+ bool on,
const location& loc)
{
- run_finish_impl (args, pr, true /* error */, l, loc);
+ run_finish (args.data (), pr, v, on, loc);
}
inline void
- run_finish (cstrings& args, process& pr, const location& loc)
+ run_finish (const char* const* args,
+ process& pr,
+ const string& l,
+ uint16_t v,
+ bool on,
+ const location& loc)
+ {
+ run_finish_impl (args, pr, true, l, v, on, loc);
+ }
+
+ inline bool
+ run_finish_code (const char* const* args,
+ process& pr,
+ uint16_t v,
+ bool on,
+ const location& loc)
+ {
+ return run_finish_impl (args, pr, false, string (), v, on, loc);
+ }
+
+ inline bool
+ run_finish_code (const cstrings& args,
+ process& pr,
+ uint16_t v,
+ bool on,
+ const location& loc)
{
- run_finish (args.data (), pr, string (), loc);
+ return run_finish_code (args.data (), pr, v, on, loc);
}
inline bool
- run_finish_code (const char* args[],
+ run_finish_code (const char* const* args,
process& pr,
const string& l,
+ uint16_t v,
+ bool on,
+ const location& loc)
+ {
+ return run_finish_impl (args, pr, false, l, v, on, loc);
+ }
+
+ inline void
+ run_finish (diag_buffer& dbuf,
+ const char* const* args,
+ process& pr,
+ uint16_t v,
+ bool on,
+ const location& loc)
+ {
+ run_finish_impl (dbuf, args, pr, true /* fail */, v, on, loc);
+ }
+
+ inline void
+ run_finish (diag_buffer& dbuf,
+ const cstrings& args,
+ process& pr,
+ uint16_t v,
+ bool on,
+ const location& loc)
+ {
+ run_finish_impl (dbuf, args.data (), pr, true, v, on, loc);
+ }
+
+ inline bool
+ run_finish_code (diag_buffer& dbuf,
+ const char* const* args,
+ process& pr,
+ uint16_t v,
+ bool on,
+ const location& loc)
+ {
+ return run_finish_impl (dbuf, args, pr, false, v, on, loc);
+ }
+
+ inline bool
+ run_finish_code (diag_buffer& dbuf,
+ const cstrings& args,
+ process& pr,
+ uint16_t v,
+ bool on,
const location& loc)
{
- return run_finish_impl (args, pr, false /* error */, l, loc);
+ return run_finish_impl (dbuf, args.data (), pr, false, v, on, loc);
+ }
+
+ template <typename T, typename F>
+ inline T
+ run (context& ctx,
+ uint16_t verbosity,
+ const process_env& pe,
+ const char* const* args,
+ F&& f,
+ bool err,
+ bool ignore_exit,
+ sha256* checksum)
+ {
+ T r;
+ if (!run (ctx,
+ verbosity,
+ pe, args,
+ verbosity - 1,
+ [&r, &f] (string& l, bool last) // Small function optimmization.
+ {
+ r = f (l, last);
+ return r.empty ();
+ },
+ true /* trim */,
+ err,
+ ignore_exit,
+ checksum))
+ r = T ();
+
+ return r;
+ }
+
+ template <typename T, typename F>
+ inline T
+ run (context& ctx,
+ const process_env& pe,
+ const char* const* args,
+ uint16_t finish_verbosity,
+ F&& f,
+ bool err,
+ bool ignore_exit,
+ sha256* checksum)
+ {
+ T r;
+ if (!run (ctx,
+ verb_never,
+ pe, args,
+ finish_verbosity,
+ [&r, &f] (string& l, bool last)
+ {
+ r = f (l, last);
+ return r.empty ();
+ },
+ true /* trim */,
+ err,
+ ignore_exit,
+ checksum))
+ r = T ();
+
+ return r;
}
inline void
@@ -241,6 +394,88 @@ namespace build2
return find_option_prefixes (ps, s[var], ic);
}
+ // hash_environment()
+ //
+ inline void
+ hash_environment (sha256& cs, const char* n)
+ {
+ cs.append (n);
+
+ if (optional<string> v = getenv (n))
+ cs.append (*v);
+ }
+
+ inline void
+ hash_environment (sha256& cs, const string& n)
+ {
+ hash_environment (cs, n.c_str ());
+ }
+
+ inline void
+ hash_environment (sha256& cs, initializer_list<const char*> ns)
+ {
+ for (const char* n: ns)
+ hash_environment (cs, n);
+ }
+
+ inline string
+ hash_environment (initializer_list<const char*> ns)
+ {
+ sha256 cs;
+ hash_environment (cs, ns);
+ return cs.string ();
+ }
+
+ inline void
+ hash_environment (sha256& cs, const cstrings& ns)
+ {
+ for (const char* n: ns)
+ hash_environment (cs, n);
+ }
+
+ inline string
+ hash_environment (const cstrings& ns)
+ {
+ sha256 cs;
+ hash_environment (cs, ns);
+ return cs.string ();
+ }
+
+ inline void
+ hash_environment (sha256& cs, const strings& ns)
+ {
+ for (const string& n: ns)
+ hash_environment (cs, n);
+ }
+
+ inline string
+ hash_environment (const strings& ns)
+ {
+ sha256 cs;
+ hash_environment (cs, ns);
+ return cs.string ();
+ }
+
+ inline void
+ hash_environment (sha256& cs, const char* const* ns)
+ {
+ if (ns != nullptr)
+ {
+ for (; *ns != nullptr; ++ns)
+ hash_environment (cs, *ns);
+ }
+ }
+
+ inline string
+ hash_environment (const char* const* ns)
+ {
+ sha256 cs;
+ hash_environment (cs, ns);
+ return cs.string ();
+ }
+
+ // find_stem()
+ //
inline size_t
find_stem (const string& s, size_t s_p, size_t s_n,
const char* stem, const char* seps)