add Library default builder

This commit is contained in:
Josh Holtrop 2013-09-12 23:35:34 -04:00
parent e04e67698a
commit 64d15602cc
8 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Rscons::Environment.new(echo: :command) do |env|
env.Program('library', ['lib.a', 'three.c'])
env.Library("lib.a", ['one.c', 'two.c'], 'CPPFLAGS' => ['-Dmake_lib'])
end

View File

@ -0,0 +1,8 @@
#include <stdio.h>
#ifdef make_lib
int main(int argc, char *argv[])
{
printf("Library\n");
}
#endif

View File

View File

View File

@ -8,12 +8,14 @@ require "rscons/monkey/module"
require "rscons/monkey/string" require "rscons/monkey/string"
# default builders # default builders
require "rscons/builders/library"
require "rscons/builders/object" require "rscons/builders/object"
require "rscons/builders/program" require "rscons/builders/program"
# Namespace module for rscons classes # Namespace module for rscons classes
module Rscons module Rscons
DEFAULT_BUILDERS = [ DEFAULT_BUILDERS = [
Library,
Object, Object,
Program, Program,
] ]

View File

@ -0,0 +1,33 @@
require 'fileutils'
module Rscons
# A default RScons builder that produces a static library archive.
class Library < Builder
def default_variables(env)
{
'AR' => 'ar',
'LIBSUFFIX' => '.a',
'ARFLAGS' => [],
'ARCOM' => ['$AR', 'rcs', '$ARFLAGS', '$TARGET', '$SOURCES']
}
end
def run(target, sources, cache, env, vars = {})
# build sources to linkable objects
objects = env.build_sources(sources, [env['OBJSUFFIX'], env['LIBSUFFIX']].flatten, cache, vars)
if objects
vars = vars.merge({
'TARGET' => target,
'SOURCES' => objects,
})
command = env.build_command(env['ARCOM'], vars)
unless cache.up_to_date?(target, command, objects)
FileUtils.rm_f(target)
return false unless env.execute("AR #{target}", command)
cache.register_build(target, command, objects)
end
target
end
end
end
end

View File

@ -194,6 +194,9 @@ module Rscons
def method_missing(method, *args) def method_missing(method, *args)
if @builders.has_key?(method.to_s) if @builders.has_key?(method.to_s)
target, source, vars, *rest = args target, source, vars, *rest = args
unless vars.nil? or vars.is_a?(Hash) or vars.is_a?(VarSet)
raise "Unexpected construction variable set: #{vars.inspect}"
end
source = [source] unless source.is_a?(Array) source = [source] unless source.is_a?(Array)
@targets[target] = { @targets[target] = {
builder: @builders[method.to_s], builder: @builders[method.to_s],

View File

@ -153,4 +153,17 @@ describe Rscons do
File.exists?('two_sources').should be_true File.exists?('two_sources').should be_true
`./two_sources`.should == "This is a C program with two sources.\n" `./two_sources`.should == "This is a C program with two sources.\n"
end end
it 'builds a static library archive' do
lines = test_dir('library')
lines.should == [
'gcc -c -o one.o -MMD -MF one.mf -Dmake_lib one.c',
'gcc -c -o two.o -MMD -MF two.mf -Dmake_lib two.c',
'ar rcs lib.a one.o two.o',
'gcc -c -o three.o -MMD -MF three.mf three.c',
'gcc -o library lib.a three.o',
]
File.exists?('library').should be_true
`ar t lib.a`.should == "one.o\ntwo.o\n"
end
end end