pass Builder#run options into Builder#finalize also

This commit is contained in:
Josh Holtrop 2017-05-24 15:13:59 -04:00
parent dce88ece14
commit 1af3c5c9a4
3 changed files with 41 additions and 21 deletions

View File

@ -145,19 +145,29 @@ module Rscons
# Finalize a build operation. # Finalize a build operation.
# #
# This method is called after the {#run} method if the {#run} method does # This method is called after the {#run} method if the {#run} method
# not return an error. # returns a {ThreadedCommand} object.
# #
# @param options [Hash] # @param options [Hash]
# Options. # Options.
# @option options [String] :target
# Target file name.
# @option options [Array<String>] :sources
# Source file name(s).
# @option options [Cache] :cache
# The Cache object.
# @option options [Environment] :env
# The Environment executing the builder.
# @option options [Hash,VarSet] :vars
# Extra construction variables.
# @option options [Object] :setup_info
# Whatever value was returned from this builder's {#setup} method call.
# @option options [true,false,nil] :command_status # @option options [true,false,nil] :command_status
# If the {#run} method returns a {ThreadedCommand}, this field will # If the {#run} method returns a {ThreadedCommand}, this field will
# contain the return value from executing the command with # contain the return value from executing the command with
# Kernel.system(). # Kernel.system().
# @option options [Object] :builder_info # @option options [ThreadedCommand] :tc
# If the {#run} method returns a {ThreadedCommand}, this field will # The {ThreadedCommand} object that was returned by the #run method.
# contain the value passed in to the :builder_info field of the
# {ThreadedCommand} object.
# #
# @return [String,false] # @return [String,false]
# Name of the target file on success or false on failure. # Name of the target file on success or false on failure.

View File

@ -88,6 +88,8 @@ module Rscons
'_SOURCES' => sources, '_SOURCES' => sources,
'_DEPFILE' => Rscons.set_suffix(target, env.expand_varref("${DEPFILESUFFIX}", vars)), '_DEPFILE' => Rscons.set_suffix(target, env.expand_varref("${DEPFILESUFFIX}", vars)),
}) })
# Store vars back into options so new keys are accessible in #finalize.
options[:vars] = vars
com_prefix = KNOWN_SUFFIXES.find do |compiler, suffix_var| com_prefix = KNOWN_SUFFIXES.find do |compiler, suffix_var|
sources.first.end_with?(*env.expand_varref("${#{suffix_var}}")) sources.first.end_with?(*env.expand_varref("${#{suffix_var}}"))
end.tap do |v| end.tap do |v|
@ -101,8 +103,7 @@ module Rscons
FileUtils.rm_f(target) FileUtils.rm_f(target)
ThreadedCommand.new( ThreadedCommand.new(
command, command,
short_description: "#{com_prefix} #{target}", short_description: "#{com_prefix} #{target}")
builder_info: options.merge(vars: vars, command: command))
end end
end end
@ -114,12 +115,12 @@ module Rscons
# Name of the target file on success or nil on failure. # Name of the target file on success or nil on failure.
def finalize(options) def finalize(options)
if options[:command_status] if options[:command_status]
target, deps, cache, env, vars, command = options[:builder_info].values_at(:target, :sources, :cache, :env, :vars, :command) target, deps, cache, env, vars = options.values_at(:target, :sources, :cache, :env, :vars)
if File.exists?(vars['_DEPFILE']) if File.exists?(vars['_DEPFILE'])
deps += Environment.parse_makefile_deps(vars['_DEPFILE'], target) deps += Environment.parse_makefile_deps(vars['_DEPFILE'], target)
FileUtils.rm_f(vars['_DEPFILE']) FileUtils.rm_f(vars['_DEPFILE'])
end end
cache.register_build(target, command, deps.uniq, env) cache.register_build(target, options[:tc].command, deps.uniq, env)
target target
end end
end end

View File

@ -322,9 +322,7 @@ module Rscons
# Process all completed {ThreadedCommand} objects. # Process all completed {ThreadedCommand} objects.
completed_tcs.each do |tc| completed_tcs.each do |tc|
result = tc.build_operation[:builder].finalize( result = finalize_builder(tc)
command_status: tc.thread.value,
builder_info: tc.builder_info)
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.build_operation)
@ -594,18 +592,15 @@ module Rscons
end end
if rv.is_a?(ThreadedCommand) 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.build_operation = build_operation
start_threaded_command(rv) start_threaded_command(rv)
if options[:allow_delayed_execution] unless options[:allow_delayed_execution]
# Store the build operation so the post-build hooks can be called
# with it when the threaded command completes.
rv.build_operation = build_operation
else
# Delayed command execution is not allowed, so we need to execute # Delayed command execution is not allowed, so we need to execute
# the command and finalize the builder now. # the command and finalize the builder now.
tc = wait_for_threaded_commands(which: [rv]) tc = wait_for_threaded_commands(which: [rv])
rv = builder.finalize( rv = finalize_builder(tc)
command_status: tc.thread.value,
builder_info: tc.builder_info)
if rv if rv
call_build_hooks[:post] call_build_hooks[:post]
else else
@ -885,6 +880,20 @@ module Rscons
command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ') command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ')
end end
# Call a builder's #finalize method after a ThreadedCommand terminates.
#
# @param tc [ThreadedCommand]
# The ThreadedCommand returned from the builder's #run method.
#
# @return [String, false]
# Result of Builder#finalize.
def finalize_builder(tc)
tc.build_operation[:builder].finalize(
tc.build_operation.merge(
command_status: tc.thread.value,
tc: tc))
end
# Parse dependencies for a given target from a Makefile. # Parse dependencies for a given target from a Makefile.
# #
# This method is used internally by Rscons builders. # This method is used internally by Rscons builders.