Skip to content

Commit f2727cb

Browse files
committed
feat: initial v8 native module setup
1 parent eabb539 commit f2727cb

File tree

40 files changed

+31982
-1
lines changed

40 files changed

+31982
-1
lines changed

packages/canvas/src-native/canvas-native/.idea/canvas-native.iml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/canvas/src-native/canvas-native/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
members = [
44
"gl-bindings",
5-
"canvas-core"
5+
"canvas-core",
6+
"v8-bindings"
67
]
78

89

packages/canvas/src-native/canvas-native/canvas-core/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ authors = ["Osei Fortune <[email protected]>"]
55
edition = "2018"
66
build = "build.rs"
77

8+
[build]
9+
rustc-wrapper = "sccache"
10+
811
[build-dependencies]
912
bindgen = "0.57.0"
1013
cmake = "0.1.44"
@@ -16,6 +19,7 @@ base64 = "0.13.0"
1619
image = "0.23.7"
1720
encoding_rs = "0.8.24"
1821
gl-bindings = { version = "0.1.0", path = "../gl-bindings" }
22+
#v8-bindings = { version = "0.1.0", path = "../v8-bindings" }
1923
lazy_static = "1.4.0"
2024
css-color-parser = "0.1.2"
2125
reqwest = { version = "0.11", features = ["blocking"] }

packages/canvas/src-native/canvas-native/gl-bindings/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ authors = ["triniwiz <[email protected]>"]
55
edition = "2018"
66
build="build.rs"
77

