Skip to content

Commit 1c5ce34

Browse files
committed
Generate BAGL glyphs
1 parent 818f5d3 commit 1c5ce34

File tree

1 file changed

+79
-28
lines changed

1 file changed

+79
-28
lines changed

ledger_secure_sdk_sys/build.rs

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fs;
44
use std::path::{Path, PathBuf};
55
use std::process::Command;
66
use std::time::Instant;
7-
use std::{env, fs::File, io::BufRead, io::BufReader, io::Read};
7+
use std::{env, fs::File, io::BufRead, io::BufReader, io::Read, io::Write};
88

99
const AUX_C_FILES: [&str; 2] = ["./src/c/src.c", "./src/c/sjlj.s"];
1010

@@ -315,10 +315,16 @@ impl SDKBuilder<'_> {
315315
.glyphs_folders
316316
.push(self.device.c_sdk.join("lib_nbgl/glyphs/24px"));
317317
}
318-
_ => {
319-
self.device
320-
.glyphs_folders
321-
.push(self.device.c_sdk.join("lib_nbgl/glyphs/nano"));
318+
DeviceName::NanoSPlus | DeviceName::NanoX => {
319+
if env::var_os("CARGO_FEATURE_NANO_NBGL").is_some() {
320+
self.device
321+
.glyphs_folders
322+
.push(self.device.c_sdk.join("lib_nbgl/glyphs/nano"));
323+
} else {
324+
self.device
325+
.glyphs_folders
326+
.push(self.device.c_sdk.join("lib_ux/glyphs"));
327+
}
322328
}
323329
}
324330

@@ -523,18 +529,17 @@ impl SDKBuilder<'_> {
523529
}
524530

525531
// BAGL or NBGL bindings
532+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
533+
let mut include_path = "-I".to_string();
534+
let glyphs = out_path.join("glyphs");
535+
include_path += glyphs.to_str().unwrap();
536+
bindings = bindings.clang_args([include_path.as_str()]);
526537
if ((self.device.name == DeviceName::NanoX || self.device.name == DeviceName::NanoSPlus)
527538
&& env::var_os("CARGO_FEATURE_NANO_NBGL").is_some())
528539
|| self.device.name == DeviceName::Stax
529540
|| self.device.name == DeviceName::Flex
530541
|| self.device.name == DeviceName::ApexP
531542
{
532-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
533-
let mut include_path = "-I".to_string();
534-
let glyphs = out_path.join("glyphs");
535-
include_path += glyphs.to_str().unwrap();
536-
bindings = bindings.clang_args([include_path.as_str()]);
537-
538543
bindings = bindings.clang_args([
539544
format!("-I{bsdk}/lib_nbgl/include/").as_str(),
540545
format!("-I{bsdk}/lib_ux_nbgl/").as_str(),
@@ -550,7 +555,11 @@ impl SDKBuilder<'_> {
550555
bindings = bindings.clang_args(["-DHAVE_NBGL", "-DNBGL_STEP", "-DNBGL_USE_CASE"]);
551556
}
552557
} else {
553-
bindings = bindings.clang_arg("-DHAVE_UX_FLOW");
558+
bindings = bindings.clang_args([
559+
format!("-I{bsdk}/lib_bagl/include/").as_str(),
560+
format!("-I{bsdk}/lib_ux/include/").as_str(),
561+
]);
562+
bindings = bindings.clang_args(["-DHAVE_BAGL", "-DHAVE_UX_FLOW"]);
554563
}
555564

556565
for define in &self.cxdefines {
@@ -878,32 +887,74 @@ fn clone_sdk(devicename: &DeviceName) -> PathBuf {
878887
}
879888

880889
fn generate_glyphs(device: &Device) {
881-
let icon2glyph = device.c_sdk.join("lib_nbgl/tools/icon2glyph.py");
882-
883890
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
884891
let dest_path = out_path.join("glyphs");
885892
if !dest_path.exists() {
886893
fs::create_dir_all(&dest_path).ok();
887894
}
888895

889-
let mut cmd = Command::new(icon2glyph.as_os_str());
890-
cmd.arg("--glyphcheader")
891-
.arg(dest_path.join("glyphs.h").as_os_str())
892-
.arg("--glyphcfile")
893-
.arg(dest_path.join("glyphs.c").as_os_str());
896+
// NBGL Glyphs
897+
if ((device.name == DeviceName::NanoSPlus || device.name == DeviceName::NanoX)
898+
&& env::var_os("CARGO_FEATURE_NANO_NBGL").is_some())
899+
|| device.name == DeviceName::Stax
900+
|| device.name == DeviceName::Flex
901+
|| device.name == DeviceName::ApexP
902+
{
903+
println!("cargo:warning=NBGL glyphs are generated");
904+
let icon2glyph = device.c_sdk.join("lib_nbgl/tools/icon2glyph.py");
894905

895-
if device.name == DeviceName::NanoSPlus || device.name == DeviceName::NanoX {
896-
cmd.arg("--reverse");
897-
}
906+
let mut cmd = Command::new(icon2glyph.as_os_str());
907+
cmd.arg("--glyphcheader")
908+
.arg(dest_path.join("glyphs.h").as_os_str())
909+
.arg("--glyphcfile")
910+
.arg(dest_path.join("glyphs.c").as_os_str());
911+
912+
if device.name == DeviceName::NanoSPlus || device.name == DeviceName::NanoX {
913+
cmd.arg("--reverse");
914+
}
898915

899-
for folder in device.glyphs_folders.iter() {
900-
for file in std::fs::read_dir(folder).unwrap() {
901-
let path = file.unwrap().path();
902-
let path_str = path.to_str().unwrap().to_string();
903-
cmd.arg(path_str);
916+
for folder in device.glyphs_folders.iter() {
917+
for file in std::fs::read_dir(folder).unwrap() {
918+
let path = file.unwrap().path();
919+
let path_str = path.to_str().unwrap().to_string();
920+
cmd.arg(path_str);
921+
}
922+
}
923+
let _ = cmd.output();
924+
}
925+
// BAGL Glyphs
926+
else {
927+
println!("cargo:warning=BAGL glyphs are generated");
928+
let icon2glyph = device.c_sdk.join("icon3.py");
929+
930+
let mut cmd1 = Command::new("python3");
931+
cmd1.arg(icon2glyph.as_os_str());
932+
cmd1.arg("--glyphcheader");
933+
let mut cmd2 = Command::new("python3");
934+
cmd2.arg(icon2glyph.as_os_str());
935+
cmd2.arg("--glyphcfile").arg("--factorize");
936+
937+
for folder in device.glyphs_folders.iter() {
938+
for file in std::fs::read_dir(folder).unwrap() {
939+
let path = file.unwrap().path();
940+
let path_str = path.to_str().unwrap().to_string();
941+
cmd1.arg(&path_str);
942+
cmd2.arg(&path_str);
943+
}
904944
}
945+
let output1 = cmd1.output().unwrap();
946+
let output2 = cmd2.output().unwrap();
947+
948+
let mut glyphs_header: File = File::create(dest_path.join("glyphs.h")).unwrap();
949+
glyphs_header
950+
.write_all(&output1.stdout)
951+
.expect("Failed to write glyphs.h");
952+
953+
let mut glyphs_cfile = File::create(dest_path.join("glyphs.c")).unwrap();
954+
glyphs_cfile
955+
.write_all(&output2.stdout)
956+
.expect("Failed to write glyphs.c");
905957
}
906-
let _ = cmd.output();
907958
}
908959

909960
/// Helper function to concatenate all paths in pathlist to c_sdk's path

0 commit comments

Comments
 (0)