Use target from env in build.rs #1003
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Cross compile is broken for wasmer-runtime-core. I tried compiling from linux -> macos (with lots of setup of dependencies first) and it compiled, but errored on the
build.rs
, which was using linux assembly instead of macos assembly.Long explanation here to capture (and share) my learnings on this:
Digging into this, it turns out the reason was the use of
if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
to check for the target inbuild.rs
. This consistently detected the os as linux, even when I didcargo build --target x86_64-apple-darwin
. However, theTARGET
env variable was showing macos.I thought this was a cargo bug, but as @nlewycky explained, build.rs is compiled for the host system (the one doing compilation), and correctly sets the target_os to linux. While the rest of the source code will have
cfg(target_os)
set tomacos
. The solution here is to rely on theCARGO_CFG_TARGET_{OS,ARCH}
environmental variables inbuild.rs
if you want to detect the system for which the library is being compiled for. (Normally they are the same, this bug only shows up during cross-compilation).Review
I didn't add unit tests, here but am testing it when imported in another project where I have a cross-compile build system set up. I can link the example code here if you want to run.