diff --git a/build_tests/simple/command_redirect.rb b/build_tests/simple/command_redirect.rb new file mode 100644 index 0000000..4a50093 --- /dev/null +++ b/build_tests/simple/command_redirect.rb @@ -0,0 +1,8 @@ +Rscons::Environment.new do |env| + env.Object("simple.o", "simple.c") + env.Command("simple.txt", + "simple.o", + "CMD" => %w[objdump --disassemble --source ${_SOURCES}], + "CMD_STDOUT" => "${_TARGET}", + "CMD_DESC" => "My Disassemble") +end diff --git a/lib/rscons/builder.rb b/lib/rscons/builder.rb index 3a3e62b..d9453d7 100644 --- a/lib/rscons/builder.rb +++ b/lib/rscons/builder.rb @@ -219,11 +219,14 @@ module Rscons # @param sources [Array] Source file name(s). # @param env [Environment] The Environment executing the builder. # @param cache [Cache] The Cache object. + # @param options [Hash] Options. + # @options options [String] :stdout + # File name to redirect standard output to. # # @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) + def standard_threaded_build(short_cmd_string, target, command, sources, env, cache, options = {}) if cache.up_to_date?(target, command, sources, env) target else @@ -231,9 +234,11 @@ module Rscons cache.mkdir_p(File.dirname(target)) FileUtils.rm_f(target) end - ThreadedCommand.new( - command, - short_description: short_cmd_string) + tc_options = {short_description: short_cmd_string} + if options[:stdout] + tc_options[:system_options] = {out: options[:stdout]} + end + ThreadedCommand.new(command, tc_options) end end diff --git a/lib/rscons/builders/command.rb b/lib/rscons/builders/command.rb index fcf5d7b..91099a4 100644 --- a/lib/rscons/builders/command.rb +++ b/lib/rscons/builders/command.rb @@ -25,7 +25,11 @@ module Rscons }) command = env.build_command("${CMD}", vars) cmd_desc = vars["CMD_DESC"] || "Command" - standard_threaded_build("#{cmd_desc} #{target}", target, command, sources, env, cache) + options = {} + if vars["CMD_STDOUT"] + options[:stdout] = env.expand_varref("${CMD_STDOUT}", vars) + end + standard_threaded_build("#{cmd_desc} #{target}", target, command, sources, env, cache, options) end # Finalize a build. diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index d4f7149..ea8869e 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -748,6 +748,18 @@ EOF expect(result.stderr).to eq "" expect(result.stdout).to eq "" end + + it "allows redirecting standard output to a file" do + test_dir("simple") + + result = run_test(rsconsfile: "command_redirect.rb") + expect(result.stderr).to eq "" + expect(lines(result.stdout)).to eq [ + "CC simple.o", + "My Disassemble simple.txt", + ] + expect(File.read("simple.txt")).to match /Disassembly of section .text:/ + end end context "Directory builder" do