From f815952ab3acf15857f7e5571a50c965b315478e Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 24 May 2017 15:41:39 -0400 Subject: [PATCH] add Builder#standard_threaded_build, #standard_finalize parallelize Program builder command --- lib/rscons/builder.rb | 53 +++++++++++++++++++++++++++++++++- lib/rscons/builders/object.rb | 10 +------ lib/rscons/builders/program.rb | 14 ++++++++- 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/lib/rscons/builder.rb b/lib/rscons/builder.rb index cee9016..fb8a8ad 100644 --- a/lib/rscons/builder.rb +++ b/lib/rscons/builder.rb @@ -148,6 +148,8 @@ module Rscons # This method is called after the {#run} method if the {#run} method # returns a {ThreadedCommand} object. # + # @since 1.10.0 + # # @param options [Hash] # Options. # @option options [String] :target @@ -175,7 +177,7 @@ module Rscons end # 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] # Short description of build action to be printed when env.echo == @@ -200,5 +202,54 @@ module Rscons end target 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] + # The command to execute to build the target. + # @param sources [Array] 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 diff --git a/lib/rscons/builders/object.rb b/lib/rscons/builders/object.rb index fe1671f..7650608 100644 --- a/lib/rscons/builders/object.rb +++ b/lib/rscons/builders/object.rb @@ -96,15 +96,7 @@ module Rscons v.nil? and raise "Error: unknown input file type: #{sources.first.inspect}" end.first command = env.build_command("${#{com_prefix}CMD}", vars) - if cache.up_to_date?(target, command, sources, env) - target - else - cache.mkdir_p(File.dirname(target)) - FileUtils.rm_f(target) - ThreadedCommand.new( - command, - short_description: "#{com_prefix} #{target}") - end + standard_threaded_build("#{com_prefix} #{target}", target, command, sources, env, cache) end # Finalize the build operation. diff --git a/lib/rscons/builders/program.rb b/lib/rscons/builders/program.rb index ce21c19..d558858 100644 --- a/lib/rscons/builders/program.rb +++ b/lib/rscons/builders/program.rb @@ -83,8 +83,20 @@ module Rscons '_SOURCES' => objects, 'LD' => ld, }) + options[:sources] = objects 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