expose depfile, add find

This commit is contained in:
kp2pml30 2024-09-09 20:31:07 +04:00
parent 394c5f430b
commit f995d8ae55

View file

@ -72,9 +72,10 @@ def escape_args_to(buf, args)
end end
class Target class Target
attr_reader :trg_name, :output_file attr_reader :trg_name, :meta
def initialize(trg_name, dependencies) def initialize(trg_name, dependencies)
@meta = OpenStruct.new
if dependencies.nil? if dependencies.nil?
raise "dependencies can't be nil" raise "dependencies can't be nil"
end end
@ -82,7 +83,7 @@ class Target
if @trg_name.kind_of?(Pathname) if @trg_name.kind_of?(Pathname)
@trg_name = @trg_name.to_s @trg_name = @trg_name.to_s
end end
raise "target name is not a string #{@trg_name}" if not @trg_name.kind_of?(String) raise "target name is not a string (got `#{@trg_name}`)" if not @trg_name.kind_of?(String)
@dependencies = dependencies @dependencies = dependencies
end end
@ -124,8 +125,9 @@ end
class CommandTarget < Target class CommandTarget < Target
attr_reader :output_file attr_reader :output_file
def initialize(output_file, dependencies, cwd, commands) def initialize(output_file, dependencies, cwd, commands, depfile)
super(output_file, dependencies) super(output_file, dependencies)
@depfile = depfile
@output_file = output_file @output_file = output_file
@cwd = cwd @cwd = cwd
@commands = commands @commands = commands
@ -135,6 +137,9 @@ class CommandTarget < Target
if not @cwd.nil? if not @cwd.nil?
buf << " WD = #{Shellwords.escape @cwd}\n" buf << " WD = #{Shellwords.escape @cwd}\n"
end end
if not @depfile.nil?
buf << " depfile = #{@depfile}\n"
end
buf << " COMMAND =" buf << " COMMAND ="
@commands.each_with_index { |c, i| @commands.each_with_index { |c, i|
if i != 0 if i != 0
@ -151,6 +156,7 @@ class CommandTarget < Target
end end
class CTarget < Target class CTarget < Target
attr_reader :output_file
def initialize(output_file, mode, dependencies, flags, cc) def initialize(output_file, mode, dependencies, flags, cc)
super(output_file, dependencies) super(output_file, dependencies)
@mode = mode @mode = mode
@ -260,9 +266,9 @@ class Configurator
def include_dir(path) def include_dir(path)
new_stack = @stack[-1].clone new_stack = @stack[-1].clone
new_stack.path = new_stack.path.join(path) new_stack.path = new_stack.path.join(path)
root_build.join(new_stack.path).mkpath
@stack.push(new_stack) @stack.push(new_stack)
begin begin
cur_build.mkpath()
run_last_stack() run_last_stack()
ensure ensure
@stack.pop @stack.pop
@ -364,7 +370,20 @@ EOF
File.write(root_build.join('build.ninja'), build_str) File.write(root_build.join('build.ninja'), build_str)
end end
def find_target(name)
suitable = @targets.filter { |trg|
trg.trg_name.match?(name)
}
raise "couldn't find target by name #{name} ; suitable: #{suitable}" if suitable.size() != 1
suitable[0]
end
private def register_target(trg)
@targets.push(trg)
end
private def return_target(trg, &block) private def return_target(trg, &block)
register_target(trg)
if not block.nil? if not block.nil?
trg.instance_eval(&block) trg.instance_eval(&block)
end end
@ -395,11 +414,13 @@ EOF
end end
def target_command( def target_command(
output_file: nil, output_file:,
dependencies: nil, dependencies:,
cwd: nil, cwd: nil,
command: nil, command: nil,
commands: nil commands: nil,
depfile: nil,
&blk
) )
if commands.nil? == command.nil? if commands.nil? == command.nil?
raise "exectly one of command or commands must be specified" raise "exectly one of command or commands must be specified"
@ -412,15 +433,11 @@ EOF
cwd = cur_src cwd = cur_src
end end
trg = CommandTarget.new(output_file, dependencies, cwd, commands) trg = CommandTarget.new(output_file, dependencies, cwd, commands, depfile)
@targets.push(trg) return_target(trg, &blk)
trg
end end
def target_c(output_file: nil, mode: nil, file: nil, objs: nil, flags: nil, cc: nil) def target_c(output_file:, mode:, file: nil, objs: nil, flags: nil, cc: nil, &blk)
if output_file.nil? or mode.nil?
raise "all of output_file and mode must be provided"
end
if (mode == "compile") == file.nil? if (mode == "compile") == file.nil?
raise "file must be provided only for compile" raise "file must be provided only for compile"
end end
@ -429,19 +446,17 @@ EOF
end end
deps = if objs.nil? then [file] else objs end deps = if objs.nil? then [file] else objs end
trg = CTarget.new(output_file, mode, deps, flags, cc) trg = CTarget.new(output_file, mode, deps, flags, cc)
@targets.push(trg) return_target(trg, &blk)
trg
end end
def target_alias(name, *dependencies) def target_alias(name, *dependencies, &blk)
name_full = @stack[-1].project name_full = @stack[-1].project
if name_full != "" if name_full != ""
name_full += "/" name_full += "/"
end end
name_full += name name_full += name
trg = AliasTarget.new(name_full, dependencies) trg = AliasTarget.new(name_full, dependencies)
@targets.push(trg) return_target(trg, &blk)
trg
end end
end end