remove Builder.features and Builder.produces?

This commit is contained in:
Josh Holtrop 2019-03-16 21:54:20 -04:00
parent ac1fc6d106
commit 3ff09978f4
11 changed files with 69 additions and 199 deletions

View File

@ -0,0 +1,7 @@
build do
Environment.new do |env|
env.Program("library.exe", ["lib.a", "three.c"])
env.Object("two.o", "two.c")
env.Library("lib.a", ["one.c", "two.o"], 'CPPFLAGS' => ['-Dmake_lib'])
end
end

View File

@ -0,0 +1,7 @@
build do
Environment.new do |env|
env["CPPPATH"] << "src/lib"
env.SharedObject("one.o", "src/lib/one.c")
libmine = env.SharedLibrary("mine", ["one.o", "src/lib/two.c"])
end
end

View File

@ -16,31 +16,6 @@ module Rscons
def name def name
super.split(":").last super.split(":").last
end end
# Return a set of build features that this builder provides.
#
# @return [Array<String>]
# Set of build features that this builder provides.
def features
[]
end
# Return whether this builder object is capable of producing a given target
# file name from a given source file name.
#
# @param target [String]
# The target file name.
# @param source [String]
# The source file name.
# @param env [Environment]
# The Environment.
#
# @return [Boolean]
# Whether this builder object is capable of producing a given target
# file name from a given source file name.
def produces?(target, source, env)
false
end
end end
# @return [String, Symbol] # @return [String, Symbol]

View File

@ -7,8 +7,13 @@ module Rscons
def initialize(options) def initialize(options)
super(options) super(options)
suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars) suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars)
# Register builders to build each source to an object file or library. @objects = @sources.map do |source|
@objects = @env.register_builds(@target, @sources, suffixes, @vars) if source.end_with?(*suffixes)
source
else
@env.register_dependency_build(@target, source, suffixes.first, @vars, Object)
end
end
end end
# Run the builder to produce a build target. # Run the builder to produce a build target.

View File

@ -12,28 +12,6 @@ module Rscons
"DC" => "DSUFFIX", "DC" => "DSUFFIX",
} }
class << self
# Return whether this builder object is capable of producing a given target
# file name from a given source file name.
#
# @param target [String]
# The target file name.
# @param source [String]
# The source file name.
# @param env [Environment]
# The Environment.
#
# @return [Boolean]
# Whether this builder object is capable of producing a given target
# file name from a given source file name.
def produces?(target, source, env)
target.end_with?(*env['OBJSUFFIX']) and
KNOWN_SUFFIXES.find do |compiler, suffix_var|
source.end_with?(*env[suffix_var])
end
end
end
# Run the builder to produce a build target. # Run the builder to produce a build target.
def run(options) def run(options)
if @command if @command

View File

@ -22,8 +22,13 @@ module Rscons
@target += @env.expand_varref("${PROGSUFFIX}", @vars) @target += @env.expand_varref("${PROGSUFFIX}", @vars)
end end
suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars) suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars)
# Register builders to build each source to an object file or library. @objects = @sources.map do |source|
@objects = @env.register_builds(@target, @sources, suffixes, @vars) if source.end_with?(*suffixes)
source
else
@env.register_dependency_build(@target, source, suffixes.first, @vars, Object)
end
end
end end
# Run the builder to produce a build target. # Run the builder to produce a build target.

View File

@ -4,16 +4,6 @@ module Rscons
# shared library. # shared library.
class SharedLibrary < Builder class SharedLibrary < Builder
class << self
# Return a set of build features that this builder provides.
#
# @return [Array<String>]
# Set of build features that this builder provides.
def features
%w[shared]
end
end
# Create an instance of the Builder to build a target. # Create an instance of the Builder to build a target.
def initialize(options) def initialize(options)
super(options) super(options)
@ -25,9 +15,13 @@ module Rscons
@target += @env.expand_varref("${SHLIBSUFFIX}", @vars) @target += @env.expand_varref("${SHLIBSUFFIX}", @vars)
end end
suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars) suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars)
# Register builders to build each source to an object file or library. @objects = @sources.map do |source|
@objects = @env.register_builds(@target, @sources, suffixes, @vars, if source.end_with?(*suffixes)
features: %w[shared]) source
else
@env.register_dependency_build(@target, source, suffixes.first, @vars, SharedObject)
end
end
end end
# Run the builder to produce a build target. # Run the builder to produce a build target.

