Skip to content

Commit

Permalink
Merge pull request #308 from ruby/pr-83d6a25e8fc9b4178f125b1878fecc2f…
Browse files Browse the repository at this point in the history
…b96d1117

Introduce rbs-based type check
  • Loading branch information
kateinoigakukun authored Nov 15, 2023
2 parents 3e57dde + 26b798a commit d5c7482
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 19 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ group :development do
gem "rake"
gem "syntax_tree", "~> 3.5"
gem "webrick"
gem "steep"
end
17 changes: 17 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
D = Steep::Diagnostic

target :lib do
signature "sig"

check "lib"
# RBS's stdlib signatures don't have rake signatures yet.
ignore "lib/ruby_wasm/rake_task.rb"

library "digest"
library "tmpdir"
library "fileutils"
library "open-uri"
library "uri"

configure_code_diagnostics(D::Ruby.default)
end
2 changes: 1 addition & 1 deletion lib/ruby_wasm/build/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def format_size(size)

def download(url, dest, message)
require "open-uri"
content_length = nil
content_length = 0
uri = URI.parse(url)
OpenURI.open_uri(
uri,
Expand Down
7 changes: 5 additions & 2 deletions lib/ruby_wasm/build/product/crossruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ def configure_args(build_triple, toolchain)
case target
when "wasm32-unknown-wasi"
xldflags << @wasi_vfs.lib_wasi_vfs_a if @wasi_vfs
args << %Q(WASMOPT=#{@toolchain.wasm_opt})
args << %Q(WASI_SDK_PATH=#{@toolchain.wasi_sdk_path})
# TODO: Find a way to force cast or update API
# @type var wasi_sdk_path: untyped
wasi_sdk_path = @toolchain
args << %Q(WASMOPT=#{wasi_sdk_path.wasm_opt})
args << %Q(WASI_SDK_PATH=#{wasi_sdk_path.wasi_sdk_path})
when "wasm32-unknown-emscripten"
ldflags.concat(%w[-s MODULARIZE=1])
else
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_wasm/build/product/openssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module RubyWasm
class OpenSSLProduct < AutoconfProduct
attr_reader :target, :install_task
attr_reader :target

OPENSSL_VERSION = "3.0.5"

Expand Down
8 changes: 5 additions & 3 deletions lib/ruby_wasm/build/product/wasi_vfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ class WasiVfsProduct < BuildProduct
def initialize(build_dir)
@build_dir = build_dir
@need_fetch_lib = ENV["LIB_WASI_VFS_A"].nil?
@cli_path = ENV["WASI_VFS_CLI"] || Toolchain.find_path("wasi-vfs")
@need_fetch_cli = @cli_path.nil?
@cli_path ||= File.join(cli_product_build_dir, "wasi-vfs")
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

def lib_product_build_dir
Expand Down
24 changes: 12 additions & 12 deletions lib/ruby_wasm/build/toolchain/wit_bindgen.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module RubyWasm
class WitBindgen < ::Rake::TaskLib
class WitBindgen
attr_reader :bin_path

def initialize(
Expand All @@ -15,17 +15,17 @@ def initialize(
def install
return if File.exist?(@bin_path)
RubyWasm::Toolchain.check_executable("cargo")
system *[
"cargo",
"install",
"--git",
"https://github.com/bytecodealliance/wit-bindgen",
"--rev",
@revision,
"--root",
@tool_dir,
"wit-bindgen-cli"
]
Kernel.system(
"cargo",
"install",
"--git",
"https://github.com/bytecodealliance/wit-bindgen",
"--rev",
@revision,
"--root",
@tool_dir,
"wit-bindgen-cli"
)
end
end
end
4 changes: 4 additions & 0 deletions sig/open_uri.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TODO: Upstream OpenURI sigs to rbs/stdlib
module OpenURI
def self.open_uri: [T] (*untyped, **untyped) { (untyped) -> T } -> T
end
257 changes: 257 additions & 0 deletions sig/ruby_wasm/build.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
module RubyWasm
VERSION: String

class BuildParams
attr_accessor name: String
attr_accessor target: String
attr_accessor default_exts: String
end

class BuildProduct
def name: -> String
end

class AutoconfProduct < BuildProduct
@target: String
@toolchain: Toolchain

def initialize: (String target, Toolchain toolchain) -> void
def system_triplet_args: -> Array[String]
| -> Array[String]
def tools_args: -> Array[String]
| -> Array[String]
def configure_args: -> Array[String]
| -> Array[String]
end

class BuildSource < BuildProduct
@params: Hash[untyped, untyped]
@build_dir: String

def initialize: (untyped params, String build_dir) -> void
def name: -> String
def cache_key: (Digest::SHA256 digest) -> void
def src_dir: -> String
def configure_file: -> String
def fetch: (BuildExecutor executor) -> void
def build: (BuildExecutor executor) -> void
end

class BaseRubyProduct < BuildProduct
@build_dir: String
@source: BuildSource
@channel: String

def initialize: (String build_dir, BuildSource source) -> void
def product_build_dir: -> String
def install_dir: -> String
def name: -> String
def build: (BuildExecutor executor) -> void
end

class ZlibProduct < AutoconfProduct
ZLIB_VERSION: String
@build_dir: String

attr_reader target: String
def initialize: (String build_dir, String target, Toolchain toolchain) -> void
def product_build_dir: -> String
def destdir: -> String
def install_root: -> String
def name: -> String
def configure_args: -> Array[String]
def build: (BuildExecutor executor) -> void
end

class LibYAMLProduct < AutoconfProduct
LIBYAML_VERSION: String
@build_dir: String

attr_reader target: String
def initialize: (String build_dir, String target, Toolchain toolchain) -> void
def product_build_dir: -> String
def destdir: -> String
def install_root: -> String
def name: -> String
def build: (BuildExecutor executor) -> void
end

class OpenSSLProduct < AutoconfProduct
OPENSSL_VERSION: String
@build_dir: String

attr_reader target: String
def initialize: (String build_dir, String target, Toolchain toolchain) -> void
def product_build_dir: -> String
def destdir: -> String
def install_root: -> String
def name: -> String
def configure_args: -> Array[String]
def build: (BuildExecutor executor) -> void
end

class WasiVfsProduct < BuildProduct
WASI_VFS_VERSION: String
@build_dir: String
@need_fetch_lib: bool
@cli_path: String
@need_fetch_cli: bool

def initialize: (String build_dir) -> void
def lib_product_build_dir: -> String
def lib_wasi_vfs_a: -> String
def cli_product_build_dir: -> String
def cli_bin_path: -> String
def name: -> String
def build: (BuildExecutor executor) -> void
def install_cli: -> bool?
def cli_download_url: -> String
end

class CrossRubyExtProduct < BuildProduct
@toolchain: Toolchain
@srcdir: String

attr_reader name: String
def initialize: (String srcdir, Toolchain toolchain, ?name: nil) -> void
def product_build_dir: (CrossRubyProduct crossruby) -> String
def linklist: (CrossRubyProduct crossruby) -> String
def make_args: (CrossRubyProduct crossruby) -> Array[String]
def build: (BuildExecutor executor, CrossRubyProduct crossruby) -> void
def do_extconf: (BuildExecutor executor, CrossRubyProduct crossruby) -> void
def do_install_rb: (BuildExecutor executor, CrossRubyProduct crossruby) -> void
def cache_key: (Digest::SHA256 digest) -> void
end

class CrossRubyProduct < AutoconfProduct
@params: BuildParams
@rubies_dir: String
@build_dir: String
@baseruby: BaseRubyProduct
@libyaml: LibYAMLProduct
@zlib: ZlibProduct
@openssl: OpenSSLProduct
@wasi_vfs: WasiVfsProduct

attr_reader source: BuildSource
attr_reader toolchain: Toolchain
attr_accessor user_exts: Array[CrossRubyExtProduct]
attr_accessor wasmoptflags: Array[String]
attr_accessor cppflags: Array[String]
attr_accessor cflags: Array[String]
attr_accessor ldflags: Array[String]
attr_accessor debugflags: Array[String]
attr_accessor xcflags: Array[String]
attr_accessor xldflags: Array[String]
def initialize: (BuildParams params, String build_dir, String rubies_dir, BaseRubyProduct baseruby, BuildSource source, Toolchain toolchain, ?user_exts: Array[CrossRubyExtProduct]) -> void
def configure: (BuildExecutor executor, ?reconfigure: bool) -> void
def build_exts: (BuildExecutor executor) -> bool?
def build: (BuildExecutor executor, ?remake: bool, ?reconfigure: bool) -> bool?
def clean: (BuildExecutor executor) -> void
def name: -> String
def cache_key: (Digest::SHA256 digest) -> void
def build_dir: -> String
def ext_build_dir: -> String
def with_libyaml: (LibYAMLProduct libyaml) -> LibYAMLProduct
def with_zlib: (ZlibProduct zlib) -> ZlibProduct
def with_wasi_vfs: (WasiVfsProduct wasi_vfs) -> WasiVfsProduct
def with_openssl: (OpenSSLProduct openssl) -> OpenSSLProduct
def dest_dir: -> String
def artifact: -> String
def extinit_obj: -> String
def extinit_c_erb: -> String
def baseruby_path: -> String
def configure_args: (String build_triple, Toolchain toolchain) -> Array[String]
end

class WitBindgen
@build_dir: String
@tool_dir: String
@revision: String

attr_reader bin_path: String
def initialize: (build_dir: String, ?revision: String) -> void
def install: -> void
end

class Toolchain
@tools: Hash[untyped, untyped]
@tools_cache: Hash[untyped, untyped]

attr_reader name: String
def initialize: -> void
def find_tool: (Symbol name) -> bot
def check_envvar: (untyped name) -> nil
def self.get: (String target, ?String? build_dir) -> (Toolchain)
def self.find_path: (String command) -> String?
def self.check_executable: (String command) -> String
def cc: -> nil
def ranlib: -> nil
def ld: -> nil
def ar: -> nil

def install: -> void
end

class WASISDK < Toolchain
@wasm_opt_path: String
@need_fetch_wasi_sdk: bool
@need_fetch_binaryen: bool
@tools: Hash[Symbol, String]
@wasi_sdk_path: String
@binaryen_version: Integer
@version_major: Integer
@version_minor: Integer
@binaryen_path: String

def initialize: (?String? wasi_sdk_path, ?build_dir: String?, ?version_major: Integer, ?version_minor: Integer, ?binaryen_version: Integer) -> void
def find_tool: (Symbol name) -> String
def wasm_opt: -> String
def wasi_sdk_path: -> String
def download_url: (Integer? version_major, Integer? version_minor) -> String
def binaryen_download_url: (Integer? version) -> String
def install_wasi_sdk: -> void
def install_binaryen: -> void
end

class Emscripten < Toolchain
@tools: Hash[Symbol, String]

def initialize: -> void
def find_tool: (Symbol name) -> String
end

class BuildExecutor
def system: (*untyped, **untyped) -> bool?
def rm_rf: (FileUtils::pathlist list) -> void
def rm_f: (FileUtils::pathlist list) -> void
def cp_r: (FileUtils::pathlist src, path dest) -> void
def mv: (FileUtils::pathlist src, path dest) -> void
def mkdir_p: (FileUtils::pathlist list) -> void
def write: (String path, _ToS data) -> void
end

class Downloader
def format_size: (Integer size) -> String

def download: (String url, String dest, String message) -> void
end

class BuildTask
@build_dir: String
@rubies_dir: String
@openssl: OpenSSLProduct

attr_accessor name: String
attr_reader source: BuildSource
attr_reader target: String
attr_reader toolchain: Toolchain
attr_reader libyaml: LibYAMLProduct
attr_reader zlib: ZlibProduct
attr_reader wasi_vfs: WasiVfsProduct
attr_reader baseruby: BaseRubyProduct
attr_reader crossruby: CrossRubyProduct
def initialize: (String name, target: String, src: untyped, ?toolchain: Toolchain?, ?build_dir: String?, ?rubies_dir: String?, **untyped) -> void
def hexdigest: -> String
end
end

0 comments on commit d5c7482

Please sign in to comment.