-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
64 lines (53 loc) · 2 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::path::PathBuf;
use anyhow::anyhow;
use anyhow::Context;
use anyhow::Result;
use pkg_config::Library;
const HEADER_NAME: &str = "trace-cmd.h";
const OUT_FILENAME: &str = "bindings.rs";
fn package_info() -> Result<(PathBuf, Library)> {
let library = pkg_config::probe_library("libtracecmd").context("failed to probe libracecmd")?;
for path in &library.include_paths {
let header_path = path.join(HEADER_NAME);
if header_path.exists() {
return Ok((header_path, library));
}
}
Err(anyhow!("{HEADER_NAME} not found"))
}
fn main() -> Result<()> {
let (header_path, library) = package_info()?;
println!("cargo:rerun-if-changed={}", header_path.as_os_str().to_str().unwrap());
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src");
let bindings = bindgen::Builder::default()
.header(header_path.to_str().unwrap())
.derive_default(true)
.clang_args(
library
.include_paths
.iter()
.map(|path| format!("-I{}", path.to_string_lossy())),
)
.generate()
.expect("failed to generate bindings");
let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path.join(OUT_FILENAME))?;
for lib in library.link_files {
println!("cargo:rustc-link-lib=dylib={:?}", lib.as_os_str());
}
Ok(())
}