Skip to content
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

Add the metadata needed by cargo-c #176

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libz-rs-sys-cdylib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ default = ["c-allocator"] # when used as a cdylib crate, use the c allocator
c-allocator = ["libz-rs-sys/c-allocator"] # by default, use malloc/free for memory allocation
rust-allocator = ["libz-rs-sys/rust-allocator", "libz-rs-sys/std"] # by default, use the rust global alloctor for memory allocation
custom-prefix = ["libz-rs-sys/custom-prefix"] # use the LIBZ_RS_SYS_PREFIX to prefix all exported symbols
capi = []

[dependencies]
libz-rs-sys = { path = "../libz-rs-sys", default-features = false }

[package.metadata.capi.library]
name = "z_rs"

[package.metadata.capi.header]
enabled = false

[package.metadata.capi.pkg_config]
name = "libz_rs"
filename = "libz_rs"
41 changes: 38 additions & 3 deletions libz-rs-sys-cdylib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Build `zlib-rs` as a drop-in replacement for the zlib dynamic library

```sh
# build the cdylib
# using `cargo build` will work but has limitations, see below
cargo build --release

# the extension of a cdylib varies per platform
Expand All @@ -14,8 +15,9 @@ cc zpipe.c target/release/libz_rs.so
```

By default this build uses libc `malloc`/`free` to (de)allocate memory, and only depends on the rust `core` library.
See below for the available feature flags.

## Features
## Feature Flags

### Allocators

Expand Down Expand Up @@ -74,6 +76,39 @@ the standard library has the following limitations:

- CPU feature detection is currently disabled. This is true for both compile-time and run-time feature detection.
This means `zlib-rs` will not make use of SIMD or other custom instructions.
- The `rust-allocator` should not be used. It internally enables the standard library, causing issues. Using `c-allocator`
or not providing an allocator at build time is still supported.On embedded it is most common to provide a custom allocator
- The `rust-allocator` should not be used. It internally enables the standard library, causing issues. Using `c-allocator`
or not providing an allocator at build time is still supported.On embedded it is most common to provide a custom allocator
that "allocates" into a custom array.

## Build for Distribution

A `cargo build` currently does not set some fields that are required or useful when using a dynamic library from C.
For instance, the soname and version are not set by a standard `cargo build`.

To build a proper, installable dynamic library, we recommend [`cargo-c`](https://github.com/lu-zero/cargo-c):

```
cargo install cargo-c
```

This tool deals with setting fields (soname, version) that a normal `cargo build` does not set (today).
It's configuration is in the `Cargo.toml`, where e.g. the library name or version can be changed.

```
> cargo cbuild --release
Compiling zlib-rs v0.2.1
Compiling libz-rs-sys v0.2.1
Compiling libz-rs-sys-cdylib v0.2.1
Finished `release` profile [optimized] target(s) in 1.86s
Building pkg-config files
> tree target
target
├── CACHEDIR.TAG
└── x86_64-unknown-linux-gnu
└── release
├── libz_rs.a
├── libz_rs.d
├── libz_rs.pc
├── libz_rs.so
└── libz_rs-uninstalled.pc
Comment on lines +104 to +113
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lu-zero is there a reason cargo cbuild puts its things in target/<target tripple>/release rather than just target/release? it's annoyingly inconsistent with a standard cargo build.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required until cargo learns to differentiate the RUSTFLAGS for the build.rs and the RUSTFLAGS for the host.

using cargo cbuild -v it emits a note with the uninstalled pkg-config path.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you aware of a cargo issue that describes the problem?

I'll be meeting with some members of the cargo team in October, and I'd like to prepare some concrete things to make progress on. Given how crucial this is for sneaking rust into C code bases I think progress can be made.

Copy link
Author

@lu-zero lu-zero Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rust-lang/cargo#4423 is another way to see the problem and rust-lang/cargo#9452 is how to solve it.

But normally cinstall is used to produce the .a that is then linked in the bigger C project. Even C projects support cross compilation ^^.

I guess I should really prepare a blogpost showing how to use cargo-c in many deployment scenarios :)

```
Loading