support registering multiple build targets with the same path - close #26

This commit is contained in:
Josh Holtrop 2015-01-28 17:10:36 -05:00
parent 02aff35222
commit 29a8684f90
3 changed files with 46 additions and 27 deletions

View File

@ -280,33 +280,35 @@ module Rscons
# #
# @return [void] # @return [void]
def process def process
expand_paths!
while @targets.size > 0 while @targets.size > 0
expand_paths!
targets = @targets targets = @targets
@targets = {} @targets = {}
cache = Cache.instance cache = Cache.instance
cache.clear_checksum_cache! cache.clear_checksum_cache!
targets_processed = {} targets_processed = Set.new
process_target = proc do |target| process_target = proc do |target|
targets_processed[target] ||= begin unless targets_processed.include?(target)
targets[target][:sources].each do |src| targets_processed << target
if targets.include?(src) and not targets_processed.include?(src) targets[target].each do |target_params|
process_target.call(src) target_params[:sources].each do |src|
if targets.include?(src) and not targets_processed.include?(src)
process_target.call(src)
end
end
result = run_builder(target_params[:builder],
target,
target_params[:sources],
cache,
target_params[:vars] || {})
unless result
raise BuildError.new("Failed to build #{target}")
end end
end end
result = run_builder(targets[target][:builder],
target,
targets[target][:sources],
cache,
targets[target][:vars] || {})
unless result
raise BuildError.new("Failed to build #{target}")
end
result
end end
end end
begin begin
targets.each do |target, target_params| targets.each_key do |target|
process_target.call(target) process_target.call(target)
end end
ensure ensure
@ -404,7 +406,8 @@ module Rscons
# #
# @return [void] # @return [void]
def add_target(target, builder, sources, vars, args) def add_target(target, builder, sources, vars, args)
@targets[target] = { @targets[target] ||= []
@targets[target] << {
builder: builder, builder: builder,
sources: sources, sources: sources,
vars: vars, vars: vars,
@ -679,14 +682,16 @@ module Rscons
# #
# @return [void] # @return [void]
def expand_paths! def expand_paths!
@targets = @targets.reduce({}) do |result, (target, target_params)| @targets = @targets.reduce({}) do |result, (target, target_params_list)|
sources = target_params[:sources].map do |source|
source = expand_path(source) if @build_root
expand_varref(source)
end.flatten
target = expand_path(target) if @build_root target = expand_path(target) if @build_root
target = expand_varref(target) target = expand_varref(target)
result[target] = target_params.merge(sources: sources) result[target] = target_params_list.map do |target_params|
sources = target_params[:sources].map do |source|
source = expand_path(source) if @build_root
expand_varref(source)
end.flatten
target_params.merge(sources: sources)
end
result result
end end
end end

View File

@ -764,4 +764,18 @@ EOF
expect(`./simple`).to eq "This is a simple C program\n" expect(`./simple`).to eq "This is a simple C program\n"
end end
it "supports registering multiple build targets with the same target path" do
test_dir("build_dir")
Rscons::Environment.new do |env|
env["CPPPATH"] << "src/two"
env.Object("one.o", "src/one/one.c")
env.Object("one.o", "src/two/two.c")
end
expect(File.exists?("one.o")).to be_truthy
expect(lines).to eq([
"CC one.o",
"CC one.o",
])
end
end end

View File

@ -305,10 +305,10 @@ module Rscons
env.Object("target.o", ["src1.c", "src2.c"], var: "val") env.Object("target.o", ["src1.c", "src2.c"], var: "val")
target = env.instance_variable_get(:@targets)["target.o"] target = env.instance_variable_get(:@targets)["target.o"]
expect(target).to_not be_nil expect(target).to_not be_nil
expect(target[:builder].is_a?(Builder)).to be_truthy expect(target[0][:builder].is_a?(Builder)).to be_truthy
expect(target[:sources]).to eq ["src1.c", "src2.c"] expect(target[0][:sources]).to eq ["src1.c", "src2.c"]
expect(target[:vars]).to eq({var: "val"}) expect(target[0][:vars]).to eq({var: "val"})
expect(target[:args]).to eq [] expect(target[0][:args]).to eq []
end end
it "raises an error when vars is not a Hash" do it "raises an error when vars is not a Hash" do