parse makefile depends generated by gcc

This commit is contained in:
Josh Holtrop 2013-06-29 20:05:41 -04:00
parent 37337f80d5
commit c7ab339753
3 changed files with 29 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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