link-arg not passing compiler options to rustc

2.5k views Asked by At

I'm generating asm.js from a Rust file like so:

rustc demo.rs --target asmjs-unknown-emscripten -C opt-level=0 -C link-arg="-s MINIMAL_RUNTIME=1" -C link-arg="-s ENVIRONMENT=web" -C link-arg="-s LEGACY_VM_SUPPORT=1" -o ../../build/bytecode/demo.asm.js

However, when I look through the generated asm.js file, I can clearly see that those compiler options were not passed through - the code still assumes it can be run in any environment, and the legacy VM polyfills are missing.

What am I missing?

This is the rust file, in case that matters:

#[allow(dead_code)]
pub fn my_add(num1: f64, num2: f64) -> f64 {
    return num1 + num2;
}

pub fn main() {
    println!("Rust loaded");
}
1

There are 1 answers

2
Victor Deleau On

I have two suggestions.

If we look at the documentation, there is both the link-arg option to pass a single argument and link-args to pass multiple arguments. Because link-args exist, and the doc specify that arguments must be separated by spaces, maybe you should use ... link-args="-s MINIMAL_RUNTIME=1", or even ... -C link-arg="-s" -C link-arg="MINIMAL_RUNTIME=1" ... Haven't tried it though.

Second, there seem to be the old way of passing arguments to the linker of a specific compiler according to this post. You can create a wrapper around the emcc compiler by creating a file named emcc_sdl in your project folder which contains:

emcc "-s" "MINIMAL_RUNTIME=1" "-s" "ENVIRONMENT=web" "-s" "LEGACY_VM_SUPPORT=1" $@

Make it executable:

> chmod +x emcc_sdl

Create or edit the .cargo/config file and add:

[target.wasm32-unknown-emscripten]
linker = "/project_dir/project_name/emcc_sdl"

[target.asmjs-unknown-emscripten]
linker = "/project_dir/project_name/emcc_sdl"

You should be able to build using:

rustc demo.rs --target asmjs-unknown-emscripten -C opt-level=0 -o ../../build/bytecode/demo.asm.js

Hope it helps.