From 446fef931f27d09ce745fc486b58e4784db12ca9 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 15 Oct 2013 16:15:37 -0400 Subject: [PATCH] add "tweakers" functionality tweakers allow the user to modify construction variables before any build is performed --- build_tests/build_dir/tweaker_build.rb | 12 ++++++++++++ lib/rscons/environment.rb | 18 +++++++++++++++++- spec/build_tests_spec.rb | 24 +++++++++++++++++------- 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 build_tests/build_dir/tweaker_build.rb diff --git a/build_tests/build_dir/tweaker_build.rb b/build_tests/build_dir/tweaker_build.rb new file mode 100644 index 0000000..1650a8d --- /dev/null +++ b/build_tests/build_dir/tweaker_build.rb @@ -0,0 +1,12 @@ +Rscons::Environment.new do |env| + env.append('CPPPATH' => Dir['src/**/*/']) + env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/') + env.add_tweaker do |build_op| + if build_op[:target] =~ %r{build_one/.*\.o} + build_op[:vars]["CFLAGS"] << "-O1" + elsif build_op[:target] =~ %r{build_two/.*\.o} + build_op[:vars]["CFLAGS"] << "-O2" + end + end + env.Program('tweaker', Dir['src/**/*.c']) +end diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 7c78a10..a999841 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -22,6 +22,7 @@ module Rscons @targets = {} @builders = {} @build_dirs = [] + @tweakers = [] @varset[:exclude_builders] ||= [] unless @varset[:exclude_builders] == :all exclude_builders = Set.new(@varset[:exclude_builders] || []) @@ -45,7 +46,7 @@ module Rscons # Make a copy of the Environment object. # The cloned environment will contain a copy of all environment options, # construction variables, builders, and build directories. It will not - # contain a copy of the targets. + # contain a copy of the targets or tweakers. # 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 clone(variables = {}) @@ -74,6 +75,11 @@ module Rscons end end + # Add a tweaker block to the Environment. + def add_tweaker(&block) + @tweakers << block + end + # Specify a build directory for this Environment. # Source files from src_dir will produce object files under obj_dir. def build_dir(src_dir, obj_dir) @@ -268,6 +274,16 @@ module Rscons # @param vars [Hash] Extra variables to pass to the builder. # Return the result of the builder's run() method. def run_builder(builder, target, sources, cache, vars) + vars = @varset.merge(vars) + @tweakers.each do |tweaker_block| + build_operation = { + builder: builder, + target: target, + sources: sources, + vars: vars, + } + tweaker_block.call(build_operation) + end builder.run(target, sources, cache, self, vars) end end diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index a713756..22ebb7e 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -11,10 +11,10 @@ describe Rscons do FileUtils.rm_rf('build_tests_run') end - def build_testdir - if File.exists?("build.rb") - build_rb = File.read("build.rb") - File.open("build.rb", "w") do |fh| + def build_testdir(build_script = "build.rb") + if File.exists?(build_script) + build_rb = File.read(build_script) + File.open(build_script, "w") do |fh| fh.puts(<