How to solve "#error Unsupported architecture" when building Rust code with wasm-pack? - rust

I'm trying to learn Rust and WebAssembly by building a demo app to manipulate some archive files.
After I added the "zip" crate, I was able to run cargo build successfully but not wasm-pack build (nor cargo build --target wasm32-unknown-unknown).
I am getting cargo:warning=#error Unsupported architecture when trying to build my file and clang seems somehow not happy with it.
At first I was getting some stdio.h not found, so I ran
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
to install them.
I am now having the following error:
Compiling bzip2-sys v0.1.7
error: failed to run custom build command for `bzip2-sys v0.1.7`
process didn't exit successfully: `/Users/projects/rust/hello-wasm/target/release/build/bzip2-sys-b7b1e1aeb1e6f42f/build-script-build` (exit code: 101)
--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("s")
HOST = Some("x86_64-apple-darwin")
CC_wasm32-unknown-unknown = None
CC_wasm32_unknown_unknown = None
TARGET_CC = None
CC = None
CFLAGS_wasm32-unknown-unknown = None
CFLAGS_wasm32_unknown_unknown = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("false")
running: "clang" "-Os" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "bzip2-1.0.6" "-D_FILE_OFFSET_BITS=64" "-DBZ_NO_STDIO" "-o" "/Users/projects/rust/hello-wasm/target/wasm32-unknown-unknown/release/build/bzip2-sys-3be942e6fa7d3879/out/lib/bzip2-1.0.6/blocksort.o" "-c" "bzip2-1.0.6/blocksort.c"
cargo:warning=In file included from bzip2-1.0.6/blocksort.c:22:
cargo:warning=In file included from bzip2-1.0.6/bzlib_private.h:25:
cargo:warning=In file included from /usr/include/stdlib.h:62:
cargo:warning=/usr/include/sys/cdefs.h:784:2: error: Unsupported architecture
cargo:warning=#error Unsupported architecture
cargo:warning= ^
cargo:warning=In file included from bzip2-1.0.6/blocksort.c:22:
cargo:warning=In file included from bzip2-1.0.6/bzlib_private.h:25:
cargo:warning=In file included from /usr/include/stdlib.h:64:
cargo:warning=In file included from /usr/include/_types.h:27:
cargo:warning=In file included from /usr/include/sys/_types.h:33:
cargo:warning=/usr/include/machine/_types.h:34:2: error: architecture not supported
cargo:warning=#error architecture not supported
cargo:warning= ^
cargo:warning=In file included from bzip2-1.0.6/blocksort.c:22:
cargo:warning=In file included from bzip2-1.0.6/bzlib_private.h:25:
cargo:warning=In file included from /usr/include/stdlib.h:64:
cargo:warning=In file included from /usr/include/_types.h:27:
cargo:warning=/usr/include/sys/_types.h:55:9: error: unknown type name '__int64_t'; did you mean '__int128_t'?
cargo:warning=typedef __int64_t __darwin_blkcnt_t; /* total blocks */
cargo:warning= ^
cargo:warning=note: '__int128_t' declared here
[...]
Internal error occurred: Command "clang" "-Os" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "bzip2-1.0.6" "-D_FILE_OFFSET_BITS=64" "-DBZ_NO_STDIO" "-o" "/Users/projects/rust/hello-wasm/target/wasm32-unknown-unknown/release/build/bzip2-sys-3be942e6fa7d3879/out/lib/bzip2-1.0.6/blocksort.o" "-c" "bzip2-1.0.6/blocksort.c" with args "clang" did not execute successfully (status code exit code: 1).
I then ran:
rustup target add wasm32-unknown-unknown --toolchain nightly
rustup default nightly
to install target wasm32-unknown-unknown but I am still getting the error.
What am I missing to compile the "zip" crate for wasm ?
Environment
macOS 10.14.4
gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
rustc --version
rustc 1.36.0-nightly (31a75a172 2019-04-21)
rustup show
Default host: x86_64-apple-darwin
installed toolchains
--------------------
stable-x86_64-apple-darwin
nightly-2019-04-16-x86_64-apple-darwin
nightly-x86_64-apple-darwin (default)
installed targets for active toolchain
--------------------------------------
wasm32-unknown-unknown
x86_64-apple-darwin
active toolchain
----------------
nightly-x86_64-apple-darwin (default)
rustc 1.36.0-nightly (31a75a172 2019-04-21)

choose one of
replace wasm32-unknown-unknown with wasm32-unknown-emscripten
remove #include <stdio.h>, printf and other standard-library calls from the C sources
implement the needed functions (printf?) for wasm32-unknown-unknown, or extract-and-copy emscripten's implementation

