Skip to content

Commit 90ab43d

Browse files
build: Pretty-print executed commands
1 parent dfa96af commit 90ab43d

File tree

8 files changed

+130
-48
lines changed

8 files changed

+130
-48
lines changed

Steepfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ target :lib do
1212
library "fileutils"
1313
library "open-uri"
1414
library "uri"
15+
library "shellwords"
1516

1617
configure_code_diagnostics(D::Ruby.default)
1718
end

ext/extinit.c.erb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
require "erb"
2+
require "optparse"
3+
4+
opts = OptionParser.new
5+
opts.on("--cc CC") {|cc| @cc = cc }
6+
opts.on("--output FILE") {|o| @o = o }
7+
8+
opts.parse!(ARGV)
9+
if @cc.nil? || @o.nil?
10+
puts opts.help
11+
exit 1
12+
end
213

314
exts = ARGV
4-
puts ERB.new(DATA.read).run
15+
16+
c_src = ERB.new(DATA.read).result
17+
IO.popen("#{@cc} -c -xc - -o #{@o}", "w") {|f| f << c_src }
518

619
__END__
720
#define init(func, name) { \

lib/ruby_wasm/build.rb

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,35 @@ def initialize(verbose: false)
99
@verbose = verbose
1010
end
1111

12-
def system(*args, **kwargs)
13-
puts args.join(" ")
12+
def system(*args, chdir: nil, out: nil, env: nil)
13+
_print_command(args, env)
14+
1415
if @verbose
15-
out = kwargs[:out] || $stdout
16+
out ||= $stdout
1617
else
17-
out = IO.pipe[1]
18+
# Capture stdout by default
19+
out_pipe = IO.pipe
20+
out = out_pipe[1]
21+
end
22+
# @type var kwargs: Hash[Symbol, untyped]
23+
kwargs = { exception: true, out: out }
24+
kwargs[:chdir] = chdir if chdir
25+
begin
26+
if env
27+
Kernel.system(env, *args.to_a.map(&:to_s), **kwargs)
28+
else
29+
Kernel.system(*args.to_a.map(&:to_s), **kwargs)
30+
end
31+
ensure
32+
out.close if out_pipe
33+
end
34+
rescue => e
35+
if out_pipe
36+
# Print the output of the failed command
37+
puts out_pipe[0].read
1838
end
19-
Kernel.system(*args, **kwargs, out: out)
39+
$stdout.flush
40+
raise e
2041
end
2142

2243
def rm_rf(list)
@@ -42,5 +63,15 @@ def mkdir_p(list)
4263
def write(path, data)
4364
File.write(path, data)
4465
end
66+
67+
private
68+
69+
def _print_command(args, env)
70+
require "shellwords"
71+
# Bold cyan
72+
print "\e[1;36m ==>\e[0m "
73+
print "env " + env.map { |k, v| "#{k}=#{v}" }.join(" ") + " " if env
74+
print args.map { |arg| Shellwords.escape(arg.to_s) }.join(" ") + "\n"
75+
end
4576
end
4677
end

lib/ruby_wasm/build/product/baseruby.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ def build(executor)
2525
@source.build(executor)
2626
return if Dir.exist?(install_dir)
2727
Dir.chdir(product_build_dir) do
28-
executor.system "#{@source.configure_file} --prefix=#{install_dir} --disable-install-doc"
29-
executor.system "make install"
28+
executor.system @source.configure_file,
29+
"--prefix=#{install_dir}",
30+
"--disable-install-doc"
31+
executor.system "make", "install"
3032
end
3133
end
3234
end

