Debian Codegraph @

vendor/mruby-source/tasks/mrbgems.rake

raw
1 MRuby.each_target do
2 active_gems_txt = "#{build_dir}/mrbgems/active_gems.txt"
3
4 if enable_gems?
5 # set up all gems
6 gems.each(&:setup)
7 gems.check self
8
9 # loader all gems
10 self.libmruby_objs << objfile("#{build_dir}/mrbgems/gem_init")
11 file objfile("#{build_dir}/mrbgems/gem_init") => ["#{build_dir}/mrbgems/gem_init.c", "#{build_dir}/LEGAL"]
12 file "#{build_dir}/mrbgems/gem_init.c" => [active_gems_txt, MRUBY_CONFIG, __FILE__] do |t|
13 mkdir_p "#{build_dir}/mrbgems"
14 open(t.name, 'w') do |f|
15 gem_func_gems = gems.select { |g| g.generate_functions }
16 gem_func_decls = ''
17 gem_funcs = ''
18 gem_func_gems.each do |g|
19 init = "GENERATED_TMP_mrb_#{g.funcname}_gem_init"
20 final = "GENERATED_TMP_mrb_#{g.funcname}_gem_final"
21 gem_func_decls << "void #{init}(mrb_state*);\n" \
22 "void #{final}(mrb_state*);\n"
23 gem_funcs << " { #{init}, #{final} },\n"
24 end
25 f.puts %Q[/*]
26 f.puts %Q[ * This file contains a list of all]
27 f.puts %Q[ * initializing methods which are]
28 f.puts %Q[ * necessary to bootstrap all gems.]
29 f.puts %Q[ *]
30 f.puts %Q[ * This file was generated by mruby/#{__FILE__.relative_path_from(MRUBY_ROOT)}.]
31 f.puts %Q[ *]
32 f.puts %Q[ * IMPORTANT:]
33 f.puts %Q[ * This file was generated!]
34 f.puts %Q[ * All manual changes will get lost.]
35 f.puts %Q[ */]
36 f.puts %Q[]
37 f.puts %Q[#include <mruby.h>]
38 f.puts %Q[#include <mruby/error.h>]
39 f.puts %Q[#include <mruby/proc.h>]
40 f.puts %Q[]
41 unless gem_funcs.empty?
42 f.write gem_func_decls
43 f.puts %Q[]
44 f.puts %Q[static const struct {]
45 f.puts %Q[ void (*init)(mrb_state*);]
46 f.puts %Q[ void (*final)(mrb_state*);]
47 f.puts %Q[} gem_funcs[] = {]
48 f.write gem_funcs
49 f.puts %Q[};]
50 f.puts %Q[]
51 f.puts %Q[#define NUM_GEMS ((int)(sizeof(gem_funcs) / sizeof(gem_funcs[0])))]
52 f.puts %Q[]
53 f.puts %Q[struct final_mrbgems {]
54 f.puts %Q[ int i;]
55 f.puts %Q[ int ai;]
56 f.puts %Q[};]
57 f.puts %Q[]
58 f.puts %Q[static mrb_value]
59 f.puts %Q[final_mrbgems_body(mrb_state *mrb, void *ud) {]
60 f.puts %Q[ struct final_mrbgems *p = (struct final_mrbgems*)ud;]
61 f.puts %Q[ for (; p->i >= 0; p->i--) {]
62 f.puts %Q[ gem_funcs[p->i].final(mrb);]
63 f.puts %Q[ mrb_gc_arena_restore(mrb, p->ai);]
64 f.puts %Q[ }]
65 f.puts %Q[ return mrb_nil_value();]
66 f.puts %Q[}]
67 f.puts %Q[]
68 f.puts %Q[static void]
69 f.puts %Q[mrb_final_mrbgems(mrb_state *mrb) {]
70 f.puts %Q[ struct final_mrbgems a = { NUM_GEMS - 1, mrb_gc_arena_save(mrb) };]
71 f.puts %Q[ for (; a.i >= 0; a.i--) {]
72 f.puts %Q[ mrb_protect_error(mrb, final_mrbgems_body, &a, NULL);]
73 f.puts %Q[ mrb_gc_arena_restore(mrb, a.ai);]
74 f.puts %Q[ }]
75 f.puts %Q[}]
76 f.puts %Q[]
77 end
78 f.puts %Q[void]
79 f.puts %Q[mrb_init_mrbgems(mrb_state *mrb) {]
80 unless gem_funcs.empty?
81 f.puts %Q[ int ai = mrb_gc_arena_save(mrb);]
82 f.puts %Q[ for (int i = 0; i < NUM_GEMS; i++) {]
83 f.puts %Q[ gem_funcs[i].init(mrb);]
84 f.puts %Q[ mrb_gc_arena_restore(mrb, ai);]
85 f.puts %Q[ mrb_vm_ci_env_clear(mrb, mrb->c->cibase);]
86 f.puts %Q[ if (mrb->exc) {]
87 f.puts %Q[ mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));]
88 f.puts %Q[ }]
89 f.puts %Q[ }]
90 f.puts %Q[ mrb_state_atexit(mrb, mrb_final_mrbgems);]
91 end
92 f.puts %Q[}]
93 end
94 end
95 end
96
97 file active_gems_txt => :generate_active_gems_txt
98 desc "generate the active gems text files"
99 task :generate_active_gems_txt do |t|
100 def t.timestamp; Time.at(0) end
101 active_gems = gems.sort_by(&:name).inject(""){|s, g| s << "#{g.name}\n"}
102 if !File.exist?(active_gems_txt) || File.read(active_gems_txt) != active_gems
103 mkdir_p File.dirname(active_gems_txt)
104 File.write(active_gems_txt, active_gems)
105 end
106 end
107
108 # legal documents
109 file "#{build_dir}/LEGAL" => [MRUBY_CONFIG, __FILE__] do |t|
110 mkdir_p File.dirname t.name
111 open(t.name, 'w+') do |f|
112 f.puts <<LEGAL
113 Copyright (c) #{Time.now.year} mruby developers
114
115 Permission is hereby granted, free of charge, to any person obtaining a
116 copy of this software and associated documentation files (the "Software"),
117 to deal in the Software without restriction, including without limitation
118 the rights to use, copy, modify, merge, publish, distribute, sublicense,
119 and/or sell copies of the Software, and to permit persons to whom the
120 Software is furnished to do so, subject to the following conditions:
121
122 The above copyright notice and this permission notice shall be included in
123 all copies or substantial portions of the Software.
124
125 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
126 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
127 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
128 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
129 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
130 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
131 DEALINGS IN THE SOFTWARE.
132 LEGAL
133
134 if enable_gems?
135 f.puts <<GEMS_LEGAL
136
137 Additional Licenses
138
139 Due to the reason that you chose additional mruby packages (GEMS),
140 please check the following additional licenses too:
141 GEMS_LEGAL
142
143 gems.map do |g|
144 authors = [g.authors].flatten.sort.join(", ")
145 f.puts
146 f.puts "GEM: #{g.name}"
147 f.puts "Copyright (c) #{Time.now.year} #{authors}"
148 f.puts "License: #{g.licenses}"
149 end
150 end
151 end
152 end
153 end