Choosing shared or static library with Cargo - shared-libraries

I am attempting to modify Racer to emit a shared library instead of an rlib.
To do this, I added crate-type = ["dylib"] to the [lib] section of the Cargo manifest, then ran cargo build --lib. This worked great, and libracer.so was emitted.
Unfortunately, now I could not build the Racer binary, which depends on a static version of the library. Running cargo build complains:
Compiling racer v1.0.0 (file:///home/georgev/dotfiles/vim/bundle/racer)
error: cannot satisfy dependencies so `std` only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `core` only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `collections` only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `rustc_unicode` only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `alloc` only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `libc` only shows up once
help: having upstream crates all available in one format will likely make this go away
error: cannot satisfy dependencies so `rand` only shows up once
help: having upstream crates all available in one format will likely make this go away
error: aborting due to 7 previous errors
Could not compile `racer`.
I changed the crate-type to ["dylib", "bin"], which allowed the compilation to succeed. However, cargo build --lib will not emit a shared library anymore (only an rlib).
How can I specify which type of library I would like to build, while still allowing said library to be built statically for inclusion in an executable?

bin is not a valid crate-type value. The valid values are rlib, lib, staticlib, and dylib. Changing the crate type to
crate-type = ["dylib", "rlib"]
will cause the behavior you're after.
The reason that only an rlib is emitted with ["dylib", "bin"] is because there is currently a Cargo bug that causes invalid values for crate-type to only produce an rlib. I've filed a pull request to fix the issue.

Related

How does cargo manage dependency versions?

I was reading this at the guessing game tutorial inside the rust book
Ensuring Reproducible Builds with the Cargo.lock File
Cargo has a mechanism that ensures you can rebuild the same artifact every time you or anyone else builds your code: Cargo will use only the versions of the dependencies you specified until you indicate otherwise. For example, say that next week version 0.8.4 of the rand crate comes out, and that version contains an important bug fix, but it also contains a regression that will break your code. To handle this, Rust creates the Cargo.lock file the first time you run cargo build, so we now have this in the guessing_game directory.
When you build a project for the first time, Cargo figures out all the versions of the dependencies that fit the criteria and then writes them to the Cargo.lock file. When you build your project in the future, Cargo will see that the Cargo.lock file exists and use the versions specified there rather than doing all the work of figuring out versions again. This lets you have a reproducible build automatically. In other words, your project will remain at 0.8.3 until you explicitly upgrade, thanks to the Cargo.lock file.
but didn't quite understand how cargo garantees that we have/need the correct version of a dependency. For instance, if we put in the [dependecies] rand="0.8.3" it will not exactly download the crate at this version, but the crate the match the needs of our program but it's compatible with this version(?)
Please clarify this logic!
When specifying a crate in Cargo.toml, you can give an exact version (e.g. =0.8.3) or more general indications (e.g. 0.8.*).
If you specify rand = "=0.8.3" in Cargo.toml, then cargo will take version 0.8.3 (note the extra = inside the version requirement). But if you specify rand = "*", then the first time you build your crate it will take the latest version and write this version to Cargo.lock. That way if you rebuild your crate later cargo will reuse the same version even if a new version has been published on crates.io in the meantime.
Note btw that specifying rand = "0.8.3" does not mean "exactly version 0.8.3", but instead means "any version >=0.8.3 and <0.9": link.

Error adding Custom RPCs for custom pallet Substrate

I've been working with parity's contracts node (latest version) and the substrate template node (tag polkadot-v0.9.18), both present the same issue when compiling.
I have a very simple pallet that stores certain items. The main structure is the following:
#[pallet::storage]
#[pallet::getter(fn items)]
/// 'Mapping' Item ID -> Item Data
pub(crate) type Items<T: Config> = StorageMap<_, Twox64Concat, T::Hash, Item<T>>;
I was trying to add a simple RPC method following this guides https://core.tetcoin.org/recipes/custom-rpc.html#rpc-to-call-a-runtime-api and https://core.tetcoin.org/recipes/runtime-api.html
I also checked some projects that already have custom RPC calls implementations, like de subsocial node and I have pretty much the same structure and dependencies.
My rpc method does nothing but return a number 2 just to make sure it works, but it doesn't. This is what the pallets directory looks like:
pallets directory
When I try to compile, the following error shows
error: the wasm32-unknown-unknown target is not supported by default, you may need to
enable the "js" feature. For more information see:
https://docs.rs/getrandom/#webassembly-support
I don't even use that module, but I've read that it is used somewhere as an indirect dependency.
I'm compiling my project with the following command
cargo build --release
Checking the documentation regarding the 'getrandom' crate issue, I added the following dependency in the Cargo.toml (I tried adding it in every Cargo.toml within the project, individually, by pairs, ...)
getrandom = { version = "0.2", features = ["js"] }
Then another error shows up:
error: failed to run custom build command for secp256k1-sys v0.4.1
Which again, doesn't make any sense to me.
The project itself has nothing but the node template base and a new pallet that implements a create and transfer function. Without the RPC implementation, it works perfectly using the Polkadot App, but as soon as I include the custom rpc, it just doesn't compile.
This is my rust configuration (rustup show)
installed toolchains
--------------------
stable-x86_64-apple-darwin (default)
nightly-2021-11-04-x86_64-apple-darwin
nightly-x86_64-apple-darwin
active toolchain
----------------
stable-x86_64-apple-darwin (default)
rustc 1.59.0 (9d1b2106e 2022-02-23)
I haven't found anyone who is dealing with this kind of issue, and I don't know where the problem might be.
This is the first issue logs:
error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
--> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:229:9
|
229 | / compile_error!("the wasm32-unknown-unknown target is not supported by \
230 | | default, you may need to enable the \"js\" feature. \
231 | | For more information see: \
232 | | https://docs.rs/getrandom/#webassembly-support");
| |________________________________________________________________________^
error[E0433]: failed to resolve: use of undeclared crate or module `imp`
--> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:256:5
|
256 | imp::getrandom_inner(dest)
| ^^^ use of undeclared crate or module `imp`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: build failed
Current status (to reproduce error): https://github.com/andresvsm/substrate-pallet-rpc/tree/items-branch
Sometimes, you can get this error from a deep dependency of another dependency, e.g. when you really build for a wasm32-unknown-unknown target, and getrandom is linked but even not used. It can be fixed (worked around) with the following trick:
In Cargo.toml, add this line:
[dependencies]
getrandom = {version = "0.2", default-features = false, features = ["custom"]}
It tells the compiler to use a dummy implementation inside of getrandom.
Fixed for me when I added "default features = 'false'" into my Cargo.toml under the dependency in question.

Unable to build rust's Rocket project because of framework dependency error

I'm following this tutorial here and have also looked into Rocket's official guide of setting up a web server.
I've set as default the nightly builds. But I get the following error:
error: failed to select a version for the requirement `ring = "^0.11.0"`
candidate versions found which didn't match: 0.16.11, 0.16.10, 0.16.9, ...
location searched: crates.io index
required by package `cookie v0.9.1`
... which is depended on by `rocket v0.3.6`
... which is depended on by `my-project`
Obviously there's some mismatch with the dependencies, but since ring is something required by the framework itself, I'm not sure how to debug this... furthermore, I'm using the latest versions of cargo and rust:
cargo 1.43.0-nightly (... 2020-02-18)
rustc 1.43.0-nightly (... 2020-02-21)
ring v0.11.0 was yanked from crates.io (see this issue for some background). Since some versions rocket depended on versions of ring that were yanked, those versions of rocket will no longer work.
Upgrading to rocket v0.4.0 (or the latest v0.4.2) should solve this issue.

Why is this unrelated crate breaking my project?

$ cargo build
Downloading pear_codegen v0.0.16
Compiling pear_codegen v0.0.16
Compiling ring v0.11.0
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> /.cargo/registry/src/github.com-1ecc6299db9ec823/pear_codegen-0.0.16/src/lib.rs:317:9
|
317 | ExprKind::Block(block) => {
| ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1
error: aborting due to previous error
For more information about this error, try `rustc --explain E0023`.
error: Could not compile `pear_codegen`.
warning: build failed, waiting for other jobs to finish...
error: build failed
When I try to compile my project I get an error that a crate that isn't in my cargo.toml is broken. Why is this unrelated crate breaking my project?
You're using Rust nightly, and the crate in question used to depend (in version 0.0.16) on the perma-unstable API of the Rust compiler, which was changed by a nightly you updated to (or just installed).
The author appears to have recently rewritten the crate to avoid that dependency, which means further breakage should be avoided, starting with version 0.0.17 of the crate.
cargo update
fixed the issue for me
https://github.com/SergioBenitez/Pear/issues/7
thanks to reddit user /u/usernamedottxt

native library `glib` is being linked to by more than one package, and can only be linked to by one package [duplicate]

I'm facing this problem when I try to cargo build:
error: native library openssl is being linked to by more than one version of the same package, but it can only be linked once; try updating or pinning your dependencies to ensure that this package only shows up once
openssl-sys v0.6.7
openssl-sys v0.7.13
Cargo and Rust versions:
$ cargo --version
cargo 0.11.0-nightly (3ff108a 2016-05-24)
$ rustc --version
rustc 1.11.0-nightly (7746a334d 2016-05-28)
Files:
Cargo.toml
Cargo.lock
can't get why this doesn't compile and how to solve this problem.
Thank you!
The way that linking works, you can only have a single version of a native library linked, otherwise you end up with duplicate symbols. Cargo's links manifest key helps prevent you from accidentally linking to the same set of symbols twice.
To solve it, you need to read through your Cargo.lock (it's not a difficult file format to understand). Find the crates that have the offending library as a dependency and note which ones have conflicting versions.
Then you have to manually resolve your dependencies so that their dependencies use the same version of the native library.
In this case, the important aspects of the dependency chain are:
server (0.0.1) => cookie (0.2.4) => openssl (0.7.13)
=> hyper (0.6.16) => cookie (0.1.21) => openssl (0.6.7)
To fix it, modify your Cargo.toml to use the same version of cookie as hyper. Then you will implicitly get the same version of openssl.
To be honest, this is one of the roughest parts of Rust at the moment. At least this version of the "multiple different versions of the same crate" strangeness provides a straight-forward Cargo error.

Resources