Compare commits

...
4 Commits
Author SHA1 Message Date
josh 31abeed3d5 v0.0.13 2013-10-15 16:29:54 -04:00
josh 446fef931f add "tweakers" functionality
tweakers allow the user to modify construction variables before any build is performed
2013-10-15 16:15:37 -04:00
josh 4a2997feec VarSet: make a deep copy of values passed to VarSet#append() 2013-10-15 16:10:56 -04:00
josh 013995bbc0 refactor into Environment#run_builder() 2013-10-15 13:36:25 -04:00
5 changed files with 63 additions and 13 deletions

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
Rscons::Environment.new do |env|
env.append('CPPPATH' => Dir['src/**/*/'])
env.build_dir(%r{^src/([^/]+)/}, 'build_\\1/')
env.add_tweaker do |build_op|
if build_op[:target] =~ %r{build_one/.*\.o}
build_op[:vars]["CFLAGS"] << "-O1"
elsif build_op[:target] =~ %r{build_two/.*\.o}
build_op[:vars]["CFLAGS"] << "-O2"
end
end
env.Program('tweaker', Dir['src/**/*.c'])
end
+32 -4
View File
@@ -22,6 +22,7 @@ module Rscons
@targets = {}
@builders = {}
@build_dirs = []
@tweakers = []
@varset[:exclude_builders] ||= []
unless @varset[:exclude_builders] == :all
exclude_builders = Set.new(@varset[:exclude_builders] || [])
@@ -45,7 +46,7 @@ module Rscons
# Make a copy of the Environment object.
# The cloned environment will contain a copy of all environment options,
# construction variables, builders, and build directories. It will not
# contain a copy of the targets.
# contain a copy of the targets or tweakers.
# If a block is given, the Environment object is yielded to the block and
# when the block returns, the {#process} method is automatically called.
def clone(variables = {})
@@ -74,6 +75,11 @@ module Rscons
end
end
# Add a tweaker block to the Environment.
def add_tweaker(&block)
@tweakers << block
end
# Specify a build directory for this Environment.
# Source files from src_dir will produce object files under obj_dir.
def build_dir(src_dir, obj_dir)
@@ -136,10 +142,10 @@ module Rscons
if @targets[target][:source].map do |src|
targets_processed.include?(src) or not @targets.include?(src) or process_target.call(src)
end.all?
@targets[target][:builder].run(target,
run_builder(@targets[target][:builder],
target,
@targets[target][:source],
cache,
self,
@targets[target][:vars] || {})
else
false
@@ -238,6 +244,7 @@ module Rscons
# @param sources [Array] List of source files to build.
# @param suffixes [Array] List of suffixes to try to convert source files into.
# @param cache [Cache] The Cache.
# @param vars [Hash] Extra variables to pass to the builder.
# Return a list of the converted file names.
def build_sources(sources, suffixes, cache, vars = {})
sources.map do |source|
@@ -249,7 +256,7 @@ module Rscons
converted_fname = get_build_fname(source, suffix)
builder = @builders.values.find { |b| b.produces?(converted_fname, source, self) }
if builder
converted = builder.run(converted_fname, [source], cache, self, vars)
converted = run_builder(builder, converted_fname, [source], cache, vars)
return nil unless converted
break
end
@@ -258,5 +265,26 @@ module Rscons
end
end
end
# Invoke a builder to build the given target based on the given sources.
# @param builder [Builder] The Builder to use.
# @param target [String] The target output file.
# @param sources [Array] List of source files.
# @param cache [Cache] The Cache.
# @param vars [Hash] Extra variables to pass to the builder.
# Return the result of the builder's run() method.
def run_builder(builder, target, sources, cache, vars)
vars = @varset.merge(vars)
@tweakers.each do |tweaker_block|
build_operation = {
builder: builder,
target: target,
sources: sources,
vars: vars,
}
tweaker_block.call(build_operation)
end
builder.run(target, sources, cache, self, vars)
end
end
end
+1 -1
View File
@@ -44,7 +44,7 @@ module Rscons
# @param values [VarSet, Hash] New set of variables.
def append(values)
values = values.vars if values.is_a?(VarSet)
@vars.merge!(values)
@vars.merge!(Marshal.load(Marshal.dump(values)))
self
end
+1 -1
View File
@@ -1,4 +1,4 @@
module Rscons
# gem version
VERSION = "0.0.12"
VERSION = "0.0.13"
end
+17 -7
View File
@@ -11,10 +11,10 @@ describe Rscons do
FileUtils.rm_rf('build_tests_run')
end
def build_testdir
if File.exists?("build.rb")
build_rb = File.read("build.rb")
File.open("build.rb", "w") do |fh|
def build_testdir(build_script = "build.rb")
if File.exists?(build_script)
build_rb = File.read(build_script)
File.open(build_script, "w") do |fh|
fh.puts(<<EOF + build_rb)
require "simplecov"
@@ -27,7 +27,7 @@ end
require "rscons"
EOF
end
IO.popen(%{ruby -I #{@owd}/lib build.rb}) do |io|
IO.popen(%{ruby -I #{@owd}/lib #{build_script}}) do |io|
io.readlines.reject do |line|
line =~ /^Coverage report/
end
@@ -35,11 +35,11 @@ EOF
end
end
def test_dir(build_test_directory)
def test_dir(build_test_directory, build_script = "build.rb")
@build_test_name = build_test_directory
FileUtils.cp_r("build_tests/#{build_test_directory}", 'build_tests_run')
Dir.chdir("build_tests_run")
build_testdir
build_testdir(build_script)
end
def file_sub(fname)
@@ -207,4 +207,14 @@ EOF
File.exists?('library').should be_true
`ar t lib.a`.should == "one.o\ntwo.o\n"
end
it 'supports tweakers to override construction variables' do
lines = test_dir("build_dir", "tweaker_build.rb")
`./tweaker`.should == "Hello from two()\n"
lines.should =~ [
'gcc -c -o build_one/one.o -MMD -MF build_one/one.mf -Isrc/one/ -Isrc/two/ -O1 src/one/one.c',
'gcc -c -o build_two/two.o -MMD -MF build_two/two.mf -Isrc/one/ -Isrc/two/ -O2 src/two/two.c',
'gcc -o tweaker build_one/one.o build_two/two.o',
]
end
end