add Rscons::BuildTarget

Return a BuildTarget object from Environment#method_missing when a build target is registered.
This commit is contained in:
Josh Holtrop 2014-06-12 13:31:17 -04:00
parent 87b25d7bfd
commit 1b50a9dd3c
8 changed files with 69 additions and 15 deletions

View File

@ -1,3 +1,4 @@
require_relative "rscons/build_target"
require_relative "rscons/builder" require_relative "rscons/builder"
require_relative "rscons/cache" require_relative "rscons/cache"
require_relative "rscons/environment" require_relative "rscons/environment"

View File

@ -0,0 +1,29 @@
module Rscons
# The BuildTarget class represents a single build target.
class BuildTarget
# Create a BuildTarget object.
#
# @param env [Environment] The Environment.
# @param target [String] Name of the target file.
def initialize(env, target)
@env = env
@target = target
end
# Manually record a given target as depending on the specified files.
#
# @param user_deps [Array<String>] Dependency files.
def depends(*user_deps)
@env.depends(@target, *user_deps)
end
# Convert the BuildTarget to a String.
#
# This method always returns the target file name.
#
# @return [String] Target file name.
def to_s
@target
end
end
end

View File

@ -19,6 +19,19 @@ module Rscons
{} {}
end end
# Create a BuildTarget object for this build target.
#
# Builder sub-classes can override this method to manipulate parameters
# (for example, add a suffix to the user-given target file name).
#
# @param env [Environment] The Environment.
# @param target [String] The user-supplied target name.
#
# @return [BuildTarget]
def create_build_target(env, target)
BuildTarget.new(env, target)
end
# Return whether this builder object is capable of producing a given target # Return whether this builder object is capable of producing a given target
# file name from a given source file name. # file name from a given source file name.
# @param target [String] The target file name. # @param target [String] The target file name.

View File

@ -18,6 +18,13 @@ module Rscons
} }
end end
def create_build_target(env, target)
unless target =~ /\./
target += env.expand_varref("${PROGSUFFIX}")
end
super(env, target)
end
def run(target, sources, cache, env, vars) def run(target, sources, cache, env, vars)
# build sources to linkable objects # build sources to linkable objects
objects = env.build_sources(sources, env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], vars).flatten, cache, vars) objects = env.build_sources(sources, env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], vars).flatten, cache, vars)
@ -32,9 +39,6 @@ module Rscons
else else
"${CC}" "${CC}"
end end
unless target =~ /\./
target += env.expand_varref("${PROGSUFFIX}", vars)
end
vars = vars.merge({ vars = vars.merge({
'_TARGET' => target, '_TARGET' => target,
'_SOURCES' => objects, '_SOURCES' => objects,

View File

@ -260,7 +260,10 @@ module Rscons
raise "Unexpected construction variable set: #{vars.inspect}" raise "Unexpected construction variable set: #{vars.inspect}"
end end
sources = Array(sources) sources = Array(sources)
add_target(target, @builders[method.to_s], sources, vars, rest) builder = @builders[method.to_s]
build_target = builder.create_build_target(self, target)
add_target(build_target.to_s, builder, sources, vars, rest)
build_target
else else
super super
end end
@ -275,9 +278,14 @@ module Rscons
} }
end end
# Manually record a given target as depending on the specified # Manually record a given target as depending on the specified files.
# dependency files. #
# @param target [String,BuildTarget] Target file.
# @param user_deps [Array<String>] Dependency files.
#
# @return [void]
def depends(target, *user_deps) def depends(target, *user_deps)
target = target.to_s
@user_deps[target] ||= [] @user_deps[target] ||= []
@user_deps[target] = (@user_deps[target] + user_deps).uniq @user_deps[target] = (@user_deps[target] + user_deps).uniq
end end

View File

@ -432,21 +432,20 @@ EOF
it 'rebuilds when user-specified dependencies change' do it 'rebuilds when user-specified dependencies change' do
test_dir('simple') test_dir('simple')
env = Rscons::Environment.new do |env| env = Rscons::Environment.new do |env|
env.Program('simple.exe', Dir['*.c']) env.Program('simple.exe', Dir['*.c']).depends("file.ld")
File.open("file.ld", "w") do |fh| File.open("file.ld", "w") do |fh|
fh.puts("foo") fh.puts("foo")
end end
env.depends('simple.exe', 'file.ld')
end end
lines.should == ["CC simple.o", "LD simple.exe"] lines.should == ["CC simple.o", "LD simple.exe"]
File.exists?('simple.o').should be_true File.exists?('simple.o').should be_true
`./simple.exe`.should == "This is a simple C program\n" `./simple.exe`.should == "This is a simple C program\n"
e2 = Rscons::Environment.new do |env| e2 = Rscons::Environment.new do |env|
env.Program('simple.exe', Dir['*.c']) program = env.Program('simple.exe', Dir['*.c'])
env.depends(program, "file.ld")
File.open("file.ld", "w") do |fh| File.open("file.ld", "w") do |fh|
fh.puts("bar") fh.puts("bar")
end end
env.depends('simple.exe', 'file.ld')
end end
lines.should == ["LD simple.exe"] lines.should == ["LD simple.exe"]
e3 = Rscons::Environment.new do |env| e3 = Rscons::Environment.new do |env|

View File

@ -5,13 +5,13 @@ module Rscons
subject {Program.new} subject {Program.new}
it "supports overriding CC construction variable" do it "supports overriding CC construction variable" do
subject.should_receive(:standard_build).with("LD prog#{env["PROGSUFFIX"]}", "prog#{env["PROGSUFFIX"]}", ["sp-c++", "-o", "prog#{env["PROGSUFFIX"]}", "prog.o"], ["prog.o"], env, :cache) subject.should_receive(:standard_build).with("LD prog", "prog", ["sp-c++", "-o", "prog", "prog.o"], ["prog.o"], env, :cache)
subject.run("prog", ["prog.o"], :cache, env, "CC" => "sp-c++") subject.run("prog", ["prog.o"], :cache, env, "CC" => "sp-c++")
end end
it "supports overriding LDCMD construction variable" do it "supports overriding LDCMD construction variable" do
subject.should_receive(:standard_build).with("LD prog.exe", "prog.exe", ["special", "LD!", "prog.o"], ["prog.o"], env, :cache) subject.should_receive(:standard_build).with("LD prog.exe", "prog.exe", ["special", "LD!", "prog.o"], ["prog.o"], env, :cache)
subject.run("prog", ["prog.o"], :cache, env, "LDCMD" => ["special", "LD!", "${_SOURCES}"]) subject.run("prog.exe", ["prog.o"], :cache, env, "LDCMD" => ["special", "LD!", "${_SOURCES}"])
end end
end end
end end

View File

@ -295,11 +295,11 @@ module Rscons
it "records the target when the target method is a known builder" do it "records the target when the target method is a known builder" do
env = Environment.new env = Environment.new
env.instance_variable_get(:@targets).should == {} env.instance_variable_get(:@targets).should == {}
env.Program("target", ["src1", "src2"], var: "val") env.Object("target.o", ["src1.c", "src2.c"], var: "val")
target = env.instance_variable_get(:@targets)["target"] target = env.instance_variable_get(:@targets)["target.o"]
target.should_not be_nil target.should_not be_nil
target[:builder].is_a?(Builder).should be_true target[:builder].is_a?(Builder).should be_true
target[:sources].should == ["src1", "src2"] target[:sources].should == ["src1.c", "src2.c"]
target[:vars].should == {var: "val"} target[:vars].should == {var: "val"}
target[:args].should == [] target[:args].should == []
end end