Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / lib / mruby / build / load_gems.rb
1 module MRuby
2   module LoadGems
3     def gembox(gemboxfile)
4       gembox = File.expand_path("#{gemboxfile}.gembox", "#{MRUBY_ROOT}/mrbgems")
5       fail "Can't find gembox '#{gembox}'" unless File.exist?(gembox)
6
7       GemBox.config = self
8       GemBox.path = gembox
9
10       instance_eval File.read(gembox)
11
12       GemBox.path = nil
13     end
14
15     def gem(gemdir, &block)
16       caller_dir = File.expand_path(File.dirname(/^(.*?):\d/.match(caller.first).to_a[1]))
17
18       if gemdir.is_a?(Hash)
19         gemdir = load_special_path_gem(gemdir)
20       elsif GemBox.path && gemdir.is_a?(String)
21         gemdir = File.expand_path(gemdir, File.dirname(GemBox.path))
22       else
23         gemdir = File.expand_path(gemdir, caller_dir)
24       end
25
26       gemrake = File.join(gemdir, "mrbgem.rake")
27
28       fail "Can't find #{gemrake}" unless File.exist?(gemrake)
29       Gem.current = nil
30       load gemrake
31       return nil unless Gem.current
32
33       Gem.current.dir = gemdir
34       Gem.current.build = self.is_a?(MRuby::Build) ? self : MRuby::Build.current
35       Gem.current.build_config_initializer = block
36       gems << Gem.current
37
38       cxx_srcs = ['src', 'test', 'tools'].map do |subdir|
39         Dir.glob("#{Gem.current.dir}/#{subdir}/*.{cpp,cxx,cc}")
40       end.flatten
41       enable_cxx_exception unless cxx_srcs.empty?
42
43       Gem.current
44     end
45
46     def load_special_path_gem(params)
47       if params[:github]
48         params[:git] = "https://github.com/#{params[:github]}.git"
49       elsif params[:bitbucket]
50         if params[:method] == "ssh"
51           params[:git] = "git@bitbucket.org:#{params[:bitbucket]}.git"
52         else
53           params[:git] = "https://bitbucket.org/#{params[:bitbucket]}.git"
54         end
55       elsif params[:mgem]
56         mgem_list_dir = "#{gem_clone_dir}/mgem-list"
57         mgem_list_url = 'https://github.com/mruby/mgem-list.git'
58         if File.exist? mgem_list_dir
59           git.run_pull mgem_list_dir, mgem_list_url if $pull_gems
60         else
61           FileUtils.mkdir_p mgem_list_dir
62           git.run_clone mgem_list_dir, mgem_list_url, "--depth 1"
63         end
64
65         require 'yaml'
66
67         conf_path = "#{mgem_list_dir}/#{params[:mgem]}.gem"
68         conf_path = "#{mgem_list_dir}/mruby-#{params[:mgem]}.gem" unless File.exist? conf_path
69         fail "mgem not found: #{params[:mgem]}" unless File.exist? conf_path
70         conf = YAML.load File.read conf_path
71
72         fail "unknown mgem protocol: #{conf['protocol']}" if conf['protocol'] != 'git'
73         params[:git] = conf['repository']
74         params[:branch] = conf['branch'] if conf['branch']
75       end
76
77       if params[:core]
78         gemdir = "#{root}/mrbgems/#{params[:core]}"
79       elsif params[:git]
80         url = params[:git]
81         gemdir = "#{gem_clone_dir}/#{url.match(/([-\w]+)(\.[-\w]+|)$/).to_a[1]}"
82
83         # by default the 'master' branch is used
84         branch = params[:branch] ? params[:branch] : 'master'
85
86         if File.exist?(gemdir)
87           if $pull_gems
88             git.run_pull gemdir, url
89           else
90             gemdir
91           end
92         else
93           options = [params[:options]] || []
94           options << "--recursive"
95           options << "--branch \"#{branch}\""
96           options << "--depth 1" unless params[:checksum_hash]
97           FileUtils.mkdir_p "#{gem_clone_dir}"
98           git.run_clone gemdir, url, options
99         end
100
101         if params[:checksum_hash]
102           # Jump to the specified commit
103           git.run_checkout gemdir, params[:checksum_hash]
104         else
105           # Jump to the top of the branch
106           git.run_checkout gemdir, branch if $pull_gems
107         end
108
109         gemdir << "/#{params[:path]}" if params[:path]
110       elsif params[:path]
111         require 'pathname'
112         gemdir = Pathname.new(params[:path]).absolute? ? params[:path] : "#{root}/#{params[:path]}"
113       else
114         fail "unknown gem option #{params}"
115       end
116
117       gemdir
118     end
119
120     def enable_gems?
121       !@gems.empty?
122     end
123   end # LoadGems
124 end # MRuby