< 返回版块

ilp64 发表于 2020-07-09 10:56

Tags:wasm,feature,simd

我现在要把 mozjpeg 通过 wasm-pack 编译成 wasm 模块使用, 我新建了一个 lib ,在 Cargo.toml 中写了这些依赖:

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2.64"
mozjpeg = "0.8.19"

最后 wasm-pack build --scope xxx 的时候报错了,我发现报错是在 mozjpeg 这个 crate 依赖的 mozjpeg-sys 中的 src/build.rs

#[cfg(feature = "with_simd")]
fn build_gas(mut c: cc::Build, target_arch: &str, abi: &str) {
    c.file(match target_arch {
        "arm" => "vendor/simd/arm/jsimd_neon.S",
        "aarch64" => "vendor/simd/arm64/jsimd_neon.S",
        "mips" => "vendor/simd/mips/jsimd_dspr2.S",
        // 就是这个panic
        _ => {panic!("The mozjpeg-sys SIMD build script is incomplete for this platform");},
    });
    c.flag("-xassembler-with-cpp");

    c.compile(&format!("mozjpegsimd{}", abi));
}

#[cfg(feature = "nasm_simd")]
fn build_nasm(root: &Path, vendor_dir: &Path, out_dir: &Path, target_arch: &str, target_os: &str) -> Vec<PathBuf> {
    let mut n = nasm_rs::Build::new();
    n.out_dir(out_dir);

    if std::env::var("PROFILE").ok().map_or(false, |s| "debug" == s) {
        n.debug(true);
    }

    n.define("PIC", None); // Rust always uses -fPIC

    match (target_os, target_arch.ends_with("64")) {
        ("windows", false) => n.define("WIN32", None),
        ("windows", true) => n.define("WIN64", None),
        ("macos", _) | ("ios", _) => n.define("MACHO", None),
        _ => n.define("ELF", None),
    };

    let arch_name = match target_arch {
        "x86" => "i386",
        "x86_64" => {
            n.define("__x86_64__", None);
            "x86_64"
        },
        _ => {panic!("The mozjpeg-sys SIMD build script is incomplete for this platform");},
    };

    // these should have had .inc extension
    let dont_compile = ["jccolext-avx2.asm", "jccolext-mmx.asm", "jccolext-sse2.asm", "jcgryext-avx2.asm",
        "jcgryext-mmx.asm", "jcgryext-sse2.asm", "jdcolext-avx2.asm", "jdcolext-mmx.asm",
        "jdcolext-sse2.asm", "jdmrgext-avx2.asm", "jdmrgext-mmx.asm", "jdmrgext-sse2.asm"];

    let simd_dir = vendor_dir.join("simd");
    let simd_arch_dir = simd_dir.join(arch_name);
    n.include(&simd_arch_dir);
    n.include(simd_dir.join("nasm"));
    n.include(vendor_dir.join("win"));
    for entry in fs::read_dir(simd_arch_dir).expect("simd subdir missing") {
        let entry = entry.unwrap();
        let path = entry.path();
        let included = path.extension().map_or(false, |e| e == "asm");
        let excluded = path.file_name().map_or(true, |f| dont_compile.iter().any(|&e| e == f));
        if included && !excluded {
            n.file(path.strip_prefix(root).unwrap_or(&path));
        }
    }
    n.compile_objects()
}

我发现 mozjpeg-sysCargo.toml 中默认开启了这个 feature

[features]
arith_dec = []
arith_enc = []
default = ["nasm_simd"]
jpeg70_abi = ["arith_dec", "arith_enc"]
jpeg80_abi = ["jpeg70_abi"]
nasm_simd = ["with_simd", "nasm-rs", "nasm-rs/parallel"]
turbojpeg_api = []
unwinding = []
with_simd = []

请问大佬们,这 个with_simd 可以在我 wasm-pack 编译的时候关掉吗🙏?

评论区

写评论
作者 ilp64 2020-07-09 13:12

谢谢大佬,学到了。

--
👇
Neutron3529: 没必要写成那样的,那么写读起来不方便

你可以用类似这样的写法:

[dependencies]
rayon = {version = "1.3.1", default-features = false}

--
👇
ilp64: 哦,解决了,还是要看文档。default-features = false 就好了

[dependencies.mozjpeg]
version = "0.8.19"
default-features = false

但是现在又报 stdlib.h 找不到的错误了。。 一个个解决😂

Neutron3529 2020-07-09 11:21

没必要写成那样的,那么写读起来不方便

你可以用类似这样的写法:

[dependencies]
rayon = {version = "1.3.1", default-features = false}

--
👇
ilp64: 哦,解决了,还是要看文档。default-features = false 就好了

[dependencies.mozjpeg]
version = "0.8.19"
default-features = false

但是现在又报 stdlib.h 找不到的错误了。。 一个个解决😂

作者 ilp64 2020-07-09 11:16

哦,解决了,还是要看文档。default-features = false 就好了

[dependencies.mozjpeg]
version = "0.8.19"
default-features = false

但是现在又报 stdlib.h 找不到的错误了。。 一个个解决😂

1 共 3 条评论, 1 页