Related

How to Cross Compile a Rust application from Linux to Windows including the ring lib without docker

I wrote an application with Rust, which is working quite fine on Windows and Linux using cargo run and cargo build.
To have only one system capable of compiling for both targets x86_64-unknown-linux-gnu and x86_64-pc-windows-msvc I set up a Ubuntu 22.04 minimum server installation with these additional commands
apt update
apt upgrade -y
apt install curl build-essential gcc make -y
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > ~/rustup.sh
cd ~
chmod u+x rustup.sh
./rustup.sh -y
. ~/.cargo/env
rustup target add x86_64-unknown-linux-gnu --toolchain stable
rustup target add x86_64-pc-windows-msvc --toolchain stable
Then switching to the application directory
cargo build --release --target x86_64-unknown-linux-gnu
works fine and produces the artifact as expected. But
cargo build --release --target x86_64-pc-windows-msvc
failes with the error message
error: failed to run custom build command for `ring v0.16.20`
Caused by:
process didn't exit successfully: `/test/backend/target/release/build/ring-3857f585b2d049fe/build-script-build` (exit status: 101)
--- stdout
OPT_LEVEL = Some("3")
TARGET = Some("x86_64-pc-windows-msvc")
HOST = Some("x86_64-unknown-linux-gnu")
CC_x86_64-pc-windows-msvc = None
CC_x86_64_pc_windows_msvc = None
TARGET_CC = None
CC = None
CROSS_COMPILE = None
CFLAGS_x86_64-pc-windows-msvc = None
CFLAGS_x86_64_pc_windows_msvc = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("false")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
--- stderr
running "cc" "-O3" "-ffunction-sections" "-fdata-sections" "-m64" "-I" "include" "-Wall" "-Wextra" "/GS" "/Gy" "/EHsc" "/GR-" "/Zc:wchar_t" "/Zc:forScope" "/Zc:inline" "/Zc:rvalueCast" "/sdl" "/Wall" "/wd4127" "/wd4464" "/wd4514" "/wd4710" "/wd4711" "/wd4820" "/wd5045" "/Ox" "-DNDEBUG" "-c" "/Fo/test/backend/target/x86_64-pc-windows-msvc/release/build/ring-e02c6ae3738202e7/out/aes_nohw.obj" "crypto/fipsmodule/aes/aes_nohw.c"
cc: error: -E or -x required when input is from standard input
thread 'main' panicked at 'execution failed', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.20/build.rs:656:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
As stated by the ring library I need to
When you build ring for a target that is different than the one you are using for the build process you need to install the rust tool chain and a C/C++ compiler that can produce binaries for the intended target.
Besides the required dependencies you need to set the environment variables TARGET_CC and TARGET_AR to the full path of the cross-compiler and the cross-archiver respectively.
Still I was not able to figure out which package to install and how to set the mentioned variables TARGET_CC and TARGET_AR in order to make the build working.
Is there a way how to cross-compile a Rust-Application for Windows under Ubuntu Linux 22.04 without using a VM or Docker-Container with a Windows installation?

error: the option `Z` is only accepted on the nightly compiler is not solved

I'm trying to use Address sanitizer in rust with this manual(https://github.com/japaric/rust-san),
but when I build this code with command RUSTFLAGS="-Z sanitier=address" cargo rustc -- --emit=llvm-ir, it cause error like this;
error: failed to run rustc to learn about target-specific information
Caused by:
process didn't exit successfully: rustc - --crate-name ___ --print=file-names -Z sanitier=address --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=cfg (exit status: 1)
--- stderr
error: the option Z is only accepted on the nightly compiler'
I think that "the option Z is only accepted on the nightly compiler" is problem, so I set up to nightly compiler with
rustup install nightly, rustup default nightly, but it cause same error when I build with that command.
How should I do?
Perhaps you should build your code with command:
RUSTFLAGS="-Z sanitier=address" cargo run ${your-binary-name} --target x86_64-unknown-linux-gnu -- --emit=llvm-ir
This works for me to check example in manual:
RUSTFLAGS="-Z sanitizer=address" cargo run --example out-of-bounds --target x86_64-unknown-linux-gnu -- --emit=llvm-ir
**==618620==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffbcbf1250 at pc 0x55adfda84e90 bp 0x7fffbcbf1210 sp 0x7fffbcbf1208
READ of size 4 at 0x7fffbcbf1250 thread T0
#0 0x55adfda84e8f in out_of_bounds::main::hf228b092630ab849 /tmp/rust-san/asan/examples/out-of-bounds.rs:3:22
#1 0x55adfda846fa in core::ops::function::FnOnce::call_once::h84454ea25b7d75ab /rustc/f1f721e64014863f41c1a386b04af04c2de25321/library/core/src/ops/function.rs:248:5
#2 0x55adfda84a94 in std::sys_common::backtrace::__rust_begin_short_backtrace::hbc7697f2c0b7e35d /rustc/f1f721e64014863f41c1a386b04af04c2de25321/library/std/src/sys_common/backtrace.rs:122:18
#3 0x55adfda853a3 in std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h01c2d6f3a231ead0 /rustc/f1f721e64014863f41c1a386b04af04c2de25321/library/std/src/rt.rs:145:18
#4 0x55adfda9704d in core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::hfce3f72e51a03fc4 /rustc/f1f721e64014863f41c1a386b04af04c2de25321/library/core/src/ops/function.rs:280:13
**
According to the manual, you should always pass --target x86_64-unknown-linux-gnu to Cargo.
Be sure to always pass --target x86_64-unknown-linux-gnu to Cargo or else you'll end up sanitizing the build scripts that Cargo runs or run into compilation error if your crate depends on a dylib.
And you could check your toolchain version with:
> rustup default
nightly-x86_64-unknown-linux-gnu (default)

