add Builder#standard_threaded_build, #standard_finalize

parallelize Program builder command
This commit is contained in:
Josh Holtrop 2017-05-24 15:41:39 -04:00
parent 1af3c5c9a4
commit f815952ab3
3 changed files with 66 additions and 11 deletions

View File

@ -148,6 +148,8 @@ module Rscons
# This method is called after the {#run} method if the {#run} method # This method is called after the {#run} method if the {#run} method
# returns a {ThreadedCommand} object. # returns a {ThreadedCommand} object.
# #
# @since 1.10.0
#
# @param options [Hash] # @param options [Hash]
# Options. # Options.
# @option options [String] :target # @option options [String] :target
@ -175,7 +177,7 @@ module Rscons
end end
# Check if the cache is up to date for the target and if not execute the # Check if the cache is up to date for the target and if not execute the
# build command. # build command. This method does not support parallelization.
# #
# @param short_cmd_string [String] # @param short_cmd_string [String]
# Short description of build action to be printed when env.echo == # Short description of build action to be printed when env.echo ==
@ -200,5 +202,54 @@ module Rscons
end end
target target
end end
# Check if the cache is up to date for the target and if not create a
# {ThreadedCommand} object to execute the build command in a thread.
#
# @since 1.10.0
#
# @param short_cmd_string [String]
# Short description of build action to be printed when env.echo ==
# :short.
# @param target [String] Name of the target file.
# @param command [Array<String>]
# The command to execute to build the target.
# @param sources [Array<String>] Source file name(s).
# @param env [Environment] The Environment executing the builder.
# @param cache [Cache] The Cache object.
#
# @return [String,ThreadedCommand]
# The name of the target if it is already up to date or the
# {ThreadedCommand} object created to update it.
def standard_threaded_build(short_cmd_string, target, command, sources, env, cache)
if cache.up_to_date?(target, command, sources, env)
target
else
unless Rscons.phony_target?(target)
cache.mkdir_p(File.dirname(target))
FileUtils.rm_f(target)
end
ThreadedCommand.new(
command,
short_description: short_cmd_string)
end
end
# Register build results from a {ThreadedCommand} with the cache.
#
# @since 1.10.0
#
# @param options [Hash]
# Builder finalize options.
#
# @return [String, nil]
# The target name on success or nil on failure.
def standard_finalize(options)
if options[:command_status]
target, sources, cache, env = options.values_at(:target, :sources, :cache, :env)
cache.register_build(target, options[:tc].command, sources, env)
target
end
end
end end
end end

View File

@ -96,15 +96,7 @@ module Rscons
v.nil? and raise "Error: unknown input file type: #{sources.first.inspect}" v.nil? and raise "Error: unknown input file type: #{sources.first.inspect}"
end.first end.first
command = env.build_command("${#{com_prefix}CMD}", vars) command = env.build_command("${#{com_prefix}CMD}", vars)
if cache.up_to_date?(target, command, sources, env) standard_threaded_build("#{com_prefix} #{target}", target, command, sources, env, cache)
target
else
cache.mkdir_p(File.dirname(target))
FileUtils.rm_f(target)
ThreadedCommand.new(
command,
short_description: "#{com_prefix} #{target}")
end
end end
# Finalize the build operation. # Finalize the build operation.

View File

@ -83,8 +83,20 @@ module Rscons
'_SOURCES' => objects, '_SOURCES' => objects,
'LD' => ld, 'LD' => ld,
}) })
options[:sources] = objects
command = env.build_command("${LDCMD}", vars) command = env.build_command("${LDCMD}", vars)
standard_build("LD #{target}", target, command, objects, env, cache) standard_threaded_build("LD #{target}", target, command, objects, env, cache)
end
# Finalize a build.
#
# @param options [Hash]
# Finalize options.
#
# @return [String, nil]
# The target name on success or nil on failure.
def finalize(options)
standard_finalize(options)
end end
end end