Skip to content

Commit

Permalink
support to compile ftgrays/ftraster/gpac-evg
Browse files Browse the repository at this point in the history
preparing for new renderer implementation
  • Loading branch information
mhfan committed Oct 24, 2023
1 parent 63da749 commit 62fa983
Show file tree
Hide file tree
Showing 6 changed files with 606 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"C_Cpp.default.defines": [ "STANDALONE_",
"FALL_THROUGH=((void)0)", "FT_BEGIN_HEADER" ],
"C_Cpp.default.includePath": [ "src/evg", "src/raster" ]
}
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,30 @@ usvg = "0.36"
kurbo = "0.10" # Bezier curves utils

#build-time = { version = "0.1", git = "https://github.com/AlephAlpha/build-time" }
png = { version = "0.17", optional = true }

[lib]
# Disable doctests as a workaround for https://github.com/rust-lang/rust-bindgen/issues/1313
#doctest = false

[features]
#default = [ "cc" ]
cc = [ "dep:cc", "dep:bindgen", "dep:glob", "dep:png" ] # implied by optional dependency

[dev-dependencies]
#rexpect = "0.5"

[build-dependencies]
cc = { version = "1.0", optional = true }
bindgen = { version = "0.68", optional = true }
glob = { version = "0.3", optional = true }
chrono = "0.4"

[profile.release]
codegen-units = 1
strip = 'debuginfo'
panic = 'abort'
lto = 'fat' # true

[workspace]

71 changes: 67 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,79 @@

fn main() { // https://doc.rust-lang.org/stable/cargo/reference/build-scripts.html
// https://doc.rust-lang.org/stable/cargo/reference/build-scripts.html
fn main() -> Result<(), Box<dyn std::error::Error>> {
//println!("cargo:rerun-if-changed=build.rs"); // XXX: prevent re-run indead
// By default, cargo always re-run the build script if any file within the package
// is changed, and no any rerun-if instruction is emitted.
println!("cargo:rerun-if-changed=src/");

println!("cargo:rerun-if-changed=.git/index");
let output = std::process::Command::new("git")
.args(["rev-parse", "--short", "HEAD"]).output().unwrap();
println!("cargo:rustc-env=BUILD_GIT_HASH={}", String::from_utf8(output.stdout).unwrap());
.args(["rev-parse", "--short", "HEAD"]).output()?;
println!("cargo:rustc-env=BUILD_GIT_HASH={}", String::from_utf8(output.stdout)?);

println!("cargo:rustc-env=BUILD_TIMESTAMP={}",
chrono::Local::now().format("%H:%M:%S%z %Y-%m-%d"));

//println!("cargo:rerun-if-changed=build.rs"); // XXX: prevent re-run indead
#[cfg(feature = "cc")] { use std::path::PathBuf;
#[derive(Debug)] struct DoctestComment;
impl bindgen::callbacks::ParseCallbacks for DoctestComment {
fn process_comment(&self, comment: &str) -> Option<String> {
Some(format!("````c,ignore\n{comment}\n````")) // FIXME:
}
}

let module = "ftgrays";
cc::Build::new().flag("-std=c17").flag("-pedantic")
.define("FALL_THROUGH", "((void)0)").file("src/raster/ftgrays.c")
.opt_level(3).define("NDEBUG", None).file("src/raster/ftraster.c")
.files(glob::glob("src/raster/stroke/*.c")?.filter_map(Result::ok))
.flag("-Wno-unused-variable")//.flag("-Wno-unused-function")
.define("STANDALONE_", None).compile(module);

// The bindgen::Builder is the main entry point to bindgen,
// and lets you build up options for the resulting bindings.
bindgen::Builder::default() //.header("src/raster/ftimage.h")
.clang_arg("-DSTANDALONE_").header("src/raster/ftgrays.h")
.clang_arg("-DFT_BEGIN_HEADER=").clang_arg("-DFT_END_HEADER=")
.clang_arg("-DFT_STATIC_BYTE_CAST(type,var)=(type)(unsigned char)(var)")
.allowlist_item("FT_OUTLINE_.*|FT_RASTER_FLAG_.*|FT_CURVE_TAG.*")
.layout_tests(false).derive_copy(false).allowlist_var("ft_standard_raster")
.allowlist_var("ft_grays_raster").allowlist_type("FT_Outline|FT_Pixel_Mode")
.default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: true })
.default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
.parse_callbacks(Box::new(DoctestComment)).generate_comments(false) // XXX:
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks)).generate()?
.write_to_file(PathBuf::from(std::env::var("OUT_DIR")?).join(format!("{module}.rs")))?;

/* ln -s /path/to/freetype2/{include/freetype/ftimage.h,src/smooth/ftgrays.[ch],\
src/raster/ft{misc.h,raster.c}} src/raster/
* ln -s /path/to/freetype2/src/{raster/ftmisc.h,base/ft{stroke,trigon}.c} src/raster/stroke/
* ln -s /path/to/freetype2/include/freetype/ft{stroke,trigon,image}.h src/raster/stroke/
* ln -s /path/to/gpac/src/evg/{ftgrays.c,rast_soft.h,stencil.c,surface.c,\
raster_{argb,rgb,565,yuv}.c},raster3d.c src/evg/
* ln -s /path/to/gpac/src/utils/{path2d{,_stroke},math,alloc,color,error}.c src/evg/
* ln -s /path/gpac/include/gpac/{evg,setup,constants,maths,color,path2d,\
tools,thread}.h src/evg/gpac/
* touch src/evg/gpac/{Remotery,config_file,configuration,main,module,version}.h
*/
let module = "gpac_evg";
cc::Build::new().flag("-std=c17").flag("-Isrc/evg").define("GPAC_FIXED_POINT", None)
.flag("-Wno-unused-parameter").define("GPAC_DISABLE_THREADS", None)
.flag("-Wno-pointer-sign").define("GPAC_DISABLE_LOG", None)
.files(glob::glob("src/evg/*.c")?.filter_map(Result::ok))
.opt_level(3).define("NDEBUG", None).compile(module);

bindgen::Builder::default().header("src/evg/gpac/evg.h").clang_arg("-Isrc/evg")
.default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: true })
.clang_arg("-DGPAC_DISABLE_THREADS").allowlist_function("gf_evg_s.*")
//.allowlist_item("GF_LINE_.*")
.clang_arg("-DGPAC_FIXED_POINT").allowlist_function("gf_path_.*")
.layout_tests(false).derive_copy(false).new_type_alias("Fixed")
.parse_callbacks(Box::new(bindgen::CargoCallbacks)).generate()?
.write_to_file(PathBuf::from(std::env::var("OUT_DIR")?).join(format!("{module}.rs")))?;
}

Ok(())
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pub mod tinyvg;
pub mod render;
pub mod convert;

#[cfg(feature = "cc")] pub mod revg;

Loading

0 comments on commit 62fa983

Please sign in to comment.