Skip to content

Commit

Permalink
Simple dev tools
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuzhao committed May 23, 2024
1 parent 6b13da1 commit ddaaa84
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[build]
rustflags = ["-Z", "tls_model=initial-exec"]
rustflags = ["-Z", "tls_model=initial-exec"]

[alias]
# Helper command to run a program with a malloc implementation
x = "run -p mallockit-dev-tools --quiet --"
200 changes: 200 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"mallockit",
"mallockit/macros",
"mallockit/dev",
"bump",
"buddy",
"hoard",
Expand Down
11 changes: 11 additions & 0 deletions mallockit/dev/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "mallockit-dev-tools"
version = "0.1.0"
edition = "2021"
publish = false



[dependencies]
anyhow = { version = "1.0.86", features = ["backtrace"] }
clap = { version = "4.5.4", features = ["derive"] }
89 changes: 89 additions & 0 deletions mallockit/dev/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::process::Command;

use clap::Parser;

/// Run a program with a custom allocator
#[derive(Parser, Debug)]
struct Options {
/// Malloc implementation to use
malloc: String,

/// Space or comma separated list of features to activate
#[arg(short = 'F', long)]
features: Vec<String>,

/// Activate all available features
#[arg(long, default_value_t = false)]
all_features: bool,

/// Do not activate the `default` feature
#[arg(long, default_value_t = false)]
no_default_features: bool,

/// Build artifacts in release mode, with optimizations
#[arg(short, long, default_value_t = false)]
release: bool,

/// Build artifacts with the specified profile
#[arg(short, long)]
profile: Option<String>,

/// The program to run, with arguments
#[arg(last = true, allow_hyphen_values = true)]
command: Vec<String>,
}

fn build_crate(opts: &Options) -> anyhow::Result<()> {
let mut cmd = Command::new("cargo");
let mut cmd = cmd.arg("build").args(["-p", &opts.malloc]);
if opts.features.len() > 0 {
cmd = cmd.arg("--features").arg(opts.features.join(","));
}
if opts.all_features {
cmd = cmd.arg("--all-features");
}
if opts.no_default_features {
cmd = cmd.arg("--no-default-features");
}
if opts.release {
cmd = cmd.arg("--release");
}
if let Some(profile) = &opts.profile {
cmd = cmd.arg("--profile").arg(profile);
}
let status = cmd.status()?;
if !status.success() {
anyhow::bail!("cargo run failed: {}", status);
}
Ok(())
}

fn main() -> anyhow::Result<()> {
let options = Options::parse();
// 1. Build the crate
build_crate(&options)?;
// 2. Run the program
let args = options.command.clone();
let mut cmd = Command::new(&args[0]);
cmd.args(&args[1..]);
let profile = if options.release { "release" } else { "debug" };
let ext = if cfg!(target_os = "macos") {
"dylib"
} else {
"so"
};
let ld_preload_env = if cfg!(target_os = "macos") {
"DYLD_INSERT_LIBRARIES"
} else {
"LD_PRELOAD"
};
cmd.env(
ld_preload_env,
format!("target/{}/lib{}.{}", profile, options.malloc, ext),
);
let status = cmd.status()?;
if !status.success() {
anyhow::bail!("program failed: {}", status);
}
Ok(())
}

0 comments on commit ddaaa84

Please sign in to comment.