From eaac473f4493049e8acf54b7d98e87dc85b26687 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 1 Jan 2019 15:49:15 -0500 Subject: [PATCH] provide Environment construction option to use stored configuration settings - close #78 --- build_tests/configure/check_cfg_use.rb | 15 +++++++++++++++ build_tests/configure/check_lib_use.rb | 15 +++++++++++++++ lib/rscons/basic_environment.rb | 21 ++++++++++++++++++--- lib/rscons/configure_op.rb | 7 ++++++- lib/rscons/environment.rb | 3 +-- spec/build_tests_spec.rb | 24 ++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 build_tests/configure/check_cfg_use.rb create mode 100644 build_tests/configure/check_lib_use.rb diff --git a/build_tests/configure/check_cfg_use.rb b/build_tests/configure/check_cfg_use.rb new file mode 100644 index 0000000..7403b76 --- /dev/null +++ b/build_tests/configure/check_cfg_use.rb @@ -0,0 +1,15 @@ +configure do + check_cfg package: "mypackage", use: "myp" +end + +build do + Environment.new(echo: :command) do |env| + env.Copy("myconfigtest1.c", "simple.c") + env.Program("myconfigtest1.exe", "myconfigtest1.c") + end + + Environment.new(echo: :command, use: "myp") do |env| + env.Copy("myconfigtest2.c", "simple.c") + env.Program("myconfigtest2.exe", "myconfigtest2.c") + end +end diff --git a/build_tests/configure/check_lib_use.rb b/build_tests/configure/check_lib_use.rb new file mode 100644 index 0000000..0638d41 --- /dev/null +++ b/build_tests/configure/check_lib_use.rb @@ -0,0 +1,15 @@ +configure do + check_lib "m", use: :m +end + +build do + Environment.new(echo: :command) do |env| + env.Copy("test1.c", "simple.c") + env.Program("test2.exe", "test1.c") + end + + Environment.new(echo: :command, use: %w[m]) do |env| + env.Copy("test2.c", "simple.c") + env.Program("test2.exe", "test2.c") + end +end diff --git a/lib/rscons/basic_environment.rb b/lib/rscons/basic_environment.rb index d284210..b30ec3c 100644 --- a/lib/rscons/basic_environment.rb +++ b/lib/rscons/basic_environment.rb @@ -3,8 +3,14 @@ module Rscons class BasicEnvironment # Create a BasicEnvironment object. - def initialize + # + # @api private + # + # @param options [Hash] + # Construction options. + def initialize(options = {}) @varset = VarSet.new(Rscons.application.default_varset) + load_configuration_data!(options) end # Get a construction variable's value. @@ -185,15 +191,24 @@ module Rscons end end - # Load construction variables saved from the configure operation. - def load_configuration_data! + # Load all construction variables saved from the configure operation. + def load_configuration_data!(options) if vars = Cache.instance.configuration_data["vars"] if default_vars = vars["_default_"] apply_configuration_data!(default_vars) end + if options[:use] + Array(options[:use]).each do |use| + if use_vars = vars[use] + apply_configuration_data!(use_vars) + end + end + end end end + # Load a specific set of construction variables saved from the configure + # operation. def apply_configuration_data!(vars) if merge_vars = vars["merge"] append(merge_vars) diff --git a/lib/rscons/configure_op.rb b/lib/rscons/configure_op.rb index 06b0b3d..1bdc423 100644 --- a/lib/rscons/configure_op.rb +++ b/lib/rscons/configure_op.rb @@ -360,7 +360,12 @@ module Rscons # @return [Hash] # Configuration Hash for storing vars. def store_common(options) - usename = options[:use] || "_default_" + usename = + if options[:use] + options[:use].to_s + else + "_default_" + end cache = Cache.instance cache.configuration_data["vars"] ||= {} cache.configuration_data["vars"][usename] ||= {} diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 3111e45..476f4ce 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -64,7 +64,7 @@ module Rscons # If a block is given, the Environment object is yielded to the block and # when the block returns, the {#process} method is automatically called. def initialize(options = {}) - super() + super(options) @id = self.class.get_id self.class.register(self) @threaded_commands = Set.new @@ -83,7 +83,6 @@ module Rscons end @echo = options[:echo] || :short @build_root = "#{Cache.instance.configuration_data["build_dir"]}/e.#{@id}" - load_configuration_data! if block_given? yield self diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index b8560a4..5afb0f6 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -1782,6 +1782,16 @@ EOF expect(result.stdout).to match /Checking for library 'm'... found/ expect(result.stdout).to match /gcc.*-lm/ end + + it "does not link against the checked library by default if :use is specified" do + test_dir "configure" + result = run_rscons(rsconscript: "check_lib_use.rb", op: "build") + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /Checking for library 'm'... found/ + expect(result.stdout).to_not match /gcc.*test1.*-lm/ + expect(result.stdout).to match /gcc.*test2.*-lm/ + end end context "check_program" do @@ -1832,6 +1842,20 @@ EOF expect(result.status).to_not eq 0 expect(result.stdout).to match /Checking 'my-config'\.\.\. not found/ end + + it "does not use the flags found by default if :use is specified" do + test_dir "configure" + create_exe "pkg-config", "echo '-DMYPACKAGE'" + result = run_rscons(rsconscript: "check_cfg_use.rb", op: "configure") + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to match /Checking for package 'mypackage'\.\.\. found/ + result = run_rscons(rsconscript: "check_cfg_use.rb", op: "build") + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(result.stdout).to_not match /gcc.*-o.*myconfigtest1.*-DMYPACKAGE/ + expect(result.stdout).to match /gcc.*-o.*myconfigtest2.*-DMYPACKAGE/ + end end context "when passed a program" do