feat: Add crate nix-c-raw
See README. Most of this is in accordance with the bindgen introduction. The c_headers function was taken from Zach Mitchell's work on bindings. Co-authored-by: Zach Mitchell <zmitchell@fastmail.com> (cherry picked from commit 83d8ae6b26d341bbd452a77d7361f58c06a2d322)
This commit is contained in:
parent
4dca986fe2
commit
858bec68bc
7 changed files with 517 additions and 0 deletions
11
rust/nix-c-raw/Cargo.toml
Normal file
11
rust/nix-c-raw/Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "nix-c-raw"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
build = "build.rs"
|
||||
|
||||
[lib]
|
||||
|
||||
[build-dependencies]
|
||||
bindgen = "0.69.4"
|
||||
pkg-config = "0.3.30"
|
||||
12
rust/nix-c-raw/README.md
Normal file
12
rust/nix-c-raw/README.md
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# nix-c-raw
|
||||
|
||||
This crate contains generated bindings for the Nix C API.
|
||||
**You should not have to use this crate directly,** and so you should probably not add it to your dependencies.
|
||||
Instead, use the `nix-util`, `nix-store` and `nix-expr` crates, which _should_ be sufficient.
|
||||
|
||||
## Design
|
||||
|
||||
Rust bindgen currently does not allow "layered" libraries to be split into separate crates.
|
||||
For example, the expr crate would have all-new types that are distinct and incompatible with the store crate.
|
||||
|
||||
Ideally bindgen will support reusing already generated modules, and we could move the code generation into the appropriate crates, so that the system dependencies of each crate become accurate.
|
||||
42
rust/nix-c-raw/build.rs
Normal file
42
rust/nix-c-raw/build.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use bindgen;
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
// Tell cargo to invalidate the built crate whenever the wrapper changes
|
||||
println!("cargo:rerun-if-changed=include/nix-c-raw.h");
|
||||
|
||||
// https://rust-lang.github.io/rust-bindgen/library-usage.html
|
||||
let bindings = bindgen::Builder::default()
|
||||
.header("include/nix-c-raw.h")
|
||||
// Find the includes
|
||||
.clang_args(c_headers())
|
||||
// Tell cargo to invalidate the built crate whenever any of the
|
||||
// included header files changed.
|
||||
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
|
||||
// Finish the builder and generate the bindings.
|
||||
.generate()
|
||||
// Unwrap the Result and panic on failure.
|
||||
.expect("Unable to generate bindings");
|
||||
|
||||
// Write the bindings to the $OUT_DIR/bindings.rs file.
|
||||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||
bindings
|
||||
.write_to_file(out_path.join("bindings.rs"))
|
||||
.expect("Couldn't write bindings!");
|
||||
}
|
||||
|
||||
fn c_headers() -> Vec<String> {
|
||||
let mut args = Vec::new();
|
||||
// args.push("-isystem".to_string());
|
||||
for path in pkg_config::probe_library("nix-expr-c")
|
||||
.unwrap()
|
||||
.include_paths
|
||||
.iter()
|
||||
{
|
||||
args.push(format!("-I{}", path.to_str().unwrap()));
|
||||
}
|
||||
// write to stderr for debugging
|
||||
eprintln!("c_headers: {:?}", args);
|
||||
args
|
||||
}
|
||||
3
rust/nix-c-raw/include/nix-c-raw.h
Normal file
3
rust/nix-c-raw/include/nix-c-raw.h
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#include <nix_api_util.h>
|
||||
#include <nix_api_store.h>
|
||||
#include <nix_api_expr.h>
|
||||
5
rust/nix-c-raw/src/lib.rs
Normal file
5
rust/nix-c-raw/src/lib.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#![allow(non_upper_case_globals)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||
Loading…
Add table
Add a link
Reference in a new issue