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
11
# File 'lib/ninja/file.rb', line 3

def initialize(path=nil, &block)
  @variables = []
  @rules = []
  @builds = []
  @subninjas = []
  @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



45
46
47
48
# File 'lib/ninja/file.rb', line 45

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

#build(rule, outputs_to_inputs = {}, variables = {}, implicit_inputs = []) ⇒ Object



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

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

#defaults(outputs) ⇒ Object



50
51
52
53
54
# File 'lib/ninja/file.rb', line 50

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



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ninja/file.rb', line 17

def rule(name, command, opts={})
  additional = {}

  if opts[:response_file]
    additional[:response_file] = Ninja::ResponseFile.new("$out.rsp", opts[:response_file])
  end

  @rules.push(Ninja::Rule.new(:name => name,
                              :command => command,
                              :dependencies => opts[:dependencies],
                              :description => opts[:description],
                              **additional))
end

#save(path) ⇒ Object



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ninja/file.rb', line 56

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"

    @subninjas.each do |subninja|
      f.write "subninja #{subninja}\n"
    end
    f.write "\n" unless @subninjas.empty?

    @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"
      if rule.response_file
        f.write "  rspfile = #{rule.response_file.name}\n"
        f.write "  rspfile_content = #{rule.response_file.contents}\n"
      end
      f.write "\n"
    end

    @builds.each do |build|
      f.write "build #{build.output}: #{build.rule} #{build.inputs.join(' ')} #{'| ' + build.implicit_inputs.join(' ') unless build.implicit_inputs.empty?}\n"
      build.variables.each do |variable|
        f.write "  #{variable.name} = #{variable.value}\n"
      end
    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

#subninja(filename) ⇒ Object



41
42
43
# File 'lib/ninja/file.rb', line 41

def subninja(filename)
  @subninjas.push(filename)
end

#variable(name, value) ⇒ Object



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

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