Class: MRubyTools

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

Defined Under Namespace

Modules: C, CLI Classes: MRubyNotFound

Constant Summary collapse

MRUBY_DIR =
File.expand_path("../mruby", __dir__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mruby_dir = nil) ⇒ MRubyTools

Returns a new instance of MRubyTools.



14
15
16
17
18
19
# File 'lib/mruby_tools.rb', line 14

def initialize(mruby_dir = nil)
  @mruby_dir = mruby_dir || self.class.mruby_dir
  @inc_path = File.join(@mruby_dir, 'include')
  @ar_path = File.join(@mruby_dir, 'build', 'host', 'lib', 'libmruby.a')
  @bin_path = File.join(@mruby_dir, 'bin')
end

Instance Attribute Details

#ar_pathObject

Returns the value of attribute ar_path.



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

def ar_path
  @ar_path
end

#bin_pathObject

Returns the value of attribute bin_path.



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

def bin_path
  @bin_path
end

#inc_pathObject

Returns the value of attribute inc_path.



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

def inc_path
  @inc_path
end

#mruby_dirObject

Returns the value of attribute mruby_dir.



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

def mruby_dir
  @mruby_dir
end

Class Method Details

.mruby_dirObject



8
9
10
# File 'lib/mruby_tools.rb', line 8

def self.mruby_dir
  ENV['MRUBY_DIR'] || MRUBY_DIR
end

Instance Method Details

#built?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/mruby_tools.rb', line 25

def built?
  File.readable?(@ar_path)
end

#compile(c_file, out_file) ⇒ Object



40
41
42
# File 'lib/mruby_tools.rb', line 40

def compile(c_file, out_file)
  system('gcc', *self.gcc_args(c_file, out_file))
end

#gcc_args(c_file, out_file) ⇒ Object



36
37
38
# File 'lib/mruby_tools.rb', line 36

def gcc_args(c_file, out_file)
  ['-std=c99', "-I", @inc_path, c_file, "-o", out_file, @ar_path, '-lm']
end

#mrbc(rb_files, out_file) ⇒ Object

interpret several rb_files into a binary out_file contaning bytecode



45
46
47
# File 'lib/mruby_tools.rb', line 45

def mrbc(rb_files, out_file)
  system(File.join(@bin_path, 'mrbc'), '-o', out_file, *rb_files)
end

#src?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/mruby_tools.rb', line 21

def src?
  File.directory?(@inc_path)
end

#validate!Object

Raises:



29
30
31
32
33
34
# File 'lib/mruby_tools.rb', line 29

def validate!
  raise(MRubyNotFound, @inc_path) unless File.directory? @inc_path
  raise(MRubyNotFound, @ar_path) unless File.readable? @ar_path
  raise(MRubyNotFound, @bin_path) unless File.directory? @bin_path
  self
end