Skip to content

Commit

Permalink
feat(build): Manually patch the HalideRuntime file instead of using
Browse files Browse the repository at this point in the history
diffy or other hacks
  • Loading branch information
uttarayan21 committed Oct 16, 2024
1 parent 7c007a9 commit bd5a4d5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 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 mnn-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cmake = { git = "https://github.com/blonteractor/cmake-rs", features = [
diffy = "0.4.0"
dunce = "1.0.4"
fs_extra = "1.3.0"
itertools = "0.13.0"
tap = "1.0.1"

[features]
Expand Down
40 changes: 21 additions & 19 deletions mnn-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ static EMSCRIPTEN_CACHE: LazyLock<String> = LazyLock::new(|| {
emscripten_cache
});

const HALIDE_PATCH_1: &str = r#"#if __cplusplus >= 201103L"#;
const HALIDE_PATCH_2: &str = r#"
#else
HALIDE_ATTRIBUTE_ALIGN(1) uint8_t code; // halide_type_code_t
#endif
"#;
const HALIDE_SEARCH: &str =
r#"HALIDE_ATTRIBUTE_ALIGN(1) halide_type_code_t code; // halide_type_code_t"#;

fn ensure_vendor_exists(vendor: impl AsRef<Path>) -> Result<()> {
if vendor
Expand Down Expand Up @@ -75,11 +71,26 @@ fn main() -> Result<()> {
// try_patch_file("patches/halide_type_t_64.patch", intptr)
// .context("Failed to patch vendor")?;

use itertools::Itertools;
let intptr_contents = std::fs::read_to_string(&intptr)?;
let patched = intptr_contents
.replace(HALIDE_PATCH_1, "")
.replace(HALIDE_PATCH_2, "");
std::fs::write(intptr, patched)?;
let patched = intptr_contents.lines().collect::<Vec<_>>();
if let Some((idx, _)) = patched
.iter()
.find_position(|line| line.contains(HALIDE_SEARCH))
{
// remove the last line and the next 3 lines
// patched.remove(idx - 1);
// patched.remove(idx);
// patched.remove(idx);
// patched.remove(idx);
let patched = patched
.into_iter()
.enumerate()
.filter(|(c_idx, _)| !(*c_idx == idx - 1 || (idx + 1..=idx + 3).contains(c_idx)))
.map(|(_, c)| c)
.collect::<Vec<_>>();
std::fs::write(intptr, patched.join("\n"))?;
}
}

let install_dir = out_dir.join("mnn-install");
Expand Down Expand Up @@ -457,12 +468,3 @@ impl CxxOption {
}
}
}

// mod cc_build {
// use super::*;
// pub fn build(source: impl AsRef<Path>) -> Result<PathBuf> {
// let mut builder = cc::Build::new();
// builder.std("c++11").cpp(true);
// todo!()
// }
// }

0 comments on commit bd5a4d5

Please sign in to comment.