Support redirecting standard output using the Command builder - close #28

This commit is contained in:
Josh Holtrop 2017-05-29 12:50:34 -04:00
parent 8cbb9fa30e
commit 43df46018c
4 changed files with 34 additions and 5 deletions

View File

@ -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

View File

@ -219,11 +219,14 @@ module Rscons
# @param sources [Array<String>] Source file name(s). # @param sources [Array<String>] Source file name(s).
# @param env [Environment] The Environment executing the builder. # @param env [Environment] The Environment executing the builder.
# @param cache [Cache] The Cache object. # @param cache [Cache] The Cache object.
# @param options [Hash] Options.
# @options options [String] :stdout
# File name to redirect standard output to.
# #
# @return [String,ThreadedCommand] # @return [String,ThreadedCommand]
# The name of the target if it is already up to date or the # The name of the target if it is already up to date or the
# {ThreadedCommand} object created to update it. # {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) if cache.up_to_date?(target, command, sources, env)
target target
else else
@ -231,9 +234,11 @@ module Rscons
cache.mkdir_p(File.dirname(target)) cache.mkdir_p(File.dirname(target))
FileUtils.rm_f(target) FileUtils.rm_f(target)
end end
ThreadedCommand.new( tc_options = {short_description: short_cmd_string}
command, if options[:stdout]
short_description: short_cmd_string) tc_options[:system_options] = {out: options[:stdout]}
end
ThreadedCommand.new(command, tc_options)
end end
end end

View File

@ -25,7 +25,11 @@ module Rscons
}) })
command = env.build_command("${CMD}", vars) command = env.build_command("${CMD}", vars)
cmd_desc = vars["CMD_DESC"] || "Command" 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 end
# Finalize a build. # Finalize a build.

View File

@ -748,6 +748,18 @@ EOF
expect(result.stderr).to eq "" expect(result.stderr).to eq ""
expect(result.stdout).to eq "" expect(result.stdout).to eq ""
end 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 end
context "Directory builder" do context "Directory builder" do