From bb4657e465b52d4502584cfe214367f1d88368e8 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 27 Feb 2022 16:55:59 -0500 Subject: [PATCH] Process environments created during task execution phase --- build_tests/simple/env_in_task.rb | 5 +++++ lib/rscons/application.rb | 11 +++++++++++ lib/rscons/script.rb | 10 +++++++++- spec/build_tests_spec.rb | 8 ++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 build_tests/simple/env_in_task.rb diff --git a/build_tests/simple/env_in_task.rb b/build_tests/simple/env_in_task.rb new file mode 100644 index 0000000..2600ce0 --- /dev/null +++ b/build_tests/simple/env_in_task.rb @@ -0,0 +1,5 @@ +default do + env do |env| + env.Program('simple.exe', Dir['*.c']) + end +end diff --git a/lib/rscons/application.rb b/lib/rscons/application.rb index ab61060..f6d0450 100644 --- a/lib/rscons/application.rb +++ b/lib/rscons/application.rb @@ -42,6 +42,16 @@ module Rscons @variant_groups = [] end + # Return if Rscons is in the task execution phase. + # + # @api private + # + # @return [Boolean] + # If Rscons is in the task execution phase. + def task_execution_phase? + @task_execution_phase + end + # Run the application. # # Execute user-specified tasks. @@ -77,6 +87,7 @@ module Rscons return 0 end apply_task_params(tasks_and_params) + @task_execution_phase = true if tasks_and_params.empty? check_process_environments if Task.tasks["default"] diff --git a/lib/rscons/script.rb b/lib/rscons/script.rb index 02c8099..e6b811c 100644 --- a/lib/rscons/script.rb +++ b/lib/rscons/script.rb @@ -130,13 +130,21 @@ module Rscons # # If a block is given it is immediately executed and passed the newly # created Environment object as an argument. + # If the Environment is created in task context, the +process+ method is + # immediately called. + # Otherwise, the Environment is not processed until the task execution + # phase. # # @yield [env] # @yieldparam env [Environment] # The created environment. def env(*args, &block) Rscons.application.check_configure - Environment.new(*args, &block) + e = Environment.new(*args, &block) + if Rscons.application.task_execution_phase? + e.process + end + e end # Construct a task parameter. diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index 1fe11a6..43ceb1d 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -195,6 +195,14 @@ EOF expect(nr(`./simple.exe`)).to eq "This is a simple C program\n" end + it "processes the environment when created within a task" do + test_dir("simple") + result = run_rscons(args: %w[-f env_in_task.rb]) + expect(result.stderr).to eq "" + expect(File.exists?("build/e.1/simple.c.o")).to be_truthy + expect(nr(`./simple.exe`)).to eq "This is a simple C program\n" + end + it "uses the build directory specified with -b" do test_dir("simple") result = run_rscons(args: %w[-b b])