Skip to content

Commit 473d9d3

Browse files
Add rake task to download prebuilt binaries
To allow developers skipping CRuby builds when editing npm packages
1 parent affbc4e commit 473d9d3

File tree

4 files changed

+94
-12
lines changed

4 files changed

+94
-12
lines changed

Rakefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,44 @@ namespace :build do
7878
end
7979
end
8080
end
81+
82+
desc "Clean build directories"
83+
task :clean do
84+
rm_rf "./build"
85+
rm_rf "./rubies"
86+
end
87+
88+
desc "Download prebuilt Ruby"
89+
task :download_prebuilt, :tag do |t, args|
90+
require "ruby_wasm/build_system/downloader"
91+
92+
release =
93+
if args[:tag]
94+
url = "https://api.github.com/repos/ruby/ruby.wasm/releases/tags/#{args[:tag]}"
95+
OpenURI.open_uri(url) do |f|
96+
JSON.load(f.read)
97+
end
98+
else
99+
url = "https://api.github.com/repos/ruby/ruby.wasm/releases?per_page=1"
100+
OpenURI.open_uri(url) do |f|
101+
JSON.load(f.read)[0]
102+
end
103+
end
104+
105+
puts "Downloading from release \"#{release["tag_name"]}\""
106+
107+
rubies_dir = "./rubies"
108+
downloader = RubyWasm::Downloader.new
109+
rm_rf rubies_dir
110+
mkdir_p rubies_dir
111+
112+
assets = release["assets"].select { |a| a["name"].end_with? ".tar.gz" }
113+
assets.each_with_index do |asset, i|
114+
url = asset["browser_download_url"]
115+
tarball = File.join("rubies", asset["name"])
116+
rm_rf tarball, verbose: false
117+
downloader.download(url, tarball, "[%2d/%2d] Downloading #{File.basename(url)}" % [i + 1, assets.size])
118+
sh "tar xzf #{tarball} -C ./rubies"
119+
end
120+
end
81121
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module RubyWasm
2+
class Downloader
3+
def format_size(size)
4+
units = %w[B KB MB GB TB]
5+
unit = 0
6+
while size > 1024 and unit < units.size - 1
7+
size /= 1024.0
8+
unit += 1
9+
end
10+
"%s #{units[unit]}" % size.round(2)
11+
end
12+
13+
def download(url, dest, message)
14+
require "open-uri"
15+
content_length = nil
16+
uri = URI.parse(url)
17+
OpenURI.open_uri(
18+
uri,
19+
content_length_proc: ->(len) { content_length = len },
20+
progress_proc: ->(size) do
21+
print "\r#{message} (#{format_size(content_length)}) %.2f%%" %
22+
(size.to_f / content_length * 100)
23+
end
24+
) { |f| File.open(dest, "wb") { |out| out.write f.read } }
25+
puts "\r"
26+
end
27+
end
28+
end

lib/ruby_wasm/build_system/product/crossruby.rb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def do_install_rb(crossruby)
7878
end
7979

8080
class CrossRubyProduct < AutoconfProduct
81-
attr_reader :source, :toolchain, :build, :configure
81+
attr_reader :source, :toolchain, :build, :configure, :install_task
8282
attr_accessor :user_exts,
8383
:wasmoptflags,
8484
:cppflags,
@@ -149,15 +149,17 @@ def define_task
149149
sh "make install DESTDIR=#{install_dir}", chdir: build_dir
150150
end
151151

152-
desc "Build #{name}"
153-
task name => [@configure, install, dest_dir] do
154-
artifact = "rubies/ruby-#{name}.tar.gz"
155-
next if File.exist?(artifact)
156-
rm_rf dest_dir
157-
cp_r install_dir, dest_dir
158-
@user_exts.each { |ext| ext.do_install_rb(self) }
159-
sh "tar cfz #{artifact} -C rubies #{name}"
160-
end
152+
@install_task =
153+
task artifact => [@configure, install, dest_dir] do
154+
rm_rf dest_dir
155+
cp_r install_dir, dest_dir
156+
@user_exts.each { |ext| ext.do_install_rb(self) }
157+
sh "tar cfz #{artifact} -C rubies #{name}"
158+
end
159+
end
160+
161+
def build
162+
@install_task.invoke
161163
end
162164

163165
def name
@@ -184,14 +186,21 @@ def with_zlib(zlib)
184186

185187
def with_wasi_vfs(wasi_vfs)
186188
@wasi_vfs = wasi_vfs
187-
install_task = wasi_vfs.install_task
188-
@dep_tasks << install_task if install_task
189+
wasi_vfs.install_task&.tap { |t| @dep_tasks << t }
189190
end
190191

191192
def dest_dir
192193
File.join(@rubies_dir, name)
193194
end
194195

196+
def artifact
197+
File.join(@rubies_dir, "ruby-#{name}.tar.gz")
198+
end
199+
200+
def built?
201+
File.exist?(artifact)
202+
end
203+
195204
def extinit_obj
196205
"#{ext_build_dir}/extinit.o"
197206
end

lib/ruby_wasm/rake_task.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ def initialize(
7272
@crossruby.with_wasi_vfs @wasi_vfs
7373

7474
@crossruby.define_task
75+
76+
task name do
77+
next if @crossruby.built?
78+
@crossruby.build
79+
end
7580
end
7681

7782
def hexdigest

0 commit comments

Comments
 (0)