šŸ’¾ Archived View for dcreager.net ā€ŗ languages ā€ŗ rust ā€ŗ build-script-links.gmi captured on 2024-03-21 at 15:15:22. Gemini links have been rewritten to link to archived content

View Raw

More Information

ā¬…ļø Previous capture (2023-12-28)

āž”ļø Next capture (2024-08-18)

-=-=-=-=-=-=-

Build script links

2023-11-15

You have an ā€œupstreamā€ crate that wraps a C library and provides Rust bindings for it. You also have a ā€œdownstreamā€ crate that wraps a different C library. The downstream C library depends on the upstream C library ā€” for example, to compile the downstream C code, you need access to the header files of the upstream C code.

The problem: How do you tell the compiler where to find the upstream crate's header files when compiling the downstream library?

The answer: the ā€˜package.linksā€™ field and ā€œbuild script metadataā€. The upstream library needs to set ā€˜package.linksā€™ in its ā€˜Cargo.tomlā€™. You can choose any value (which I'll call ā€˜LINK_NAMEā€™), but convention is that it's the name of the library that the Rust crate is providing bindings for. In the upstream library's ā€˜build.rsā€™, you output additional cargo metadata:

cargo:VAR_NAME=VALUE

Then, in your downstream library, this custom metadata will be available in an environment variable that you can read: ā€˜DEP_${LINK_NAME}_${VAR_NAME}ā€™.

For this compilation example, you output a carefully named ā€˜includeā€™ metadata variable, whose value is the actual location of the upstream header files. In your downstream ā€˜build.rsā€™, you read the ā€˜DEP_${LINK_NAME}_INCLUDEā€™ env var, and add that to the compiler's include search path.

The ā€˜linksā€™ manifest key [The Cargo book]

An example

I encountered this situation specifically with the upstream ā€˜luaā€™ crate, which provides Rust bindings for the Lua programming language, and the downstream ā€˜ltreesitterā€™ crate, which provides Lua bindings to tree-sitter, which I wanted to make available to a Lua environment managed by Rust code.

ā€˜luaā€™ crate

ā€˜ltreesitterā€™ Lua package

My ā€˜ltreesitterā€™ fork with Rust bindings

I had to patch the ā€˜luaā€™ crate to output the custom metadata, and then use the resulting env var in the ā€˜ltreesitterā€™ build script.

Output the custom metadata upstream

Use the custom metadata downstream

..