From c7ab3397534204a6a9c413caf0a715f83415a026 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 29 Jun 2013 20:05:41 -0400 Subject: [PATCH] parse makefile depends generated by gcc --- lib/rscons/builders/cc.rb | 11 +++++++++-- lib/rscons/environment.rb | 19 +++++++++++++++++++ spec/build_tests_spec.rb | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/rscons/builders/cc.rb b/lib/rscons/builders/cc.rb index 3a1c645..8fe8dab 100644 --- a/lib/rscons/builders/cc.rb +++ b/lib/rscons/builders/cc.rb @@ -7,7 +7,8 @@ module Rscons 'CPPFLAGS' => [], 'OBJSUFFIX' => '.o', 'CSUFFIX' => '.c', - 'CCCOM' => ['$CC', '-c', '-o', '$TARGET', '$CPPFLAGS', '$CFLAGS', '$SOURCES'] + 'CCDEPGEN' => ['-MMD', '-MF', '$DEPFILE'], + 'CCCOM' => ['$CC', '-c', '-o', '$TARGET', '$CCDEPGEN', '$CPPFLAGS', '$CFLAGS', '$SOURCES'] } end @@ -22,9 +23,15 @@ module Rscons vars = { 'TARGET' => target, 'SOURCES' => source, + 'DEPFILE' => env.stem(target) + '.mf', } env.execute("CC #{target}", env['CCCOM'], vars) - Cache.open.register_build(target, [source]) + deps = [source] + if File.exists?(vars['DEPFILE']) + deps += env.parse_makefile_deps(vars['DEPFILE'], target) + FileUtils.rm_f(vars['DEPFILE']) + end + Cache.open.register_build(target, deps.uniq) end target end diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 70eabe4..1367b90 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -107,5 +107,24 @@ module Rscons orig_method_missing(method, *args) end end + + def parse_makefile_deps(mf, fname) + deps = [] + buildup = '' + File.read(mf).each_line do |line| + if line =~ /^(.*)\\\s*$/ + buildup += ' ' + $1 + else + if line =~ /^(.*): (.*)$/ + target, tdeps = $1.strip, $2 + if target == fname + deps += tdeps.split(' ').map(&:strip) + end + end + buildup = '' + end + end + deps + end end end diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index a85979a..02cdaea 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -49,7 +49,7 @@ describe Rscons do end it 'does not rebuild the program if no sources changed' do - $stdout.should_receive(:puts).once.with('gcc -c -o simple.o simple.c') + $stdout.should_receive(:puts).once.with("gcc -c -o simple.o -MMD -MF simple.mf simple.c") $stdout.should_receive(:puts).once.with('gcc -o simple simple.o') setup_testdir(['simple.c']) do env = Rscons::Environment.new