cargo fmt, fix build with variants, more build logging

Signed-off-by: strawberry <strawberry@pupbrain.dev>
This commit is contained in:
strawberry 2023-11-12 18:14:46 -05:00
parent 3f517c73c8
commit 3169b1e6fd
3 changed files with 54 additions and 39 deletions

View file

@ -2,11 +2,16 @@
the sys repo, rust wrapper
to build, just run `cargo build -r` which will build the light variant by default.
if you want default (called `standard` in this crate) variant, do `cargo build -r --features=standard --no-default-features`
### TODO:
- [ ] test if this even works
- [ ] add support for explicit make config args on top of choosing variant
- [ ] make build script better overall
- [ ] support C preprocessor macro definitions
- [ ] maybe add support for building both variants if both are specified, or dont use a default light variant
- [ ] add support for hardened_malloc `make clean` upon `cargo clean`
- [ ] potentially add support for cross-compiling so i can build on apple silicon for linux x86?
- [ ] add support for hardened_malloc's tests and our own tests
- [ ] add github CI/CD

View file

@ -1,49 +1,60 @@
use std::{env, process::Command, path::Path};
use std::{
env::{self, current_dir},
path::Path,
process::Command,
};
/// If submodules were not synced, sync them to actually build hardened_malloc
fn update_submodules() {
let program = "git";
let dir = "../";
let args = ["submodule", "update", "--init", "--recursive"];
println!(
"[hardened_malloc-sys]: Running command: \"{} {}\" in directory: {}",
"[hardened_malloc-sys]: Running command: \"{} {}\" in directory: {:?}",
program,
args.join(" "),
dir
current_dir(),
);
let ret = Command::new(program).current_dir(dir).args(args).status();
let ret = Command::new(program).args(args).status();
match ret.map(|status| (status.success(), status.code())) {
Ok((true, _)) => (),
Ok((false, Some(c))) => panic!("[hardened_malloc-sys]: Command failed with error code {}", c),
Ok((false, None)) => panic!("[hardened_malloc-sys]: Command exited with no error code, possibly killed by system"),
Ok((false, Some(c))) => panic!(
"[hardened_malloc-sys]: Command failed with error code {}",
c
),
Ok((false, None)) => panic!(
"[hardened_malloc-sys]: Command exited with no error code, possibly killed by system"
),
Err(e) => panic!("[hardened_malloc-sys]: Command failed with error: {}", e),
}
}
fn main() {
if !Path::new("src/hardened_malloc/Makefile").exists() {
println!("src/hardened_malloc/Makefile does not exist, running submodule sync");
update_submodules();
}
let variant: &str;
let variant;
if cfg!(feature = "light") {
variant = "light";
} else {
variant = "default";
variant = "default"; // "default" is hardened_malloc's default.mk. this crate's feature uses "standard" for "default"
}
let build_args = ["VARIANT=".to_owned() + variant, "V=".to_owned() + "1"];
//TODO: handle support for explicit make flags like N_ARENA=1 and such
let mut make_command = Command::new("make");
println!("running {:?} with args {:?}", make_command, build_args);
let make_output = make_command
.current_dir("src/hardened_malloc/")
.env("V", "1") // always verbose mode for cargo
.env("VARIANT", variant)
.output()
.unwrap_or_else(|error| {
panic!("Failed to run 'make {}': ", error);
});
.current_dir("src/hardened_malloc/")
.args(build_args)
.output()
.unwrap_or_else(|error| {
panic!("Failed to run 'make {}': ", error);
});
if !make_output.status.success() {
panic!(
"[hardened_malloc-sys]: building hardened_malloc failed:\n{:?}\n{}\n{}",
@ -54,8 +65,8 @@ fn main() {
}
//println!("cargo:rustc-link-search=native=src/hardened_malloc");
//println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/hardened_malloc/");
//println!("cargo:out_dir={}", env::var("OUT_DIR").unwrap());
}
}

View file

@ -1,23 +1,23 @@
#![no_std]
use core::ffi::{c_void, c_int};
use core::ffi::{c_int, c_void};
extern crate libc;
extern "C" {
/*
TODO: implement this
TODO: implement this
#ifdef __ANDROID__
#define H_MALLOC_USABLE_SIZE_CONST const
#else
#define H_MALLOC_USABLE_SIZE_CONST
#endif
#ifdef __ANDROID__
#define H_MALLOC_USABLE_SIZE_CONST const
#else
#define H_MALLOC_USABLE_SIZE_CONST
#endif
for:
// glibc extensions
size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
*/
for:
// glibc extensions
size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
*/
/* C standard */
@ -47,13 +47,13 @@ size_t h_malloc_usable_size(H_MALLOC_USABLE_SIZE_CONST void *ptr);
pub fn h_malloc_set_state(ptr: *mut c_void) -> c_int;
/*TODO: implement this see the top:
#if defined(__GLIBC__) || defined(__ANDROID__)
struct mallinfo h_mallinfo(void);
#endif
#ifndef __ANDROID__
int h_malloc_info(int options, FILE *fp);
#endif
*/
#if defined(__GLIBC__) || defined(__ANDROID__)
struct mallinfo h_mallinfo(void);
#endif
#ifndef __ANDROID__
int h_malloc_info(int options, FILE *fp);
#endif
*/
/* hardened_malloc extensions */
@ -63,7 +63,6 @@ int h_malloc_info(int options, FILE *fp);
/// similar to malloc_object_size, but avoids locking so the results are much more limited
pub fn h_malloc_object_size_fast(ptr: *const c_void) -> usize;
/// The free function with an extra parameter for passing the size requested at
/// allocation time.
///
@ -75,4 +74,4 @@ int h_malloc_info(int options, FILE *fp);
/// allocator implementation uses it to improve security by checking that the
/// passed size matches the allocated size.
pub fn h_free_sized(ptr: *mut c_void, expected_size: usize) -> c_void;
}
}