Class: RubyWasm::WasiVfsProduct

Inherits:
BuildProduct show all
Defined in:
lib/ruby_wasm/build/product/wasi_vfs.rb

Constant Summary collapse

WASI_VFS_VERSION =
"0.5.0"

Instance Method Summary collapse

Constructor Details

#initialize(build_dir) ⇒ WasiVfsProduct

Returns a new instance of WasiVfsProduct.



7
8
9
10
11
12
13
14
15
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 7

def initialize(build_dir)
  @build_dir = build_dir
  @need_fetch_lib = ENV["LIB_WASI_VFS_A"].nil?
  installed_cli_path =
    ENV["WASI_VFS_CLI"] || Toolchain.find_path("wasi-vfs")
  @need_fetch_cli = installed_cli_path.nil?
  @cli_path =
    installed_cli_path || File.join(cli_product_build_dir, "wasi-vfs")
end

Instance Method Details

#build(executor) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 45

def build(executor)
  return if !@need_fetch_lib || File.exist?(lib_wasi_vfs_a)
  require "tmpdir"
  lib_wasi_vfs_url =
    "https://github.com/kateinoigakukun/wasi-vfs/releases/download/v#{WASI_VFS_VERSION}/libwasi_vfs-wasm32-unknown-unknown.zip"
  Dir.mktmpdir do |tmpdir|
    executor.system "curl",
                    "-L",
                    lib_wasi_vfs_url,
                    "-o",
                    "#{tmpdir}/libwasi_vfs.zip"
    executor.system "unzip", "#{tmpdir}/libwasi_vfs.zip", "-d", tmpdir
    executor.mkdir_p File.dirname(lib_wasi_vfs_a)
    executor.mv File.join(tmpdir, "libwasi_vfs.a"), lib_wasi_vfs_a
  end
end

#cli_bin_pathObject



37
38
39
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 37

def cli_bin_path
  @cli_path
end

#cli_download_urlObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 70

def cli_download_url
  assets = [
    [/x86_64-linux/, "wasi-vfs-cli-x86_64-unknown-linux-gnu.zip"],
    [/x86_64-darwin/, "wasi-vfs-cli-x86_64-apple-darwin.zip"],
    [/arm64e?-darwin/, "wasi-vfs-cli-aarch64-apple-darwin.zip"]
  ]
  asset = assets.find { |os, _| os =~ RUBY_PLATFORM }&.at(1)
  if asset.nil?
    raise "unsupported platform for fetching wasi-vfs CLI: #{RUBY_PLATFORM}"
  end
  "https://github.com/kateinoigakukun/wasi-vfs/releases/download/v#{WASI_VFS_VERSION}/#{asset}"
end

#cli_product_build_dirObject



29
30
31
32
33
34
35
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 29

def cli_product_build_dir
  File.join(
    @build_dir,
    RbConfig::CONFIG["host"],
    "wasi-vfs-#{WASI_VFS_VERSION}"
  )
end

#install_cliObject



62
63
64
65
66
67
68
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 62

def install_cli
  return if !@need_fetch_cli || File.exist?(cli_bin_path)
  FileUtils.mkdir_p cli_product_build_dir
  zipfile = File.join(cli_product_build_dir, "wasi-vfs-cli.zip")
  system "curl", "-L", "-o", zipfile, self.cli_download_url
  system "unzip", zipfile, "-d", cli_product_build_dir
end

#lib_product_build_dirObject



17
18
19
20
21
22
23
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 17

def lib_product_build_dir
  File.join(
    @build_dir,
    "wasm32-unknown-wasi",
    "wasi-vfs-#{WASI_VFS_VERSION}"
  )
end

#lib_wasi_vfs_aObject



25
26
27
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 25

def lib_wasi_vfs_a
  ENV["LIB_WASI_VFS_A"] || File.join(lib_product_build_dir, "libwasi_vfs.a")
end

#nameObject



41
42
43
# File 'lib/ruby_wasm/build/product/wasi_vfs.rb', line 41

def name
  "wasi-vfs-#{WASI_VFS_VERSION}-#{RbConfig::CONFIG["host"]}"
end