mirror of
https://review.coreboot.org/flashrom.git
synced 2025-04-27 23:22:37 +02:00
bindings/rust/libflashrom: Create fat rust binding
Create a rust library wrapping libflashrom-sys in a more idiomatic rust API. BUG=b:230545739 BRANCH=None TEST=cargo test Change-Id: Ie3bcfde40dc475f6a9439ccab8e2446967f7d6dd Signed-off-by: Evan Benn <evanbenn@chromium.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/65281 Reviewed-by: Peter Marheine <pmarheine@chromium.org> Reviewed-by: Edward O'Callaghan <quasisec@chromium.org> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
parent
06d32e1147
commit
401d9bfa71
2
bindings/rust/libflashrom/.cargo/config.toml
Normal file
2
bindings/rust/libflashrom/.cargo/config.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[env]
|
||||
RUST_TEST_THREADS = "1"
|
20
bindings/rust/libflashrom/Cargo.toml
Normal file
20
bindings/rust/libflashrom/Cargo.toml
Normal file
@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "libflashrom"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
libflashrom-sys = { path = "../libflashrom-sys" }
|
||||
libc = "0.2.124"
|
||||
regex = "1.5.5"
|
||||
once_cell = "1.7.2"
|
||||
|
||||
[dev-dependencies]
|
||||
rusty-fork = "0.3.0"
|
||||
gag = "1"
|
||||
|
||||
[build-dependencies]
|
||||
pkg-config = "0.3.19"
|
||||
cc = "1.0.72"
|
17
bindings/rust/libflashrom/build.rs
Normal file
17
bindings/rust/libflashrom/build.rs
Normal file
@ -0,0 +1,17 @@
|
||||
extern crate cc;
|
||||
|
||||
fn main() {
|
||||
// pkg_config is needed only to pick up the include path for log.c to use.
|
||||
// libflashrom-sys tells cargo how to link to libflashrom.
|
||||
let flashrom = pkg_config::Config::new()
|
||||
.cargo_metadata(false)
|
||||
.probe("flashrom")
|
||||
.unwrap();
|
||||
let mut log_c = cc::Build::new();
|
||||
log_c.file("src/log.c");
|
||||
for p in flashrom.include_paths {
|
||||
log_c.include(p);
|
||||
}
|
||||
log_c.compile("log.o");
|
||||
println!("cargo:rerun-if-changed=src/log.c");
|
||||
}
|
1094
bindings/rust/libflashrom/src/lib.rs
Normal file
1094
bindings/rust/libflashrom/src/lib.rs
Normal file
File diff suppressed because it is too large
Load Diff
61
bindings/rust/libflashrom/src/log.c
Normal file
61
bindings/rust/libflashrom/src/log.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* This file is part of the flashrom project.
|
||||
*
|
||||
* Copyright (C) 2022 The Chromium OS Authors
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 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 General Public License for more details.
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "libflashrom.h"
|
||||
|
||||
void log_rust(enum flashrom_log_level level, const char *format);
|
||||
|
||||
// LOG_LEVEL = 0 (ERROR) means no messages are printed.
|
||||
enum flashrom_log_level current_level = 0;
|
||||
|
||||
static int log_c(enum flashrom_log_level level, const char *format, va_list ap)
|
||||
{
|
||||
static char *buf = NULL;
|
||||
static int len = 0;
|
||||
if (level >= current_level) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
va_list ap2;
|
||||
va_copy(ap2, ap);
|
||||
// when buf is NULL, len is zero and this will not write to buf
|
||||
int req_len = vsnprintf(buf, len, format, ap2);
|
||||
va_end(ap2);
|
||||
|
||||
if (req_len > len) {
|
||||
char *new_buf = realloc(buf, req_len + 1);
|
||||
if (!new_buf) {
|
||||
return 0;
|
||||
}
|
||||
buf = new_buf;
|
||||
len = req_len + 1;
|
||||
req_len = vsnprintf(buf, len, format, ap);
|
||||
}
|
||||
|
||||
if (req_len > 0) {
|
||||
log_rust(level, buf);
|
||||
}
|
||||
return req_len;
|
||||
}
|
||||
|
||||
void set_log_callback()
|
||||
{
|
||||
flashrom_set_log_callback(log_c);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user