From 5b6353395dac6b1c0a57ad25cf9e13e99ef3e67f Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 14 Feb 2022 21:52:35 -0500 Subject: [PATCH] Store configure task parameters in configuration cache data - close #151 --- .../configure/check_c_compiler_non_default.rb | 6 +++ build_tests/tasks/configure_params.rb | 11 +++++ build_tests/typical/install.rb | 12 ++--- doc/user_guide.md | 17 +++++++ lib/rscons/application.rb | 2 +- lib/rscons/basic_environment.rb | 10 +++++ lib/rscons/configure_op.rb | 1 + lib/rscons/task.rb | 2 +- lib/rscons/util.rb | 5 +++ spec/build_tests_spec.rb | 44 +++++++++++++++++++ 10 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 build_tests/configure/check_c_compiler_non_default.rb create mode 100644 build_tests/tasks/configure_params.rb diff --git a/build_tests/configure/check_c_compiler_non_default.rb b/build_tests/configure/check_c_compiler_non_default.rb new file mode 100644 index 0000000..052c9ef --- /dev/null +++ b/build_tests/configure/check_c_compiler_non_default.rb @@ -0,0 +1,6 @@ +configure do + check_c_compiler "clang" +end +env do |env| + env.Program("simple.exe", "simple.c") +end diff --git a/build_tests/tasks/configure_params.rb b/build_tests/tasks/configure_params.rb new file mode 100644 index 0000000..7f96a78 --- /dev/null +++ b/build_tests/tasks/configure_params.rb @@ -0,0 +1,11 @@ +configure params: [ + param("with-xyz", "xyz", true, "Set xyz"), + param("flag", nil, false, "Set flag"), +] do + check_c_compiler +end + +default do + puts "xyz: #{Task["configure"]["with-xyz"]}" + puts "flag: #{Task["configure"]["flag"].inspect}" +end diff --git a/build_tests/typical/install.rb b/build_tests/typical/install.rb index 5f2d6f3..d0a4255 100644 --- a/build_tests/typical/install.rb +++ b/build_tests/typical/install.rb @@ -11,12 +11,12 @@ env do |env| end task "install", depends: "build" do - env.InstallDirectory("${prefix}/bin") - env.Install("${prefix}/bin", "^/program.exe") - env.InstallDirectory("${prefix}/share") - env.Install("${prefix}/share/proj/install.rb", "install.rb") - env.Install("${prefix}/mult", ["install.rb", "copy.rb"]) - env.Install("${prefix}/src", "src") + env.InstallDirectory("${configure:prefix}/bin") + env.Install("${configure:prefix}/bin", "^/program.exe") + env.InstallDirectory("${configure:prefix}/share") + env.Install("${configure:prefix}/share/proj/install.rb", "install.rb") + env.Install("${configure:prefix}/mult", ["install.rb", "copy.rb"]) + env.Install("${configure:prefix}/src", "src") end end diff --git a/doc/user_guide.md b/doc/user_guide.md index 6dcf6ec..cf5204c 100644 --- a/doc/user_guide.md +++ b/doc/user_guide.md @@ -24,6 +24,7 @@ build steps, but also provides an extensible framework for performing custom build operations as well. Rscons takes inspiration from: + * [SCons](https://scons.org/) * [waf](https://waf.io/) * [rake](https://github.com/ruby/rake) @@ -293,6 +294,22 @@ task "two" do end ``` +Task parameters can also be referenced via construction variables. +Each task parameter is stored in a construction variable. +The name for the construction variable is created by joining the task name +and the parameter name with a ":" character. +For example: + +```ruby +task "build", params: [ + param("heap-size", "1024", true, "Set heap size"), +] do + env["CPPDEFINES"] << "HEAP_SIZE=${build:heap-size}" + env.Program("^/myprog", glob("src/**/*.c")) + env.Install("${configure:prefix}/bin/myprog", "^/myprog") +end +``` + ###> Tasks with Special Meaning Rscons recognizes special meaning for a few tasks: diff --git a/lib/rscons/application.rb b/lib/rscons/application.rb index ced8ecd..0498449 100644 --- a/lib/rscons/application.rb +++ b/lib/rscons/application.rb @@ -31,7 +31,6 @@ module Rscons # Create Application instance. def _initialize - @script = Script.new @silent_configure = true @build_dir = ENV["RSCONS_BUILD_DIR"] || "build" ENV.delete("RSCONS_BUILD_DIR") @@ -55,6 +54,7 @@ module Rscons # Process exit code (0 on success). def run(rsconscript, tasks_and_params, show_tasks) Cache.instance["failed_commands"] = [] + @script = Script.new @script.load(rsconscript) if show_tasks show_script_tasks diff --git a/lib/rscons/basic_environment.rb b/lib/rscons/basic_environment.rb index b268e31..5560a76 100644 --- a/lib/rscons/basic_environment.rb +++ b/lib/rscons/basic_environment.rb @@ -12,6 +12,7 @@ module Rscons def initialize(options = {}) @varset = VarSet.new(Rscons::DEFAULT_CONSTRUCTION_VARIABLES) load_configuration_data!(options) + load_task_param_variables! end # Access the value of a construction variable. @@ -271,5 +272,14 @@ module Rscons end end + # Load task parameters as construction variables. + def load_task_param_variables! + Task[].each do |task_name, task| + task.param_values.each do |param_name, param_value| + self["#{task.name}:#{param_name}"] = param_value + end + end + end + end end diff --git a/lib/rscons/configure_op.rb b/lib/rscons/configure_op.rb index d1d40e5..294565c 100644 --- a/lib/rscons/configure_op.rb +++ b/lib/rscons/configure_op.rb @@ -44,6 +44,7 @@ module Rscons @log_fh = nil cache = Cache.instance cache["configuration_data"]["configured"] = success + cache["configuration_data"]["params"] = Task["configure"].param_values cache.write end diff --git a/lib/rscons/task.rb b/lib/rscons/task.rb index 105140b..136ef74 100644 --- a/lib/rscons/task.rb +++ b/lib/rscons/task.rb @@ -144,7 +144,7 @@ module Rscons unless param raise RsconsError.new("Could not find parameter '#{param_name}'") end - param.value + @param_values[param_name] end # Execute a task's actions. diff --git a/lib/rscons/util.rb b/lib/rscons/util.rb index f60f8d8..c199e1c 100644 --- a/lib/rscons/util.rb +++ b/lib/rscons/util.rb @@ -206,6 +206,11 @@ module Rscons else task = Task.new(name, options, &block) end + if name == "configure" + if configuration_params = Cache.instance["configuration_data"]["params"] + task.param_values.merge!(configuration_params) + end + end task end diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index babd3c7..dba0a78 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -1820,6 +1820,16 @@ EOF expect(Dir.exist?("bb/_configure")).to be_truthy end + it "applies the configured settings to top-level created environments" do + test_dir "configure" + + result = run_rscons(args: %w[-f check_c_compiler_non_default.rb -v]) + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /Checking for C compiler\.\.\./ + expect(result.stdout).to match /clang.*simple\.exe/ + end + context "check_c_compiler" do {"check_c_compiler.rb" => "when no arguments are given", "check_c_compiler_find_first.rb" => "when arguments are given"}.each_pair do |rsconscript, desc| @@ -2995,4 +3005,38 @@ EOF end end + context "configure task parameters" do + it "allows access to configure task parameters from another task" do + test_dir "tasks" + + result = run_rscons(args: %w[-f configure_params.rb]) + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /xyz: xyz/ + expect(result.stdout).to match /flag: nil/ + + result = run_rscons(args: %w[-f configure_params.rb configure --with-xyz=foo --flag default]) + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /xyz: foo/ + expect(result.stdout).to match /flag: true/ + end + + it "stores configure task parameters in the cache for subsequent invocations" do + test_dir "tasks" + + result = run_rscons(args: %w[-f configure_params.rb configure --with-xyz=foo --flag default]) + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /xyz: foo/ + expect(result.stdout).to match /flag: true/ + + result = run_rscons(args: %w[-f configure_params.rb]) + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /xyz: foo/ + expect(result.stdout).to match /flag: true/ + end + end + end