Class: Ninja::File

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, &block) ⇒ File

Returns a new instance of File.



3
4
5
6
7
8
9
10
# File 'lib/ninja/file.rb', line 3

def initialize(path=nil, &block)
  @variables = []
  @rules = []
  @builds = []
  @defaults = []
  Delegator.new(self, :except => [:save]).instance_eval(&block) if block_given?
  self.save(path) if path
end

Instance Method Details

#alias(from, to) ⇒ Object



28
29
30
31
# File 'lib/ninja/file.rb', line 28

def alias(from, to)
  # Pretty clever, huh?
  @builds.push(Ninja::Build.new(:rule => 'phony', :inputs => [*to], :output => from))
end

#build(rule, outputs_to_inputs = {}) ⇒ Object



22
23
24
25
26
# File 'lib/ninja/file.rb', line 22

def build(rule, outputs_to_inputs={})
  outputs_to_inputs.each do |output, inputs|
    @builds.push(Ninja::Build.new(:rule => rule, :inputs => [*inputs], :output => output))
  end
end

#defaults(outputs) ⇒ Object



33
34
35
36
37
# File 'lib/ninja/file.rb', line 33

def defaults(outputs)
  # TODO(mtwilliams): Accept variables (\$[\w]|\$\{[\w]\}).
  # raise "Expected output(s) to be paths." unless [*outputs].all?{|output| /\A(?:[-\w\.]+\/?)+\z/.match(output)}
  @defaults.push(*outputs)
end

#rule(name, command, opts = {}) ⇒ Object



16
17
18
19
20
# File 'lib/ninja/file.rb', line 16

def rule(name, command, opts={})
  @rules.push(Ninja::Rule.new(:name => name,
                              :command => command,
                              :dependencies => opts[:dependencies]))
end

#save(path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ninja/file.rb', line 39

def save(path)
  raise "Path not specified!" unless path
   # TODO(mtwilliams): Check if everything up to |path| exists.
  ::File.open(path, 'w') do |f|
    f.write "# This file was auto-generated by \"#{::File.basename($PROGRAM_NAME, ::File.extname($0))}\".\n"
    f.write "# Do not modify! Instead, modify the aforementioned program.\n\n"
    f.write "# We require Ninja >= 1.3 for `deps` and >= 1.5 for `msvc_deps_prefix`.\n"
    f.write "ninja_required_version = 1.5\n\n"

    @variables.each do |variable|
      # TODO(mtwilliams): Escape.
      f.write "#{variable.name} = #{variable.value}\n"
    end
    f.write "\n" unless @variables.empty?

    @rules.each do |rule|
      f.write "rule #{rule.name}\n"
      if rule.dependencies
        if (rule.dependencies == :gcc) or (rule.dependencies == :clang)
          f.write "  depfile = $out.d\n"
          f.write "  deps = gcc\n"
        elsif rule.dependencies == :msvc
          # TODO(mtwilliams): Handle non-English output.
          # f.write "  msvc_deps_prefix = Note: including file: \n"
          f.write "  deps = msvc\n"
        else
          f.write "  depfile = #{rule.dependencies}\n"
        end
      end
      f.write "  command = #{rule.command}\n\n"
    end

    @builds.each do |build|
      f.write "build #{build.output}: #{build.rule} #{build.inputs.join(' ')}\n"
    end

    unless @defaults.empty?
      f.write "\n" unless @builds.empty?
      f.write "default #{@defaults.join(' ')}\n" unless @defaults.empty?
    end

    # TODO(mtwilliams): Execute other files (via 'subninja').
    # TODO(mtwilliams): Specify pools, to optimize compilation times.
  end
end

#variable(name, value) ⇒ Object



12
13
14
# File 'lib/ninja/file.rb', line 12

def variable(name, value)
  @variables.push(Ninja::Variable.new(name, value))
end