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

View File

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

View File

@ -13,22 +13,22 @@ module Rscons
} }
end end
def produces?(target, source) def produces?(target, source, env)
target.has_suffix?(@env['OBJSUFFIX']) and source.has_suffix?(@env['CSUFFIX']) target.has_suffix?(env['OBJSUFFIX']) and source.has_suffix?(env['CSUFFIX'])
end end
def run(target, sources, cache) def run(target, sources, cache, env)
vars = { vars = {
'TARGET' => target, 'TARGET' => target,
'SOURCES' => sources, 'SOURCES' => sources,
'DEPFILE' => target.set_suffix('.mf'), '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) 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 deps = sources
if File.exists?(vars['DEPFILE']) 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']) FileUtils.rm_f(vars['DEPFILE'])
end end
cache.register_build(target, command, deps.uniq) cache.register_build(target, command, deps.uniq)

View File

@ -12,27 +12,27 @@ module Rscons
} }
end end
def run(target, sources, cache) def run(target, sources, cache, env)
# convert sources to object file names # convert sources to object file names
sources = sources.map do |source| sources = sources.map do |source|
if source.has_suffix?([@env['OBJSUFFIX'], @env['LIBSUFFIX']]) if source.has_suffix?([env['OBJSUFFIX'], env['LIBSUFFIX']])
source source
else else
o_file = @env.get_build_fname(source, @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 = 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 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
end end
if sources if sources
vars = { vars = {
'TARGET' => target, 'TARGET' => target,
'SOURCES' => sources, '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) 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) cache.register_build(target, command, sources)
end end
target target

View File

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