-
Notifications
You must be signed in to change notification settings - Fork 88
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
Support different lib.crate-type
#227
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few questions!
#[panic_handler] | ||
fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are these two lines for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Added a code suggestion; worth having the clarification near the code.
@@ -174,7 +174,7 @@ rec | |||
attrs = | |||
# 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see! I believe this will break on cargo tomls that have lib = ...
. Can you give some example Cargo.toml
s 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any example which uses wasm-bindgen
for instance:
https://github.com/rustwasm/wasm-bindgen/tree/main/examples
@@ -209,7 +209,11 @@ rec | |||
pushd $out/$member > /dev/null | |||
mkdir -p src | |||
# 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#![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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
#[panic_handler] | ||
fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Added a code suggestion; worth having the clarification near the code.
@@ -174,7 +174,7 @@ rec | |||
attrs = | |||
# 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see! I believe this will break on cargo tomls that have lib = ...
. Can you give some example Cargo.toml
s 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-deps
derivation strips the[lib]
section of theCargo.toml
, so onlylib*.d
andlib*.rlib
files 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 forcdylib
in order to generate a wasm binary.