-
Notifications
You must be signed in to change notification settings - Fork 55
/
Rakefile
172 lines (153 loc) · 4.28 KB
/
Rakefile
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
require "rake"
require "json"
require "open-uri"
$LOAD_PATH << File.join(File.dirname(__FILE__), "lib")
require "bundler/gem_tasks"
require "ruby_wasm/rake_task"
require "ruby_wasm/packager"
require "ruby_wasm/cli"
BUILD_SOURCES = %w[3.3 3.2 head]
BUILD_PROFILES = %w[full minimal]
BUILDS =
BUILD_SOURCES
.product(BUILD_PROFILES)
.map { |src, profile| [src, "wasm32-unknown-wasip1", profile] } +
BUILD_SOURCES.map { |src| [src, "wasm32-unknown-emscripten", "full"] }
NPM_PACKAGES = [
{
name: "ruby-head-wasm-emscripten",
ruby_version: "head",
gemfile: nil,
target: "wasm32-unknown-emscripten"
},
{
name: "ruby-head-wasm-wasi",
ruby_version: "head",
gemfile: "packages/npm-packages/ruby-head-wasm-wasi/Gemfile",
target: "wasm32-unknown-wasip1",
},
{
name: "ruby-head-wasm-wasip2",
ruby_version: "head",
gemfile: "packages/npm-packages/ruby-head-wasm-wasip2/Gemfile",
target: "wasm32-unknown-wasip2",
enable_component_model: true,
},
{
name: "ruby-3.3-wasm-wasi",
ruby_version: "3.3",
gemfile: "packages/npm-packages/ruby-3.3-wasm-wasi/Gemfile",
target: "wasm32-unknown-wasip1"
},
{
name: "ruby-3.2-wasm-wasi",
ruby_version: "3.2",
gemfile: "packages/npm-packages/ruby-3.2-wasm-wasi/Gemfile",
target: "wasm32-unknown-wasip1"
},
{ name: "ruby-wasm-wasi", target: "wasm32-unknown-wasip1" }
]
STANDALONE_PACKAGES = [
{ name: "ruby", build: "head-wasm32-unknown-wasip1-full" },
{ name: "irb", build: "head-wasm32-unknown-wasip1-full" }
]
LIB_ROOT = File.dirname(__FILE__)
TOOLCHAINS = {}
BUILDS
.map { |_, target, _| target }
.uniq
.each do |target|
build_dir = File.join(LIB_ROOT, "build")
toolchain = RubyWasm::Toolchain.get(target, build_dir)
TOOLCHAINS[toolchain.name] = toolchain
end
class BuildTask < Struct.new(:name, :target, :build_command)
def ruby_cache_key
return @key if @key
require "open3"
env = { "RUBY_WASM_ROOT" => LIB_ROOT }
cmd = build_command + ["--print-ruby-cache-key"]
stdout, status = Open3.capture2(env, *cmd)
unless status.success?
raise "Command failed with status (#{status.exitstatus}): #{cmd.join " "}"
end
require "json"
@key = JSON.parse(stdout)
end
def hexdigest
ruby_cache_key["hexdigest"]
end
def artifact
ruby_cache_key["artifact"]
end
end
namespace :build do
BUILD_TASKS =
BUILDS.map do |src, target, profile|
name = "#{src}-#{target}-#{profile}"
build_command = [
"exe/rbwasm",
"build",
"--ruby-version",
src,
"--target",
target,
"--build-profile",
profile,
"--disable-gems",
"-o",
"/dev/null"
]
desc "Cross-build Ruby for #{target}"
task name do
sh *build_command
end
namespace name do
task :remake do
sh *build_command, "--remake"
end
task :reconfigure do
sh *build_command, "--reconfigure"
end
task :clean do
sh *build_command, "--clean"
end
end
BuildTask.new(name, target, build_command)
end
desc "Clean build directories"
task :clean do
rm_rf "./build"
rm_rf "./rubies"
end
desc "Download prebuilt Ruby"
task :download_prebuilt, :tag do |t, args|
require "ruby_wasm/build/downloader"
release =
if args[:tag]
url =
"https://api.github.com/repos/ruby/ruby.wasm/releases/tags/#{args[:tag]}"
OpenURI.open_uri(url) { |f| JSON.load(f.read) }
else
url = "https://api.github.com/repos/ruby/ruby.wasm/releases?per_page=1"
OpenURI.open_uri(url) { |f| JSON.load(f.read)[0] }
end
puts "Downloading from release \"#{release["tag_name"]}\""
rubies_dir = "./rubies"
downloader = RubyWasm::Downloader.new
rm_rf rubies_dir
mkdir_p rubies_dir
assets = release["assets"].select { |a| a["name"].end_with? ".tar.gz" }
assets.each_with_index do |asset, i|
url = asset["browser_download_url"]
tarball = File.join("rubies", asset["name"])
rm_rf tarball, verbose: false
downloader.download(
url,
tarball,
"[%2d/%2d] Downloading #{File.basename(url)}" % [i + 1, assets.size]
)
sh "tar xzf #{tarball} -C ./rubies"
end
end
end