rustexits/build.rs

80 lines
2.5 KiB
Rust
Raw Permalink Normal View History

2024-02-11 20:07:09 +00:00
/*
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* This file incorporates work covered by the following copyright and permission
* notice:
*
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: FSFAP
*
* Copying and distribution of this file, with or without modification, are
* permitted in any medium without royalty provided the copyright notice and
* this notice are preserved. This file is offered as-is, without any
* warranty.
*/
use std::{
2024-04-25 02:18:56 +00:00
io::{ Error, Write },
2024-04-25 02:34:57 +00:00
process::{ Command, Stdio },
2024-02-11 20:07:09 +00:00
path::PathBuf,
};
use bindgen::{ Builder, CargoCallbacks, MacroTypeVariation };
2024-04-25 02:18:56 +00:00
fn main() -> Result<(), Error> {
2024-02-11 20:07:09 +00:00
let mut header = String::new();
/* Locate sysexits.h using cpp(1) */
let mut process = Command::new("cpp")
.arg("-M")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
if let Some(mut child_stdin) = process.stdin.take() {
child_stdin.write_all("#include <sysexits.h>\n".as_bytes())?;
}
let output = process.wait_with_output()?.stdout;
2024-04-25 02:34:57 +00:00
let headers = String::from_utf8(output).map_err(|e| {
Error::other(e.to_string())
})?;
2024-02-11 20:07:09 +00:00
/* Split headers by spaces because cpp(1) returns more than one */
for h in headers.split(' ') {
if h.contains("sysexits.h") {
header = h.trim().to_string();
/* If sysexits.h cannot be located, then use the dummy sysexits.h */
} else { header = "src/sysexits.h".to_string() }
}
/* Make sysexits bindings depend only on core and use signed integers for
* compatibility with Rusts exit() implementation */
2024-04-25 02:37:44 +00:00
let bindings = Builder::default()
2024-02-11 20:07:09 +00:00
.use_core()
.default_macro_constant_type(MacroTypeVariation::Signed)
2024-04-25 02:37:44 +00:00
.header(header)
.parse_callbacks(Box::new(CargoCallbacks::new()))
.generate()
.map_err(|e| Error::other(e.to_string()))?;
2024-02-11 20:07:09 +00:00
2024-04-25 02:37:44 +00:00
bindings.write_to_file(PathBuf::from("src/").join("lib.rs"))?;
2024-02-11 20:07:09 +00:00
Ok(())
}