diff --git a/lib/rscons.rb b/lib/rscons.rb index e9cbbf6..7b8afd7 100644 --- a/lib/rscons.rb +++ b/lib/rscons.rb @@ -33,6 +33,7 @@ module Rscons :Lex, :Library, :Object, + :Precompile, :Preprocess, :Program, :SharedLibrary, @@ -152,6 +153,7 @@ require_relative "rscons/builders/disassemble" require_relative "rscons/builders/lex" require_relative "rscons/builders/library" require_relative "rscons/builders/object" +require_relative "rscons/builders/precompile" require_relative "rscons/builders/preprocess" require_relative "rscons/builders/program" require_relative "rscons/builders/shared_library" diff --git a/lib/rscons/builders/mixins/object_deps.rb b/lib/rscons/builders/mixins/object_deps.rb index d83a5cc..a96b99b 100644 --- a/lib/rscons/builders/mixins/object_deps.rb +++ b/lib/rscons/builders/mixins/object_deps.rb @@ -15,11 +15,16 @@ module Rscons # List of paths to the object or static library dependencies. def register_object_deps(builder_class) suffixes = @env.expand_varref(["${OBJSUFFIX}", "${LIBSUFFIX}"], @vars) + barrier_target = @env.setup_precompile(@sources) @sources.map do |source| if source.end_with?(*suffixes) source 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 diff --git a/lib/rscons/builders/precompile.rb b/lib/rscons/builders/precompile.rb new file mode 100644 index 0000000..d11fe51 --- /dev/null +++ b/lib/rscons/builders/precompile.rb @@ -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 #{Util.short_format_paths(@sources)}", command) + end + end + end + + end + end +end diff --git a/lib/rscons/default_construction_variables.rb b/lib/rscons/default_construction_variables.rb index aa8aa7a..e148df4 100644 --- a/lib/rscons/default_construction_variables.rb +++ b/lib/rscons/default_construction_variables.rb @@ -43,6 +43,8 @@ module Rscons "DFLAGS" => [], "DISASM_CMD" => %w[${OBJDUMP} ${DISASM_FLAGS} ${_SOURCES}], "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], "D_IMPORT_PATH" => [], "INCPREFIX" => "-I", diff --git a/lib/rscons/environment.rb b/lib/rscons/environment.rb index ebaaba6..13f73f2 100644 --- a/lib/rscons/environment.rb +++ b/lib/rscons/environment.rb @@ -291,6 +291,17 @@ module Rscons 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. # # When a block is passed to Environment.new, this method is automatically @@ -600,6 +611,40 @@ module Rscons @builder_sets << build_builder_set end + # Set up builders and barrier for a precompile phase if needed. + # + # @param sources [Array] + # 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 # Build a BuilderSet. diff --git a/lib/rscons/script.rb b/lib/rscons/script.rb index 55ee043..a48752d 100644 --- a/lib/rscons/script.rb +++ b/lib/rscons/script.rb @@ -34,15 +34,7 @@ module Rscons # # @return [Array] 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 + Util.glob(*patterns) end # Download a file. diff --git a/lib/rscons/util.rb b/lib/rscons/util.rb index c199e1c..8bd41fb 100644 --- a/lib/rscons/util.rb +++ b/lib/rscons/util.rb @@ -16,6 +16,25 @@ module Rscons 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. # # @param message [String] @@ -120,6 +139,29 @@ module Rscons end end + # Find the D import path that will be used to import the given module. + # + # @param import_paths [Array] + # 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. # # @return [String] @@ -141,6 +183,52 @@ module Rscons result 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] 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. # # @param path [String]