-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rb
220 lines (185 loc) · 5.59 KB
/
build.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env ruby
require 'tmpdir'
require 'fileutils'
require 'uri'
def pipe(command)
output = ""
IO.popen(command) do |io|
until io.eof?
buffer = io.gets
output << buffer
puts buffer
end
end
output
end
def run!(cmd)
result = `#{cmd}`
raise "Error running #{cmd}, result: #{result}" unless $?.success?
result
end
def fetch(url, name = nil)
uri = URI.parse(url)
binary = uri.to_s.split("/").last
if File.exists?(binary)
puts "Using #{binary}"
else
puts "Fetching #{binary}"
if name
run!("curl #{uri} -s -o #{name}")
else
run!("curl #{uri} -s -O")
end
end
end
workspace_dir = ARGV[0]
output_dir = ARGV[1]
cache_dir = ARGV[2]
LIBYAML_VERSION = "0.1.7"
LIBFFI_VERSION = "3.2.1"
vendor_url = "https://s3.amazonaws.com/#{ENV['S3_BUCKET_NAME'] ? ENV['S3_BUCKET_NAME'] : 'heroku-buildpack-ruby'}"
full_version = ENV['VERSION']
full_name = "ruby-#{full_version}"
version = full_version.split('-').first
name = "ruby-#{version}"
major_ruby = version.match(/\d\.\d/)[0]
build = false
build = true if ENV["BUILD"]
debug = nil
debug = true if ENV['DEBUG']
jobs = ENV['JOBS'] || 2
rubygems = ENV['RUBYGEMS_VERSION'] ? ENV['RUBYGEMS_VERSION'] : nil
git_url = ENV["GIT_URL"]
svn_url = ENV["SVN_URL"]
relname = ENV["RELNAME"]
stack = ENV["STACK"]
treeish = nil
if ENV['PATCH_URL']
patch = ENV['PATCH_URL']
patch_name = patch.split("/").last
else
patch = nil
end
# create cache dir if it doesn't exist
FileUtils.mkdir_p(cache_dir)
# fetch deps
Dir.chdir(cache_dir) do
tarball = "#{full_name}.tar.gz"
if git_url
uri = URI.parse(git_url)
treeish = uri.fragment
uri.fragment = nil
full_name = uri.to_s.split('/').last.sub(".git", "")
if File.exists?(full_name)
Dir.chdir(full_name) do
puts "Updating git repo"
pipe "git pull"
end
else
puts "Fetching #{git_url}"
pipe "git clone #{uri}"
end
elsif svn_url
uri = URI.parse(svn_url)
if File.exists?(full_name)
puts "Using existing svn checkout: #{full_name}"
pipe "svn update"
else
pipe "svn co #{svn_url} #{full_name}"
end
Dir.chdir(full_name) do
cmd = "ruby tool/make-snapshot -archname=#{full_name} build #{relname}"
puts cmd
pipe cmd
end
FileUtils.mv("#{full_name}/build/#{tarball}", ".")
else
url = "http://ftp.ruby-lang.org/pub/ruby/#{major_ruby}/#{tarball}"
puts "Downloading #{url}"
fetch(url)
end
if stack.match(/cedar/)
["libyaml-#{LIBYAML_VERSION}.tgz", "libffi-#{LIBFFI_VERSION}.tgz"].each do |binary|
fetch("#{vendor_url}/#{stack}/#{binary}")
end
end
if rubygems
rubygems_binary = "rubygems-#{rubygems}"
fetch("http://production.cf.rubygems.org/rubygems/#{rubygems_binary}.tgz")
end
fetch(patch, patch_name) if patch
end
Dir.mktmpdir("ruby-vendor-") do |vendor_dir|
if git_url
FileUtils.cp_r("#{cache_dir}/#{full_name}", ".")
else
run!("tar zxf #{cache_dir}/#{full_name}.tar.gz")
end
Dir.chdir(vendor_dir) do
run!("tar zxf #{cache_dir}/libyaml-#{LIBYAML_VERSION}.tgz") if stack.match(/cedar/)
run!("tar zxf #{cache_dir}/libffi-#{LIBFFI_VERSION}.tgz") if stack.match(/cedar/)
run!("tar zxf #{cache_dir}/rubygems-#{rubygems}.tgz") if rubygems
end
prefix = "/app/vendor/#{name}"
prefix = "/tmp/#{name}" if build
puts "prefix: #{prefix}"
Dir.chdir(full_name) do
pipe "git checkout #{treeish}" if treeish
pipe "patch -p0 < #{cache_dir}/#{patch_name}" if patch
if debug
configure_env = "debugflags=\"-ggdb3\""
else
configure_env = "debugflags=\"-g\""
end
configure_opts = "--disable-install-doc --prefix #{prefix}"
configure_opts += " --enable-load-relative" if major_ruby != "1.8" && version != "1.9.2"
if stack != "cedar-14"
configure_opts += " --enable-shared"
end
puts "configure env: #{configure_env}"
puts "configure opts: #{configure_opts}"
cmds = [
"#{configure_env} ./configure #{configure_opts}",
"env CPATH=#{vendor_dir}/include:\\$CPATH CPPATH=#{vendor_dir}/include:\\$CPPATH LIBRARY_PATH=#{vendor_dir}/lib:\\$LIBRARY_PATH make -j#{jobs}",
"make install"
]
cmds.unshift("#{configure_env} autoconf") if git_url
cmds.unshift("chmod +x ./tool/*") if git_url
pipe(cmds.join(" && "))
end
if rubygems
Dir.chdir("#{vendor_dir}/rubygems-#{rubygems}") do
pipe("#{prefix}/bin/ruby setup.rb")
end
gem_bin_file = "#{prefix}/bin/gem"
gem = File.read(gem_bin_file)
File.open(gem_bin_file, 'w') do |file|
file.puts "#!/usr/bin/env ruby"
lines = gem.split("\n")
lines.shift
lines.each {|line| file.puts line }
end
end
Dir.chdir(prefix) do
# Fix malformed binstubs
Dir.entries("bin").each do |entry|
next unless File.file?("bin/#{entry}")
shebang = `cat bin/#{entry} | head -n 1`
next unless shebang.force_encoding("UTF-8").valid_encoding? # Check if binstub is a binary file
regex = /#{Regexp.escape("#!/app/vendor/#{full_name}/bin/ruby")}/
next unless shebang =~ regex
puts "Fixing binstub for bin/#{entry}"
contents = File.read("bin/#{entry}").gsub(regex, "#!/usr/bin/env ruby")
File.open("bin/#{entry}", "w") {|f| f.write(contents) }
end
filename =
if build
"ruby-build-#{full_version}.tgz"
else
"#{full_name}.tgz"
end
puts "Writing #{filename}"
FileUtils.mkdir_p("#{output_dir}/#{stack}")
pipe("tar czf #{output_dir}/#{stack}/#{filename} *")
end
end