Skip to content

Support compiling to wasm32-wasi target #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Build {

fn cmd_make(&self) -> Command {
let host = &self.host.as_ref().expect("HOST dir not set")[..];
let target = &self.target.as_ref().expect("TARGET dir not set")[..];
if host.contains("dragonfly")
|| host.contains("freebsd")
|| host.contains("openbsd")
Expand All @@ -61,7 +62,13 @@ impl Build {
{
Command::new("gmake")
} else {
Command::new("make")
if target.contains("wasm") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can wrap this in a single else if statement

let mut cmd = Command::new("wasimake");

std::mem::replace(cmd.arg("make"), Command::new("wasimake"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is wasimake and wasiconfigure? I don't think these are provided by OpenSSL and otherwise I don't want to rely on too many external programs. (I've also never heard of these myself)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is wasimake and wasiconfigure? I don't think these are provided by OpenSSL and otherwise I don't want to rely on too many external programs. (I've also never heard of these myself)

Yes these commands are included in the wasienv toolchain https://docs.wasmer.io/ecosystem/wasienv and right now i only found this way to compile openssl for wasm.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this patch be written without requiring those tools? Without knowing what they do it seems like they could introduce hidden dependencies we aren't aware of.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this patch be written without requiring those tools? Without knowing what they do it seems like they could introduce hidden dependencies we aren't aware of.

I dont think so, but this only requires the user to install the wasienv toolchain.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I do not want to land this with a dependency on the wasienv toolchain. I don't know why it's necessary as opposed to the standard wasi-sdk.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I do not want to land this with a dependency on the wasienv toolchain. I don't know why it's necessary as opposed to the standard wasi-sdk.

I tried compiling using the standard wasi-sdk but it didnt work no matter what i tried, this seems to be the proper way to configure the build for wasi as far as i know.

} else {
Command::new("make")
}
}
}

Expand Down Expand Up @@ -129,10 +136,17 @@ impl Build {
let inner_dir = build_dir.join("src");
fs::create_dir_all(&inner_dir).unwrap();
cp_r(&source_dir(), &inner_dir);
let mut program = String::new();

let perl_program =
env::var("OPENSSL_SRC_PERL").unwrap_or(env::var("PERL").unwrap_or("perl".to_string()));
let mut configure = Command::new(perl_program);
// use wasiconfigure when targeting wasm
if target.contains("wasm") {
program = String::from("wasiconfigure");
} else {
program = env::var("OPENSSL_SRC_PERL")
.unwrap_or(env::var("PERL").unwrap_or("perl".to_string()));
}

let mut configure = Command::new(program);
configure.arg("./Configure");
if host.contains("pc-windows-gnu") {
configure.arg(&format!("--prefix={}", sanitize_sh(&install_dir)));
Expand All @@ -155,7 +169,7 @@ impl Build {
// Avoid multilib-postfix for build targets that specify it
.arg("--libdir=lib");

if cfg!(not(feature = "legacy")) {
if cfg!(not(feature = "legacy")) && !target.contains("wasm") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this condition here added for wasm?

Copy link
Author

@mdrokz mdrokz Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this condition here added for wasm?

Ah my bad, i accidentally left that condition there.

This condition is required for building OpenSSL 1.1.1d

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this repository only has one version of OpenSSL via a submodule, which is currently 3+

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this repository only has one version of OpenSSL via a submodule, which is currently 3+

Yes this repo only contains openssl 3, but right now only OpenSSL 1.1.1d is supported with specific patches for wasm32-wasi, see #113 (comment)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case then I don't think this can land right now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case then I don't think this can land right now.

I will try patching the latest OpenSSL 3 and see if this works with that or not.

configure.arg("no-legacy");
}

Expand Down Expand Up @@ -203,6 +217,21 @@ impl Build {
configure.arg("no-stdio");
}

if target.contains("wasm") {
// -no-threads we dont have thread support in WASI
configure.arg("no-threads");
// -no-sock we dont have sockets in WASI
configure.arg("no-sock");
configure.arg("no-afalgeng");
configure.arg("-DOPENSSL_SYS_NETWARE");
configure.arg("-DSIG_DFL=0");
configure.arg("-DSIG_IGN=0");
configure.arg("-DHAVE_FORK=0");
configure.arg("-DOPENSSL_NO_AFALGENG=1");
// --with-rand-seed=getrandom (needed to force using getentropy because WASI has no /dev/random or getrandom)
configure.arg("--with-rand-seed=getrandom");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some comments for what's going on here for wasm?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some comments for what's going on here for wasm?

Sure

}

if target.contains("msvc") {
// On MSVC we need nasm.exe to compile the assembly files.
// ASM compiling will be enabled if nasm.exe is installed, unless
Expand Down