test build hooks overwriting entire vars hash as a non-backwards-compatiblity test

This commit is contained in:
Josh Holtrop 2019-02-10 22:07:08 -05:00
parent 7992450383
commit a1f5f022d1
3 changed files with 29 additions and 37 deletions

View File

@ -1,25 +0,0 @@
class MyObject < Rscons::Builder
def run(options)
target, sources, cache, env, vars = options.values_at(:target, :sources, :cache, :env, :vars)
env.run_builder(env.builders["Object"].new(target: target, sources: sources, cache: cache, env: env, vars: vars), target, sources, cache, vars)
end
end
build do
Environment.new(echo: :command) do |env|
env.add_builder(MyObject)
env.append('CPPPATH' => Rscons.glob('src/**'))
env.add_build_hook do |build_op|
if build_op[:builder].name == "MyObject" && build_op[:sources].first =~ %r{one\.c}
build_op[:vars]["CFLAGS"] << "-O1"
build_op[:sources] = ['src/two/two.c']
elsif build_op[:builder].name == "MyObject" && build_op[:target] =~ %r{two\.o}
new_vars = build_op[:vars].clone
new_vars["CFLAGS"] << "-O2"
build_op[:vars] = new_vars
end
end
env.MyObject('one.o', 'src/one/one.c')
env.MyObject('two.o', 'src/two/two.c')
end
end

View File

@ -0,0 +1,17 @@
build do
Environment.new(echo: :command) do |env|
env.append('CPPPATH' => Rscons.glob('src/**'))
env.add_build_hook do |build_op|
if build_op[:builder].name == "Object" && build_op[:sources].first =~ %r{one\.c}
build_op[:vars]["CFLAGS"] << "-O1"
build_op[:sources] = ['src/two/two.c']
elsif build_op[:builder].name == "Object" && build_op[:target] =~ %r{two\.o}
new_vars = build_op[:vars].clone
new_vars["CFLAGS"] << "-O2"
build_op[:vars] = new_vars
end
end
env.Object('one.o', 'src/one/one.c')
env.Object('two.o', 'src/two/two.c')
end
end

View File

@ -402,6 +402,18 @@ EOF
expect(`./build_hook.exe`).to eq "Hello from two()\n"
end
it 'supports build hooks to override the entire vars hash' do
test_dir("typical")
result = run_rscons(rsconscript: "build_hooks_override_vars.rb")
expect(result.stderr).to eq ""
expect(lines(result.stdout)).to include *[
'gcc -c -o one.o -MMD -MF one.mf -Isrc -Isrc/one -Isrc/two -O1 src/two/two.c',
'gcc -c -o two.o -MMD -MF two.mf -Isrc -Isrc/one -Isrc/two -O2 src/two/two.c'
]
expect(File.exists?('one.o')).to be_truthy
expect(File.exists?('two.o')).to be_truthy
end
it 'rebuilds when user-specified dependencies change' do
test_dir('simple')
@ -851,18 +863,6 @@ EOF
expect(result.stderr).to match /Failed to build/
expect(result.stdout).to match /Failed command was: gcc/
end
it 'supports build hooks to override construction variables' do
test_dir("typical")
result = run_rscons(rsconscript: "backward_compatible_build_hooks.rb")
expect(result.stderr).to eq ""
expect(lines(result.stdout)).to include *[
'gcc -c -o one.o -MMD -MF one.mf -Isrc -Isrc/one -Isrc/two -O1 src/two/two.c',
'gcc -c -o two.o -MMD -MF two.mf -Isrc -Isrc/one -Isrc/two -O2 src/two/two.c'
]
expect(File.exists?('one.o')).to be_truthy
expect(File.exists?('two.o')).to be_truthy
end
end
context "CFile builder" do