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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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