This commit is contained in:
Josh Holtrop 2026-01-18 11:14:29 -05:00
parent 94a86e3433
commit 67c135f610
7 changed files with 173 additions and 10 deletions

View File

@ -33,6 +33,7 @@ module Rscons
:Lex, :Lex,
:Library, :Library,
:Object, :Object,
:Precompile,
:Preprocess, :Preprocess,
:Program, :Program,
:SharedLibrary, :SharedLibrary,
@ -152,6 +153,7 @@ require_relative "rscons/builders/disassemble"
require_relative "rscons/builders/lex" require_relative "rscons/builders/lex"
require_relative "rscons/builders/library" require_relative "rscons/builders/library"
require_relative "rscons/builders/object" require_relative "rscons/builders/object"
require_relative "rscons/builders/precompile"
require_relative "rscons/builders/preprocess" require_relative "rscons/builders/preprocess"
require_relative "rscons/builders/program" require_relative "rscons/builders/program"
require_relative "rscons/builders/shared_library" require_relative "rscons/builders/shared_library"

View File

@ -15,11 +15,16 @@ module Rscons
# List of paths to the object or static library dependencies. # List of paths to the object or static library dependencies.
def register_object_deps(builder_class) def register_object_deps(builder_class)
suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars) suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars)
barrier_target = @env.setup_precompile(@sources)
@sources.map do |source| @sources.map do |source|
if source.end_with?(*suffixes) if source.end_with?(*suffixes)
source source
else else
@env.register_dependency_build(@target, source, suffixes.first, @vars, builder_class) object_file_path = @env.register_dependency_build(@target, source, suffixes.first, @vars, builder_class)
if barrier_target
@env.depends(object_file_path, barrier_target)
end
object_file_path
end end
end end
end end

View File

@ -0,0 +1,29 @@
module Rscons
module Builders
# The Precompile builder generates .di interface files from .d source files
# for D.
class Precompile < Builder
# Run the builder to produce a build target.
def run(options)
if @command
finalize_command
else
if @sources.find {|s| s.end_with?(*@env.expand_varref("${DSUFFIX}", @vars))}
pcc = @env.expand_varref("${DC}")
if pcc =~ /ldc/
dpc_cmd = "${DPC_CMD:ldc}"
else
dpc_cmd = "${DPC_CMD:gdc}"
end
@vars["_TARGET"] = @target
@vars["_SOURCES"] = @sources
command = @env.build_command(dpc_cmd, @vars)
standard_command("Precompile <source>#{Util.short_format_paths(@sources)}<reset>", command)
end
end
end
end
end
end

View File

@ -43,6 +43,8 @@ module Rscons
"DFLAGS" => [], "DFLAGS" => [],
"DISASM_CMD" => %w[${OBJDUMP} ${DISASM_FLAGS} ${_SOURCES}], "DISASM_CMD" => %w[${OBJDUMP} ${DISASM_FLAGS} ${_SOURCES}],
"DISASM_FLAGS" => %w[--disassemble --source], "DISASM_FLAGS" => %w[--disassemble --source],
"DPC_CMD:ldc" => %w[${DC} -H -Hf ${_TARGET} -o- ${INCPREFIX}${D_IMPORT_PATH} ${DFLAGS} ${_SOURCES}],
"DPC_CMD:gdc" => %w[${DC} -H -Hf ${_TARGET} -fsyntax-only ${INCPREFIX}${D_IMPORT_PATH} ${DFLAGS} ${_SOURCES}],
"DSUFFIX" => %w[.d], "DSUFFIX" => %w[.d],
"D_IMPORT_PATH" => [], "D_IMPORT_PATH" => [],
"INCPREFIX" => "-I", "INCPREFIX" => "-I",

View File

