Skip to content

Commit

Permalink
build(cargo): Add cbindgen support via build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Oct 7, 2022
1 parent 7da5544 commit 4ac2324
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 2 deletions.
193 changes: 191 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rekordcrate-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ exclude = [".*"]
[dependencies]
rekordcrate = { version = "0.1.1", path = "../rekordcrate" }

[build-dependencies]
cbindgen = "0.24.3"

[lib]
crate_type = ["cdylib"]
36 changes: 36 additions & 0 deletions rekordcrate-ffi/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022 Jan Holthuis <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy
// of the MPL was not distributed with this file, You can obtain one at
// http://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0

use std::env;
use std::path::PathBuf;

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let output_file = target_dir()
.join("include")
.join("rekordcrate.h")
.display()
.to_string();

cbindgen::Builder::new()
.with_crate(crate_dir)
.generate()
.expect("Unable to generate bindings")
.write_to_file(&output_file);
}

/// Find the location of the `target/` directory. Note that this may be
/// overridden by `cmake`, so we also need to check the `CARGO_TARGET_DIR`
/// variable.
fn target_dir() -> PathBuf {
if let Ok(target) = env::var("CARGO_TARGET_DIR") {
PathBuf::from(target)
} else {
PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target")
}
}

0 comments on commit 4ac2324

Please sign in to comment.