Class: MilkTea::ToolchainCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/tooling/toolchain_cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, out:, err:, help_printer:) ⇒ ToolchainCLI

Returns a new instance of ToolchainCLI.



9
10
11
12
13
14
15
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 9

def initialize(argv, out:, err:, help_printer:)
  @argv = argv.dup
  @out = out
  @err = err
  @help_printer = help_printer
  @quiet = false
end

Class Method Details

.start(argv = ARGV, out:, err:, help_printer:) ⇒ Object



5
6
7
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 5

def self.start(argv = ARGV, out:, err:, help_printer:)
  new(argv, out:, err:, help_printer:).start
end

Instance Method Details

#bootstrap_commandObject



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
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 68

def bootstrap_command
  if @argv.any?
    @err.puts("unknown toolchain option #{@argv.first}")
    print_help
    return 1
  end

  require_relative "../bindings"

  sources = UpstreamSources.default_sources(root: MilkTea.root)
  info "Bootstrapping #{sources.length} vendored libraries..."
  info ""

  ok = 0
  skipped = 0
  failed = 0

  sources.each do |source|
    if source.complete?
      info "  #{source.name} (already present)"
      skipped += 1
      next
    end

    info "  #{source.name} \e[2mcloning...\e[0m"
    @out.flush
    begin
      result = source.bootstrap!
      verb = result.status == :present ? "kept" : "bootstrapped"
      info "\r  #{source.name} \e[32m#{verb}\e[0m"
      ok += 1
    rescue UpstreamSources::Error => e
      info "\r  #{source.name} \e[31mfailed\e[0m"
      @err.puts("    #{e.message}")
      failed += 1
    end
  end

  info ""
  total = ok + skipped + failed
  info "#{total} source(s): #{ok} bootstrapped, #{skipped} skipped, #{failed} failed"
  failed.zero? ? 0 : 1
end

#doctor_commandObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 112

def doctor_command
  if @argv.any?
    @err.puts("unknown toolchain option #{@argv.first}")
    print_help
    return 1
  end

  require_relative "../bindings"

  cc = ENV.fetch("CC", "cc")
  ar = ENV.fetch("AR", "ar")
  checks = []

  info "Checking system tools..."
  checks << ["ruby", true, RUBY_DESCRIPTION]
  checks << ["cc", executable_available?(cc), cc]
  checks << ["ar", executable_available?(ar), ar]
  checks << ["bundle", executable_available?("bundle"), "bundle"]
  checks << ["git", executable_available?("git"), "git"]
  checks << ["clang", executable_available?(ENV.fetch("CLANG", "clang")), ENV.fetch("CLANG", "clang")]
  checks << ["cmake", executable_available?(ENV.fetch("CMAKE", "cmake")), ENV.fetch("CMAKE", "cmake")]
  checks << ["ninja", executable_available?("ninja"), "ninja"]

  info "Checking vendored libraries..."
  UpstreamSources.default_sources(root: MilkTea.root).each do |source|
    missing = source.sentinel_paths.reject do |relative_path|
      File.exist?(source.checkout_root.join(relative_path))
    end
    if missing.empty?
      checks << ["source/#{source.name}", true, source.checkout_root.to_s]
    else
      checks << ["source/#{source.name}", false, "missing #{missing.first} under #{source.checkout_root}"]
    end
  end

  info "Checking bindings..."
  RawBindings.default_registry(root: MilkTea.root)
    .select { |binding| binding.module_name.start_with?("std.c.") }
    .sort_by(&:name)
    .each do |binding|
    begin
      header_path = binding.header_path(env: ENV)
      checks << ["binding/#{binding.name}", true, "#{binding.module_name} -> #{header_path}"]
    rescue RawBindings::Error => header_error
      unless checks.assoc("cc")[1]
        checks << ["binding/#{binding.name}", false, "skipped (missing C compiler)"]
        next
      end

      unless binding.respond_to?(:prepare!)
        checks << ["binding/#{binding.name}", false, header_error.message]
        next
      end

      begin
        binding.prepare!(env: ENV, cc:)
        header_path = binding.header_path(env: ENV)
        checks << ["binding/#{binding.name}", true, "#{binding.module_name} -> #{header_path}"]
      rescue RawBindings::Error => e
        checks << ["binding/#{binding.name}", false, e.message]
      end
    end
  end

  info ""
  checks.each do |name, ok, detail|
    @out.puts("#{ok ? "ok" : "fail"} #{name}: #{detail}")
  end

  checks.all? { |_, ok, _| ok } ? 0 : 1
end

#executable_available?(program) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
210
211
212
213
214
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 207

def executable_available?(program)
  return File.file?(program) && File.executable?(program) if program.include?(File::SEPARATOR)

  ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).any? do |entry|
    candidate = File.join(entry, program)
    File.file?(candidate) && File.executable?(candidate)
  end
end

#extract_options!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 41

def extract_options!
  remaining = []
  until @argv.empty?
    arg = @argv.first
    case arg
    when "-q", "--quiet"
      @quiet = true
      @argv.shift
    when "--"
      @argv.shift
      remaining.concat(@argv)
      @argv.clear
    else
      remaining << @argv.shift
    end
  end
  @argv.replace(remaining)
end

#info(message) ⇒ Object



60
61
62
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 60

def info(message)
  @out.puts(message) unless @quiet
end


64
65
66
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 64

def print_help
  @help_printer.call(@err)
end

#startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 17

def start
  extract_options!

  subcommand = @argv.shift
  unless subcommand
    @err.puts("missing toolchain subcommand")
    print_help
    return 1
  end

  case subcommand
  when "bootstrap"
    bootstrap_command
  when "doctor"
    doctor_command
  when "tools"
    tools_command
  else
    @err.puts("unknown toolchain subcommand #{subcommand}")
    print_help
    1
  end
end

#tools_commandObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/milk_tea/tooling/toolchain_cli.rb', line 184

def tools_command
  if @argv.any?
    @err.puts("unknown toolchain option #{@argv.first}")
    print_help
    return 1
  end

  require_relative "../bindings"

  tools = VendoredTools.all(root: MilkTea.root)
  info "Building #{tools.length} vendored tool(s)..."
  info ""

  results = VendoredTools.build_all!(root: MilkTea.root)
  results.each do |result|
    info "  #{result[:tool].name} \e[32mbuilt\e[0m -> #{result[:binary]}"
  end
  0
rescue VendoredTool::Error => e
  @err.puts(e.message)
  1
end