š¾ 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
ā¬ ļø Previous capture (2023-12-28)
ā”ļø Next capture (2024-08-18)
-=-=-=-=-=-=-
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]
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.
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