Skip to content

Commit

Permalink
docs: add static vs dynamic docs, cargo-c instructions
Browse files Browse the repository at this point in the history
This commit details our current thinking w.r.t static vs dynamic linking
for rustls-ffi. It also adds initial documentation of using `cargo-c` to
build a dynamic library. In particular we emphasize that:

a) we make **no** ABI stability guarantees
b) the dynamic library/cargo-c support is considered experimental
  • Loading branch information
cpu committed Oct 16, 2023
1 parent fa06a4c commit f1888c2
Showing 1 changed file with 72 additions and 4 deletions.
76 changes: 72 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,37 @@ need to find another library to provide the cryptographic primitives.
# Build

You'll need to [install the Rust toolchain](https://rustup.rs/) version 1.60
or above and a C compiler (gcc and clang should both work). To build in optimized mode:
or above and a C compiler (gcc and clang should both work).

## Static Library

In its current for rustls-ffi's `Makefile` infrastructure will generate a static
system library (e.g. `--crate-type=staticlib`), producing a `.a` or `.lib` file
(depending on the OS).

We recommend using rustls-ffi as a static library as we make no guarantees of
[ABI](https://en.wikipedia.org/wiki/Application_binary_interface) stability across
versions at this time, and dynamic library support is considered **experimental**.

### Building a Static Library

To build a static library in optimized mode:

make

To install in /usr/local/:

sudo make install

To build in debug mode:
To build a static library in debug mode:

make PROFILE=debug

To link against the resulting library, on **Linux**:
To link against the resulting static library, on **Linux**:

-lrustls -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc

To link against the resulting library, on **macOS**:
To link against the resulting static library, on **macOS**:

-lrustls -framework Security -liconv -lSystem -lc -l

Expand All @@ -45,6 +59,60 @@ via](https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-c

RUSTFLAGS="--print native-static-libs" cargo build

## Dynamic Library

Using rustls-ffi as a static library has some downsides. Notably each application
that links the static library will need to be rebuilt for each update to rustls-ffi,
and duplicated copies of rustls-ffi will be included in each application.

Building rustls-ffi as a dynamic library (`--crate-type=cdylib`) can resolve these
issues, however this approach comes with its own trade-offs. We currently consider
this option **experimental**.

### ABI Stability

At this time rustls-ffi makes **no** guarantees about
[ABI](https://en.wikipedia.org/wiki/Application_binary_interface) stability.
Each release of rustls-ffi may introduce breaking changes to the ABI and so
the built library should use the exact rustls-ffi version as the dynamic library
[SONAME](https://en.wikipedia.org/wiki/Soname).

### Building a Dynamic Library

Since building a useful dynamic library is more complex than building a static
library, rustls-ffi uses [cargo-ci](https://github.com/lu-zero/cargo-c) in place
of the `Makefile` system used for the static library.

This takes care of:
* Generating the `rustls.h` header file.
* Building a `.so` or `.dylib` file (depending on the OS).
* Generating a [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/) `.pc` file.
* Installing the library and header files in the appropriate location.

If your operating system doesn't package `cargo-c` natively
(see [package availability](https://github.com/lu-zero/cargo-c#availability)),
you can install it with:

cargo install cargo-c

To build a dynamic library in optimized mode:

cargo capi build --release

To install in `/usr/local/`:

sudo cargo capi install

To build a static library in debug mode:

cargo capi build

To link against the resulting dynamic library, use `pkg-config` to populate your
`LDLIBS` and `CFLAGS` as appropriate:

pkg-config --libs rustls
pkg-config --cflags rustls

# Overview

Rustls doesn't do any I/O on its own. It provides the protocol handling, and
Expand Down

0 comments on commit f1888c2

Please sign in to comment.