Skip to content

Commit ece373f

Browse files
Merge pull request #361 from ruby/katei/remove-wasi-vfs-cli-dep
Introduce `rbwasm pack` command as an alias of `wasi-vfs pack`
2 parents 9abee2c + 4864614 commit ece373f

File tree

12 files changed

+43
-52
lines changed

12 files changed

+43
-52
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmarks/vm_deep_call.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#
33
# Example runs
44
# $ ruby vm_deep_call.rb
5-
# $ RUBY_EXE="wasmtime run --mapdir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
6-
# $ RUBY_EXE="wasmtime run --env RUBY_FIBER_MACHINE_STACK_SIZE=20971520 --mapdir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
5+
# $ RUBY_EXE="wasmtime run --dir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
6+
# $ RUBY_EXE="wasmtime run --env RUBY_FIBER_MACHINE_STACK_SIZE=20971520 --dir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
77

88
def vm_rec n
99
vm_rec n - 1 if n > 0

ext/ruby_wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ wizer = "3.0.0"
1515
wasmtime-wasi = "9.0.4"
1616
wasi-cap-std-sync = "9.0.4"
1717
wasi-vfs-cli = { git = "https://github.com/kateinoigakukun/wasi-vfs/", rev = "b1e4e5d9cd6322e8745e67c092b495973835a94f" }
18+
structopt = "0.3.26"

ext/ruby_wasm/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use magnus::{
77
wrap, Error, ExceptionClass, RModule, Ruby,
88
};
99
use wizer::Wizer;
10+
use structopt::StructOpt;
1011

