Skip to content

Commit 3d12684

Browse files
build: Move source alias and build source computation to CLI
1 parent cb7382e commit 3d12684

File tree

4 files changed

+107
-87
lines changed

4 files changed

+107
-87
lines changed

lib/ruby_wasm/cli.rb

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def run(args)
4242
end
4343

4444
def build(args)
45-
# @type var options: Hash[Symbol, untyped]
45+
# @type var options: cli_options
4646
options = {
4747
save_temps: false,
4848
optimize: false,
@@ -152,7 +152,8 @@ def pack(args)
152152
private
153153

154154
def build_config(options)
155-
config = { target: options[:target_triplet], src: options[:ruby_version] }
155+
# @type var config: Packager::build_config
156+
config = { target: options[:target_triplet], src: compute_build_source(options) }
156157
case options[:profile]
157158
when "full"
158159
config[:default_exts] = RubyWasm::Packager::ALL_DEFAULT_EXTS
@@ -170,6 +171,71 @@ def build_config(options)
170171
config
171172
end
172173

174+
def compute_build_source(options)
175+
src_name = options[:ruby_version]
176+
aliases = self.class.build_source_aliases(root)
177+
aliases[src_name] ||
178+
raise(
179+
"Unknown Ruby source: #{src_name} (available: #{aliases.keys.join(", ")})"
180+
)
181+
end
182+
183+
# Retrieves the alias definitions for the Ruby sources.
184+
def self.build_source_aliases(root)
185+
# @type var sources: Hash[string, RubyWasm::Packager::build_source]
186+
sources = {
187+
"head" => {
188+
type: "github",
189+
repo: "ruby/ruby",
190+
rev: "master"
191+
},
192+
"3.3" => {
193+
type: "tarball",
194+
url: "https://cache.ruby-lang.org/pub/ruby/3.3/ruby-3.3.0.tar.gz"
195+
},
196+
"3.2" => {
197+
type: "tarball",
198+
url: "https://cache.ruby-lang.org/pub/ruby/3.2/ruby-3.2.3.tar.gz"
199+
}
200+
}
201+
sources.each do |name, source|
202+
source[:name] = name
203+
patches = Dir[File.join(root, "patches", name, "*.patch")]
204+
.map { |p| File.expand_path(p) }
205+
source[:patches] = patches
206+
end
207+
208+
build_manifest = File.join(root, "build_manifest.json")
209+
if File.exist?(build_manifest)
210+
begin
211+
manifest = JSON.parse(File.read(build_manifest))
212+
manifest["ruby_revisions"].each do |name, rev|
213+
sources[name][:rev] = rev
214+
end
215+
rescue StandardError => e
216+
RubyWasm.logger.warn "Failed to load build_manifest.json: #{e}"
217+
end
218+
end
219+
sources
220+
end
221+
222+
# Retrieves the root directory of the Ruby project.
223+
def root
224+
__skip__ =
225+
@root ||=
226+
begin
227+
if explicit = ENV["RUBY_WASM_ROOT"]
228+
File.expand_path(explicit)
229+
elsif defined?(Bundler)
230+
Bundler.root
231+
else
232+
Dir.pwd
233+
end
234+
rescue Bundler::GemfileNotFound
235+
Dir.pwd
236+
end
237+
end
238+
173239
def derive_packager(options)
174240
__skip__ =
175241
if defined?(Bundler) && !options[:disable_gems]

lib/ruby_wasm/packager.rb

Lines changed: 11 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
class RubyWasm::Packager
33
# Initializes a new instance of the RubyWasm::Packager class.
44
#
5+
# @param root [String] The root directory of the Ruby project.
6+
# The root directory (will) contain the following files:
7+
# * patches/{source}/*.patch
8+
# * build_manifest.json
9+
# * rubies
10+
# * build
511
# @param config [Hash] The build config used for building Ruby.
612
# @param definition [Bundler::Definition] The Bundler definition.
7-
def initialize(config = nil, definition = nil)
13+
def initialize(root, config = nil, definition = nil)
14+
@root = root
815
@definition = definition
916
@config = config
1017
end
@@ -60,75 +67,13 @@ def support_dynamic_linking?
6067
ENV["RUBY_WASM_EXPERIMENTAL_DYNAMIC_LINKING"] == "1"
6168
end
6269

63-
# Retrieves the root directory of the Ruby project.
64-
# The root directory contains the following stuff:
65-
# * patches/{source}/*.patch
66-
# * build_manifest.json
67-
# * rubies
68-
# * build
69-
def root
70-
__skip__ =
71-
@root ||=
72-
begin
73-
if explicit = ENV["RUBY_WASM_ROOT"]
74-
File.expand_path(explicit)
75-
elsif defined?(Bundler)
76-
Bundler.root
77-
else
78-
Dir.pwd
79-
end
80-
rescue Bundler::GemfileNotFound
81-
Dir.pwd
82-
end
83-
end
84-
85-
# Retrieves the alias definitions for the Ruby sources.
86-
def self.build_source_aliases(root)
87-
# @type var sources: Hash[string, RubyWasm::Packager::build_source]
88-
sources = {
89-
"head" => {
90-
type: "github",
91-
repo: "ruby/ruby",
92-
rev: "master"
93-
},
94-
"3.3" => {
95-
type: "tarball",
96-
url: "https://cache.ruby-lang.org/pub/ruby/3.3/ruby-3.3.0.tar.gz"
97-
},
98-
"3.2" => {
99-
type: "tarball",
100-
url: "https://cache.ruby-lang.org/pub/ruby/3.2/ruby-3.2.3.tar.gz"
101-
}
102-
}
103-
sources.each do |name, source|
104-
source[:name] = name
105-
patches = Dir[File.join(root, "patches", name, "*.patch")]
106-
.map { |p| File.expand_path(p) }
107-
source[:patches] = patches
108-
end
109-
110-
build_manifest = File.join(root, "build_manifest.json")
111-
if File.exist?(build_manifest)
112-
begin
113-
manifest = JSON.parse(File.read(build_manifest))
114-
manifest["ruby_revisions"].each do |name, rev|
115-
sources[name][:rev] = rev
116-
end
117-
rescue StandardError => e
118-
RubyWasm.logger.warn "Failed to load build_manifest.json: #{e}"
119-
end
120-
end
121-
sources
122-
end
123-
12470
ALL_DEFAULT_EXTS =
12571
"bigdecimal,cgi/escape,continuation,coverage,date,dbm,digest/bubblebabble,digest,digest/md5,digest/rmd160,digest/sha1,digest/sha2,etc,fcntl,fiber,gdbm,json,json/generator,json/parser,nkf,objspace,pathname,psych,racc/cparse,rbconfig/sizeof,ripper,stringio,strscan,monitor,zlib,openssl"
12672

12773
# Retrieves the build options used for building Ruby itself.
12874
def build_options
12975
default = {
13076
target: "wasm32-unknown-wasi",
131-
src: "3.3",
13277
default_exts: ALL_DEFAULT_EXTS
13378
}
13479
override = @config || {}
@@ -139,25 +84,14 @@ def build_options
13984
# Retrieves the resolved build options
14085
def full_build_options
14186
options = build_options
142-
build_dir = File.join(root, "build")
143-
rubies_dir = File.join(root, "rubies")
87+
build_dir = File.join(@root, "build")
88+
rubies_dir = File.join(@root, "rubies")
14489
toolchain = RubyWasm::Toolchain.get(options[:target], build_dir)
145-
src =
146-
if options[:src].is_a?(Hash)
147-
options[:src]
148-
else
149-
src_name = options[:src]
150-
aliases = self.class.build_source_aliases(root)
151-
aliases[src_name] ||
152-
raise(
153-
"Unknown Ruby source: #{src_name} (available: #{aliases.keys.join(", ")})"
154-
)
155-
end
15690
options.merge(
15791
toolchain: toolchain,
15892
build_dir: build_dir,
15993
rubies_dir: rubies_dir,
160-
src: src
94+
src: options[:src]
16195
)
16296
end
16397
end

sig/ruby_wasm/cli.rbs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
module RubyWasm
22
class CLI
3+
type cli_options = {
4+
print_ruby_cache_key: bool,
5+
save_temps: bool,
6+
output: String,
7+
8+
optimize: bool,
9+
remake: bool,
10+
reconfigure: bool,
11+
clean: bool,
12+
13+
ruby_version: String,
14+
target_triplet: String,
15+
profile: String,
16+
stdlib: bool,
17+
disable_gems: bool,
18+
format: String,
19+
}
320
DEFAULT_RUBIES_DIR: string
421

522
@stdout: IO
@@ -12,11 +29,14 @@ module RubyWasm
1229

1330
private
1431

15-
def build_config: (Hash[untyped, untyped] options) -> Hash[untyped, untyped]
32+
def build_config: (cli_options options) -> Packager::build_config
33+
def compute_build_source: (cli_options options) -> Packager::build_source
34+
def self.build_source_aliases: (string root) -> Hash[string, Packager::build_source]
35+
def root: () -> string
1636

17-
def derive_packager: (Hash[untyped, untyped] options) -> Packager
37+
def derive_packager: (cli_options options) -> Packager
1838
def do_print_ruby_cache_key: (Packager) -> void
19-
def do_build: (BuildExecutor, string tmpdir, Packager, Hash[untyped, untyped] options) -> void
39+
def do_build: (BuildExecutor, string tmpdir, Packager, cli_options options) -> void
2040

2141
def require_extension: () -> void
2242
end

sig/ruby_wasm/packager.rbs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

22
class RubyWasm::Packager
3+
type build_source = Hash[Symbol, (string | Array[String])]
4+
type build_config = Hash[untyped, untyped]
5+
36
@definition: untyped
4-
@config: Hash[untyped, untyped]
7+
@config: build_config
58

6-
def initialize: (untyped?, Hash[untyped, untyped]?) -> void
9+
def initialize: (build_config?, untyped?) -> void
710

811
def package: (RubyWasm::BuildExecutor, string dest_dir, untyped options) -> Array[Integer]
912

@@ -17,9 +20,6 @@ class RubyWasm::Packager
1720

1821
def root: () -> string
1922

20-
type build_source = Hash[Symbol, (string | Array[String])]
21-
def self.build_source_aliases: (string root) -> Hash[string, build_source]
22-
2323
ALL_DEFAULT_EXTS: string
2424

2525
def build_options: () -> Hash[Symbol, untyped]

0 commit comments

Comments
 (0)