add "tweakers" functionality

tweakers allow the user to modify construction variables before any build is performed
This commit is contained in:
Josh Holtrop 2013-10-15 16:15:37 -04:00
parent 4a2997feec
commit 446fef931f
3 changed files with 46 additions and 8 deletions

View File

@ -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

View File

@ -22,6 +22,7 @@ module Rscons
@targets = {} @targets = {}
@builders = {} @builders = {}
@build_dirs = [] @build_dirs = []
@tweakers = []
@varset[:exclude_builders] ||= [] @varset[:exclude_builders] ||= []
unless @varset[:exclude_builders] == :all unless @varset[:exclude_builders] == :all
exclude_builders = Set.new(@varset[:exclude_builders] || []) exclude_builders = Set.new(@varset[:exclude_builders] || [])
@ -45,7 +46,7 @@ module Rscons
# Make a copy of the Environment object. # Make a copy of the Environment object.
# The cloned environment will contain a copy of all environment options, # The cloned environment will contain a copy of all environment options,
# construction variables, builders, and build directories. It will not # 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 # If a block is given, the Environment object is yielded to the block and
# when the block returns, the {#process} method is automatically called. # when the block returns, the {#process} method is automatically called.
def clone(variables = {}) def clone(variables = {})
@ -74,6 +75,11 @@ module Rscons
end end
end end
# Add a tweaker block to the Environment.
def add_tweaker(&block)
@tweakers << block
end
# Specify a build directory for this Environment. # Specify a build directory for this Environment.
# Source files from src_dir will produce object files under obj_dir. # Source files from src_dir will produce object files under obj_dir.
def build_dir(src_dir, 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. # @param vars [Hash] Extra variables to pass to the builder.
# Return the result of the builder's run() method. # Return the result of the builder's run() method.
def run_builder(builder, target, sources, cache, vars) 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) builder.run(target, sources, cache, self, vars)
end end
end end

View File

@ -11,10 +11,10 @@ describe Rscons do
FileUtils.rm_rf('build_tests_run') FileUtils.rm_rf('build_tests_run')
end end
def build_testdir def build_testdir(build_script = "build.rb")
if File.exists?("build.rb") if File.exists?(build_script)
build_rb = File.read("build.rb") build_rb = File.read(build_script)
File.open("build.rb", "w") do |fh| File.open(build_script, "w") do |fh|
fh.puts(<<EOF + build_rb) fh.puts(<<EOF + build_rb)
require "simplecov" require "simplecov"
@ -27,7 +27,7 @@ end
require "rscons" require "rscons"
EOF EOF
end end
IO.popen(%{ruby -I #{@owd}/lib build.rb}) do |io| IO.popen(%{ruby -I #{@owd}/lib #{build_script}}) do |io|
io.readlines.reject do |line| io.readlines.reject do |line|
line =~ /^Coverage report/ line =~ /^Coverage report/
end end
@ -35,11 +35,11 @@ EOF
end end
end end
def test_dir(build_test_directory) def test_dir(build_test_directory, build_script = "build.rb")
@build_test_name = build_test_directory @build_test_name = build_test_directory
FileUtils.cp_r("build_tests/#{build_test_directory}", 'build_tests_run') FileUtils.cp_r("build_tests/#{build_test_directory}", 'build_tests_run')
Dir.chdir("build_tests_run") Dir.chdir("build_tests_run")
build_testdir build_testdir(build_script)
end end
def file_sub(fname) def file_sub(fname)
@ -207,4 +207,14 @@ EOF
File.exists?('library').should be_true File.exists?('library').should be_true
`ar t lib.a`.should == "one.o\ntwo.o\n" `ar t lib.a`.should == "one.o\ntwo.o\n"
end end
it 'supports tweakers to override construction variables' do
lines = test_dir("build_dir", "tweaker_build.rb")
`./tweaker`.should == "Hello from two()\n"
lines.should =~ [
'gcc -c -o build_one/one.o -MMD -MF build_one/one.mf -Isrc/one/ -Isrc/two/ -O1 src/one/one.c',
'gcc -c -o build_two/two.o -MMD -MF build_two/two.mf -Isrc/one/ -Isrc/two/ -O2 src/two/two.c',
'gcc -o tweaker build_one/one.o build_two/two.o',
]
end
end end