source: EcnlProtoTool/trunk/mruby-1.3.0/tasks/mruby_build_commands.rake@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 11.9 KB
Line 
1require 'forwardable'
2require 'digest/md5'
3
4module MRuby
5 class Command
6 include Rake::DSL
7 extend Forwardable
8 def_delegators :@build, :filename, :objfile, :libfile, :exefile, :cygwin_filename
9 attr_accessor :build, :command
10
11 def initialize(build)
12 @build = build
13 end
14
15 # clone is deep clone without @build
16 def clone
17 target = super
18 excepts = %w(@build)
19 instance_variables.each do |attr|
20 unless excepts.include?(attr.to_s)
21 val = Marshal::load(Marshal.dump(instance_variable_get(attr))) # deep clone
22 target.instance_variable_set(attr, val)
23 end
24 end
25 target
26 end
27
28 NotFoundCommands = {}
29
30 private
31 def _run(options, params={})
32 return sh command + ' ' + ( options % params ) if NotFoundCommands.key? @command
33 begin
34 sh build.filename(command) + ' ' + ( options % params )
35 rescue RuntimeError
36 NotFoundCommands[@command] = true
37 _run options, params
38 end
39 end
40 end
41
42 class Command::Compiler < Command
43 attr_accessor :flags, :include_paths, :defines, :source_exts
44 attr_accessor :compile_options, :option_define, :option_include_path, :out_ext
45 attr_accessor :cxx_compile_flag, :cxx_exception_flag
46
47 def initialize(build, source_exts=[])
48 super(build)
49 @command = ENV['CC'] || 'cc'
50 @flags = [ENV['CFLAGS'] || []]
51 @source_exts = source_exts
52 @include_paths = ["#{MRUBY_ROOT}/include"]
53 @defines = %w()
54 @option_include_path = '-I%s'
55 @option_define = '-D%s'
56 @compile_options = '%{flags} -o %{outfile} -c %{infile}'
57 end
58
59 alias header_search_paths include_paths
60 def search_header_path(name)
61 header_search_paths.find do |v|
62 File.exist? build.filename("#{v}/#{name}").sub(/^"(.*)"$/, '\1')
63 end
64 end
65
66 def search_header(name)
67 path = search_header_path name
68 path && build.filename("#{path}/#{name}").sub(/^"(.*)"$/, '\1')
69 end
70
71 def all_flags(_defineds=[], _include_paths=[], _flags=[])
72 define_flags = [defines, _defineds].flatten.map{ |d| option_define % d }
73 include_path_flags = [include_paths, _include_paths].flatten.map do |f|
74 if MRUBY_BUILD_HOST_IS_CYGWIN
75 option_include_path % cygwin_filename(f)
76 else
77 option_include_path % filename(f)
78 end
79 end
80 [flags, define_flags, include_path_flags, _flags].flatten.join(' ')
81 end
82
83 def run(outfile, infile, _defineds=[], _include_paths=[], _flags=[])
84 FileUtils.mkdir_p File.dirname(outfile)
85 _pp "CC", infile.relative_path, outfile.relative_path
86 if MRUBY_BUILD_HOST_IS_CYGWIN
87 _run compile_options, { :flags => all_flags(_defineds, _include_paths, _flags),
88 :infile => cygwin_filename(infile), :outfile => cygwin_filename(outfile),
89 :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
90 else
91 _run compile_options, { :flags => all_flags(_defineds, _include_paths, _flags),
92 :infile => filename(infile), :outfile => filename(outfile),
93 :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
94 end
95 end
96
97 def define_rules(build_dir, source_dir='')
98 @out_ext = build.exts.object
99 gemrake = File.join(source_dir, "mrbgem.rake")
100 rakedep = File.exist?(gemrake) ? [ gemrake ] : []
101
102 if build_dir.include? "mrbgems/"
103 generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(.*)#{Regexp.escape out_ext}$")
104 else
105 generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(?!mrbgems/.+/)(.*)#{Regexp.escape out_ext}$")
106 end
107 source_exts.each do |ext, compile|
108 rule generated_file_matcher => [
109 proc { |file|
110 file.sub(generated_file_matcher, "#{source_dir}/\\1#{ext}")
111 },
112 proc { |file|
113 get_dependencies(file) + rakedep
114 }
115 ] do |t|
116 run t.name, t.prerequisites.first
117 end
118
119 rule generated_file_matcher => [
120 proc { |file|
121 file.sub(generated_file_matcher, "#{build_dir}/\\1#{ext}")
122 },
123 proc { |file|
124 get_dependencies(file) + rakedep
125 }
126 ] do |t|
127 run t.name, t.prerequisites.first
128 end
129 end
130 end
131
132 private
133 def get_dependencies(file)
134 file = file.ext('d') unless File.extname(file) == '.d'
135 if File.exist?(file)
136 File.read(file).gsub("\\\n ", "").scan(/^\S+:\s+(.+)$/).flatten.map {|s| s.split(' ') }.flatten
137 else
138 []
139 end + [ MRUBY_CONFIG ]
140 end
141 end
142
143 class Command::Linker < Command
144 attr_accessor :flags, :library_paths, :flags_before_libraries, :libraries, :flags_after_libraries
145 attr_accessor :link_options, :option_library, :option_library_path
146
147 def initialize(build)
148 super
149 @command = ENV['LD'] || 'ld'
150 @flags = (ENV['LDFLAGS'] || [])
151 @flags_before_libraries, @flags_after_libraries = [], []
152 @libraries = []
153 @library_paths = []
154 @option_library = '-l%s'
155 @option_library_path = '-L%s'
156 @link_options = "%{flags} -o %{outfile} %{objs} %{flags_before_libraries} %{libs} %{flags_after_libraries}"
157 end
158
159 def all_flags(_library_paths=[], _flags=[])
160 library_path_flags = [library_paths, _library_paths].flatten.map do |f|
161 if MRUBY_BUILD_HOST_IS_CYGWIN
162 option_library_path % cygwin_filename(f)
163 else
164 option_library_path % filename(f)
165 end
166 end
167 [flags, library_path_flags, _flags].flatten.join(' ')
168 end
169
170 def library_flags(_libraries)
171 [libraries, _libraries].flatten.map{ |d| option_library % d }.join(' ')
172 end
173
174 def run(outfile, objfiles, _libraries=[], _library_paths=[], _flags=[], _flags_before_libraries=[], _flags_after_libraries=[])
175 FileUtils.mkdir_p File.dirname(outfile)
176 library_flags = [libraries, _libraries].flatten.map { |d| option_library % d }
177
178 _pp "LD", outfile.relative_path
179 if MRUBY_BUILD_HOST_IS_CYGWIN
180 _run link_options, { :flags => all_flags(_library_paths, _flags),
181 :outfile => cygwin_filename(outfile) , :objs => cygwin_filename(objfiles).join(' '),
182 :flags_before_libraries => [flags_before_libraries, _flags_before_libraries].flatten.join(' '),
183 :flags_after_libraries => [flags_after_libraries, _flags_after_libraries].flatten.join(' '),
184 :libs => library_flags.join(' '),
185 :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
186 else
187 _run link_options, { :flags => all_flags(_library_paths, _flags),
188 :outfile => filename(outfile) , :objs => filename(objfiles).join(' '),
189 :flags_before_libraries => [flags_before_libraries, _flags_before_libraries].flatten.join(' '),
190 :flags_after_libraries => [flags_after_libraries, _flags_after_libraries].flatten.join(' '),
191 :libs => library_flags.join(' '),
192 :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
193 end
194 end
195 end
196
197 class Command::Archiver < Command
198 attr_accessor :archive_options
199
200 def initialize(build)
201 super
202 @command = ENV['AR'] || 'ar'
203 @archive_options = 'rs %{outfile} %{objs}'
204 end
205
206 def run(outfile, objfiles)
207 FileUtils.mkdir_p File.dirname(outfile)
208 _pp "AR", outfile.relative_path
209
210 # reference from emar.py
211 outfilebase = File.dirname(outfile)
212 to_delete = []
213 newargs = []
214 objfiles.each do |orig_name|
215 dir_name = File.dirname(orig_name)
216 dir_name = dir_name.relative_path_from(Dir.pwd)
217 base_name = File.basename(orig_name)
218 parts = base_name.split('.')
219 # h = Digest::MD5.new.update(orig_name).to_s
220 h = Digest::MD5.new.update(orig_name).to_s.slice(0,4)
221 parts[0] += '_' + h
222 newname = parts.join('.')
223 full_newname = File.join(dir_name, newname)
224 if not File.exists?(full_newname)
225 begin # it is ok to fail here, we just don't get hashing
226 FileUtils.cp(orig_name, full_newname)
227 newargs << full_newname
228 to_delete << full_newname
229 rescue
230 end
231 end
232 end
233
234 if MRUBY_BUILD_HOST_IS_CYGWIN
235 _run archive_options, { :outfile => cygwin_filename(outfile), :objs => cygwin_filename(newargs).join(' '), :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
236 else
237 _run archive_options, { :outfile => filename(outfile), :objs => filename(newargs).join(' '), :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
238 end
239
240 to_delete.each do |d|
241 FileUtils.rm(d)
242 end
243 end
244 end
245
246 class Command::Yacc < Command
247 attr_accessor :compile_options
248
249 def initialize(build)
250 super
251 @command = 'bison'
252 @compile_options = '-o %{outfile} %{infile}'
253 end
254
255 def run(outfile, infile)
256 FileUtils.mkdir_p File.dirname(outfile)
257 _pp "YACC", infile.relative_path, outfile.relative_path
258 _run compile_options, { :outfile => filename(outfile) , :infile => filename(infile) }
259 end
260 end
261
262 class Command::Gperf < Command
263 attr_accessor :compile_options
264
265 def initialize(build)
266 super
267 @command = 'gperf'
268 @compile_options = '-L ANSI-C -C -p -j1 -i 1 -g -o -t -N mrb_reserved_word -k"1,3,$" %{infile} > %{outfile}'
269 end
270
271 def run(outfile, infile)
272 FileUtils.mkdir_p File.dirname(outfile)
273 _pp "GPERF", infile.relative_path, outfile.relative_path
274 _run compile_options, { :outfile => filename(outfile) , :infile => filename(infile) }
275 end
276 end
277
278 class Command::Git < Command
279 attr_accessor :flags
280 attr_accessor :clone_options, :pull_options, :checkout_options
281
282 def initialize(build)
283 super
284 @command = 'git'
285 @flags = %w[]
286 @clone_options = "clone %{flags} %{url} %{dir}"
287 @pull_options = "pull"
288 @checkout_options = "checkout %{checksum_hash}"
289 end
290
291 def run_clone(dir, url, _flags = [])
292 _pp "GIT", url, dir.relative_path
293 _run clone_options, { :flags => [flags, _flags].flatten.join(' '), :url => url, :dir => filename(dir) }
294 end
295
296 def run_pull(dir, url)
297 root = Dir.pwd
298 Dir.chdir dir
299 _pp "GIT PULL", url, dir.relative_path
300 _run pull_options
301 Dir.chdir root
302 end
303
304 def run_checkout(dir, checksum_hash)
305 root = Dir.pwd
306 Dir.chdir dir
307 _pp "GIT CHECKOUT", checksum_hash
308 _run checkout_options, { :checksum_hash => checksum_hash }
309 Dir.chdir root
310 end
311 end
312
313 class Command::Mrbc < Command
314 attr_accessor :compile_options
315
316 def initialize(build)
317 super
318 @command = nil
319 @compile_options = "-B%{funcname} -o-"
320 end
321
322 def run(out, infiles, funcname)
323 @command ||= @build.mrbcfile
324 infiles = [infiles].flatten
325 infiles.each do |f|
326 _pp "MRBC", f.relative_path, nil, :indent => 2
327 end
328 IO.popen("#{filename @command} #{@compile_options % {:funcname => funcname}} #{filename(infiles).join(' ')}", 'r+') do |io|
329 out.puts io.read
330 end
331 # if mrbc execution fail, drop the file
332 if $?.exitstatus != 0
333 File.delete(out.path)
334 exit(-1)
335 end
336 end
337 end
338
339 class Command::CrossTestRunner < Command
340 attr_accessor :runner_options
341 attr_accessor :verbose_flag
342 attr_accessor :flags
343
344 def initialize(build)
345 super
346 @command = nil
347 @runner_options = '%{flags} %{infile}'
348 @verbose_flag = ''
349 @flags = []
350 end
351
352 def run(testbinfile)
353 puts "TEST for " + @build.name
354 _run runner_options, { :flags => [flags, verbose_flag].flatten.join(' '), :infile => testbinfile }
355 end
356 end
357
358end
Note: See TracBrowser for help on using the repository browser.