This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rb
193 lines (168 loc) · 4.8 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
#!/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 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
`curl #{uri} -s -o #{name}`
else
`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
fetch("http://ftp.ruby-lang.org/pub/ruby/#{major_ruby}/#{tarball}")
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
`tar zxf #{cache_dir}/#{full_name}.tar.gz`
end
Dir.chdir(vendor_dir) do
`tar zxf #{cache_dir}/libyaml-#{LIBYAML_VERSION}.tgz`
`tar zxf #{cache_dir}/libffi-#{LIBFFI_VERSION}.tgz`
`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
filename =
if build
"ruby-build-#{full_version}.tgz"
else
"#{full_name}.tgz"
end
pipe "ls"
puts "Writing #{filename}"
FileUtils.mkdir_p("#{output_dir}/#{stack}")
pipe("tar czf #{output_dir}/#{stack}/#{filename} *")
end
end