1112
static RUBY_WASM: value::Lazy<RModule> =
1213
value::Lazy::new(|ruby| ruby.define_module("RubyWasmExt").unwrap());
@@ -35,6 +36,15 @@ struct WasiVfsInner {
3536
struct WasiVfs(std::cell::RefCell<WasiVfsInner>);
3637

3738
impl WasiVfs {
39+
fn run_cli(args: Vec<String>) -> Result<(), Error> {
40+
wasi_vfs_cli::App::from_iter(args).execute().map_err(|e| {
41+
Error::new(
42+
exception::standard_error(),
43+
format!("failed to run wasi vfs cli: {}", e),
44+
)
45+
})
46+
}
47+
3848
fn new() -> Self {
3949
Self(std::cell::RefCell::new(WasiVfsInner { map_dirs: vec![] }))
4050
}
@@ -63,6 +73,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
6373

6474
let wasi_vfs = module.define_class("WasiVfs", ruby.class_object())?;
6575
wasi_vfs.define_singleton_method("new", function!(WasiVfs::new, 0))?;
76+
wasi_vfs.define_singleton_method("run_cli", function!(WasiVfs::run_cli, 1))?;
6677
wasi_vfs.define_method("map_dir", method!(WasiVfs::map_dir, 2))?;
6778
wasi_vfs.define_method("pack", method!(WasiVfs::pack, 1))?;
6879
Ok(())

lib/ruby_wasm/build/product/wasi_vfs.rb

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ class WasiVfsProduct < BuildProduct
77
def initialize(build_dir)
88
@build_dir = build_dir
99
@need_fetch_lib = ENV["LIB_WASI_VFS_A"].nil?
10-
installed_cli_path =
11-
ENV["WASI_VFS_CLI"] || Toolchain.find_path("wasi-vfs")
12-
@need_fetch_cli = installed_cli_path.nil?
13-
@cli_path =
14-
installed_cli_path || File.join(cli_product_build_dir, "wasi-vfs")
1510
end
1611

1712
def lib_product_build_dir
@@ -26,18 +21,6 @@ def lib_wasi_vfs_a
2621
ENV["LIB_WASI_VFS_A"] || File.join(lib_product_build_dir, "libwasi_vfs.a")
2722
end
2823

29-
def cli_product_build_dir
30-
File.join(
31-
@build_dir,
32-
RbConfig::CONFIG["host"],
33-
"wasi-vfs-#{WASI_VFS_VERSION}"
34-
)
35-
end
36-
37-
def cli_bin_path
38-
@cli_path
39-
end
40-
4124
def name
4225
"wasi-vfs-#{WASI_VFS_VERSION}-#{RbConfig::CONFIG["host"]}"
4326
end
@@ -58,26 +41,5 @@ def build(executor)
5841
executor.mv File.join(tmpdir, "libwasi_vfs.a"), lib_wasi_vfs_a
5942
end
6043
end
61-
62-
def install_cli
63-
return if !@need_fetch_cli || File.exist?(cli_bin_path)
64-
FileUtils.mkdir_p cli_product_build_dir
65-
zipfile = File.join(cli_product_build_dir, "wasi-vfs-cli.zip")
66-
system "curl", "-L", "-o", zipfile, self.cli_download_url
67-
system "unzip", zipfile, "-d", cli_product_build_dir
68-
end
69-
70-
def cli_download_url
71-
assets = [
72-
[/x86_64-linux/, "wasi-vfs-cli-x86_64-unknown-linux-gnu.zip"],
73-
[/x86_64-darwin/, "wasi-vfs-cli-x86_64-apple-darwin.zip"],
74-
[/arm64e?-darwin/, "wasi-vfs-cli-aarch64-apple-darwin.zip"]
75-
]
76-
asset = assets.find { |os, _| os =~ RUBY_PLATFORM }&.at(1)
77-
if asset.nil?
78-
raise "unsupported platform for fetching wasi-vfs CLI: #{RUBY_PLATFORM}"
79-
end
80-
"https://github.com/kateinoigakukun/wasi-vfs/releases/download/v#{WASI_VFS_VERSION}/#{asset}"
81-
end
8244
end
8345
end

lib/ruby_wasm/cli.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(stdout:, stderr:)
99
end
1010

1111
def run(args)
12-
available_commands = %w[build]
12+
available_commands = %w[build pack]
1313
parser =
1414
OptionParser.new do |opts|
1515
opts.banner = <<~USAGE
@@ -32,6 +32,8 @@ def run(args)
3232
case command
3333
when "build"
3434
build(args)
35+
when "pack"
36+
pack(args)
3537
else
3638
@stderr.puts parser
3739
exit
@@ -141,6 +143,11 @@ def build(args)
141143
end
142144
end
143145

146+
def pack(args)
147+
self.require_extension
148+
RubyWasmExt::WasiVfs.run_cli([$0, "pack", *args])
149+
end
150+
144151
private
145152

146153
def build_config(options)
@@ -180,7 +187,7 @@ def do_print_ruby_cache_key(packager)
180187
end
181188

182189
def do_build(executor, tmpdir, packager, options)
183-
require_relative "ruby_wasm.so"
190+
self.require_extension
184191
wasm_bytes = packager.package(executor, tmpdir, options)
185192
RubyWasm.logger.info "Size: #{SizeFormatter.format(wasm_bytes.size)}"
186193
case options[:output]
@@ -191,5 +198,15 @@ def do_build(executor, tmpdir, packager, options)
191198
RubyWasm.logger.debug "Wrote #{options[:output]}"
192199
end
193200
end
201+
202+
def require_extension
203+
# Tries to require the extension for the given Ruby version first
204+
begin
205+
RUBY_VERSION =~ /(\d+\.\d+)/
206+
require_relative "#{Regexp.last_match(1)}/ruby_wasm.so"
207+
rescue LoadError
208+
require_relative "ruby_wasm.so"
209+
end
210+
end
194211
end
195212
end

packages/standalone/irb/build-package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ cp -R "$ruby_root" "$workdir/ruby-root"
2222
(
2323
cd "$workdir" && \
2424
"$WASMOPT" --strip-debug ruby-root/usr/local/bin/ruby -o ./ruby-root/ruby.wasm && \
25-
"$WASI_VFS_CLI" pack ./ruby-root/ruby.wasm --mapdir /usr::./ruby-root/usr --mapdir /gems::$package_dir/gems -o "$dist_dir/irb.wasm" && \
25+
"$WASI_VFS_CLI" pack ./ruby-root/ruby.wasm --dir ./ruby-root/usr::/usr --dir $package_dir/gems::/gems -o "$dist_dir/irb.wasm" && \
2626
wasi-preset-args "$dist_dir/irb.wasm" -o "$dist_dir/irb.wasm" -- -I/gems/lib /gems/libexec/irb --prompt default
2727
)

packages/standalone/ruby/build-package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ cp -R "$ruby_root" "$workdir/ruby-root"
2222
(
2323
cd "$workdir" && \
2424
"$WASMOPT" --strip-debug ruby-root/usr/local/bin/ruby -o ./ruby-root/ruby.wasm && \
25-
"$WASI_VFS_CLI" pack ./ruby-root/ruby.wasm --mapdir /usr::./ruby-root/usr -o "$dist_dir/ruby.wasm"
25+
"$WASI_VFS_CLI" pack ./ruby-root/ruby.wasm --dir ./ruby-root/usr::/usr -o "$dist_dir/ruby.wasm"
2626
)

rakelib/packaging.rake

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
wasi_vfs = RubyWasm::WasiVfsProduct.new(File.join(Dir.pwd, "build"))
22
wasi_sdk = TOOLCHAINS["wasi-sdk"]
33
tools = {
4-
"WASI_VFS_CLI" => wasi_vfs.cli_bin_path,
4+
"WASI_VFS_CLI" => File.expand_path(File.join(__dir__, "..", "exe", "rbwasm")),
55
"WASMOPT" => wasi_sdk.wasm_opt
66
}
77

@@ -93,7 +93,6 @@ namespace :npm do
9393

9494
desc "Make tarball for npm package #{pkg[:name]}"
9595
task pkg[:name] do
96-
wasi_vfs.install_cli
9796
wasi_sdk.install_binaryen
9897
Rake::Task["npm:#{pkg[:name]}:build"].invoke
9998
sh "npm pack", chdir: pkg_dir
@@ -137,7 +136,6 @@ namespace :standalone do
137136

138137
desc "Build standalone package #{pkg[:name]}"
139138
task "#{pkg[:name]}" => ["build:#{pkg[:build]}"] do
140-
wasi_vfs.install_cli
141139
wasi_sdk.install_binaryen
142140
base_dir = Dir.pwd
143141
sh tools,

sig/ruby_wasm/build.rbs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,8 @@ module RubyWasm
135135
def initialize: (String build_dir) -> void
136136
def lib_product_build_dir: -> String
137137
def lib_wasi_vfs_a: -> String
138-
def cli_product_build_dir: -> String
139-
def cli_bin_path: -> String
140138
def name: -> String
141139
def build: (BuildExecutor executor) -> void
142-
def install_cli: -> bool?
143-
def cli_download_url: -> String
144140
end
145141

146142
class CrossRubyExtProduct < BuildProduct

0 commit comments

Comments
 (0)