-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathpackaging.rake
More file actions
227 lines (202 loc) · 7.27 KB
/
packaging.rake
File metadata and controls
227 lines (202 loc) · 7.27 KB
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
221
222
223
224
225
226
227
wasi_vfs = RubyWasm::WasiVfsProduct.new(File.join(Dir.pwd, "build"))
binaryen = RubyWasm::Binaryen.new(build_dir: File.join(Dir.pwd, "build"))
def exe_rbwasm = File.expand_path(File.join(__dir__, "..", "exe", "rbwasm"))
tools = {
"WASI_VFS_CLI" => exe_rbwasm,
"WASMOPT" => binaryen.wasm_opt
}
def npm_pkg_build_command(pkg)
# Skip if the package does not require building ruby
return nil unless pkg[:ruby_version] && pkg[:target]
[
exe_rbwasm,
"build",
"--ruby-version",
pkg[:ruby_version],
"--target",
pkg[:target],
"--build-profile",
"full"
]
end
def npm_pkg_rubies_cache_key(pkg)
vendor_gem_cache(pkg)
build_command = npm_pkg_build_command(pkg)
return nil unless build_command
require "open3"
cmd = build_command + ["--print-ruby-cache-key"]
chdir = pkg[:gemfile] ? File.dirname(pkg[:gemfile]) : Dir.pwd
env = { "RUBY_WASM_ROOT" => LIB_ROOT }
stdout, status = Open3.capture2(env, *cmd, chdir: chdir)
unless status.success?
raise "Command failed with status (#{status.exitstatus}): #{cmd.join " "}"
end
require "json"
JSON.parse(stdout)["hexdigest"]
end
def vendor_gem_cache(pkg)
return unless pkg[:gemfile]
pkg_dir = File.dirname(pkg[:gemfile])
pkg_dir = File.expand_path(pkg_dir)
vendor_cache_dir = File.join(pkg_dir, "vendor", "cache")
mkdir_p vendor_cache_dir
require_relative "../packages/gems/js/lib/js/version"
sh "gem", "-C", "packages/gems/js", "build", "-o",
File.join(vendor_cache_dir, "js-#{JS::VERSION}.gem")
JS::VERSION
end
def build_ruby(pkg, base_dir, pkg_dir, binaryen, clean: false)
build_command = npm_pkg_build_command(pkg)
# Skip if the package does not require building ruby
return unless build_command
js_gem_version = vendor_gem_cache(pkg)
env = {
# Share ./build and ./rubies in the same workspace
"RUBY_WASM_ROOT" => base_dir
}
# We vendor a freshly built js gem in package workspaces. Bundler checksum
# validation against rubygems.org can fail for the same version.
env["BUNDLE_DISABLE_CHECKSUM_VALIDATION"] = "true" if pkg[:gemfile]
cwd = base_dir
if gemfile_path = pkg[:gemfile]
cwd = File.dirname(gemfile_path)
else
# Explicitly disable rubygems integration since Bundler finds
# Gemfile in the repo root directory.
build_command.push "--disable-gems"
end
dist_dir = File.join(pkg_dir, "dist")
mkdir_p dist_dir
if pkg[:target].start_with?("wasm32-unknown-wasi")
if pkg[:enable_component_model]
component_path = File.join(pkg_dir, "tmp", "ruby.component.wasm")
FileUtils.mkdir_p(File.dirname(component_path))
# Remove js gem from the ./bundle directory to force Bundler to re-install it
rm_rf FileList[File.join(pkg_dir, "bundle", "**", "js-#{js_gem_version}")]
Dir.chdir(cwd) do
sh env.merge("RUBY_WASM_EXPERIMENTAL_DYNAMIC_LINKING" => "1"),
*build_command, *(clean ? ["--clean"] : []),
"-o", component_path
end
sh "npx", "jco", "transpile",
"--no-wasi-shim", "--instantiation", "--valid-lifting-optimization",
component_path, "-o", File.join(dist_dir, "component")
# ./component/package.json is required to be an ES module
File.write(File.join(dist_dir, "component", "package.json"), '{ "type": "module" }')
else
Dir.chdir(cwd) do
# uninstall js gem to re-install just-built js gem
sh "gem", "uninstall", "js", "-v", js_gem_version, "--force"
# install gems including js gem. Bundler may compare checksums with
# rubygems.org for the same version and reject our just-built local gem.
# Disable checksum validation only for this install command.
sh({ "BUNDLE_DISABLE_CHECKSUM_VALIDATION" => "true" }, "bundle", "install")
sh env,
"bundle", "exec",
*build_command,
"--no-stdlib", "--remake",
*(clean ? ["--clean"] : []),
"-o",
File.join(dist_dir, "ruby.wasm")
sh env,
"bundle", "exec",
*build_command,
"-o",
File.join(dist_dir, "ruby.debug+stdlib.wasm")
end
sh binaryen.wasm_opt,
"--strip-debug",
File.join(dist_dir, "ruby.wasm"),
"-o",
File.join(dist_dir, "ruby.wasm")
sh binaryen.wasm_opt,
"--strip-debug",
File.join(dist_dir, "ruby.debug+stdlib.wasm"),
"-o",
File.join(dist_dir, "ruby+stdlib.wasm")
end
elsif pkg[:target] == "wasm32-unknown-emscripten"
Dir.chdir(cwd || base_dir) do
sh env, *build_command, "-o", "/dev/null"
end
end
end
namespace :npm do
NPM_PACKAGES.each do |pkg|
base_dir = Dir.pwd
pkg_dir = "#{Dir.pwd}/packages/npm-packages/#{pkg[:name]}"
namespace pkg[:name] do
desc "Build ruby for npm package #{pkg[:name]}"
task "ruby" do
build_ruby(pkg, base_dir, pkg_dir, binaryen)
end
desc "Build npm package #{pkg[:name]}"
task "build" => ["ruby"] do
sh tools, "npm run build", chdir: pkg_dir
end
desc "Clean and build npm package #{pkg[:name]}"
task "clean-build" do
build_ruby(pkg, base_dir, pkg_dir, binaryen, clean: true)
end
desc "Check npm package #{pkg[:name]}"
task "check" do
sh "npm test", chdir: pkg_dir
end
end
desc "Make tarball for npm package #{pkg[:name]}"
task pkg[:name] do
executor = RubyWasm::BuildExecutor.new
binaryen.install(executor)
Rake::Task["npm:#{pkg[:name]}:build"].invoke
sh "npm pack", chdir: pkg_dir
end
end
desc "Configure for pre-release"
task :configure_prerelease, [:prerel] do |t, args|
require "json"
prerel = args[:prerel]
new_pkgs = {}
NPM_PACKAGES.each do |pkg|
pkg_dir = "#{Dir.pwd}/packages/npm-packages/#{pkg[:name]}"
pkg_json = "#{pkg_dir}/package.json"
package = JSON.parse(File.read(pkg_json))
version = package["version"] + "-#{prerel}"
new_pkgs[package["name"]] = version
sh *["npm", "pkg", "set", "version=#{version}"], chdir: pkg_dir
end
NPM_PACKAGES.each do |pkg|
pkg_dir = "#{Dir.pwd}/packages/npm-packages/#{pkg[:name]}"
pkg_json = "#{pkg_dir}/package.json"
package = JSON.parse(File.read(pkg_json))
(package["dependencies"] || []).each do |dep, _|
next unless new_pkgs[dep]
sh *["npm", "pkg", "set", "dependencies.#{dep}=#{new_pkgs[dep]}"],
chdir: pkg_dir
end
end
end
desc "Build all npm packages"
multitask all: NPM_PACKAGES.map { |pkg| pkg[:name] }
end
namespace :standalone do
STANDALONE_PACKAGES.each do |pkg|
pkg_dir = "#{Dir.pwd}/packages/standalone/#{pkg[:name]}"
desc "Build standalone package #{pkg[:name]}"
task "#{pkg[:name]}" => ["build:#{pkg[:build]}"] do
executor = RubyWasm::SilentExecutor.new
binaryen.install(executor)
base_dir = Dir.pwd
sh tools,
"./build-package.sh #{base_dir}/rubies/ruby-#{pkg[:build]}",
chdir: pkg_dir
end
end
end
namespace :gem do
task :update_component_adapters do
["command", "reactor"].each do |exec_model|
sh "curl", "-L", "-o", "lib/ruby_wasm/packager/component_adapter/wasi_snapshot_preview1.#{exec_model}.wasm",
"https://github.com/bytecodealliance/wasmtime/releases/download/v19.0.1/wasi_snapshot_preview1.#{exec_model}.wasm"
end
end
end