Debian Codegraph @

vendor/mruby-source/lib/mruby/build.rb

raw
1 require "mruby/core_ext"
2 require "mruby/build/load_gems"
3 require "mruby/build/command"
4 autoload :Find, "find"
5
6 module MRuby
7 autoload :Gem, "mruby/gem"
8 autoload :Lockfile, "mruby/lockfile"
9 autoload :Presym, "mruby/presym"
10
11 INSTALL_PREFIX = ENV['PREFIX'] || ENV['INSTALL_PREFIX'] || '/usr/local'
12 INSTALL_DESTDIR = ENV['DESTDIR'] || ''
13
14 class << self
15 def targets
16 @targets ||= {}
17 end
18
19 def each_target(&block)
20 return to_enum(:each_target) if block.nil?
21 @targets.each do |key, target|
22 target.instance_eval(&block)
23 end
24 end
25 end
26
27 class Toolchain
28 class << self
29 attr_accessor :toolchains
30
31 def guess
32 if cc = ENV["CC"] || ENV["CXX"]
33 return "clang" if cc.include?("clang")
34 else
35 return "clang" if RUBY_PLATFORM =~ /darwin|(?:free|open)bsd/
36 return "gcc" if RUBY_PLATFORM.include?("cygwin")
37 return "visualcpp" if ENV.include?("VisualStudioVersion")
38 return "visualcpp" if ENV.include?("VSINSTALLDIR")
39 end
40 "gcc"
41 end
42 end
43
44 def initialize(name, &block)
45 @name, @initializer = name.to_s, block
46 MRuby::Toolchain.toolchains[@name] = self
47 end
48
49 def setup(conf, params={})
50 conf.instance_exec(conf, params, &@initializer)
51 end
52
53 self.toolchains = {}
54 end
55
56 class Build
57 class << self
58 attr_accessor :current
59
60 def mruby_config_path
61 path = ENV['MRUBY_CONFIG'] || ENV['CONFIG']
62 if path.nil? || path.empty?
63 path = "#{MRUBY_ROOT}/build_config/default.rb"
64 elsif !File.file?(path) && !Pathname.new(path).absolute?
65 f = "#{MRUBY_ROOT}/build_config/#{path}.rb"
66 path = File.exist?(f) ? f : File.extname(path).empty? ? f : path
67 end
68 path
69 end
70
71 def install_dir
72 @install_dir ||= ENV['INSTALL_DIR'] || "#{MRUBY_ROOT}/bin"
73 end
74 end
75
76 include Rake::DSL
77 include LoadGems
78 attr_accessor :name, :bins, :exts, :file_separator, :build_dir, :gem_clone_dir, :defines
79 attr_reader :products, :libmruby_core_objs, :libmruby_objs, :gems, :toolchains, :presym, :mrbc_build, :gem_dir_to_repo_url
80
81 alias libmruby libmruby_objs
82
83 COMPILERS = %w(cc cxx objc asm)
84 COMMANDS = COMPILERS + %w(linker archiver yacc gperf git exts mrbc)
85 attr_block MRuby::Build::COMMANDS
86
87 Exts = Struct.new(:object, :executable, :library, :presym_preprocessed)
88
89 def initialize(name='host', build_dir=nil, internal: false, &block)
90 @name = name.to_s
91
92 unless current = MRuby.targets[@name]
93 if ENV['OS'] == 'Windows_NT'
94 @exts = Exts.new('.o', '.exe', '.a', '.pi')
95 else
96 @exts = Exts.new('.o', '', '.a', '.pi')
97 end
98
99 build_dir = build_dir || ENV['MRUBY_BUILD_DIR'] || "#{MRUBY_ROOT}/build"
100
101 @file_separator = '/'
102 @build_dir = "#{build_dir}/#{@name}"
103 @gem_clone_dir = "#{build_dir}/repos/#{@name}"
104 @install_prefix = nil
105 @defines = []
106 @cc = Command::Compiler.new(self, %w(.c), label: "CC")
107 @cxx = Command::Compiler.new(self, %w(.cc .cxx .cpp), label: "CXX")
108 @objc = Command::Compiler.new(self, %w(.m), label: "OBJC")
109 @asm = Command::Compiler.new(self, %w(.S .asm .s), label: "ASM")
110 @linker = Command::Linker.new(self)
111 @archiver = Command::Archiver.new(self)
112 @yacc = Command::Yacc.new(self)
113 @gperf = Command::Gperf.new(self)
114 @git = Command::Git.new(self)
115 @mrbc = Command::Mrbc.new(self)
116
117 @products = []
118 @bins = []
119 @gems = MRuby::Gem::List.new
120 @libmruby_core_objs = []
121 @libmruby_objs = [@libmruby_core_objs]
122 @enable_libmruby = true
123 @build_mrbtest_lib_only = false
124 @cxx_exception_enabled = false
125 @cxx_exception_disabled = false
126 @cxx_abi_enabled = false
127 @enable_bintest = false
128 @enable_test = false
129 @enable_lock = true
130 @enable_presym = true
131 @enable_benchmark = true
132 @mrbcfile_external = false
133 @internal = internal
134 @toolchains = []
135 @gem_dir_to_repo_url = {}
136
137 MRuby.targets[@name] = current = self
138 end
139
140 MRuby::Build.current = current
141 begin
142 current.instance_eval(&block)
143 ensure
144 if current.libmruby_enabled? && !current.mrbcfile_external?
145 if current.presym_enabled?
146 current.create_mrbc_build if current.host? || current.gems["mruby-bin-mrbc"]
147 elsif current.host?
148 current.build_mrbc_exec
149 end
150 end
151 current.presym = Presym.new(current) if current.presym_enabled?
152 end
153 end
154
155 def libmruby_enabled?
156 @enable_libmruby
157 end
158
159 def disable_libmruby
160 @enable_libmruby = false
161 end
162
163 def debug_enabled?
164 @enable_debug
165 end
166
167 def enable_debug
168 compilers.each do |c|
169 c.defines += %w(MRB_DEBUG)
170 c.setup_debug(self)
171 end
172 @mrbc.compile_options += ' -g'
173
174 @enable_debug = true
175 end
176
177 def presym_enabled?
178 @enable_presym
179 end
180
181 def disable_presym
182 if @enable_presym
183 @enable_presym = false
184 compilers.each{|c| c.defines << "MRB_NO_PRESYM"}
185 end
186 end
187
188 def disable_lock
189 @enable_lock = false
190 end
191
192 def lock_enabled?
193 Lockfile.enabled? && @enable_lock
194 end
195
196 def disable_cxx_exception
197 if @cxx_exception_enabled or @cxx_abi_enabled
198 raise "cxx_exception already enabled"
199 end
200 @cxx_exception_disabled = true
201 end
202
203 def enable_cxx_exception
204 return if @cxx_exception_enabled
205 return if @cxx_abi_enabled
206 if @cxx_exception_disabled
207 raise "cxx_exception disabled"
208 end
209 @cxx_exception_enabled = true
210 compilers.each { |c|
211 c.defines += %w(MRB_USE_CXX_EXCEPTION)
212 c.flags << c.cxx_exception_flag
213 }
214 linker.command = cxx.command if toolchains.find { |v| v == 'gcc' }
215 end
216
217 def cxx_exception_enabled?
218 @cxx_exception_enabled
219 end
220
221 def cxx_abi_enabled?
222 @cxx_abi_enabled
223 end
224
225 def enable_cxx_abi
226 return if @cxx_abi_enabled
227 if @cxx_exception_enabled
228 raise "cxx_exception already enabled"
229 end
230 compilers.each { |c|
231 c.defines += %w(MRB_USE_CXX_EXCEPTION MRB_USE_CXX_ABI)
232 c.flags << c.cxx_compile_flag
233 c.flags = c.flags.flatten - c.cxx_invalid_flags.flatten
234 }
235 linker.command = cxx.command if toolchains.find { |v| v == 'gcc' }
236 @cxx_abi_enabled = true
237 end
238
239 def benchmark_enabled?
240 @enable_benchmark
241 end
242
243 def disable_benchmark
244 @enable_benchmark = false
245 end
246
247 def compile_as_cxx(src, cxx_src = nil, obj = nil, includes = [])
248 #
249 # If `cxx_src` is specified, this method behaves the same as before as
250 # compatibility mode, but `.d` file is not read.
251 #
252 # If `cxx_src` is omitted, `.d` file is read by using mruby standard
253 # Rake rule (C++ source name is also changed).
254 #
255 if cxx_src
256 obj ||= cxx_src + @exts.object
257 dsts = [obj]
258 dsts << (cxx_src + @exts.presym_preprocessed) if presym_enabled?
259 defines = []
260 include_paths = ["#{MRUBY_ROOT}/src", *includes]
261 dsts.each do |dst|
262 file dst => cxx_src do |t|
263 cxx.run t.name, t.prerequisites.first, defines, include_paths
264 end
265 end
266 else
267 cxx_src = "#{build_dir}/#{src.relative_path.to_s.remove_leading_parents}".ext << "-cxx.cxx"
268 obj = cxx_src.ext(@exts.object)
269 end
270
271 file cxx_src => [src, __FILE__] do |t|
272 mkdir_p File.dirname t.name
273 IO.write t.name, <<EOS
274 #define __STDC_CONSTANT_MACROS
275 #define __STDC_LIMIT_MACROS
276
277 #ifndef MRB_USE_CXX_ABI
278 extern "C" {
279 #endif
280 #include "#{File.absolute_path src}"
281 #ifndef MRB_USE_CXX_ABI
282 }
283 #endif
284 EOS
285 end
286
287 obj
288 end
289
290 def enable_bintest
291 @enable_bintest = true
292 end
293
294 def bintest_enabled?
295 @enable_bintest
296 end
297
298 def toolchain(name=Toolchain.guess, params={})
299 name = name.to_s
300 tc = Toolchain.toolchains[name] || begin
301 path = "#{MRUBY_ROOT}/tasks/toolchains/#{name}.rake"
302 fail "Unknown #{name} toolchain" unless File.exist?(path)
303 load path
304 Toolchain.toolchains[name]
305 end
306 tc.setup(self, params)
307 @toolchains.unshift name
308 end
309
310 def primary_toolchain
311 @toolchains.first
312 end
313
314 def root
315 MRUBY_ROOT
316 end
317
318 def enable_test
319 @enable_test = true
320 end
321 alias build_mrbtest enable_test
322
323 def test_enabled?
324 @enable_test
325 end
326
327 def build_mrbc_exec
328 gem :core => 'mruby-bin-mrbc' unless @gems['mruby-bin-mrbc']
329 end
330
331 def locks
332 Lockfile.build(@name)
333 end
334
335 def mrbcfile
336 return @mrbcfile if @mrbcfile
337
338 gem_name = "mruby-bin-mrbc"
339 if (gem = @gems[gem_name])
340 @mrbcfile = exefile("#{gem.build.build_dir}/bin/mrbc")
341 elsif !host? && (host = MRuby.targets["host"])
342 if (gem = host.gems[gem_name])
343 @mrbcfile = exefile("#{gem.build.build_dir}/bin/mrbc")
344 elsif host.mrbcfile_external?
345 @mrbcfile = host.mrbcfile
346 end
347 end
348 @mrbcfile || fail("external mrbc or mruby-bin-mrbc gem in current('#{@name}') or 'host' build is required")
349 end
350
351 def mrbcfile=(path)
352 @mrbcfile = path
353 @mrbcfile_external = true
354 end
355
356 def mrbcfile_external?
357 @mrbcfile_external
358 end
359
360 def compilers
361 COMPILERS.map do |c|
362 instance_variable_get("@#{c}")
363 end
364 end
365
366 def define_rules
367 compilers.each do |compiler|
368 compiler.defines << "MRB_NO_GEMS" unless enable_gems? && libmruby_enabled?
369 end
370 [@cc, *(@cxx if cxx_exception_enabled?)].each do |compiler|
371 compiler.define_rules(@build_dir, MRUBY_ROOT, @exts.object)
372 compiler.define_rules(@build_dir, MRUBY_ROOT, @exts.presym_preprocessed) if presym_enabled?
373 end
374 end
375
376 def define_installer_outline(src, dst)
377 file dst => src do
378 _pp "GEN", src.relative_path, dst.relative_path
379 mkdir_p(File.dirname(dst))
380 yield dst
381 end
382 dst
383 end
384
385 if ENV['OS'] == 'Windows_NT'
386 def define_installer(src)
387 dst = "#{self.class.install_dir}/#{File.basename(src)}".pathmap("%X.bat")
388 define_installer_outline(src, dst) do
389 File.write dst, <<~BATCHFILE
390 @echo off
391 call "#{File.expand_path(src)}" %*
392 BATCHFILE
393 end
394 end
395 else
396 def define_installer(src)
397 dst = "#{self.class.install_dir}/#{File.basename(src)}"
398 define_installer_outline(src, dst) do
399 File.unlink(dst) rescue nil
400 File.symlink(File.expand_path(src), dst)
401 end
402 end
403 end
404
405 def define_installer_if_needed(bin)
406 exe = exefile("#{build_dir}/bin/#{bin}")
407 host? ? define_installer(exe) : exe
408 end
409
410 def filename(name)
411 if name.is_a?(Array)
412 name.flatten.map { |n| filename(n) }
413 else
414 name.gsub('/', file_separator)
415 end
416 end
417
418 def exefile(name)
419 if name.is_a?(Array)
420 name.flatten.map { |n| exefile(n) }
421 elsif File.extname(name).empty?
422 "#{name}#{exts.executable}"
423 else
424 # `name` sometimes have (non-standard) extension (e.g. `.bat`).
425 name
426 end
427 end
428
429 def objfile(name)
430 if name.is_a?(Array)
431 name.flatten.map { |n| objfile(n) }
432 else
433 "#{name}#{exts.object}"
434 end
435 end
436
437 def libfile(name)
438 if name.is_a?(Array)
439 name.flatten.map { |n| libfile(n) }
440 else
441 "#{name}#{exts.library}"
442 end
443 end
444
445 def build_mrbtest_lib_only
446 @build_mrbtest_lib_only = true
447 end
448
449 def build_mrbtest_lib_only?
450 @build_mrbtest_lib_only
451 end
452
453 def verbose_flag
454 Rake.verbose ? ' -v' : ''
455 end
456
457 def run_test
458 puts ">>> Test #{name} <<<"
459 mrbtest = exefile("#{build_dir}/bin/mrbtest")
460 sh "#{filename mrbtest.relative_path}#{verbose_flag}"
461 puts
462 end
463
464 def run_bintest
465 puts ">>> Bintest #{name} <<<"
466 targets = @gems.select { |v| File.directory? "#{v.dir}/bintest" }.map { |v| filename v.dir }
467 mrbc = @gems["mruby-bin-mrbc"] ? exefile("#{@build_dir}/bin/mrbc") : mrbcfile
468 env = {"BUILD_DIR" => @build_dir, "MRBCFILE" => mrbc}
469 bintest = File.join(MRUBY_ROOT, "test/bintest.rb")
470 sh env, "ruby #{bintest}#{verbose_flag} #{targets.join ' '}"
471 end
472
473 def print_build_summary
474 puts "================================================"
475 puts " Config Name: #{@name}"
476 puts " Output Directory: #{self.build_dir.relative_path}"
477 puts " Binaries: #{@bins.join(', ')}" unless @bins.empty?
478 unless @gems.empty?
479 puts " Included Gems:"
480 gems = @gems.sort_by { |gem| gem.name }
481 gems.each do |gem|
482 gem_version = " - #{gem.version}" if gem.version != '0.0.0'
483 gem_summary = " - #{gem.summary}" if gem.summary
484 puts " #{gem.name}#{gem_version}#{gem_summary}"
485 puts " - Binaries: #{gem.bins.join(', ')}" unless gem.bins.empty?
486 end
487 end
488 puts "================================================"
489 puts
490 end
491
492 def libmruby_static
493 libfile("#{build_dir}/lib/libmruby")
494 end
495
496 def libmruby_core_static
497 libfile("#{build_dir}/lib/libmruby_core")
498 end
499
500 def libraries
501 [libmruby_static]
502 end
503
504 def host?
505 @name == "host"
506 end
507
508 def internal?
509 @internal
510 end
511
512 def each_header_files(&block)
513 return to_enum(__method__) unless block
514
515 basedir = File.join(MRUBY_ROOT, "include")
516 Find.find(basedir) do |d|
517 next unless File.file? d
518 yield d
519 end
520
521 @gems.each { |g| g.each_header_files(&block) }
522
523 self
524 end
525
526 def install_prefix
527 @install_prefix || (self.name == "host" ? MRuby::INSTALL_PREFIX :
528 File.join(MRuby::INSTALL_PREFIX, "mruby/#{self.name}"))
529 end
530
531 def install_prefix=(dir)
532 @install_prefix = dir&.to_s
533 end
534
535 protected
536
537 attr_writer :presym
538
539 def create_mrbc_build
540 exclusions = %i[@name @build_dir @gems @enable_test @enable_bintest @internal]
541 name = "#{@name}/mrbc"
542 MRuby.targets.delete(name)
543 build = self.class.new(name, internal: true){}
544 build.build_dir = "#{@build_dir}/mrbc"
545 instance_variables.each do |n|
546 next if exclusions.include?(n)
547 v = instance_variable_get(n)
548 v = case v
549 when nil, true, false, Numeric; v
550 when String; v.clone
551 when Command; v.clone.tap { |u| u.build = build }
552 else Marshal.load(Marshal.dump(v)) # deep clone
553 end
554 build.instance_variable_set(n, v)
555 end
556 build.build_mrbc_exec
557 build.disable_libmruby
558 build.disable_presym
559 @mrbc_build = build
560 self.mrbcfile = build.mrbcfile
561 build
562 end
563 end # Build
564
565 class CrossBuild < Build
566 attr_block %w(test_runner)
567 # cross compiling targets for building native extensions.
568 # host - arch of where the built binary will run
569 # build - arch of the machine building the binary
570 attr_accessor :host_target, :build_target
571
572 def initialize(name, build_dir=nil, &block)
573 @test_runner = Command::CrossTestRunner.new(self)
574 super
575 unless mrbcfile_external? || MRuby.targets['host']
576 # add minimal 'host'
577 MRuby::Build.new('host') do |conf|
578 conf.toolchain
579 conf.build_mrbc_exec
580 conf.disable_libmruby
581 conf.disable_presym
582 end
583 end
584 end
585
586 def mrbcfile
587 mrbcfile_external? ? super : MRuby::targets['host'].mrbcfile
588 end
589
590 def run_test
591 @test_runner.runner_options << verbose_flag
592 mrbtest = exefile("#{build_dir}/bin/mrbtest")
593 if (@test_runner.command == nil)
594 puts "You should run #{mrbtest} on target device."
595 puts
596 else
597 @test_runner.run(mrbtest)
598 end
599 end
600
601 def run_bintest
602 puts ">>> Bintest #{name} <<<"
603 targets = @gems.select { |v| File.directory? "#{v.dir}/bintest" }.map { |v| filename v.dir }
604 mrbc = @gems["mruby-bin-mrbc"] ? exefile("#{@build_dir}/bin/mrbc") : mrbcfile
605
606 emulator = @test_runner.command
607 emulator = @test_runner.shellquote(emulator) if emulator
608
609 env = {
610 "BUILD_DIR" => @build_dir,
611 "MRBCFILE" => mrbc,
612 "EMULATOR" => @test_runner.emulator,
613 }
614 bintest = File.join(MRUBY_ROOT, "test/bintest.rb")
615 sh env, "ruby #{bintest}#{verbose_flag} #{targets.join ' '}"
616 end
617
618 protected
619
620 def create_mrbc_build; end
621 end # CrossBuild
622 end # MRuby