From 75f241857042a5acb760dd649eedacd2bc245568 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 12 Sep 2013 22:38:52 -0400 Subject: [PATCH] refactor some Program functionality into Environment#build_sources() --- lib/rscons/builders/program.rb | 13 ++----------- lib/rscons/environment.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/rscons/builders/program.rb b/lib/rscons/builders/program.rb index 927786a..d7eec0f 100644 --- a/lib/rscons/builders/program.rb +++ b/lib/rscons/builders/program.rb @@ -15,17 +15,8 @@ module Rscons end def run(target, sources, cache, env) - # convert sources to object file names - objects = sources.map do |source| - 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, env) } - builder or raise "No builder found to convert input source #{source.inspect} to an object file." - builder.run(o_file, [source], cache, env) or break - end - end + # build sources to linkable objects + objects = env.build_sources(sources, [env['OBJSUFFIX'], env['LIBSUFFIX']].flatten, cache) if objects use_cxx = sources.map do |s| s.has_suffix?(env['CXXSUFFIX']) diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index 2c714c2..53c4d2c 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -228,5 +228,32 @@ module Rscons end deps end + + # Build a list of source files into files containing one of the suffixes + # given by suffixes. + # This method is used internally by RScons builders. + # @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. + # Return a list of the converted file names. + def build_sources(sources, suffixes, cache) + sources.map do |source| + if source.has_suffix?(suffixes) + source + else + converted = nil + suffixes.each do |suffix| + 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) + return nil unless converted + break + end + end + converted or raise "Could not find a builder to handle #{source.inspect}." + end + end + end end end