Skip to content

Commit b1723b3

Browse files
Cross-build OpenSSL for WebAssembly
1 parent 424fda6 commit b1723b3

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ BUILD_SOURCES = {
1818
}
1919

2020
FULL_EXTS =
21-
"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"
21+
"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"
2222

2323
BUILD_PROFILES = {
2424
"minimal" => {

lib/ruby_wasm/build_system/product.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
require_relative "product/baseruby"
44
require_relative "product/zlib"
55
require_relative "product/libyaml"
6+
require_relative "product/openssl"
67
require_relative "product/wasi_vfs"
78
require_relative "product/crossruby"

lib/ruby_wasm/build_system/product/crossruby.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def build(remake: false, reconfigure: false)
131131
FileUtils.mkdir_p dest_dir
132132
FileUtils.mkdir_p build_dir
133133
@toolchain.install
134-
[@source, @baseruby, @libyaml, @zlib, @wasi_vfs].each(&:build)
134+
[@source, @baseruby, @libyaml, @zlib, @openssl, @wasi_vfs].each(&:build)
135135
dep_tasks.each(&:invoke)
136136
configure(reconfigure: reconfigure)
137137
build_exts
@@ -178,6 +178,10 @@ def with_wasi_vfs(wasi_vfs)
178178
@wasi_vfs = wasi_vfs
179179
end
180180

181+
def with_openssl(openssl)
182+
@openssl = openssl
183+
end
184+
181185
def dest_dir
182186
File.join(@rubies_dir, name)
183187
end
@@ -215,6 +219,7 @@ def configure_args(build_triple, toolchain)
215219
args << %Q(--with-ext="#{default_exts}")
216220
args << %Q(--with-libyaml-dir="#{@libyaml.install_root}")
217221
args << %Q(--with-zlib-dir="#{@zlib.install_root}")
222+
args << %Q(--with-openssl-dir="#{@openssl.install_root}") if @openssl
218223
args << %Q(--with-baseruby="#{baseruby_path}")
219224

220225
case target
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
require "rake"
2+
require_relative "./product"
3+
4+
module RubyWasm
5+
class OpenSSLProduct < AutoconfProduct
6+
attr_reader :target, :install_task
7+
8+
OPENSSL_VERSION = "3.0.5"
9+
10+
def initialize(build_dir, target, toolchain)
11+
@build_dir = build_dir
12+
@target = target
13+
super(target, toolchain)
14+
end
15+
16+
def product_build_dir
17+
File.join(@build_dir, target, "openssl-#{OPENSSL_VERSION}")
18+
end
19+
20+
def destdir
21+
File.join(product_build_dir, "opt")
22+
end
23+
24+
def install_root
25+
File.join(destdir, "usr", "local")
26+
end
27+
28+
def name
29+
product_build_dir
30+
end
31+
32+
def configure_args
33+
args = %w[
34+
-static
35+
-no-asm
36+
-no-threads
37+
-no-afalgeng
38+
-no-ui-console
39+
-no-tests
40+
-no-sock
41+
-no-dgram
42+
--libdir=lib
43+
-DNO_SYSLOG
44+
-Wl,--allow-undefined
45+
]
46+
if @target == "wasm32-unknown-wasi"
47+
args.concat %w[
48+
-D_WASI_EMULATED_SIGNAL
49+
-D_WASI_EMULATED_PROCESS_CLOCKS
50+
-D_WASI_EMULATED_MMAN
51+
]
52+
end
53+
args + tools_args
54+
end
55+
56+
def build
57+
return if Dir.exist?(install_root)
58+
59+
FileUtils.mkdir_p File.dirname(product_build_dir)
60+
FileUtils.rm_rf product_build_dir
61+
system "curl -L https://www.openssl.org/source/openssl-#{OPENSSL_VERSION}.tar.gz | tar xz",
62+
chdir: File.dirname(product_build_dir)
63+
64+
system "./Configure #{configure_args.join(" ")}", chdir: product_build_dir
65+
# Use "install_sw" instead of "install" because it tries to install docs and it's very slow.
66+
# OpenSSL build system doesn't have well support for parallel build, so force -j1.
67+
system "make -j1 install_sw DESTDIR=#{destdir}", chdir: product_build_dir
68+
end
69+
end
70+
end

lib/ruby_wasm/rake_task.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def initialize(
4747
@wasi_vfs = RubyWasm::WasiVfsProduct.new(@build_dir)
4848
@source = RubyWasm::BuildSource.new(src, @build_dir)
4949
@baseruby = RubyWasm::BaseRubyProduct.new(@build_dir, @source)
50+
@openssl = RubyWasm::OpenSSLProduct.new(@build_dir, @target, @toolchain)
5051

5152
build_params =
5253
RubyWasm::BuildParams.new(options.merge(name: name, target: @target))
@@ -65,6 +66,7 @@ def initialize(
6566
@crossruby.with_libyaml @libyaml
6667
@crossruby.with_zlib @zlib
6768
@crossruby.with_wasi_vfs @wasi_vfs
69+
@crossruby.with_openssl @openssl
6870

6971
desc "Cross-build Ruby for #{@target}"
7072
task name do

0 commit comments

Comments
 (0)