View File

@ -13,36 +13,6 @@ module Rscons
"SHDC" => "DSUFFIX", "SHDC" => "DSUFFIX",
} }
class << self
# Return a set of build features that this builder provides.
#
# @return [Array<String>]
# Set of build features that this builder provides.
def features
%w[shared]
end
# Return whether this builder object is capable of producing a given target
# file name from a given source file name.
#
# @param target [String]
# The target file name.
# @param source [String]
# The source file name.
# @param env [Environment]
# The Environment.
#
# @return [Boolean]
# Whether this builder object is capable of producing a given target
# file name from a given source file name.
def produces?(target, source, env)
target.end_with?(*env['OBJSUFFIX']) and
KNOWN_SUFFIXES.find do |compiler, suffix_var|
source.end_with?(*env[suffix_var])
end
end
end
# Run the builder to produce a build target. # Run the builder to produce a build target.
def run(options) def run(options)
if @command if @command

View File

@ -238,16 +238,13 @@ module Rscons
# Source file name. # Source file name.
# @param suffix [String] # @param suffix [String]
# Suffix, including "." if desired. # Suffix, including "." if desired.
# @param options [Hash] # @param builder_class [Class]
# Extra options. # The builder in use.
# @option options [Array<String>] :features
# Builder features to be used for this build. See {#register_builds}.
# #
# @return [String] # @return [String]
# The file name to be built from +source_fname+ with suffix +suffix+. # The file name to be built from +source_fname+ with suffix +suffix+.
def get_build_fname(source_fname, suffix, options = {}) def get_build_fname(source_fname, suffix, builder_class)
options[:features] ||= [] extra_path = builder_class == Builders::SharedObject ? "/_shared" : ""
extra_path = options[:features].include?("shared") ? "/_shared" : ""
"#{@build_root}#{extra_path}/#{Util.make_relative_path(Rscons.set_suffix(source_fname, suffix))}".gsub("\\", "/") "#{@build_root}#{extra_path}/#{Util.make_relative_path(Rscons.set_suffix(source_fname, suffix))}".gsub("\\", "/")
end end
@ -421,52 +418,31 @@ module Rscons
@user_deps[target] @user_deps[target]
end end
# Find and register builders to build source files into files containing # Register a builder to build a source file into an output with the given
# one of the suffixes given by suffixes. # suffix.
# #
# This method is used internally by Rscons builders. It can be called # This method is used internally by Rscons builders. It can be called
# from the builder's #initialize method. # from the builder's #initialize method.
# #
# @since 1.10.0
#
# @param target [String] # @param target [String]
# The target that depends on these builds. # The target that depends on these builds.
# @param sources [Array<String>] # @param source [String]
# List of source file(s) to build. # Source file to build.
# @param suffixes [Array<String>] # @param suffix [String]
# List of suffixes to try to convert source files into. # Suffix to try to convert source files into.
# @param vars [Hash] # @param vars [Hash]
# Extra variables to pass to the builders. # Extra variables to pass to the builders.
# @param options [Hash] # @param builder_class [Class]
# Extra options. # The builder class to use.
# @option options [Array<String>] :features
# Set of features the builder must provide. Each feature can be proceeded
# by a "-" character to indicate that the builder must /not/ provide the
# given feature.
# * shared - builder builds a shared object/library
# #
# @return [Array<String>] # @return [String]
# List of the output file name(s). # Output file name.
def register_builds(target, sources, suffixes, vars, options = {}) def register_dependency_build(target, source, suffix, vars, builder_class)
options[:features] ||= [] output_fname = get_build_fname(source, suffix, builder_class)
self.__send__(builder_class.name, output_fname, source, vars)
@registered_build_dependencies[target] ||= Set.new @registered_build_dependencies[target] ||= Set.new
sources.map do |source| @registered_build_dependencies[target] << output_fname
if source.end_with?(*suffixes) output_fname
source
else
output_fname = nil
suffixes.each do |suffix|
attempt_output_fname = get_build_fname(source, suffix, features: options[:features])
if builder = find_builder_for(attempt_output_fname, source, options[:features])
output_fname = attempt_output_fname
self.__send__(builder.name, output_fname, source, vars)
@registered_build_dependencies[target] << output_fname
break
end
end
output_fname or raise "Could not find a builder for #{source.inspect}."
end
end
end end
# Expand a path to be relative to the Environment's build root. # Expand a path to be relative to the Environment's build root.
@ -706,46 +682,6 @@ module Rscons
end end
end end
# Find a builder that meets the requested features and produces a target
# of the requested name.
#
# @param target [String]
# Target file name.
# @param source [String]
# Source file name.
# @param features [Array<String>]
# See {#register_builds}.
#
# @return [Builder, nil]
# The builder found, if any.
def find_builder_for(target, source, features)
@builders.values.find do |builder_class|
features_met?(builder_class, features) and builder_class.produces?(target, source, self)
end
end
# Determine if a builder meets the requested features.
#
# @param builder_class [Class]
# The builder.
# @param features [Array<String>]
# See {#register_builds}.
#
# @return [Boolean]
# Whether the builder meets the requested features.
def features_met?(builder_class, features)
builder_features = builder_class.features
features.all? do |feature|
want_feature = true
if feature =~ /^-(.*)$/
want_feature = false
feature = $1
end
builder_has_feature = builder_features.include?(feature)
want_feature ? builder_has_feature : !builder_has_feature
end
end
end end
Environment.class_init Environment.class_init

