This commit is contained in:
Robin Müller 2024-05-31 12:52:55 +02:00
parent 89a90f55b4
commit 40f813d0b3
Signed by: muellerr
GPG Key ID: A649FB78196E3849
6 changed files with 79 additions and 53 deletions

22
examples/autoconfig.h Normal file
View File

@ -0,0 +1,22 @@
#define CSP_POSIX 1
#define CSP_ZEPHYR 0
#define CSP_HAVE_STDIO 1
#define CSP_ENABLE_CSP_PRINT 1
#define CSP_PRINT_STDIO 1
#define CSP_REPRODUCIBLE_BUILDS 0
#define CSP_QFIFO_LEN 16
#define CSP_PORT_MAX_BIND 16
#define CSP_CONN_RXQUEUE_LEN 16
#define CSP_CONN_MAX 8
#define CSP_BUFFER_SIZE 256
#define CSP_BUFFER_COUNT 15
#define CSP_RDP_MAX_WINDOW 5
#define CSP_RTABLE_SIZE 10
#define CSP_USE_RDP 1
#define CSP_USE_HMAC 1
#define CSP_USE_PROMISC 1
#define CSP_USE_RTABLE 0
#define CSP_HAVE_LIBSOCKETCAN 0
#define CSP_HAVE_LIBZMQ 0

View File

@ -1,7 +1,7 @@
const CSP_CONN_RXQUEUE_LEN: usize = 16 pub const CSP_CONN_RXQUEUE_LEN: usize = 16;
const CSP_QFIFO_LEN: usize = 16 pub const CSP_QFIFO_LEN: usize = 16;
const CSP_PORT_MAX_BIND: usize = 16 pub const CSP_PORT_MAX_BIND: usize = 16;
const CSP_CONN_MAX: usize = 8 pub const CSP_CONN_MAX: usize = 8;
const CSP_BUFFER_SIZE: usize = 256 pub const CSP_BUFFER_SIZE: usize = 256;
const CSP_RDP_MAX_WINDOW: usize = 5 pub const CSP_RDP_MAX_WINDOW: usize = 5;
const CSP_RTABLE_SIZE: usize = 10 pub const CSP_RTABLE_SIZE: usize = 10;

View File

@ -1,21 +1,20 @@
use std::{env, path::PathBuf}; use std::{env, path::PathBuf};
use libcsp_cargo_build::Builder; use libcsp_cargo_build::{generate_autoconf_header_file, Builder};
fn main() { fn main() {
let out_dir = env::var("OUT_DIR").unwrap_or_default(); let out_dir = env::var("OUT_DIR").unwrap_or_default();
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap_or_default(); let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
let manifest_path = PathBuf::from(&manifest_dir);
let libcsp_path = "../lib/libcsp"; let libcsp_path = "../lib/libcsp";
let mut csp_builder = Builder::new(PathBuf::from(libcsp_path), PathBuf::from(&out_dir)); let mut csp_builder = Builder::new(PathBuf::from(libcsp_path), PathBuf::from(&out_dir));
let update_autoconf = match env::var("UPDATE_CSP_AUTOCONF") { // We always re-generate the header file.
Ok(update_autoconf) => update_autoconf == "1", generate_autoconf_header_file(manifest_path.clone(), &csp_builder.cfg)
Err(_e) => false, .expect("generating header file failed");
};
if update_autoconf {
csp_builder csp_builder
.generate_autoconf_rust_file(PathBuf::from(&manifest_dir)) .generate_autoconf_rust_file(manifest_path)
.expect("generating autoconfig.rs failed"); .expect("generating autoconfig.rs failed");
println!("cargo:warning=autoconfig.rs updated");
}
csp_builder.compile().expect("compiling libcsp failed"); csp_builder.compile().expect("compiling libcsp failed");
// If we change the libcsp build configuration, we need to re-run the build.
println!("cargo::rerun-if-changed=build.rs");
} }

