Program builder cleanup, track object file source language - #94, #87

This commit is contained in:
Josh Holtrop 2019-04-09 19:41:13 -04:00
parent a655d04541
commit 3b00016278
8 changed files with 81 additions and 59 deletions

View File

@ -169,6 +169,7 @@ end
require_relative "rscons/builders/mixins/depfile"
require_relative "rscons/builders/mixins/object"
require_relative "rscons/builders/mixins/object_deps"
require_relative "rscons/builders/mixins/program"
# default builders
require_relative "rscons/builders/cfile"

View File

@ -1,2 +1,2 @@
Rscons::Builders::Object.register(command: "${CXXCMD}", suffix: "${CXXSUFFIX}")
Rscons::Builders::SharedObject.register(command: "${SHCXXCMD}", suffix: "${CXXSUFFIX}")
Rscons::Builders::Object.register(command: "${CXXCMD}", suffix: "${CXXSUFFIX}", preferred_ld: "${CXX}")
Rscons::Builders::SharedObject.register(command: "${SHCXXCMD}", suffix: "${CXXSUFFIX}", preferred_ld: "${SHCXX}")

View File

@ -1,2 +1,2 @@
Rscons::Builders::Object.register(command: "${DCCMD}", suffix: "${DSUFFIX}")
Rscons::Builders::SharedObject.register(command: "${SHDCCMD}", suffix: "${DSUFFIX}")
Rscons::Builders::Object.register(command: "${DCCMD}", suffix: "${DSUFFIX}", preferred_ld: "${DC}")
Rscons::Builders::SharedObject.register(command: "${SHDCCMD}", suffix: "${DSUFFIX}", preferred_ld: "${SHDC}")

View File

@ -1,4 +1,11 @@
module Rscons
class Builder
# @return [String, nil]
# Preferred linker for this object file, or a construction variable
# reference thereto.
attr_reader :preferred_ld
end
module Builders
module Mixins
module Object
@ -31,6 +38,9 @@ module Rscons
# @option params [String] :short_description
# Short description to be printed when the builder runs (default:
# "Compiling")
# @option params [String] :preferred_ld
# Preferred linker for this object file, or a construction variable
# reference thereto.
def register(params)
providers << params
end
@ -47,6 +57,7 @@ module Rscons
end
@command_template = build_params[:command]
@short_description = build_params[:short_description] || "Compiling"
@preferred_ld = build_params[:preferred_ld]
end
# Run the builder to produce a build target.

View File

@ -0,0 +1,37 @@
module Rscons
module Builders
module Mixins
# Mixin providing functionality for a builder that links object files
# together into a program.
module Program
# Run the builder to produce a build target.
def run(options)
if @command
finalize_command(sources: @objects)
else
ld = @env.expand_varref("${#{ld_var}}", @vars)
if ld == ""
@objects.find do |object|
if builder = @env.builder_for(object)
if ld = builder.preferred_ld
true
end
end
end
end
if ld.nil? || ld == ""
ld = default_ld
end
@vars["_TARGET"] = @target
@vars["_SOURCES"] = @objects
@vars["#{ld_var}"] = ld
command = @env.build_command("${#{ld_var}CMD}", @vars)
standard_command("Linking => #{@target}", command, sources: @objects)
end
end
end
end
end
end

View File

@ -5,19 +5,9 @@ module Rscons
class Program < Builder
include Mixins::ObjectDeps
include Mixins::Program
# Create an instance of the Builder to build a target.
#
# @param options [Hash]
# Options.
# @option options [String] :target
# Target file name.
# @option options [Array<String>] :sources
# Source file name(s).
# @option options [Environment] :env
# The Environment executing the builder.
# @option options [Hash,VarSet] :vars
# Extra construction variables.
def initialize(options)
super(options)
unless File.basename(@target)["."]
@ -26,28 +16,14 @@ module Rscons
@objects = register_object_deps(Object)
end
# Run the builder to produce a build target.
def run(options)
if @command
finalize_command(sources: @objects)
true
else
ld = @env.expand_varref("${LD}", @vars)
ld = if ld != ""
ld
elsif @sources.find {|s| s.end_with?(*@env.expand_varref("${DSUFFIX}", @vars))}
"${DC}"
elsif @sources.find {|s| s.end_with?(*@env.expand_varref("${CXXSUFFIX}", @vars))}
"${CXX}"
else
private
def default_ld
"${CC}"
end
@vars["_TARGET"] = @target
@vars["_SOURCES"] = @objects
@vars["LD"] = ld
command = @env.build_command("${LDCMD}", @vars)
standard_command("Linking => #{@target}", command, sources: @objects)
end
def ld_var
"LD"
end
end

View File

@ -5,6 +5,7 @@ module Rscons
class SharedLibrary < Builder
include Mixins::ObjectDeps
include Mixins::Program
# Create an instance of the Builder to build a target.
def initialize(options)
@ -19,28 +20,14 @@ module Rscons
@objects = register_object_deps(SharedObject)
end
# Run the builder to produce a build target.
def run(options)
if @command
finalize_command(sources: @objects)
true
else
ld = @env.expand_varref("${SHLD}", @vars)
ld = if ld != ""
ld
elsif @sources.find {|s| s.end_with?(*@env.expand_varref("${DSUFFIX}", @vars))}
"${SHDC}"
elsif @sources.find {|s| s.end_with?(*@env.expand_varref("${CXXSUFFIX}", @vars))}
"${SHCXX}"
else
private
def default_ld
"${SHCC}"
end
@vars["_TARGET"] = @target
@vars["_SOURCES"] = @objects
@vars["SHLD"] = ld
command = @env.build_command("${SHLDCMD}", @vars)
standard_command("Linking => #{@target}", command, sources: @objects)
end
def ld_var
"SHLD"
end
end

View File

@ -71,6 +71,7 @@ module Rscons
@registered_build_dependencies = {}
@side_effects = {}
@builder_set = BuilderSet.new(@registered_build_dependencies, @side_effects)
@build_targets = {}
@user_deps = {}
# Hash of builder name (String) => builder class (Class).
@builders = {}
@ -318,6 +319,7 @@ module Rscons
env: self,
vars: vars)
@builder_set << builder
@build_targets[target] = builder
builder
else
super
@ -517,6 +519,14 @@ module Rscons
Ansi.write($stdout, :cyan, message, :reset, "\n") if message
end
# Get the Builder for a target.
#
# @return [Builder, nil]
# The {Builder} for target, or +nil+ if none found.
def builder_for(target)
@build_targets[target]
end
private
# Run a builder and process its return value.