Class: MRuby::Build

Inherits:
#Object show all
Includes:
LoadGems, Rake::DSL
Defined in:
ext/enterprise_script_service/mruby/lib/mruby/build.rb

Direct Known Subclasses

CrossBuild

Defined Under Namespace

Classes: Exts

Constant Summary collapse

COMPILERS =
%w(cc cxx objc asm)
COMMANDS =
COMPILERS + %w(linker archiver yacc gperf git exts mrbc)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoadGems

#enable_gems?, #gem, #gembox, #load_special_path_gem

Constructor Details

#initialize(name = 'host', build_dir = nil, &block) ⇒ Build



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
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 57

def initialize(name='host', build_dir=nil, &block)
  @name = name.to_s

  unless MRuby.targets[@name]
    if ENV['OS'] == 'Windows_NT'
      @exts = Exts.new('.o', '.exe', '.a')
    else
      @exts = Exts.new('.o', '', '.a')
    end

    build_dir = build_dir || ENV['MRUBY_BUILD_DIR'] || "#{MRUBY_ROOT}/build"

    @file_separator = '/'
    @build_dir = "#{build_dir}/#{@name}"
    @gem_clone_dir = "#{build_dir}/mrbgems"
    @cc = Command::Compiler.new(self, %w(.c))
    @cxx = Command::Compiler.new(self, %w(.cc .cxx .cpp))
    @objc = Command::Compiler.new(self, %w(.m))
    @asm = Command::Compiler.new(self, %w(.S .asm))
    @linker = Command::Linker.new(self)
    @archiver = Command::Archiver.new(self)
    @yacc = Command::Yacc.new(self)
    @gperf = Command::Gperf.new(self)
    @git = Command::Git.new(self)
    @mrbc = Command::Mrbc.new(self)

    @bins = []
    @gems, @libmruby = MRuby::Gem::List.new, []
    @build_mrbtest_lib_only = false
    @cxx_exception_enabled = false
    @cxx_exception_disabled = false
    @cxx_abi_enabled = false
    @enable_bintest = false
    @enable_test = false
    @toolchains = []

    MRuby.targets[@name] = self
  end

  MRuby::Build.current = MRuby.targets[@name]
  MRuby.targets[@name].instance_eval(&block)

  build_mrbc_exec if name == 'host'
  build_mrbtest if test_enabled?
end

Class Attribute Details

.currentObject

Returns the value of attribute current.



43
44
45
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 43

def current
  @current
end

Instance Attribute Details

#binsObject

Returns the value of attribute bins.



47
48
49
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 47

def bins
  @bins
end

#build_dirObject

Returns the value of attribute build_dir.



47
48
49
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 47

def build_dir
  @build_dir
end

#enable_bintestObject



184
185
186
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 184

def enable_bintest
  @enable_bintest = true
end

#enable_testObject



207
208
209
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 207

def enable_test
  @enable_test = true
end

#extsObject

Returns the value of attribute exts.



47
48
49
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 47

def exts
  @exts
end

#file_separatorObject

Returns the value of attribute file_separator.



47
48
49
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 47

def file_separator
  @file_separator
end

#gem_clone_dirObject

Returns the value of attribute gem_clone_dir.



47
48
49
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 47

def gem_clone_dir
  @gem_clone_dir
end

#gemsObject (readonly)

Returns the value of attribute gems.



48
49
50
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 48

def gems
  @gems
end

#libmrubyObject (readonly)

Returns the value of attribute libmruby.



48
49
50
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 48

def libmruby
  @libmruby
end

#nameObject

Returns the value of attribute name.



47
48
49
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 47

def name
  @name
end

#toolchainsObject (readonly)

Returns the value of attribute toolchains.



48
49
50
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 48

def toolchains
  @toolchains
end

Instance Method Details

#bintest_enabled?Boolean



188
189
190
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 188

def bintest_enabled?
  @enable_bintest
end