View File

@ -1,6 +1,6 @@
use std::{ use std::{
io::{self, Write}, io::{self, Write},
path::PathBuf, path::{Path, PathBuf},
}; };
pub mod autoconf { pub mod autoconf {
@ -64,23 +64,23 @@ const ARCH_SRCS_UNIX: &[&str] = &[
]; ];
pub struct Config { pub struct Config {
have_stdio: bool, pub have_stdio: bool,
print_stdio: bool, pub print_stdio: bool,
reproducible_builds: bool, pub reproducible_builds: bool,
qfifo_len: u32, pub qfifo_len: u32,
port_max_bind: u32, pub port_max_bind: u32,
conn_rx_queue_len: u32, pub conn_rx_queue_len: u32,
conn_max: u32, pub conn_max: u32,
buffer_size: u32, pub buffer_size: u32,
buffer_count: u32, pub buffer_count: u32,
rdp_max_window: u32, pub rdp_max_window: u32,
rtable_size: u32, pub rtable_size: u32,
hmac: bool, pub hmac: bool,
rtable: bool, pub rtable: bool,
csp_print: bool, pub csp_print: bool,
promisc: bool, pub promisc: bool,
rdp: bool, pub rdp: bool,
yaml: bool, pub yaml: bool,
} }
impl Default for Config { impl Default for Config {
@ -112,7 +112,7 @@ pub struct Builder {
libcsp_path: PathBuf, libcsp_path: PathBuf,
libcsp_src_path_base: PathBuf, libcsp_src_path_base: PathBuf,
out_dir: PathBuf, out_dir: PathBuf,
cfg: Config, pub cfg: Config,
build: cc::Build, build: cc::Build,
} }
@ -136,7 +136,7 @@ impl Builder {
pub fn compile(&mut self) -> io::Result<()> { pub fn compile(&mut self) -> io::Result<()> {
if self.generate_autoconf_file { if self.generate_autoconf_file {
self.generate_autoconf_file()?; self.generate_autoconf_header_file_default_location()?;
} }
for src in SRCS_LIST { for src in SRCS_LIST {
let mut next_file = self.libcsp_src_path_base.clone(); let mut next_file = self.libcsp_src_path_base.clone();
@ -194,20 +194,25 @@ impl Builder {
} }
} }
pub fn generate_autoconf_file(&mut self) -> io::Result<()> { pub fn generate_autoconf_header_file_default_location(&mut self) -> io::Result<()> {
let mut autoconf_dir = self.out_dir.join("cfg"); let mut autoconf_dir = self.out_dir.join("cfg");
self.build.include(&autoconf_dir); self.build.include(&autoconf_dir);
autoconf_dir.push("csp"); autoconf_dir.push("csp");
std::fs::create_dir_all(&autoconf_dir)?; std::fs::create_dir_all(&autoconf_dir)?;
generate_autoconf_header_file(autoconf_dir, &self.cfg) generate_autoconf_header_file(&autoconf_dir, &self.cfg)
} }
pub fn generate_autoconf_rust_file(&self, out_dir: PathBuf) -> io::Result<()> { pub fn generate_autoconf_header_file(&mut self, dir: impl AsRef<Path>) -> io::Result<()> {
generate_autoconf_rust_file(out_dir, &self.cfg) generate_autoconf_header_file(dir, &self.cfg)
}
pub fn generate_autoconf_rust_file(&self, dir: impl AsRef<Path>) -> io::Result<()> {
generate_autoconf_rust_file(dir, &self.cfg)
} }
} }
pub fn generate_autoconf_header_file(out_dir: PathBuf, cfg: &Config) -> io::Result<()> { pub fn generate_autoconf_header_file(out_dir: impl AsRef<Path>, cfg: &Config) -> io::Result<()> {
let out_dir = out_dir.as_ref();
let mut autoconf_file_string = String::new(); let mut autoconf_file_string = String::new();
#[cfg(unix)] #[cfg(unix)]
autoconf_file_string.push_str("#define CSP_POSIX 1\n"); autoconf_file_string.push_str("#define CSP_POSIX 1\n");
@ -310,40 +315,41 @@ pub fn generate_autoconf_header_file(out_dir: PathBuf, cfg: &Config) -> io::Resu
Ok(()) Ok(())
} }
pub fn generate_autoconf_rust_file(out_dir: PathBuf, cfg: &Config) -> io::Result<()> { pub fn generate_autoconf_rust_file(out_dir: impl AsRef<Path>, cfg: &Config) -> io::Result<()> {
let out_dir = out_dir.as_ref();
let mut autoconf_file_string = String::new(); let mut autoconf_file_string = String::new();
autoconf_file_string.push_str(&format!( autoconf_file_string.push_str(&format!(
"const {}: usize = {}\n", "pub const {}: usize = {};\n",
autoconf::CFG_CONN_RXQUEUE_LEN, autoconf::CFG_CONN_RXQUEUE_LEN,
cfg.conn_rx_queue_len cfg.conn_rx_queue_len
)); ));
autoconf_file_string.push_str(&format!( autoconf_file_string.push_str(&format!(
"const {}: usize = {}\n", "pub const {}: usize = {};\n",
autoconf::CFG_QFIFO_LEN, autoconf::CFG_QFIFO_LEN,
cfg.qfifo_len cfg.qfifo_len
)); ));
autoconf_file_string.push_str(&format!( autoconf_file_string.push_str(&format!(
"const {}: usize = {}\n", "pub const {}: usize = {};\n",
autoconf::CFG_PORT_MAX_BIND, autoconf::CFG_PORT_MAX_BIND,
cfg.port_max_bind cfg.port_max_bind
)); ));
autoconf_file_string.push_str(&format!( autoconf_file_string.push_str(&format!(
"const {}: usize = {}\n", "pub const {}: usize = {};\n",
autoconf::CFG_CONN_MAX, autoconf::CFG_CONN_MAX,
cfg.conn_max cfg.conn_max
)); ));
autoconf_file_string.push_str(&format!( autoconf_file_string.push_str(&format!(
"const {}: usize = {}\n", "pub const {}: usize = {};\n",
autoconf::CFG_BUFFER_SIZE, autoconf::CFG_BUFFER_SIZE,
cfg.buffer_size cfg.buffer_size
)); ));
autoconf_file_string.push_str(&format!( autoconf_file_string.push_str(&format!(
"const {}: usize = {}\n", "pub const {}: usize = {};\n",
autoconf::CFG_RDP_MAX_WINDOW, autoconf::CFG_RDP_MAX_WINDOW,
cfg.rdp_max_window cfg.rdp_max_window
)); ));
autoconf_file_string.push_str(&format!( autoconf_file_string.push_str(&format!(
"const {}: usize = {}\n", "pub const {}: usize = {};\n",
autoconf::CFG_RTABLE_SIZE, autoconf::CFG_RTABLE_SIZE,
cfg.rtable_size cfg.rtable_size
)); ));

View File

@ -13,7 +13,6 @@ fn main() {
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set") env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set")
} }
}; };
println!("cargo:warning=CSP_CONFIG_DIR={}", csp_conf_dir);
let mut csp_conf_path = PathBuf::new(); let mut csp_conf_path = PathBuf::new();
csp_conf_path.push(csp_conf_dir); csp_conf_path.push(csp_conf_dir);
csp_conf_path.push("autoconfig.rs"); csp_conf_path.push("autoconfig.rs");

View File

@ -5,8 +5,8 @@ extern crate alloc;
#[cfg(any(feature = "std", test))] #[cfg(any(feature = "std", test))]
extern crate std; extern crate std;
pub mod ffi;
pub mod config; pub mod config;
pub mod ffi;
use core::time::Duration; use core::time::Duration;