pass environment to builder functions instead of as an initialization parameter

This commit is contained in:
Josh Holtrop 2013-07-15 21:41:33 -04:00
parent bcfeadeebf
commit c9f6bdb2e2
5 changed files with 19 additions and 21 deletions

View File

@ -1,5 +1,5 @@
class MySource < Rscons::Builder
def run(target, sources, cache)
def run(target, sources, cache, env)
File.open(target, 'w') do |fh|
fh.puts <<EOF
#define THE_VALUE 5678
@ -10,7 +10,7 @@ EOF
end
Rscons::Environment.new(echo: :short) do |env|
env.add_builder(MySource.new(env))
env.add_builder(MySource.new)
env.MySource('inc.h', [])
env.Program('program', Dir['*.c'])
end

View File

@ -1,12 +1,9 @@
module Rscons
class Builder
def initialize(env)
@env = env
end
def default_variables(env)
{}
end
def produces?(target, source)
def produces?(target, source, env)
false
end
end

View File

@ -13,22 +13,22 @@ module Rscons
}
end
def produces?(target, source)
target.has_suffix?(@env['OBJSUFFIX']) and source.has_suffix?(@env['CSUFFIX'])
def produces?(target, source, env)
target.has_suffix?(env['OBJSUFFIX']) and source.has_suffix?(env['CSUFFIX'])
end
def run(target, sources, cache)
def run(target, sources, cache, env)
vars = {
'TARGET' => target,
'SOURCES' => sources,
'DEPFILE' => target.set_suffix('.mf'),
}
command = @env.build_command(@env['CCCOM'], vars)
command = env.build_command(env['CCCOM'], vars)
unless cache.up_to_date?(target, command, sources)
return false unless @env.execute("CC #{target}", command)
return false unless env.execute("CC #{target}", command)
deps = sources
if File.exists?(vars['DEPFILE'])
deps += @env.parse_makefile_deps(vars['DEPFILE'], target)
deps += env.parse_makefile_deps(vars['DEPFILE'], target)
FileUtils.rm_f(vars['DEPFILE'])
end
cache.register_build(target, command, deps.uniq)

View File

@ -12,27 +12,27 @@ module Rscons
}
end
def run(target, sources, cache)
def run(target, sources, cache, env)
# convert sources to object file names
sources = sources.map do |source|
if source.has_suffix?([@env['OBJSUFFIX'], @env['LIBSUFFIX']])
if source.has_suffix?([env['OBJSUFFIX'], env['LIBSUFFIX']])
source
else
o_file = @env.get_build_fname(source, @env['OBJSUFFIX', :string])
builder = @env.builders.values.find { |b| b.produces?(o_file, source) }
o_file = env.get_build_fname(source, env['OBJSUFFIX', :string])
builder = env.builders.values.find { |b| b.produces?(o_file, source, env) }
builder or raise "No builder found to convert input source #{source.inspect} to an object file."
builder.run(o_file, [source], cache) or break
builder.run(o_file, [source], cache, env) or break
end
end
if sources
vars = {
'TARGET' => target,
'SOURCES' => sources,
'LD' => @env['LD'] || @env['CC'], # TODO: figure out whether to use CC or CXX
'LD' => env['LD'] || env['CC'], # TODO: figure out whether to use CC or CXX
}
command = @env.build_command(@env['LDCOM'], vars)
command = env.build_command(env['LDCOM'], vars)
unless cache.up_to_date?(target, command, sources)
return false unless @env.execute("LD #{target}", command)
return false unless env.execute("LD #{target}", command)
cache.register_build(target, command, sources)
end
target

View File

@ -21,7 +21,7 @@ module Rscons
exclude_builders = Set.new(@varset[:exclude_builders] || [])
DEFAULT_BUILDERS.each do |builder_class|
unless exclude_builders.include?(builder_class.short_name)
add_builder(builder_class.new(self))
add_builder(builder_class.new)
end
end
end
@ -81,6 +81,7 @@ module Rscons
@targets[target][:builder].run(target,
@targets[target][:source],
cache,
self,
*@targets[target][:args])
else
false