View File

@ -1410,6 +1410,14 @@ EOF
expect(result.stderr).to eq "" expect(result.stderr).to eq ""
expect(lines(result.stdout)).to include "ar rcf lib.a build/e.1/one.o build/e.1/three.o build/e.1/two.o" expect(lines(result.stdout)).to include "ar rcf lib.a build/e.1/one.o build/e.1/three.o build/e.1/two.o"
end end
it "allows passing object files as sources" do
test_dir("library")
result = run_rscons(rsconscript: "library_from_object.rb")
expect(result.stderr).to eq ""
expect(File.exists?("two.o")).to be_truthy
expect(lines(result.stdout)).to include "Building static library archive => lib.a"
end
end end
context "SharedLibrary builder" do context "SharedLibrary builder" do
@ -1425,6 +1433,13 @@ EOF
expect(slines).to include("Linking => libmine.so") expect(slines).to include("Linking => libmine.so")
end end
end end
it "allows passing object files as sources" do
test_dir "shared_library"
result = run_rscons(rsconscript: "shared_library_from_object.rb")
expect(result.stderr).to eq ""
expect(File.exists?("one.o"))
end
end end
context "multi-threading" do context "multi-threading" do

View File

@ -233,27 +233,5 @@ module Rscons
expect {env.__send__(:find_finished_thread, false)}.to raise_error /No threads to wait for/ expect {env.__send__(:find_finished_thread, false)}.to raise_error /No threads to wait for/
end end
end end
describe "#features_met?" do
it "returns true when the builder provides all requested features" do
builder = Struct.new(:features).new(%w[shared other])
expect(subject.__send__(:features_met?, builder, %w[shared])).to be_truthy
end
it "returns true when no features are requested" do
builder = Struct.new(:features).new([])
expect(subject.__send__(:features_met?, builder, [])).to be_truthy
end
it "returns false when a builder does not provide a requested feature" do
builder = Struct.new(:features).new(%w[shared other])
expect(subject.__send__(:features_met?, builder, %w[other2])).to be_falsey
end
it "returns false when a builder provides a feature that is not desired" do
builder = Struct.new(:features).new(%w[shared other])
expect(subject.__send__(:features_met?, builder, %w[-shared])).to be_falsey
end
end
end end
end end