Support different lib.crate-type - #227
Conversation
| #[panic_handler] | ||
| fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } |
There was a problem hiding this comment.
what are these two lines for?
There was a problem hiding this comment.
If you compile with crate-type=cdylib, and also use no_std, the compilation will fail because no panic handler has been provided (the bit that unwinds the stack trace, usually provided by the standard library). This is just a dummy stub to get compilation to succeed. See https://doc.rust-lang.org/nomicon/panic-handler.html for more details.
There was a problem hiding this comment.
Thanks! Added a code suggestion; worth having the clarification near the code.
| # Since we pretend everything is a lib, we remove any mentions | ||
| # of binaries | ||
| removeAttrs cargotoml [ "bin" "example" "lib" "test" "bench" "default-run" ] | ||
| removeAttrs cargotoml [ "bin" "example" "test" "bench" "default-run" ] |
There was a problem hiding this comment.
what happens if the project mentions a [lib] that isn't src/lib.rs? Won't cargo try to build that and, not finding any files, fail?
There was a problem hiding this comment.
To be honest, this change in particular is the one I'm not sure about. In order for WASM dependencies to be built, lib.crate-type needs to be retained. This was merely the quickest fix I could think of, I didn't really consider the other effects, and perhaps there's a better way to achieve the same goal.
There was a problem hiding this comment.
I see! I believe this will break on cargo tomls that have lib = .... Can you give some example Cargo.tomls that use lib.crate-type? Maybe we can just remove e.g. the path of the lib attribute?
There was a problem hiding this comment.
Any example which uses wasm-bindgen for instance:
https://github.com/rustwasm/wasm-bindgen/tree/main/examples
| # Avoid accidentally pulling `std` for no-std crates. | ||
| echo '#![no_std]' >src/lib.rs | ||
| cat <<EOF >src/lib.rs | ||
| #![no_std] |
There was a problem hiding this comment.
| #![no_std] | |
| #![no_std] | |
| # If you compile with crate-type=cdylib, and also use no_std, | |
| # the compilation will fail because no panic handler has been | |
| # provided (the bit that unwinds the stack trace, usually provided | |
| # by the standard library). This is just a dummy stub to get | |
| # compilation to succeed. | |
| # See https://doc.rust-lang.org/nomicon/panic-handler.html for more details. |
| #[panic_handler] | ||
| fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } |
There was a problem hiding this comment.
Thanks! Added a code suggestion; worth having the clarification near the code.
| # Since we pretend everything is a lib, we remove any mentions | ||
| # of binaries | ||
| removeAttrs cargotoml [ "bin" "example" "lib" "test" "bench" "default-run" ] | ||
| removeAttrs cargotoml [ "bin" "example" "test" "bench" "default-run" ] |
There was a problem hiding this comment.
I see! I believe this will break on cargo tomls that have lib = .... Can you give some example Cargo.tomls that use lib.crate-type? Maybe we can just remove e.g. the path of the lib attribute?
|
@NickHu Are you still working on this? If not, I'll pick it up because I need this working 😆 |
This is particularly important for the case of WASM binaries, which are necessarily compiled with
crate-type=cdylib. Without this, when compiling forCARGO_BUILD_TARGET=wasm32-unknown-unknown, the-depsderivation strips the[lib]section of theCargo.toml, so onlylib*.dandlib*.rlibfiles artifacts are generated undertarget/. Then when the main derivation is compiled, the[lib]section is not stripped, so it must recompile all the dependencies again forcdylibin order to generate a wasm binary.