#build_mrbc_execObject



219
220
221
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 219

def build_mrbc_exec
  gem :core => 'mruby-bin-mrbc'
end

#build_mrbtestObject



215
216
217
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 215

def build_mrbtest
  gem :core => 'mruby-test'
end

#build_mrbtest_lib_onlyObject



288
289
290
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 288

def build_mrbtest_lib_only
  @build_mrbtest_lib_only = true
end

#build_mrbtest_lib_only?Boolean



292
293
294
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 292

def build_mrbtest_lib_only?
  @build_mrbtest_lib_only
end

#compile_as_cxx(src, cxx_src, obj = nil, includes = []) ⇒ Object



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 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 156

def compile_as_cxx src, cxx_src, obj = nil, includes = []
  src = File.absolute_path src
  cxx_src = File.absolute_path cxx_src
  obj = objfile(cxx_src) if obj.nil?

  file cxx_src => [src, __FILE__] do |t|
    FileUtils.mkdir_p File.dirname t.name
    IO.write t.name, "#define __STDC_CONSTANT_MACROS\n#define __STDC_LIMIT_MACROS\n\n#ifndef MRB_ENABLE_CXX_ABI\nextern \"C\" {\n#endif\n#include \"\#{src}\"\n#ifndef MRB_ENABLE_CXX_ABI\n}\n#endif\n"
  end

  file obj => cxx_src do |t|
    cxx.run t.name, t.prerequisites.first, [], ["#{MRUBY_ROOT}/src"] + includes
  end

  obj
end

#compilersObject



231
232
233
234
235
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 231

def compilers
  COMPILERS.map do |c|
    instance_variable_get("@#{c}")
  end
end

#cxx_abi_enabled?Boolean



138
139
140
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 138

def cxx_abi_enabled?
  @cxx_abi_enabled
end

#cxx_exception_enabled?Boolean



134
135
136
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 134

def cxx_exception_enabled?
  @cxx_exception_enabled
end

#cygwin_filename(name) ⇒ Object



256
257
258
259
260
261
262
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 256

def cygwin_filename(name)
  if name.is_a?(Array)
    name.flatten.map { |n| cygwin_filename(n) }
  else
    '"%s"' % `cygpath -w "#{filename(name)}"`.strip
  end
end

#define_rulesObject



237
238
239
240
241
242
243
244
245
246
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 237

def define_rules
  compilers.each do |compiler|
    if respond_to?(:enable_gems?) && enable_gems?
      compiler.defines -= %w(DISABLE_GEMS)
    else
      compiler.defines += %w(DISABLE_GEMS)
    end
    compiler.define_rules build_dir, File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
  end
end

#disable_cxx_exceptionObject



113
114
115
116
117
118
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 113

def disable_cxx_exception
  if @cxx_exception_enabled or @cxx_abi_enabled
    raise "cxx_exception already enabled"
  end
  @cxx_exception_disabled = true
end

#enable_cxx_abiObject



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 142

def enable_cxx_abi
  return if @cxx_abi_enabled
  if @cxx_exception_enabled
    raise "cxx_exception already enabled"
  end
  compilers.each { |c|
    c.defines += %w(MRB_ENABLE_CXX_EXCEPTION MRB_ENABLE_CXX_ABI)
    c.flags << c.cxx_compile_flag
  }
  compilers.each { |c| c.flags << c.cxx_compile_flag }
  linker.command = cxx.command if toolchains.find { |v| v == 'gcc' }
  @cxx_abi_enabled = true
end

#enable_cxx_exceptionObject



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 120

def enable_cxx_exception
  return if @cxx_exception_enabled
  return if @cxx_abi_enabled
  if @cxx_exception_disabled
    raise "cxx_exception disabled"
  end
  @cxx_exception_enabled = true
  compilers.each { |c|
    c.defines += %w(MRB_ENABLE_CXX_EXCEPTION)
    c.flags << c.cxx_exception_flag
  }
  linker.command = cxx.command if toolchains.find { |v| v == 'gcc' }
end

#enable_debugObject



103
104
105
106
107
108
109
110
111
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 103

def enable_debug
  compilers.each do |c|
    c.defines += %w(MRB_DEBUG)
    if toolchains.any? { |toolchain| toolchain == "gcc" }
      c.flags += %w(-g3 -O0)
    end
  end
  @mrbc.compile_options += ' -g'
end

#exefile(name) ⇒ Object



264
265
266
267
268
269
270
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 264

def exefile(name)
  if name.is_a?(Array)
    name.flatten.map { |n| exefile(n) }
  else
    "#{name}#{exts.executable}"
  end
end

#filename(name) ⇒ Object



248
249
250
251
252
253
254
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 248

def filename(name)
  if name.is_a?(Array)
    name.flatten.map { |n| filename(n) }
  else
    '"%s"' % name.gsub('/', file_separator)
  end
end

#libfile(name) ⇒ Object



280
281
282
283
284
285
286
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 280

def libfile(name)
  if name.is_a?(Array)
    name.flatten.map { |n| libfile(n) }
  else
    "#{name}#{exts.library}"
  end
end

#mrbcfileObject



223
224
225
226
227
228
229
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 223

def mrbcfile
  return @mrbcfile if @mrbcfile

  mrbc_build = MRuby.targets['host']
  gems.each { |v| mrbc_build = self if v.name == 'mruby-bin-mrbc' }
  @mrbcfile = mrbc_build.exefile("#{mrbc_build.build_dir}/bin/mrbc")
end

#objfile(name) ⇒ Object



272
273
274
275
276
277
278
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 272

def objfile(name)
  if name.is_a?(Array)
    name.flatten.map { |n| objfile(n) }
  else
    "#{name}#{exts.object}"
  end
end

#primary_toolchainObject



199
200
201
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 199

def primary_toolchain
  @toolchains.first
end


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 310

def print_build_summary
  puts "================================================"
  puts "      Config Name: #{@name}"
  puts " Output Directory: #{self.build_dir.relative_path}"
  puts "         Binaries: #{@bins.join(', ')}" unless @bins.empty?
  unless @gems.empty?
    puts "    Included Gems:"
    @gems.map do |gem|
      gem_version = " - #{gem.version}" if gem.version != '0.0.0'
      gem_summary = " - #{gem.summary}" if gem.summary
      puts "             #{gem.name}#{gem_version}#{gem_summary}"
      puts "               - Binaries: #{gem.bins.join(', ')}" unless gem.bins.empty?
    end
  end
  puts "================================================"
  puts
end

#rootObject



203
204
205
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 203

def root
  MRUBY_ROOT
end

#run_bintestObject



304
305
306
307
308
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 304

def run_bintest
  targets = @gems.select { |v| File.directory? "#{v.dir}/bintest" }.map { |v| filename v.dir }
  targets << filename(".") if File.directory? "./bintest"
  sh "ruby test/bintest.rb #{targets.join ' '}"
end

#run_testObject



296
297
298
299
300
301
302
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 296

def run_test
  puts ">>> Test #{name} <<<"
  mrbtest = exefile("#{build_dir}/bin/mrbtest")
  sh "#{filename mrbtest.relative_path}#{$verbose ? ' -v' : ''}"
  puts
  run_bintest if bintest_enabled?
end

#test_enabled?Boolean



211
212
213
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 211

def test_enabled?
  @enable_test
end

#toolchain(name, params = {}) ⇒ Object



192
193
194
195
196
197
# File 'ext/enterprise_script_service/mruby/lib/mruby/build.rb', line 192

def toolchain(name, params={})
  tc = Toolchain.toolchains[name.to_s]
  fail "Unknown #{name} toolchain" unless tc
  tc.setup(self, params)
  @toolchains.unshift name.to_s
end