@ -291,6 +291,17 @@ module Rscons
end end
end end
# Get a path under the build root for precompile outputs.
#
# @param end_path [String]
# Path to append to precompile build root.
#
# @return [String]
# Precompile directory name.
def get_pc_build_dir(end_path)
"#{@build_root}/pc/#{Util.make_relative_path(end_path)}"
end
# Build all build targets specified in the Environment. # Build all build targets specified in the Environment.
# #
# When a block is passed to Environment.new, this method is automatically # When a block is passed to Environment.new, this method is automatically
@ -600,6 +611,40 @@ module Rscons
@builder_sets << build_builder_set @builder_sets << build_builder_set
end end
# Set up builders and barrier for a precompile phase if needed.
#
# @param sources [Array<String>]
# Sources for the build operation.
#
# @return [Symbol, nil]
# Barrier target name if a precompile phase is needed, otherwise nil.
def setup_precompile(sources)
barrier_target = nil
precompile_paths = Set.new
sources.each do |source|
next unless source.end_with?(".d")
unless barrier_target
barrier_target = Rscons.gen_phony_target
self.Barrier(barrier_target)
end
next unless module_name = Util.get_module_name(source)
next unless import_path = Util.find_import_path_for_d_source(self["D_IMPORT_PATH"], source, module_name)
precompile_path = get_pc_build_dir(import_path)
unless precompile_paths.include?(precompile_path)
Util.clean_d_precompile_path(precompile_path, import_path)
precompile_paths << precompile_path
end
unless self["D_IMPORT_PATH"].include?(precompile_path)
# Put the precompile path containing the .di files for the given
# import path immediately before the import path.
index = self["D_IMPORT_PATH"].find_index {|ip| ip == import_path}
self["D_IMPORT_PATH"].insert(index, precompile_path)
end
self.Precompile("#{precompile_path}/#{module_name.gsub(".", "/")}.di", source)
end
barrier_target
end
private private
# Build a BuilderSet. # Build a BuilderSet.

View File

@ -34,15 +34,7 @@ module Rscons
# #
# @return [Array<String>] Paths matching the specified pattern(s). # @return [Array<String>] Paths matching the specified pattern(s).
def glob(*patterns) def glob(*patterns)
require "pathname" Util.glob(*patterns)
patterns.reduce([]) do |result, pattern|
if pattern.end_with?("/**")
pattern += "/"
end
result += Dir.glob(pattern).map do |path|
Pathname.new(path.gsub("\\", "/")).cleanpath.to_s
end
end.sort
end end
# Download a file. # Download a file.

View File

@ -16,6 +16,25 @@ module Rscons
end end
end end
# Remove any stale .di files from the precompile path.
#
# @param pc_path [String]
# Path to precompile build directory containing .di generated
# interface files.
# @param import_path [String]
# D import path containing .d source files.
#
# @return [void]
def clean_d_precompile_path(pc_path, import_path)
glob("#{pc_path}/**/*.di").each do |di_path|
end_path = di_path[(pc_path.size+1)..]
path = "#{import_path}/#{end_path}".sub(/\.di$/, ".d")
unless File.exist?(path)
FileUtils.rm_f(path)
end
end
end
# Colorize a builder run message. # Colorize a builder run message.
# #
# @param message [String] # @param message [String]
@ -120,6 +139,29 @@ module Rscons
end end
end end
# Find the D import path that will be used to import the given module.
#
# @param import_paths [Array<String>]
# Import paths.
# @param source [String]
# Source file name.
# @param module_name [String]
# Module name.
#
# @return [String, nil]
# Import path used to import the given module.
def find_import_path_for_d_source(import_paths, source, module_name)
source = source.gsub("\\", "/")
module_path = module_name.gsub(".", "/") + ".d"
import_paths.each do |import_path|
path = "#{import_path}/#{module_path}".gsub("\\", "/")
if path == source
return import_path
end
end
nil
end
# Format an elapsed time in human-readable format. # Format an elapsed time in human-readable format.
# #
# @return [String] # @return [String]
@ -141,6 +183,52 @@ module Rscons
result result
end end
# Get the module name for a D source file.
#
# @param source_path [String]
# D source file.
#
# @return [String, nil]
# Module name.
def get_module_name(source_path)
if File.exist?(source_path)
if File.binread(source_path) =~ /^\s*module\s(\S*);/
return $1
end
name = File.basename(source_path).sub(/\.d$/, "")
if name =~ /^\w+$/
return name
end
end
end
# Return a list of paths matching the specified pattern(s).
#
# A pattern can contain a "/**" component to recurse through directories.
# If the pattern ends with "/**" then only the recursive list of
# directories will be returned.
#
# Examples:
# - "src/**": return all directories under "src", recursively (including
# "src" itself).
# - "src/**/*": return all files and directories recursively under the src
# directory.
# - "src/**/*.c": return all .c files recursively under the src directory.
# - "dir/*/": return all directories in dir, but no files.
#
# @return [Array<String>] Paths matching the specified pattern(s).
def glob(*patterns)
require "pathname"
patterns.reduce([]) do |result, pattern|
if pattern.end_with?("/**")
pattern += "/"
end
result += Dir.glob(pattern).map do |path|
Pathname.new(path.gsub("\\", "/")).cleanpath.to_s
end
end.sort
end
# Make a relative path corresponding to a possibly absolute one. # Make a relative path corresponding to a possibly absolute one.
# #
# @param path [String] # @param path [String]