8+
[build]
9+
rustc-wrapper = "sccache"
10+
811
[build-dependencies]
912
bindgen = "0.55.1"
1013
cmake = "0.1.44"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "v8-bindings"
3+
version = "0.1.0"
4+
authors = ["triniwiz <[email protected]>"]
5+
edition = "2018"
6+
build = "build.rs"
7+
8+
[build]
9+
rustc-wrapper = "sccache"
10+
11+
[build-dependencies]
12+
bindgen = "0.58.1"
13+
cmake = "0.1.44"
14+
cc = "1.0.56"
15+
16+
17+
[dependencies]
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
use std::borrow::Borrow;
2+
use std::{fmt, env};
3+
use std::fmt::{Display, Formatter};
4+
5+
#[derive(Clone, Debug)]
6+
pub struct Target {
7+
pub architecture: String,
8+
pub vendor: String,
9+
pub system: String,
10+
pub abi: Option<String>,
11+
}
12+
13+
impl Target {
14+
pub fn as_strs(&self) -> (&str, &str, &str, Option<&str>) {
15+
(
16+
self.architecture.as_str(),
17+
self.vendor.as_str(),
18+
self.system.as_str(),
19+
self.abi.as_deref(),
20+
)
21+
}
22+
}
23+
24+
impl Display for Target {
25+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
26+
write!(
27+
f,
28+
"{}-{}-{}",
29+
&self.architecture, &self.vendor, &self.system
30+
)?;
31+
32+
if let Some(ref abi) = self.abi {
33+
write!(f, "-{}", abi)
34+
} else {
35+
Result::Ok(())
36+
}
37+
}
38+
}
39+
40+
const ROOT_DIR: &str = "./vendor/android/";
41+
42+
fn get_path(path: &str)-> String{
43+
format!("{}{}",ROOT_DIR, path)
44+
}
45+
46+
pub fn env_var(name: impl AsRef<str>) -> Option<String> {
47+
let name = name.as_ref();
48+
env::var(name).ok()
49+
}
50+
51+
52+
fn main() {
53+
let target_str = std::env::var("TARGET").unwrap();
54+
let mut include_libs_dir = String::from("");
55+
let arm_libs_dir = get_path("armeabi-v7a");
56+
let arm_64_libs_dir = get_path("arm64-v8a");
57+
let x86_libs_dir = get_path("x86");
58+
let x86_64_libs_dir = get_path("x86_64");
59+
let ndk = env_var("ANDROID_NDK").expect("ANDROID_NDK variable not set");
60+
let mut target_ndk = "";
61+
let arm_ndk = "/tmp/ndk_arm";
62+
let arm_64_ndk = "/tmp/ndk_arm64";
63+
let x86_ndk = "/tmp/ndk_x86";
64+
let x86_64_ndk = "/tmp/ndk_x86_64";
65+
66+
let target: Vec<String> = target_str.split('-').map(|s| s.into()).collect();
67+
if target.len() < 3 {
68+
panic!("Failed to parse TARGET {}", target_str);
69+
}
70+
71+
let abi = if target.len() > 3 {
72+
Some(target[3].clone())
73+
} else {
74+
None
75+
};
76+
77+
let target = Target {
78+
architecture: target[0].clone(),
79+
vendor: target[1].clone(),
80+
system: target[2].clone(),
81+
abi,
82+
};
83+
println!("cargo:rerun-if-changed=build.rs");
84+
85+
match target.system.borrow() {
86+
"android" | "androideabi" => {
87+
let build_target;
88+
if target.architecture.eq("armv7") {
89+
target_ndk = arm_ndk;
90+
build_target = "armv7-linux-androideabi";
91+
include_libs_dir.push_str(&arm_libs_dir);
92+
} else if target.architecture.eq("aarch64") {
93+
build_target = "aarch64-linux-android";
94+
target_ndk = arm_64_ndk;
95+
include_libs_dir.push_str(&arm_64_libs_dir);
96+
} else if target.architecture.eq("i686") {
97+
build_target = "i686-linux-android";
98+
target_ndk = x86_ndk;
99+
include_libs_dir.push_str(&x86_libs_dir);
100+
} else if target.architecture.eq("x86_64") {
101+
build_target = "x86_64-linux-android";
102+
target_ndk = x86_64_ndk;
103+
include_libs_dir.push_str(&x86_64_libs_dir);
104+
} else {
105+
return;
106+
}
107+
108+
println!("target {:?}", build_target);
109+
let root_include = format!("--sysroot={}/sysroot",ndk);
110+
let cpu_features = format!("-I{}/sources/android/cpufeatures", ndk);
111+
let c_lang = format!("-isystem{}/sources/cxx-stl/llvm-libc++/include", ndk);
112+
113+
//println!("cargo:rustc-link-search=native={}", include_dir);
114+
// println!("cargo:rustc-link-search=native={}", cpu_features);
115+
//println!("cargo:rustc-link-search=native={}", c_lang);
116+
println!("cargo:rustc-link-search=native={}", include_libs_dir);
117+
println!("cargo:rustc-link-lib=v8"); // the "-l" flag
118+
println!("cargo:rustc-link-lib=zip"); // the "-l" flag
119+
120+
let bindings = bindgen::Builder::default()
121+
.header("wrapper.h")
122+
//.clang_arg(format!("--target={}", build_target))
123+
.clang_arg("-x")
124+
.clang_arg("c++")
125+
.clang_arg("-std=c++17")
126+
.clang_arg(root_include)
127+
.clang_arg(c_lang)
128+
129+
//.clang_arg(cpu_features)
130+
// .clang_arg(include_dir)
131+
//.clang_arg(include_libs_dir)
132+
// Finish the builder and generate the bindings.
133+
.generate()
134+
// Unwrap the Result and panic on failure.
135+
.expect("Unable to generate bindings");
136+
137+
// Write the bindings to the $OUT_DIR/bindings.rs file.
138+
139+
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
140+
bindings
141+
.write_to_file(out_path.join("bindings.rs"))
142+
.expect("Couldn't write bindings!");
143+
144+
}
145+
_ => {}
146+
}
147+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[cfg(test)]
2+
mod tests {
3+
#[test]
4+
fn it_works() {
5+
assert_eq!(2 + 2, 4);
6+
}
7+
}

0 commit comments

Comments
 (0)