lib/ruby_wasm/build/product/crossruby.rb

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ def build(executor, crossruby)
3232
objdir = product_build_dir crossruby
3333
executor.mkdir_p objdir
3434
do_extconf executor, crossruby
35-
executor.system %Q(make -C "#{objdir}" #{make_args(crossruby).join(" ")} #{lib}.a)
35+
executor.system "make",
36+
"-C",
37+
"#{objdir}",
38+
*make_args(crossruby),
39+
"#{lib}.a"
3640
# A ext can provide link args by link.filelist. It contains only built archive file by default.
3741
unless File.exist?(linklist(crossruby))
3842
executor.write(
@@ -66,14 +70,17 @@ def do_extconf(executor, crossruby)
6670
"-I#{crossruby.build_dir}"
6771
]
6872
# Clear RUBYOPT to avoid loading unrelated bundle setup
69-
executor.system ({ "RUBYOPT" => "" }),
70-
"#{crossruby.baseruby_path} #{extconf_args.join(" ")}",
71-
chdir: objdir
73+
executor.system crossruby.baseruby_path,
74+
*extconf_args,
75+
chdir: objdir,
76+
env: {
77+
"RUBYOPT" => ""
78+
}
7279
end
7380

7481
def do_install_rb(executor, crossruby)
7582
objdir = product_build_dir crossruby
76-
executor.system %Q(make -C "#{objdir}" #{make_args(crossruby).join(" ")} install-rb)
83+
executor.system "make", "-C", objdir, *make_args(crossruby), "install-rb"
7784
end
7885

7986
def cache_key(digest)
@@ -129,17 +136,22 @@ def initialize(
129136
def configure(executor, reconfigure: false)
130137
if !File.exist?("#{build_dir}/Makefile") || reconfigure
131138
args = configure_args(RbConfig::CONFIG["host"], toolchain)
132-
executor.system "#{source.configure_file} #{args.join(" ")}",
133-
chdir: build_dir
139+
executor.system source.configure_file, *args, chdir: build_dir
134140
end
135141
# NOTE: we need rbconfig.rb at configuration time to build user given extensions with mkmf
136-
executor.system "make rbconfig.rb", chdir: build_dir
142+
executor.system "make", "rbconfig.rb", chdir: build_dir
137143
end
138144

139145
def build_exts(executor)
140146
@user_exts.each { |prod| prod.build(executor, self) }
141147
executor.mkdir_p File.dirname(extinit_obj)
142-
executor.system %Q(ruby #{extinit_c_erb} #{@user_exts.map(&:name).join(" ")} | #{toolchain.cc} -c -x c - -o #{extinit_obj})
148+
executor.system "ruby",
149+
extinit_c_erb,
150+
*@user_exts.map(&:name),
151+
"--cc",
152+
toolchain.cc,
153+
"--output",
154+
extinit_obj
143155
end
144156

145157
def build(executor, remake: false, reconfigure: false)
@@ -154,13 +166,16 @@ def build(executor, remake: false, reconfigure: false)
154166

155167
install_dir = File.join(build_dir, "install")
156168
if !File.exist?(install_dir) || remake || reconfigure
157-
executor.system "make install DESTDIR=#{install_dir}", chdir: build_dir
169+
executor.system "make",
170+
"install",
171+
"DESTDIR=#{install_dir}",
172+
chdir: build_dir
158173
end
159174

160175
executor.rm_rf dest_dir
161176
executor.cp_r install_dir, dest_dir
162177
@user_exts.each { |ext| ext.do_install_rb(executor, self) }
163-
executor.system "tar cfz #{artifact} -C rubies #{name}"
178+
executor.system "tar", "cfz", artifact, "-C", "rubies", name
164179
end
165180

166181
def clean(executor)
@@ -241,11 +256,11 @@ def configure_args(build_triple, toolchain)
241256

242257
args = self.system_triplet_args + ["--build", build_triple]
243258
args << "--with-static-linked-ext"
244-
args << %Q(--with-ext="#{default_exts}")
245-
args << %Q(--with-libyaml-dir="#{@libyaml.install_root}")
246-
args << %Q(--with-zlib-dir="#{@zlib.install_root}")
247-
args << %Q(--with-openssl-dir="#{@openssl.install_root}") if @openssl
248-
args << %Q(--with-baseruby="#{baseruby_path}")
259+
args << %Q(--with-ext=#{default_exts})
260+
args << %Q(--with-libyaml-dir=#{@libyaml.install_root})
261+
args << %Q(--with-zlib-dir=#{@zlib.install_root})
262+
args << %Q(--with-openssl-dir=#{@openssl.install_root}) if @openssl
263+
args << %Q(--with-baseruby=#{baseruby_path})
249264

250265
case target
251266
when "wasm32-unknown-wasi"
@@ -270,13 +285,13 @@ def configure_args(build_triple, toolchain)
270285
xcflags << "-DWASM_FIBER_STACK_BUFFER_SIZE=24576"
271286
xcflags << "-DWASM_SCAN_STACK_BUFFER_SIZE=24576"
272287

273-
args << %Q(LDFLAGS="#{ldflags.join(" ")}")
274-
args << %Q(XLDFLAGS="#{xldflags.join(" ")}")
275-
args << %Q(XCFLAGS="#{xcflags.join(" ")}")
276-
args << %Q(debugflags="#{@debugflags.join(" ")}")
277-
args << %Q(cppflags="#{@cppflags.join(" ")}")
288+
args << %Q(LDFLAGS=#{ldflags.join(" ")})
289+
args << %Q(XLDFLAGS=#{xldflags.join(" ")})
290+
args << %Q(XCFLAGS=#{xcflags.join(" ")})
291+
args << %Q(debugflags=#{@debugflags.join(" ")})
292+
args << %Q(cppflags=#{@cppflags.join(" ")})
278293
unless wasmoptflags.empty?
279-
args << %Q(wasmoptflags="#{@wasmoptflags.join(" ")}")
294+
args << %Q(wasmoptflags=#{@wasmoptflags.join(" ")})
280295
end
281296
args << "--disable-install-doc"
282297
args

lib/ruby_wasm/build/product/libyaml.rb

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,35 @@ def build(executor)
3333

3434
executor.mkdir_p File.dirname(product_build_dir)
3535
executor.rm_rf product_build_dir
36-
executor.system "curl -L https://github.com/yaml/libyaml/releases/download/#{LIBYAML_VERSION}/yaml-#{LIBYAML_VERSION}.tar.gz | tar xz",
37-
chdir: File.dirname(product_build_dir)
36+
executor.mkdir_p product_build_dir
37+
tarball_path =
38+
File.join(product_build_dir, "libyaml-#{LIBYAML_VERSION}.tar.gz")
39+
executor.system "curl",
40+
"-o",
41+
tarball_path,
42+
"-L",
43+
"https://github.com/yaml/libyaml/releases/download/#{LIBYAML_VERSION}/yaml-#{LIBYAML_VERSION}.tar.gz"
44+
executor.system "tar",
45+
"xzf",
46+
tarball_path,
47+
"-C",
48+
product_build_dir,
49+
"--strip-components=1"
3850

3951
# obtain the latest config.guess and config.sub for Emscripten and WASI triple support
40-
executor.system "curl -o #{product_build_dir}/config/config.guess 'https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'"
41-
executor.system "curl -o #{product_build_dir}/config/config.sub 'https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'"
52+
executor.system "curl",
53+
"-o",
54+
"#{product_build_dir}/config/config.guess",
55+
"https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD"
56+
executor.system "curl",
57+
"-o",
58+
"#{product_build_dir}/config/config.sub",
59+
"https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD"
4260

43-
executor.system "./configure #{configure_args.join(" ")}",
44-
chdir: product_build_dir
45-
executor.system "make install DESTDIR=#{destdir}",
61+
executor.system "./configure", *configure_args, chdir: product_build_dir
62+
executor.system "make",
63+
"install",
64+
"DESTDIR=#{destdir}",
4665
chdir: product_build_dir
4766
end
4867
end

lib/ruby_wasm/build/product/zlib.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ def build(executor)
4141
executor.rm_rf product_build_dir
4242

4343
executor.system "curl -L https://zlib.net/zlib-#{ZLIB_VERSION}.tar.gz | tar xz",
44-
chdir: File.dirname(product_build_dir),
45-
exception: true
44+
chdir: File.dirname(product_build_dir)
4645

4746
executor.system "#{configure_args.join(" ")} ./configure --static",
4847
chdir: product_build_dir

sig/ruby_wasm/build.rbs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ module RubyWasm
145145
attr_accessor xldflags: Array[String]
146146
def initialize: (BuildParams params, String build_dir, String rubies_dir, BaseRubyProduct baseruby, BuildSource source, Toolchain toolchain, ?user_exts: Array[CrossRubyExtProduct]) -> void
147147
def configure: (BuildExecutor executor, ?reconfigure: bool) -> void
148-
def build_exts: (BuildExecutor executor) -> bool?
149-
def build: (BuildExecutor executor, ?remake: bool, ?reconfigure: bool) -> bool?
148+
def build_exts: (BuildExecutor executor) -> void
149+
def build: (BuildExecutor executor, ?remake: bool, ?reconfigure: bool) -> void
150150
def clean: (BuildExecutor executor) -> void
151151
def name: -> String
152152
def cache_key: (Digest::SHA256 digest) -> void
@@ -180,15 +180,15 @@ module RubyWasm
180180

181181
attr_reader name: String
182182
def initialize: -> void
183-
def find_tool: (Symbol name) -> bot
184-
def check_envvar: (untyped name) -> nil
183+
def find_tool: (Symbol name) -> String
184+
def check_envvar: (untyped name) -> void
185185
def self.get: (String target, ?String? build_dir) -> (Toolchain)
186186
def self.find_path: (String command) -> String?
187187
def self.check_executable: (String command) -> String
188-
def cc: -> nil
189-
def ranlib: -> nil
190-
def ld: -> nil
191-
def ar: -> nil
188+
def cc: -> String
189+
def ranlib: -> String
190+
def ld: -> String
191+
def ar: -> String
192192

193193
def install: -> void
194194
end
@@ -225,13 +225,15 @@ module RubyWasm
225225
@verbose: bool
226226

227227
def initialize: (?verbose: bool) -> void
228-
def system: (*untyped, **untyped) -> bool?
228+
def system: (*_ToS args, ?chdir: String?, ?out: Kernel::redirect_fd?, ?env: Hash[String, String]?) -> void
229229
def rm_rf: (FileUtils::pathlist list) -> void
230230
def rm_f: (FileUtils::pathlist list) -> void
231231
def cp_r: (FileUtils::pathlist src, path dest) -> void
232232
def mv: (FileUtils::pathlist src, path dest) -> void
233233
def mkdir_p: (FileUtils::pathlist list) -> void
234234
def write: (String path, _ToS data) -> void
235+
236+
private def _print_command: (Array[_ToS] command, Hash[String, String]? env) -> void
235237
end
236238

237239
class Downloader

0 commit comments

Comments
 (0)