From 1fe2c6401d6e4a9efe239ee17777d370ce9053ce Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 27 Mar 2014 13:30:11 -0400 Subject: [PATCH] expand ^/ in target paths to be relative to the build root --- lib/rscons/environment.rb | 24 +++++++++++++++++++++++- spec/build_tests_spec.rb | 17 +++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 12a6eb1..dfc0fd6 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -171,6 +171,7 @@ module Rscons # When a block is passed to Environment.new, this method is automatically # called after the block returns. def process + clean_target_paths! cache = Cache.new targets_processed = {} process_target = proc do |target| @@ -192,7 +193,7 @@ module Rscons result end end - @targets.each do |target, info| + @targets.each do |target, target_params| process_target.call(target) end cache.write @@ -331,6 +332,27 @@ module Rscons builder.run(target, sources, cache, self, vars) end + private + + # Expand all target paths that begin with ^/ to be relative to the + # Environment's build root, if present + def clean_target_paths! + if @build_root + expand = lambda do |path| + path.sub(%r{^\^(?=[\\/])}, @build_root) + end + + new_targets = {} + @targets.each_pair do |target, target_params| + target_params[:sources].map! do |source| + expand[source] + end + new_targets[expand[target]] = target_params + end + @targets = new_targets + end + end + # Parse dependencies for a given target from a Makefile. # This method is used internally by Rscons builders. # @param mf_fname [String] File name of the Makefile to read. diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index ec61fd7..685a285 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -180,6 +180,23 @@ describe Rscons do lines.should == ["CC build_root/src/one/one.o", "CC build_root/src/two/two.o", "LD build_dir"] end + it "expands target and source paths starting with ^/ to be relative to the build root" do + test_dir('build_dir') + Rscons::Environment.new(echo: :command) do |env| + env.append('CPPPATH' => Dir['src/**/*/']) + env.build_root = "build_root" + FileUtils.mkdir_p(env.build_root) + FileUtils.mv("src/one/one.c", "build_root") + env.Object("^/one.o", "^/one.c") + env.Program('build_dir', Dir['src/**/*.c'] + ["^/one.o"]) + end + lines.should == [ + %q{gcc -c -o build_root/one.o -MMD -MF build_root/one.mf -Isrc/one/ -Isrc/two/ build_root/one.c}, + %q{gcc -c -o build_root/src/two/two.o -MMD -MF build_root/src/two/two.mf -Isrc/one/ -Isrc/two/ src/two/two.c}, + %q{gcc -o build_dir build_root/src/two/two.o build_root/one.o}, + ] + end + it 'cleans built files' do test_dir('build_dir') Rscons::Environment.new do |env|