cargo install cargo-generate --features vendored-openssl failed

I am trying to install terrain from terra-money. For that, the instructions said to follow the commands. I have run these instructions on ubuntu 20.04
1. Set 'stable' as the default release channel:
rustup default stable
# 2. Add WASM as the compilation target:
rustup target add wasm32-unknown-unknown
# 3. Install the following packages to generate the contract:
cargo install cargo-generate --features vendored-openssl
cargo install cargo-run-script
but when I run it, I am getting an error in the 3rd instruction. Can anyone please help
error: failed to run custom build command for `openssl-sys v0.9.72`
Caused by:
process didn't exit successfully: `/tmp/cargo-installLlewUy/release/build/openssl-sys-3aec6783a3bc6a31/build-script-main` (exit status: 101)
--- stdout
cargo:rustc-cfg=const_fn
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_NO_VENDOR
X86_64_UNKNOWN_LINUX_GNU_OPENSSL_NO_VENDOR unset
cargo:rerun-if-env-changed=OPENSSL_NO_VENDOR
OPENSSL_NO_VENDOR unset
CC_x86_64-unknown-linux-gnu = None
CC_x86_64_unknown_linux_gnu = None
HOST_CC = None
CC = None
CFLAGS_x86_64-unknown-linux-gnu = None
CFLAGS_x86_64_unknown_linux_gnu = None
HOST_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("false")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
running "perl" "./Configure" "--prefix=/tmp/cargo-installLlewUy/release/build/openssl-sys-d4c820e0a8c7c40f/out/openssl-build/install" "no-dso" "no-shared" "no-ssl3" "no-unit-test" "no-comp" "no-zlib" "no-zlib-dynamic" "no-md2" "no-rc5" "no-weak-ssl-ciphers" "no-camellia" "no-idea" "no-seed" "linux-x86_64" "-O2" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64"
Configuring OpenSSL version 1.1.1n (0x101010efL) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile
**********************************************************************
*** ***
*** OpenSSL has been successfully configured ***
*** ***
*** If you encounter a problem while building, please open an ***
*** issue on GitHub <https://github.com/openssl/openssl/issues> ***
*** and include the output from the following command: ***
*** ***
*** perl configdata.pm --dump ***
*** ***
*** (If you are new to OpenSSL, you might want to consult the ***
*** 'Troubleshooting' section in the INSTALL file first) ***
*** ***
**********************************************************************
running "make" "depend"
--- stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', /home/raju/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-src-111.18.0+1.1.1n/src/lib.rs:477:39
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: failed to compile `cargo-generate v0.13.0`, intermediate artifacts can be found at `/tmp/cargo-installLlewUy`
Caused by:
build failed
I just stumbled across the same error. I checked the OpenSSL requirements as per INSTALL file:
OPENSSL INSTALLATION
--------------------
This document describes installation on all supported operating
systems (the Unix/Linux family (which includes Mac OS/X), OpenVMS,
and Windows).
To install OpenSSL, you will need:
* A make implementation
* Perl 5 with core modules (please read NOTES.PERL)
* The perl module Text::Template (please read NOTES.PERL)
* an ANSI C compiler
* a development environment in the form of development libraries and C
header files
* a supported operating system
I did not had perl installed, after installing it the build completed successfully.
I hope you are only missing this perl installation.

Gcc.exe not installed error not letting me install sqlx-cli

