diff --git a/build_tests/build_dir/build.rb b/build_tests/build_dir/build.rb new file mode 100644 index 0000000..65b14fd --- /dev/null +++ b/build_tests/build_dir/build.rb @@ -0,0 +1,5 @@ +Rscons::Environment.new do |env| + env.append('CPPPATH' => Dir['src/**/*/']) + env.build_dir('src', 'build') + env.Program('build_dir', Dir['src/**/*.c']) +end diff --git a/build_tests/build_dir/src/one/one.c b/build_tests/build_dir/src/one/one.c new file mode 100644 index 0000000..e0f31d7 --- /dev/null +++ b/build_tests/build_dir/src/one/one.c @@ -0,0 +1,6 @@ +#include "two.h" + +int main(int argc, char *argv[]) +{ + two(); +} diff --git a/build_tests/build_dir/src/two/two.c b/build_tests/build_dir/src/two/two.c new file mode 100644 index 0000000..d9a9257 --- /dev/null +++ b/build_tests/build_dir/src/two/two.c @@ -0,0 +1,7 @@ +#include +#include "two.h" + +void two(void) +{ + printf("Hello from two()\n"); +} diff --git a/build_tests/build_dir/src/two/two.h b/build_tests/build_dir/src/two/two.h new file mode 100644 index 0000000..a49837a --- /dev/null +++ b/build_tests/build_dir/src/two/two.h @@ -0,0 +1,6 @@ +#ifndef TWO_H +#define TWO_H + +void two(void); + +#endif diff --git a/lib/rscons/builders/program.rb b/lib/rscons/builders/program.rb index 897d820..c0d1437 100644 --- a/lib/rscons/builders/program.rb +++ b/lib/rscons/builders/program.rb @@ -18,23 +18,25 @@ module Rscons if source.has_suffix?([@env['OBJSUFFIX'], @env['LIBSUFFIX']]) source else - o_file = source.set_suffix(@env['OBJSUFFIX', :string]) + o_file = @env.get_build_fname(source, @env['OBJSUFFIX', :string]) builder = @env.builders.values.find { |b| b.produces?(o_file, source) } builder or raise "No builder found to convert input source #{source.inspect} to an object file." - builder.run(o_file, [source], cache) + builder.run(o_file, [source], cache) or break end end - vars = { - 'TARGET' => target, - 'SOURCES' => sources, - 'LD' => @env['LD'] || @env['CC'], # TODO: figure out whether to use CC or CXX - } - command = @env.build_command(@env['LDCOM'], vars) - unless cache.up_to_date?(target, command, sources) - return false unless @env.execute("LD #{target}", command) - cache.register_build(target, command, sources) + if sources + vars = { + 'TARGET' => target, + 'SOURCES' => sources, + 'LD' => @env['LD'] || @env['CC'], # TODO: figure out whether to use CC or CXX + } + command = @env.build_command(@env['LDCOM'], vars) + unless cache.up_to_date?(target, command, sources) + return false unless @env.execute("LD #{target}", command) + cache.register_build(target, command, sources) + end + target end - target end end end diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index d1d35c4..ce36e64 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -99,7 +99,7 @@ module Rscons def execute(short_desc, command) if @varset[:echo] == :command - puts command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ') + puts command.map { |c| c =~ /\s/ ? "'#{c}'" : c }.join(' ') elsif @varset[:echo] == :short puts short_desc end diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index a3d5c0c..c1776a6 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -112,4 +112,11 @@ describe Rscons do lines = build_testdir lines.should == ['gcc -o simple simple.o -lc'] end + + it 'builds object files in a different build directory' do + lines = test_dir('build_dir') + `./build_dir`.should == "Hello from two()\n" + File.exists?('build/one/one.o').should be_true + File.exists?('build/two/two.o').should be_true + end end