pass Builder object to build hooks instead of build_operation Hash

This commit is contained in:
Josh Holtrop 2019-02-13 21:13:13 -05:00
parent 86594c62b6
commit 33504f586b
7 changed files with 37 additions and 43 deletions

View File

@ -1,11 +1,11 @@
build do
env1 = Environment.new(echo: :command) do |env|
env['CFLAGS'] = '-O2'
env.add_build_hook do |build_op|
build_op[:vars]['CPPFLAGS'] = '-DSTRING="Hello"'
env.add_build_hook do |builder|
builder.vars['CPPFLAGS'] = '-DSTRING="Hello"'
end
env.add_post_build_hook do |build_op|
$stdout.puts "post #{build_op[:target]}"
env.add_post_build_hook do |builder|
$stdout.puts "post #{builder.target}"
end
env.Program('program.exe', Dir['src/*.c'])
end

View File

@ -1,9 +1,9 @@
build do
Environment.new do |env|
env.Program("simple.exe", Dir["*.c"])
env.add_build_hook do |build_op|
if build_op[:target].end_with?(".o")
env.Disassemble("#{build_op[:target]}.txt", build_op[:target])
env.add_build_hook do |builder|
if builder.target.end_with?(".o")
env.Disassemble("#{builder.target}.txt", builder.target)
end
end
end

View File

@ -1,11 +1,11 @@
build do
Environment.new(echo: :command) do |env|
env.append('CPPPATH' => Rscons.glob('src/**/*/'))
env.add_build_hook do |build_op|
if File.basename(build_op[:target]) == "one.o"
build_op[:vars]["CFLAGS"] << "-O1"
elsif File.basename(build_op[:target]) == "two.o"
build_op[:vars]["CFLAGS"] << "-O2"
env.add_build_hook do |builder|
if File.basename(builder.target) == "one.o"
builder.vars["CFLAGS"] << "-O1"
elsif File.basename(builder.target) == "two.o"
builder.vars["CFLAGS"] << "-O2"
end
end
env.Program('build_hook.exe', Rscons.glob('src/**/*.c'))

View File

@ -1,14 +1,14 @@
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
env.add_build_hook do |builder|
if builder.name == "Object" && builder.sources.first =~ %r{one\.c}
builder.vars["CFLAGS"] << "-O1"
builder.sources = ['src/two/two.c']
elsif builder.name == "Object" && builder.target =~ %r{two\.o}
new_vars = builder.vars.clone
new_vars["CFLAGS"] << "-O2"
build_op[:vars] = new_vars
builder.vars = new_vars
end
end
env.Object('one.o', 'src/one/one.c')

View File

@ -2,8 +2,8 @@ build do
Environment.new do |env|
env["CPPPATH"] << "src/two"
env.Object("one.o", "src/one/one.c")
env.add_post_build_hook do |build_op|
if build_op[:target] == "one.o"
env.add_post_build_hook do |builder|
if builder.target == "one.o"
env["MODULE"] = "two"
env.Object("${MODULE}.o", "src/${MODULE}/${MODULE}.c")
end

View File

@ -267,7 +267,7 @@ module Rscons
job = nil
else
targets_still_building = @threaded_commands.map do |tc|
tc.build_operation[:target]
tc.builder.target
end
job = @job_set.get_next_job_to_run(targets_still_building)
end
@ -280,8 +280,7 @@ module Rscons
result = run_builder(job[:builder],
job[:target],
job[:sources],
cache,
job[:vars])
cache)
unless result
failure = "Failed to build #{job[:target]}"
Ansi.write($stderr, :red, failure, :reset, "\n")
@ -307,13 +306,13 @@ module Rscons
result = finalize_builder(tc)
if result
@build_hooks[:post].each do |build_hook_block|
build_hook_block.call(tc.build_operation)
build_hook_block.call(tc.builder)
end
else
unless @echo == :command
print_failed_command(tc.command)
end
failure = "Failed to build #{tc.build_operation[:target]}"
failure = "Failed to build #{tc.builder.target}"
Ansi.write($stderr, :red, failure, :reset, "\n")
break
end
@ -515,39 +514,34 @@ module Rscons
# @param target [String] The target output file.
# @param sources [Array<String>] List of source files.
# @param cache [Cache] The Cache.
# @param vars [Hash] Extra variables to pass to the builder.
# @param options [Hash]
# @since 1.10.0
# Options.
#
# @return [String,false] Return value from the {Builder}'s +run+ method.
def run_builder(builder, target, sources, cache, vars, options = {})
vars = @varset.merge(vars)
def run_builder(builder, target, sources, cache, options = {})
builder.vars = @varset.merge(builder.vars)
build_operation = {
builder: builder,
target: target,
sources: sources,
cache: cache,
env: self,
vars: vars,
vars: builder.vars,
}
call_build_hooks = lambda do |sec|
@build_hooks[sec].each do |build_hook_block|
build_hook_block.call(build_operation)
build_hook_block.call(builder)
end
end
# Invoke pre-build hooks.
call_build_hooks[:pre]
# TODO: remove build_operation fields and just pass in the Builder.
builder.sources = build_operation[:sources]
builder.vars = build_operation[:vars]
# Call the builder's #run method.
rv = builder.run(build_operation)
(@side_effects[build_operation[:target]] || []).each do |side_effect_file|
(@side_effects[builder.target] || []).each do |side_effect_file|
# Register side-effect files as build targets so that a Cache clean
# operation will remove them.
cache.register_build(side_effect_file, nil, [], self)
@ -556,6 +550,8 @@ module Rscons
if rv.is_a?(ThreadedCommand)
# Store the build operation so the post-build hooks can be called
# with it when the threaded command completes.
rv.builder = builder
# TODO: remove
rv.build_operation = build_operation
start_threaded_command(rv)
else
@ -746,7 +742,7 @@ module Rscons
# @return [String, false]
# Result of Builder#finalize.
def finalize_builder(tc)
tc.build_operation[:builder].finalize(
tc.builder.finalize(
tc.build_operation.merge(
command_status: tc.thread.value,
tc: tc))

View File

@ -8,11 +8,6 @@ module Rscons
# The command to execute.
attr_reader :command
# @return [Object]
# Arbitrary object to store builder-specific info. This object value will
# be passed back into the builder's #finalize method.
attr_reader :builder_info
# @return [String]
# Short description of the command. This will be printed to standard
# output if the Environment's echo mode is :short.
@ -31,6 +26,10 @@ module Rscons
# command is executing.
attr_accessor :build_operation
# @return [Builder]
# {Builder} executing this command.
attr_accessor :builder
# @return [Thread]
# The thread waiting on this command to terminate.
attr_accessor :thread
@ -53,7 +52,6 @@ module Rscons
# Options Hash to pass to Kernel#system.
def initialize(command, options = {})
@command = command
@builder_info = options[:builder_info]
@short_description = options[:short_description]
@system_env = options[:system_env]
@system_options = options[:system_options]