So when I run cargo install sqlx-cli I get this error, I can run rust code, I have checked windows SDK and C++ boxes. I dont k so what can I do to solve this problem? I’m using windows 11
error: failed to run custom build command for `wepoll-ffi v0.1.2`
Caused by:
process didn't exit successfully: `C:\Users\ali07\AppData\Local\Temp\cargo-installitFylM\release\build\wepoll-ffi-cc6f1ac54f6b60ea\build-script-build` (exit code: 1)
--- stdout
TARGET = Some("x86_64-pc-windows-gnu")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-pc-windows-gnu")
CC_x86_64-pc-windows-gnu = None
CC_x86_64_pc_windows_gnu = None
HOST_CC = None
CC = None
CFLAGS_x86_64-pc-windows-gnu = None
CFLAGS_x86_64_pc_windows_gnu = None
HOST_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("false")
CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
running: "gcc.exe" "-O0" "-ffunction-sections" "-fdata-sections" "-m64" "-Wall" "-Wextra" "-DNULL_OVERLAPPED_WAKEUPS_PATCH" "-o" "C:\\Users\\ali07\\AppData\\Local\\Temp\\cargo-installitFylM\\release\\build\\wepoll-ffi-74b6f87d03081710\\out\\vendor/wepoll/wepoll.o" "-c" "vendor/wepoll/wepoll.c"
--- stderr
error occurred: Failed to find tool. Is `gcc.exe` installed? (see https://github.com/alexcrichton/cc-rs#compile-time-requirements for help)
warning: build failed, waiting for other jobs to finish...
error: failed to compile `sqlx-cli v0.5.9`, intermediate artifacts can be found at `C:\Users\ali07\AppData\Local\Temp\cargo-installitFylM`
Caused by:
build failed
The line TARGET = Some("x86_64-pc-windows-gnu") means cargo is trying to build your project with a gnu toolchain (gcc). However if you have only installed the msvc (visual studio) c++ compiler then you will not have gcc.
There are two solutions:
A: Easiest: Tell cargo to use msvc
Add the --target flag to use msvc instead:
cargo install sqlx-cli --target x86_64-pc-windows-msvc
B: Install gcc
This depends on your development envrionment. If you are using cygwin then you have to rerun the cygwin setup and make sure to install gcc during the setup process. See: https://superuser.com/questions/304541/how-to-install-new-packages-on-cygwin

Statically linking ffmpeg-sys on Amazon Linux fails with undefined references

My project depends on the ffmpeg-sys crate which is configured to build statically, as follows:
[dependencies.ffmpeg-sys]
version = "3.4.1"
default-features = false
features = ["avcodec", "avformat", "swresample", "build", "static"]
My project consists of a single simple file:
extern crate ffmpeg_sys;
use ffmpeg_sys::av_register_all;
fn main() {
unsafe { av_register_all() };
println!("Hello, world!");
}
When compiling with cargo build I get the following error:
Compiling sample v0.1.0 (file:///home/ec2-user/sample)
error: linking with 'cc' failed: exit code: 1 | = note: "cc"
"-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L"
[... elided for clarity ...]
In function 'sample::main::hbbb19855251826d6':
/home/ec2-user/sample/src/main.rs:6: undefined reference to 'av_register_all'
collect2: error: ld returned 1 exit status
The required static libraries, libavformat.a and friends, are found in the target/build/debug folder, showing that ffmpeg-sys successfully compiled the libraries.
Here's the rustc command that is failing:
Caused by: process didn't exit successfully: 'rustc --crate-name
sample src/main.rs --crate-type bin --emit=dep-info,link -C
debuginfo=2 -C metadata=250bf40eb277d05a -C
extra-filename=-250bf40eb277d05a --out-dir
/home/ec2-user/sample/target/debug/deps -C
incremental=/home/ec2-user/sample/target/debug/incremental -L
dependency=/home/ec2-user/sample/target/debug/deps --extern
ffmpeg_sys=/home/ec2-user/sample/target/debug/deps/libffmpeg_sys-fa3ff87f80f2d27e.rlib
-L native=/home/ec2-user/sample/target/debug/build/ffmpeg-sys-0b3c813f29a9a20e/out/dist/lib'
(exit code: 1)
libffmpeg_sys-fa3ff87f80f2d27e.rlib is 207M and I assume therefore contains all the statically compiled ffmpeg code.
This only happens when I build on an Amazon Linux instance. Compiling on my regular Fedora 28 desktop results in a working binary.
How would I go about figuring out the root cause of this error?
I solved this problem by building llvm 6.0.1 and then rebuilding with LIBCLANG_PATH set to point to the newer version.
It would appear that rustc has a minimum version requirement on libclang.so, but I could not find an official source documenting that. The version installed on amazon-linux is 3.6.2 which is evidently too old.

Resources