1MRuby.each_target do
2  if enable_gems?
3    # set up all gems
4    gems.each(&:setup)
5    gems.check self
6
7    # loader all gems
8    self.libmruby_objs << objfile("#{build_dir}/mrbgems/gem_init")
9    file objfile("#{build_dir}/mrbgems/gem_init") => ["#{build_dir}/mrbgems/gem_init.c", "#{build_dir}/LEGAL"]
10    file "#{build_dir}/mrbgems/gem_init.c" => [MRUBY_CONFIG, __FILE__, *Dir.glob("#{build_dir}/mrbgems/mruby-*/*.c")] do |t|
11      mkdir_p "#{build_dir}/mrbgems"
12      open(t.name, 'w') do |f|
13        gem_func_gems = gems.select { |g| g.generate_functions }
14        gem_func_decls = gem_func_gems.each_with_object('') do |g, s|
15          s << "void GENERATED_TMP_mrb_#{g.funcname}_gem_init(mrb_state*);\n" \
16               "void GENERATED_TMP_mrb_#{g.funcname}_gem_final(mrb_state*);\n"
17        end
18        gem_init_calls = gem_func_gems.each_with_object('') do |g, s|
19          s << "  GENERATED_TMP_mrb_#{g.funcname}_gem_init(mrb);\n"
20        end
21        gem_final_calls = gem_func_gems.reverse_each.with_object('') do |g, s|
22          s << "  GENERATED_TMP_mrb_#{g.funcname}_gem_final(mrb);\n"
23        end
24        f.puts %Q[/*]
25        f.puts %Q[ * This file contains a list of all]
26        f.puts %Q[ * initializing methods which are]
27        f.puts %Q[ * necessary to bootstrap all gems.]
28        f.puts %Q[ *]
29        f.puts %Q[ * IMPORTANT:]
30        f.puts %Q[ *   This file was generated!]
31        f.puts %Q[ *   All manual changes will get lost.]
32        f.puts %Q[ */]
33        f.puts %Q[]
34        f.puts %Q[#include <mruby.h>]
35        f.puts %Q[]
36        f.write gem_func_decls
37        unless gem_final_calls.empty?
38        f.puts %Q[]
39          f.puts %Q[static void]
40          f.puts %Q[mrb_final_mrbgems(mrb_state *mrb) {]
41          f.write gem_final_calls
42          f.puts %Q[}]
43        end
44        f.puts %Q[]
45        f.puts %Q[void]
46        f.puts %Q[mrb_init_mrbgems(mrb_state *mrb) {]
47        f.write gem_init_calls
48        f.puts %Q[  mrb_state_atexit(mrb, mrb_final_mrbgems);] unless gem_final_calls.empty?
49        f.puts %Q[}]
50      end
51    end
52  end
53
54  # legal documents
55  file "#{build_dir}/LEGAL" => [MRUBY_CONFIG, __FILE__] do |t|
56    mkdir_p File.dirname t.name
57    open(t.name, 'w+') do |f|
58     f.puts <<LEGAL
59Copyright (c) #{Time.now.year} mruby developers
60
61Permission is hereby granted, free of charge, to any person obtaining a
62copy of this software and associated documentation files (the "Software"),
63to deal in the Software without restriction, including without limitation
64the rights to use, copy, modify, merge, publish, distribute, sublicense,
65and/or sell copies of the Software, and to permit persons to whom the
66Software is furnished to do so, subject to the following conditions:
67
68The above copyright notice and this permission notice shall be included in
69all copies or substantial portions of the Software.
70
71THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
72IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
73FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
74AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
75LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
76FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
77DEALINGS IN THE SOFTWARE.
78LEGAL
79
80      if enable_gems?
81        f.puts <<GEMS_LEGAL
82
83Additional Licenses
84
85Due to the reason that you choosed additional mruby packages (GEMS),
86please check the following additional licenses too:
87GEMS_LEGAL
88
89        gems.map do |g|
90          authors = [g.authors].flatten.sort.join(", ")
91          f.puts
92          f.puts "GEM: #{g.name}"
93          f.puts "Copyright (c) #{Time.now.year} #{authors}"
94          f.puts "License: #{g.licenses}"
95        end
96      end
97    end
98  end
99end
100