Skip to content

Use target from env in build.rs #1003

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 2 commits into from
Closed

Use target from env in build.rs #1003

wants to merge 2 commits into from

Conversation

ethanfrey
Copy link
Contributor

@ethanfrey ethanfrey commented Nov 22, 2019

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 in build.rs. This consistently detected the os as linux, even when I did cargo build --target x86_64-apple-darwin. However, the TARGET 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 to macos. The solution here is to rely on the CARGO_CFG_TARGET_{OS,ARCH} environmental variables in build.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.

  • Add a short description of the the change to the CHANGELOG.md file

@ethanfrey
Copy link
Contributor Author

To demo this working, you can check out the code from CosmWasm/wasmvm#14

and run make docker-image to build the cross-compile environment
and make docker-build to build for linux and osx.

Output should be api/libgo_cosmwasm.{so,dylib}. This compiling shows that the build.rs file now builds a valid output. If you have a recent version of go installed, you can try make test to ensure that the cross-compiled dylib actually behaves correctly. (A friend has manually verified this).

@nlewycky
Copy link
Contributor

bors r+

@bors
Copy link
Contributor

bors bot commented Nov 22, 2019

Merge conflict

@ethanfrey
Copy link
Contributor Author

This was fixed by a different PR and included in the 0.11.0 release. Thanks for the update and now cross-compiling works much better.

@ethanfrey ethanfrey closed this Nov 29, 2019
@ghost ghost mentioned this pull request Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants