Skip to content

Experiment: Use the cpp crate to create a binding #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions skia-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ embed-freetype = []
[dependencies]
mozjpeg-sys = { version = "1", features = ["with_simd"], optional = true }
lazy_static = { version = "1.4.0", optional = true }
cpp = "0.5.7"

[build-dependencies]
cc = "1.0.37"
bindgen = "0.63.0"
cpp_build = "0.5.7"

# For enum variant name replacements.
regex = "1.4.5"
Expand Down
12 changes: 12 additions & 0 deletions skia-bindings/build_support/skia_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pub fn generate_bindings(
}

let mut cc_build = Build::new();
let mut cpp_build = cpp_build::Config::new();

for source in &build.binding_sources {
cc_build.file(source);
Expand All @@ -179,6 +180,7 @@ pub fn generate_bindings(

bindgen_args.push(format!("-I{}", include_path.display()));
cc_build.include(include_path);
cpp_build.include(include_path);

for (name, value) in &build.definitions {
match value {
Expand Down Expand Up @@ -208,6 +210,7 @@ pub fn generate_bindings(
let target_str = &target.to_string();
cc_build.target(target_str);
bindgen_args.push(format!("--target={target_str}"));
cpp_build.flag(&format!("--target={target_str}"));

// Platform specific arguments and flags.
{
Expand All @@ -230,10 +233,12 @@ pub fn generate_bindings(

for (var, val) in cc_defines {
cc_build.define(var, val);
cpp_build.define(var, Some(val));
}

for arg in cc_args {
cc_build.flag(&arg);
cpp_build.flag(&arg);
}

// we add skia-bindings later on.
Expand All @@ -253,6 +258,13 @@ pub fn generate_bindings(
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

{
println!("GENERATING CPP BINDINGS");
let src_dir = std::env::current_dir().unwrap().join("src");
cpp_build.include(src_dir);
cpp_build.build("src/lib.rs");
}
}

const ALLOWLISTED_FUNCTIONS: &[&str] = &[
Expand Down
15 changes: 15 additions & 0 deletions skia-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ pub mod icu;
#[doc(hidden)]
#[cfg(feature = "use-system-jpeg-turbo")]
use mozjpeg_sys;

use cpp::cpp;

cpp! {{
#include "include/codec/SkCodec.h"
#include "bindings.h"
}}

pub fn make_from_data(data: *mut SkData) -> *mut SkCodec {
unsafe {
cpp!([data as "SkData*"] -> *mut SkCodec as "SkCodec*" {
return SkCodec::MakeFromData(sp(data)).release();
})
}
}