Skip to content

Commit

Permalink
Merge pull request #25 from mkroening/nottoml
Browse files Browse the repository at this point in the history
feat(build.rs): remove `statliclib/Cargo.nottoml`
  • Loading branch information
mkroening authored Mar 25, 2024
2 parents 42826aa + 5113bc9 commit 4c37c86
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 76 deletions.
13 changes: 5 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
[workspace]

members = [
"rftrace",
"rftrace-frontend",
"rftrace-frontend-ffi",
"examples/rust",
"examples/hermitrust",
]


# re-define workspace here, else we get issues with sub-compiling libhermit-rs in the target folder of a workspace!
# needed only for examples/hermitrust
exclude = [
"target"
]
"rftrace",
# re-define workspace here, else we get issues with sub-compiling libhermit-rs in the target folder of a workspace!
# needed only for examples/hermitrust
"target",
]
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,6 @@ gdb /dev/null /proc/kcore -ex 'x/uw 0x'$(grep '\<tsc_khz\>' /proc/kallsyms | cut
```
[StackOverflow: Getting TSC rate in x86 kernel](https://stackoverflow.com/questions/35123379/getting-tsc-rate-in-x86-kernel)

### Build Process
The build process is a bit weird, since we build a static rust library and then link against it.
- cargo build
- runs `build.rs::build()`
- compiles part of the code as a native staticlib, by using the Cargo.toml in `/staticlib` and passing the staticlib feature to cargo build
- compiles the library as normal, without the staticlib feature and links the 'precompiled' static library from the previous step.

We need the second manifest, since it is [not possible to change the library type outside of it](https://github.com/rust-lang/cargo/issues/6160#issuecomment-428778868).

You can compile only static lib manually by renaming `rftrace/staticlib/Cargo.nottoml` to `rftrace/staticlib/Cargo.toml` and running
`cargo build --manifest-path rftrace/staticlib/Cargo.toml --target-dir target_static --features staticlib -vv`


## Future Work
- create frontend which can output the trace over network, so no file access is needed
Expand Down
2 changes: 1 addition & 1 deletion rftrace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ repository = "https://github.com/tlambertz/rftrace"
include = [
"**/*.rs",
"Cargo.toml",
"staticlib/Cargo.nottoml",
]


[features]
staticlib = []
interruptsafe = [] # backup and restore all scratch registers in the mcount_return trampoline. Needed if we instrument interrupt routines

default = []
Expand Down
46 changes: 13 additions & 33 deletions rftrace/build.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
use std::collections::HashSet;
use std::env;
use std::fs::{self, File};
use std::io::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

fn prepare_staticlib_toml(out_dir: &str) -> std::io::Result<String> {
let manifest = format!("{}/staticlib/Cargo.toml", out_dir);
fs::create_dir_all(format!("{}/staticlib", out_dir))
.expect("Could not create directory for staticlib");
let toml = fs::read_to_string("staticlib/Cargo.nottoml")?;

// Adapt path
let mut lib = env::current_dir()?;
lib.push("src");
lib.push("lib.rs");
let toml = toml.replace(
"../src/lib.rs",
lib.to_str().expect("Invalid staticlib path"),
);

let mut toml_out = File::create(&manifest)?;
toml_out.write_all(toml.as_bytes())?;
//fs::copy("", format!("{}/staticlib/Cargo.toml", out_dir)).expect("Could not copy staticlib Cargo.toml");
Ok(manifest)
}

fn build_backend() {
println!("Building Backend!");
// Get envvars from cargo
Expand All @@ -35,19 +13,13 @@ fn build_backend() {
let target = "x86_64-unknown-none";

let mut cmd = cargo();
cmd.arg("build");
cmd.arg("rustc");

cmd.args(&["--target", target]);

// Output all build artifacts in output dir of parent-lib
cmd.args(&["--target-dir", &full_target_dir]);

// Use custom manifest, which defines that this compilation is a staticlib
// crates.io allows only one Cargo.toml per package, so copy here
let manifest =
prepare_staticlib_toml(&out_dir).expect("Could not prepare staticlib toml file!");
cmd.args(&["--manifest-path", &manifest]);

// Enable the staticlib feature, so we can do #[cfg(feature='staticlib')] gate our code
// Pass-through interruptsafe and reexportsymbols features
cmd.arg("--features=staticlib");
Expand All @@ -72,6 +44,10 @@ fn build_backend() {

cmd.arg("--release");

cmd.arg("--");

cmd.arg("-Cpanic=abort");

// Ensure rustflags does NOT contain instrument-mcount!
let rustflags = env::var("RUSTFLAGS").unwrap_or_default();
if rustflags.contains("mcount") {
Expand All @@ -95,7 +71,7 @@ fn build_backend() {
let dist_dir = format!("{}/{}/release", &full_target_dir, &target);

retain_symbols(
Path::new(&format!("{}/librftrace_backend.a", &dist_dir)),
Path::new(&format!("{}/librftrace.a", &dist_dir)),
HashSet::from([
"mcount",
"rftrace_backend_disable",
Expand All @@ -108,15 +84,19 @@ fn build_backend() {

// Link parent-lib against this staticlib
println!("cargo:rustc-link-search=native={}", &dist_dir);
println!("cargo:rustc-link-lib=static=rftrace_backend");
println!("cargo:rustc-link-lib=static=rftrace");

println!("cargo:rerun-if-changed=staticlib/Cargo.toml");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=src/backend.rs");
println!("cargo:rerun-if-changed=src/interface.rs");
println!("cargo:rerun-if-changed=src/lib.rs");
}

fn main() {
if env::var_os("CARGO_FEATURE_STATICLIB").is_some() {
return;
}

build_backend();
}

Expand Down
22 changes: 0 additions & 22 deletions rftrace/staticlib/Cargo.nottoml

This file was deleted.

0 comments on commit 4c37c86

Please sign in to comment.