From 2a9204cab666d47770bf3809d95a689088019121 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 27 May 2020 15:27:57 +0200 Subject: Various minor fixes and cleanups --- libbuild2/build/script/parser.cxx | 3 -- libbuild2/build/script/parser.test.cxx | 2 +- libbuild2/build/script/script.cxx | 19 ++++----- libbuild2/build/script/script.hxx | 24 ++++++------ libbuild2/build/script/token.cxx | 2 +- libbuild2/build/script/token.hxx | 2 +- libbuild2/rule.cxx | 4 +- libbuild2/script/lexer.hxx | 10 +++-- libbuild2/script/run.cxx | 4 +- libbuild2/script/script.hxx | 71 +++++++++++++++------------------- 10 files changed, 65 insertions(+), 76 deletions(-) diff --git a/libbuild2/build/script/parser.cxx b/libbuild2/build/script/parser.cxx index 648cc7b..e64db91 100644 --- a/libbuild2/build/script/parser.cxx +++ b/libbuild2/build/script/parser.cxx @@ -269,9 +269,6 @@ namespace build2 { const script& s (*script_); - if (s.temp_dir) - environment_->create_temp_dir (); - runner_->enter (*environment_, s.start_loc); // Note that we rely on "small function object" optimization for the diff --git a/libbuild2/build/script/parser.test.cxx b/libbuild2/build/script/parser.test.cxx index 42681e3..9046312 100644 --- a/libbuild2/build/script/parser.test.cxx +++ b/libbuild2/build/script/parser.test.cxx @@ -166,7 +166,7 @@ namespace build2 { case mode::run: { - environment e (perform_update_id, tt); + environment e (perform_update_id, tt, false /* temp_dir */); print_runner r (print_line); p.execute (ctx.global_scope, ctx.global_scope, e, s, r); break; diff --git a/libbuild2/build/script/script.cxx b/libbuild2/build/script/script.cxx index e344b59..3485f54 100644 --- a/libbuild2/build/script/script.cxx +++ b/libbuild2/build/script/script.cxx @@ -22,7 +22,7 @@ namespace build2 static const optional wd_name ("current directory"); environment:: - environment (action a, const target_type& t) + environment (action a, const target_type& t, bool temp) : build2::script::environment ( t.ctx, cast (t.ctx.global_scope["build.host"]), @@ -34,9 +34,7 @@ namespace build2 target (t), vars (context, false /* global */) { - // Set special variables. Note that the $~ variable is set later and - // only if the temporary directory is required for the script - // execution (see create_temp_dir() for details). + // Set special variables. // { // $> @@ -65,6 +63,14 @@ namespace build2 assign (var_pool.insert ("<")) = move (ns); } + + // Set the $~ special variable. + // + if (temp) + { + create_temp_dir (); + assign (var_pool.insert ("~")) = temp_dir.path; + } } void environment:: @@ -118,11 +124,6 @@ namespace build2 << e; } - // Set the $~ special variable. - // - value& v (assign (var_pool.insert ("~"))); - v = td; - if (verb >= 3) text << "mkdir " << td; } diff --git a/libbuild2/build/script/script.hxx b/libbuild2/build/script/script.hxx index 7d27840..2118568 100644 --- a/libbuild2/build/script/script.hxx +++ b/libbuild2/build/script/script.hxx @@ -29,11 +29,10 @@ namespace build2 // Notes: // // - Once parsed, the script can be executed in multiple threads with - // the state (variable values, etc) maintained in the environment - // object. + // the state (variable values, etc) maintained in the environment. // - // - The default script command redirects semantics is none for stdin, - // merge into stderr for stdout, and pass for stderr. + // - The default script command redirects semantics is 'none' for stdin, + // 'merge' into stderr for stdout, and 'pass' for stderr. // class script { @@ -53,7 +52,7 @@ namespace build2 // properly tracked (the variable value change will not trigger the // target rebuild). // - small_vector vars; + small_vector vars; // 2 for command and options. // True if script references the $~ special variable. // @@ -68,7 +67,7 @@ namespace build2 public: using target_type = build2::target; - environment (action, const target_type&); + environment (action, const target_type&, bool temp_dir); environment (environment&&) = delete; environment (const environment&) = delete; @@ -76,15 +75,16 @@ namespace build2 environment& operator= (const environment&) = delete; public: + // Primary target this environment is for. + // const target_type& target; - // Script-local variable pool. + // Script-local variable pool and map. // - variable_pool var_pool; - - // Note that if we pass the variable name as a string, then it will - // be looked up in the wrong pool. + // Note that if we lookup the variable by passing name as a string, + // then it will be looked up in the wrong pool. // + variable_pool var_pool; variable_map vars; // Temporary directory for the script run. @@ -110,8 +110,6 @@ namespace build2 const string& attrs, const location&) override; - // Create the temporary directory and set the $~ variable. - // virtual void create_temp_dir () override; diff --git a/libbuild2/build/script/token.cxx b/libbuild2/build/script/token.cxx index 7c15dff..8f8477b 100644 --- a/libbuild2/build/script/token.cxx +++ b/libbuild2/build/script/token.cxx @@ -14,7 +14,7 @@ namespace build2 void token_printer (ostream& os, const token& t, print_mode m) { - // No build script-specific tokens so far. + // No buildscript-specific tokens so far. // build2::script::token_printer (os, t, m); } diff --git a/libbuild2/build/script/token.hxx b/libbuild2/build/script/token.hxx index 90c1379..954b412 100644 --- a/libbuild2/build/script/token.hxx +++ b/libbuild2/build/script/token.hxx @@ -19,7 +19,7 @@ namespace build2 { using base_type = build2::script::token_type; - // No build script-specific tokens so far. + // No buildscript-specific tokens so far. // token_type () = default; diff --git a/libbuild2/rule.cxx b/libbuild2/rule.cxx index eabe578..773d42e 100644 --- a/libbuild2/rule.cxx +++ b/libbuild2/rule.cxx @@ -631,7 +631,7 @@ namespace build2 { const scope& bs (t.base_scope ()); - build::script::environment e (a, t); + build::script::environment e (a, t, script.temp_dir); build::script::parser p (ctx); build::script::default_runner r; p.execute (*bs.root_scope (), bs, e, script, r); @@ -664,7 +664,7 @@ namespace build2 { const scope& bs (t.base_scope ()); - build::script::environment e (a, t); + build::script::environment e (a, t, script.temp_dir); build::script::parser p (ctx); build::script::default_runner r; p.execute (*bs.root_scope (), bs, e, script, r); diff --git a/libbuild2/script/lexer.hxx b/libbuild2/script/lexer.hxx index bdeba66..dbfdfcc 100644 --- a/libbuild2/script/lexer.hxx +++ b/libbuild2/script/lexer.hxx @@ -33,7 +33,8 @@ namespace build2 lexer_mode (base_type v): base_type (v) {} }; - // Redirects the <, <<, <<<, >, >>, and >>> aliases resolve to. + // Actual redirects (as tokens) for the the <, <<, <<<, and >, >>, >>> + // aliases. // struct redirect_aliases { @@ -44,9 +45,10 @@ namespace build2 optional gg; // >> optional ggg; // >>> - // If the token type is a redirect alias then return the token type - // it resolves to and the passed type otherwise. It's the caller's - // responsibility to make sure that the corresponding alias is present. + // If the token type is a redirect alias then return the token type it + // resolves to and the passed token type otherwise. Note that it's the + // caller's responsibility to make sure that the corresponding alias is + // present (normally by not recognizing absent aliases as tokens). // token_type resolve (token_type t) const noexcept diff --git a/libbuild2/script/run.cxx b/libbuild2/script/run.cxx index 64286fd..38436b9 100644 --- a/libbuild2/script/run.cxx +++ b/libbuild2/script/run.cxx @@ -192,7 +192,7 @@ namespace build2 // For targets other than Windows leave the string intact. // - if (env.platform.class_ != "windows") + if (env.host.class_ != "windows") return s; // Convert forward slashes to Windows path separators (escape for @@ -335,7 +335,7 @@ namespace build2 // Ignore Windows newline fluff if that's what we are running on. // - if (env.platform.class_ == "windows") + if (env.host.class_ == "windows") args.push_back ("--strip-trailing-cr"); args.push_back (eop.string ().c_str ()); diff --git a/libbuild2/script/script.hxx b/libbuild2/script/script.hxx index b80c44a..f4998b7 100644 --- a/libbuild2/script/script.hxx +++ b/libbuild2/script/script.hxx @@ -15,7 +15,7 @@ namespace build2 { namespace script { - // Pre-parse representation. + // Pre-parsed representation. // enum class line_type @@ -207,10 +207,10 @@ namespace build2 redirect (redirect&&) noexcept; redirect& operator= (redirect&&) noexcept; - // Note that defining optional movable-only redirects in the command - // class make the older C++ compilers (GCC 4.9, Clang 4, VC 15) fail to - // compile the command vector manipulating code. Thus, we make the - // redirect class copyable to workaround the issue. + // @@ Defining optional movable-only redirects in the command class make + // the older C++ compilers (GCC 4.9, Clang 4, VC 15) fail to compile the + // command vector manipulating code. Thus, we make the redirect class + // copyable to workaround the issue. // redirect (const redirect&); redirect& operator= (const redirect&); @@ -354,80 +354,71 @@ namespace build2 public: build2::context& context; - // A platform the script-executed programs run at. + // The platform script programs run on. // - const target_triplet& platform; + const target_triplet& host; - // Used as the builtin/process CWD and to complete relative paths. Any - // attempt to remove or move this directory (or its parent directory) - // using the rm or mv builtins will fail the script execution. Must be - // an absolute path. + // The work directory is used as the builtin/process CWD and to complete + // relative paths. Any attempt to remove or move this directory (or its + // parent directory) using the rm or mv builtins will fail. Must be an + // absolute path. // const dir_name_view work_dir; - // If path is not NULL, then any attempt to remove or move a filesystem - // entry outside this directory using an explicit cleanup or the rm/mv - // builtins will fail the script execution, unless the --force option is - // specified for the builtin. Must be an absolute path, unless is NULL. + // If the sanbox directory is not NULL, then any attempt to remove or + // move a filesystem entry outside this directory using an explicit + // cleanup or the rm/mv builtins will fail, unless the --force option is + // specified for the builtin. Must be an absolute path. // const dir_name_view sandbox_dir; - // Used by the script running machinery to create special files in it. - // Must be an absolute path, unless empty. Can be empty until the - // create_temp_dir() function call, which can be used for the create-on- - // demand strategy implementation. + // The temporary directory is used by the script running machinery to + // create special files. Must be an absolute path, unless empty. Can be + // empty until the create_temp_dir() function call, which can be used + // for creating this directory on demand. // const dir_path& temp_dir; - // The temporary directory will not be removed on the script failure, - // which allows the script running machinery to refer to the special - // files in the diagnostics. + // If true, the temporary directory will not be removed on the script + // failure. In particular, this allows the script running machinery to + // refer to the special files in diagnostics. // const bool temp_dir_keep; - // Process streams default redirects. + // Default process streams redirects. // // If a stream redirect is not specified on the script command line, - // then the respective redirect data member will be used. + // then the respective redirect data member will be used as the default. // const redirect in; const redirect out; const redirect err; environment (build2::context& ctx, - const target_triplet& pt, + const target_triplet& h, const dir_name_view& wd, const dir_name_view& sd, const dir_path& td, bool tk, redirect&& i = redirect (redirect_type::pass), redirect&& o = redirect (redirect_type::pass), redirect&& e = redirect (redirect_type::pass)) - : context (ctx), - platform (pt), - work_dir (wd), - sandbox_dir (sd), - temp_dir (td), - temp_dir_keep (tk), - in (move (i)), - out (move (o)), - err (move (e)) + : context (ctx), host (h), + work_dir (wd), sandbox_dir (sd), temp_dir (td), temp_dir_keep (tk), + in (move (i)), out (move (o)), err (move (e)) { } // Create environment without the sandbox. // environment (build2::context& ctx, - const target_triplet& pt, + const target_triplet& h, const dir_name_view& wd, const dir_path& td, bool tk, redirect&& i = redirect (redirect_type::pass), redirect&& o = redirect (redirect_type::pass), redirect&& e = redirect (redirect_type::pass)) - : environment (ctx, - pt, - wd, - dir_name_view (), - td, tk, + : environment (ctx, h, + wd, dir_name_view (), td, tk, move (i), move (o), move (e)) { } -- cgit v1.1