1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
//! rlgvl-creator CLI module.
//!
//! Provides CLI utilities for managing rlvgl assets. Supports the `init`, `scan`, `check`,
//! `vendor`, `convert`, `preview`, `add-target`, `sync`, `scaffold`, `apng`, `schema`, `fonts`,
//! `svg`, `lottie`, `svelte`, `sim`, and `ui` commands to bootstrap asset directories, update a
//! manifest, validate asset policies, copy assets to build outputs, regenerate feature lists,
//! generate thumbnails, register targets, build animations, pack fonts, render SVGs, import
//! Lottie animations, align Svelte tokens, and launch the desktop UI or simulator.
use std::path::PathBuf;
use anyhow::{Result, anyhow};
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
pub mod add_target;
pub mod apng;
pub mod bsp_gen;
pub mod chakra;
pub mod check;
pub mod compress;
pub mod convert;
pub mod fonts;
pub mod gen_lib;
pub mod init;
pub mod lottie;
pub mod manifest;
pub mod new;
pub mod preview;
pub mod raw;
pub mod run;
pub mod scaffold;
pub mod scan;
pub mod schema;
pub mod sim;
pub mod svelte;
pub mod svg;
pub mod sync;
pub mod util;
pub mod vendor;
fn resolve_out_arg(
positional: Option<PathBuf>,
flagged: Option<PathBuf>,
command: &str,
) -> Result<PathBuf> {
positional
.or(flagged)
.ok_or_else(|| anyhow!("missing output path for `{command}`"))
}
/// Dual-core selector for BSP generation.
#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum)]
enum CoreSel {
Cm7,
Cm4,
}
/// Target to run.
#[derive(Copy, Clone, Debug, Eq, PartialEq, ValueEnum)]
enum RunTarget {
Sim,
}
/// CLI arguments for rlgvl-creator.
#[derive(Parser)]
#[command(
author,
version,
about,
long_about = None,
after_help = "Example:\n rlgvl-creator scan assets/\n rlgvl-creator --manifest custom.yml check assets/",
arg_required_else_help = true
)]
struct Cli {
/// Path to the asset manifest
#[arg(
short,
long,
value_name = "FILE",
default_value = "manifest.yml",
global = true
)]
manifest: PathBuf,
/// Increase output verbosity
#[arg(short, long, global = true, action = ArgAction::Count)]
verbose: u8,
/// Suppress non-error output (hides splash and info messages)
#[arg(long, global = true)]
silent: bool,
/// Subcommand to execute
#[command(subcommand)]
command: Command,
}
/// Available subcommands.
#[derive(Subcommand)]
enum Command {
/// Initialize asset directories and a default manifest
Init,
/// Create a new rlvgl workspace
New {
/// Name of the project
name: String,
/// Target MCU (optional)
#[arg(long)]
mcu: Option<String>,
},
/// Build and run a target
Run {
/// Target to run
#[arg(value_enum)]
target: RunTarget,
},
/// Scan a directory for assets and update the manifest
Scan {
/// Root path containing assets
path: PathBuf,
},
/// Validate manifest entries against asset files
Check {
/// Root path containing assets
path: PathBuf,
/// Apply fixes to the manifest
#[arg(long)]
fix: bool,
},
/// Copy assets and generate an `rlvgl_assets.rs` module
Vendor {
/// Root path containing assets
path: PathBuf,
/// Directory to copy assets into
#[arg(
value_name = "OUT",
required_unless_present = "out_flag",
conflicts_with = "out_flag"
)]
out: Option<PathBuf>,
/// Directory to copy assets into
#[arg(
long = "out",
value_name = "OUT",
required_unless_present = "out",
conflicts_with = "out"
)]
out_flag: Option<PathBuf>,
/// Allow only assets with these licenses
#[arg(long, value_name = "LICENSE")]
allow: Vec<String>,
/// Deny assets with these licenses
#[arg(long, value_name = "LICENSE")]
deny: Vec<String>,
},
/// Convert assets to normalized formats and update manifest
Convert {
/// Root path containing assets
path: PathBuf,
/// Rebuild all assets even if cached
#[arg(long)]
force: bool,
},
/// Compress an image to an RLEC blob for firmware use
Compress {
/// Input image (PNG, BMP, etc.) or RLVGLRAW .raw file
input: PathBuf,
/// Output .rle file
output: PathBuf,
},
/// Decompress an RLEC .rle blob back to a PNG image
Decompress {
/// Input .rle file
input: PathBuf,
/// Output PNG file
output: PathBuf,
},
/// Generate thumbnails for quick previews
Preview {
/// Root path containing assets
path: PathBuf,
},
/// Register a target with a vendor directory
AddTarget {
/// Name of the target
name: String,
/// Directory where assets will be vendored
vendor_dir: PathBuf,
},
/// Regenerate Cargo features and an asset index from the manifest
Sync {
/// Directory to write generated files
#[arg(
value_name = "OUT",
required_unless_present = "out_flag",
conflicts_with = "out_flag"
)]
out: Option<PathBuf>,
/// Directory to write generated files
#[arg(
long = "out",
value_name = "OUT",
required_unless_present = "out",
conflicts_with = "out"
)]
out_flag: Option<PathBuf>,
/// Print changes instead of writing files
#[arg(long)]
dry_run: bool,
},
/// Scaffold a dual-mode assets crate
Scaffold {
/// Directory where the new crate will be created
path: PathBuf,
},
/// Build an APNG from a sequence of PNG frames
Apng {
/// Directory containing PNG frames
frames: PathBuf,
/// Output APNG file
out: PathBuf,
/// Frame delay in milliseconds
#[arg(long, default_value_t = 100)]
delay: u16,
/// Number of animation loops (0 = infinite)
#[arg(long, default_value_t = 0)]
loops: u32,
},
/// Output a JSON schema for the manifest structure
Schema,
/// Font-related commands
Fonts {
#[command(subcommand)]
cmd: FontsCommand,
},
/// Lottie-related commands (direct import requires Linux; CLI mode works everywhere)
Lottie {
#[command(subcommand)]
cmd: LottieCommand,
},
/// Render an SVG into raw images
Svg {
/// Path to the SVG file
svg: PathBuf,
/// Directory to write raw images into
out: PathBuf,
/// DPI values to render at
#[arg(long, value_name = "DPI", action = ArgAction::Append, default_values_t = [96.0])]
dpi: Vec<f32>,
/// Monochrome threshold (0-255)
#[arg(long)]
threshold: Option<u8>,
},
/// Svelte alignment commands
Svelte {
#[command(subcommand)]
cmd: SvelteCommand,
},
/// Launch the desktop UI.
Ui,
/// Run the desktop simulator.
Sim(sim::SimArgs),
/// Generate a `lib.rs` from generated BSP fragments
GenLib {
/// Directory containing generated modules
#[arg(long)]
src: PathBuf,
/// Path to output `lib.rs`
#[arg(long)]
out: PathBuf,
/// Prelude re-export form (e.g., `hal:split` or `none`)
#[arg(long, default_value = "hal:split")]
prelude: String,
/// Features to gate (comma-separated)
#[arg(
long,
value_delimiter = ',',
default_value = "hal,pac,split,flat,summaries,pinreport"
)]
features: Vec<String>,
/// Optional feature prefix for family gates
#[arg(long)]
family_feature_prefix: Option<String>,
/// Inline includes rather than `mod` shims
#[arg(long)]
inline_includes: bool,
},
/// Board support package generation commands
Bsp {
#[command(subcommand)]
cmd: BspCommand,
},
/// Extract BSP IR from vendor C sources (experimental)
Ast {
#[command(subcommand)]
cmd: AstCommand,
},
}
#[derive(Subcommand)]
enum FontsCommand {
/// Pack TTF/OTF fonts into bitmaps and metrics files
Pack {
/// Root path containing font files
path: PathBuf,
/// Point size for rasterization
#[arg(long, default_value_t = 32)]
size: u16,
/// Characters to include in the pack
#[arg(
long,
default_value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
)]
chars: String,
},
}
#[derive(Subcommand)]
enum SvelteCommand {
/// Generate token outputs for web + rlvgl
Tokens {
/// Input token YAML
input: PathBuf,
/// Output directory
out: PathBuf,
/// Optional token mode (defaults to the first/only mode)
#[arg(long)]
mode: Option<String>,
},
/// Ingest a Chakra UI theme (.ts) and emit a tokens.yaml
Chakra {
/// Input Chakra theme TypeScript file
input: PathBuf,
/// Output directory (tokens.yaml written here)
out: PathBuf,
},
/// Compile Svelte components into rlvgl output
Compile {
/// Input Svelte file or directory
input: PathBuf,
/// Output directory
out: PathBuf,
/// Optional token YAML to resolve styles
#[arg(long)]
tokens: Option<PathBuf>,
},
/// Emit renderer glue for Svelte → WASM → rlvgl
Wasm {
/// Output directory
out: PathBuf,
/// Optional package name for generated files
#[arg(long)]
name: Option<String>,
},
/// Output JSON schema for tokens + UI IR
Schema {
/// Optional output file (defaults to stdout)
#[arg(long)]
out: Option<PathBuf>,
},
/// Validate tokens + Svelte subset constraints
Check {
/// Input Svelte file or directory
input: PathBuf,
/// Optional token YAML
#[arg(long)]
tokens: Option<PathBuf>,
/// Optional token mode (defaults to the first/only mode)
#[arg(long)]
mode: Option<String>,
},
}
#[derive(Subcommand)]
enum LottieCommand {
/// Import a Lottie JSON into PNG frames and an optional APNG using the
/// default `lottie-cli` executable on `PATH`
Import {
/// Path to the Lottie JSON file
json: PathBuf,
/// Directory to write PNG frames into
out: PathBuf,
/// Optional APNG file to generate
#[arg(long)]
apng: Option<PathBuf>,
},
/// Use an external CLI to convert a Lottie JSON into frames and an optional APNG
Cli {
/// Path to the external CLI binary
#[arg(long, default_value = "lottie-cli")]
bin: PathBuf,
/// Path to the Lottie JSON file
json: PathBuf,
/// Directory to write PNG frames into
out: PathBuf,
/// Optional APNG file to generate
#[arg(long)]
apng: Option<PathBuf>,
},
}
#[derive(Subcommand)]
enum BspCommand {
/// Render Rust source from a CubeMX `.ioc` file
FromIoc {
/// Input `.ioc` file
ioc: PathBuf,
/// Output directory for generated files
#[arg(long)]
out: PathBuf,
/// Generate per-core outputs (cm7/ and cm4/) instead of unified
#[arg(long)]
split_cores: bool,
/// Restrict output to a single core in unified mode
#[arg(long, value_enum)]
core: Option<CoreSel>,
/// Override which core initializes system clocks
#[arg(long, value_enum)]
clock_init_core: Option<CoreSel>,
/// Assign ownership to peripherals (comma-separated name=core pairs)
/// Example: --periph-core usart1=cm4,spi1=cm7
#[arg(long, value_delimiter = ',')]
periph_core: Vec<String>,
/// Render using the built-in HAL template
#[arg(long)]
emit_hal: bool,
/// Render using the built-in PAC template
#[arg(long)]
emit_pac: bool,
/// MiniJinja template to render
#[arg(long, conflicts_with_all = ["emit_hal", "emit_pac"])]
template: Option<PathBuf>,
/// Collapse RCC writes by register
#[arg(long)]
grouped_writes: bool,
/// Emit a single consolidated file
#[arg(long, group = "layout")]
one_file: bool,
/// Emit one file per peripheral
#[arg(long, group = "layout")]
per_peripheral: bool,
/// Include optional de-initialization helpers
#[arg(long)]
with_deinit: bool,
/// Permit configuration of reserved SWD pins (PA13/PA14)
#[arg(long)]
allow_reserved: bool,
/// Use label-based identifiers when available
#[arg(long)]
use_label_names: bool,
/// Prefix to apply to label identifiers when needed
#[arg(long)]
label_prefix: Option<String>,
/// Fail if two labels sanitize to the same identifier
#[arg(long)]
fail_on_duplicate_labels: bool,
/// Emit a `pins` module with label constants (PAC)
#[arg(long)]
emit_label_consts: bool,
},
/// Render Rust source from vendor C sources (experimental)
FromC {
/// Input C files or directories (recurses)
inputs: Vec<PathBuf>,
/// Output directory for generated files
#[arg(long)]
out: PathBuf,
/// MCU identifier (e.g., STM32H747XIHx)
#[arg(long)]
mcu: String,
/// Package identifier (e.g., LQFP176)
#[arg(long)]
package: String,
/// Render using the built-in HAL template
#[arg(long)]
emit_hal: bool,
/// Render using the built-in PAC template
#[arg(long)]
emit_pac: bool,
/// MiniJinja template to render
#[arg(long, conflicts_with_all = ["emit_hal", "emit_pac"])]
template: Option<PathBuf>,
/// Collapse RCC writes by register
#[arg(long)]
grouped_writes: bool,
/// Include optional de-initialization helpers
#[arg(long)]
with_deinit: bool,
/// Emit a single consolidated file
#[arg(long, group = "layout")]
one_file: bool,
/// Emit one file per peripheral
#[arg(long, group = "layout")]
per_peripheral: bool,
/// Use label-based identifiers when available
#[arg(long)]
use_label_names: bool,
/// Prefix to apply to label identifiers when needed
#[arg(long)]
label_prefix: Option<String>,
/// Fail if two labels sanitize to the same identifier
#[arg(long)]
fail_on_duplicate_labels: bool,
/// Emit a `pins` module with label constants (PAC)
#[arg(long)]
emit_label_consts: bool,
},
}
#[derive(Subcommand)]
enum AstCommand {
/// Extract IR from one or more C files
FromC {
/// MCU identifier (e.g., STM32H747XIHx)
#[arg(long)]
mcu: String,
/// Package identifier (e.g., LQFP176)
#[arg(long)]
package: String,
/// Input C files or directories (recurses)
inputs: Vec<PathBuf>,
/// Output path for the generated IR JSON (stdout if omitted)
#[arg(long)]
out: Option<PathBuf>,
},
}
/// Run the rlgvl-creator command-line interface.
pub fn run() -> Result<()> {
let cli = Cli::parse();
if !cli.silent {
println!("rlvgl v{} • rlvgl-creator", env!("CARGO_PKG_VERSION"));
if cli.verbose > 0 {
eprintln!("Using manifest {}", cli.manifest.display());
}
}
match cli.command {
Command::Init => init::run(&cli.manifest)?,
Command::New { name, mcu } => new::run(&name, mcu.as_deref())?,
Command::Run { target } => match target {
RunTarget::Sim => run::sim()?,
},
Command::Scan { path } => scan::run(&path, &cli.manifest)?,
Command::Check { path, fix } => check::run(&path, &cli.manifest, fix)?,
Command::Vendor {
path,
out,
out_flag,
allow,
deny,
} => {
let out = resolve_out_arg(out, out_flag, "vendor")?;
vendor::run(&path, &cli.manifest, &out, &allow, &deny)?
}
Command::Convert { path, force } => convert::run(&path, &cli.manifest, force)?,
Command::Compress { input, output } => compress::run(&input, &output)?,
Command::Decompress { input, output } => compress::decompress(&input, &output)?,
Command::Preview { path } => preview::run(&path, &cli.manifest)?,
Command::AddTarget { name, vendor_dir } => {
add_target::run(&cli.manifest, &name, &vendor_dir)?
}
Command::Sync {
out,
out_flag,
dry_run,
} => {
let out = resolve_out_arg(out, out_flag, "sync")?;
sync::run(&cli.manifest, &out, dry_run)?
}
Command::Scaffold { path } => scaffold::run(&path, &cli.manifest)?,
Command::Apng {
frames,
out,
delay,
loops,
} => apng::run(&frames, &out, delay, loops)?,
Command::Schema => schema::run()?,
Command::Fonts { cmd } => match cmd {
FontsCommand::Pack { path, size, chars } => {
fonts::pack(&path, &cli.manifest, size as f32, &chars)?
}
},
Command::Lottie { cmd } => match cmd {
LottieCommand::Import { json, out, apng } => {
lottie::import(&json, &out, apng.as_deref())?
}
LottieCommand::Cli {
bin,
json,
out,
apng,
} => lottie::import_cli(&bin, &json, &out, apng.as_deref())?,
},
Command::Svg {
svg,
out,
dpi,
threshold,
} => svg::run(&svg, &out, &dpi, threshold)?,
Command::Svelte { cmd } => match cmd {
SvelteCommand::Chakra { input, out } => chakra::ingest(&input, &out)?,
SvelteCommand::Tokens { input, out, mode } => {
svelte::tokens(&input, &out, mode.as_deref())?
}
SvelteCommand::Compile { input, out, tokens } => {
svelte::compile(&input, &out, tokens.as_deref())?
}
SvelteCommand::Wasm { out, name } => svelte::wasm(&out, name.as_deref())?,
SvelteCommand::Schema { out } => svelte::schema(out.as_deref())?,
SvelteCommand::Check {
input,
tokens,
mode,
} => svelte::check(&input, tokens.as_deref(), mode.as_deref())?,
},
Command::Ui => {
#[cfg(feature = "creator_ui")]
crate::ui::run()?;
#[cfg(not(feature = "creator_ui"))]
return Err(anyhow!("creator_ui feature is not enabled"));
}
Command::Sim(args) => sim::run(args)?,
Command::GenLib {
src,
out,
prelude,
features,
family_feature_prefix,
inline_includes,
} => {
let df = if prelude == "none" {
None
} else {
let parts: Vec<_> = prelude.split(':').collect();
if parts.len() != 2 {
return Err(anyhow!("prelude must be kind:form or 'none'"));
}
Some((parts[0].to_string(), parts[1].to_string()))
};
let df_ref = df.as_ref().map(|(a, b)| (a.as_str(), b.as_str()));
gen_lib::emit_lib_rs(
&src,
&out,
df_ref,
&features,
family_feature_prefix.as_deref(),
inline_includes,
)?;
}
Command::Bsp { cmd } => match cmd {
BspCommand::FromIoc {
ioc,
out,
split_cores,
core,
clock_init_core,
periph_core,
emit_hal,
emit_pac,
template,
grouped_writes,
one_file: _,
per_peripheral,
with_deinit,
allow_reserved,
use_label_names,
label_prefix,
fail_on_duplicate_labels,
emit_label_consts,
} => {
let mut kinds = Vec::new();
if emit_hal {
kinds.push(bsp_gen::TemplateKind::Hal);
}
if emit_pac {
kinds.push(bsp_gen::TemplateKind::Pac);
}
if let Some(t) = template {
kinds.push(bsp_gen::TemplateKind::Custom(t));
}
if kinds.is_empty() {
return Err(anyhow!("select --emit-hal, --emit-pac, or --template"));
}
let layout = if per_peripheral {
bsp_gen::Layout::PerPeripheral
} else {
bsp_gen::Layout::OneFile
};
let to_ir_core = |c: CoreSel| match c {
CoreSel::Cm7 => crate::bsp::ir::Core::Cm7,
CoreSel::Cm4 => crate::bsp::ir::Core::Cm4,
};
// Build overrides map if provided
let mut overrides: indexmap::IndexMap<String, crate::bsp::ir::Core> =
indexmap::IndexMap::new();
for entry in periph_core {
if let Some((name, core_s)) = entry.split_once('=') {
let c = match core_s.to_ascii_lowercase().as_str() {
"cm7" => Some(crate::bsp::ir::Core::Cm7),
"cm4" => Some(crate::bsp::ir::Core::Cm4),
_ => None,
};
if let Some(c) = c {
overrides.insert(name.to_ascii_lowercase(), c);
} else {
return Err(anyhow!("invalid core in periph-core: {}", core_s));
}
} else {
return Err(anyhow!("periph-core entries must be name=core"));
}
}
let overrides_ref = if overrides.is_empty() {
None
} else {
Some(&overrides)
};
let init_override = clock_init_core.map(to_ir_core);
// Auto-split when both cores are present in the .ioc and no single-core was requested
let mut do_split = split_cores;
if !do_split && core.is_none() {
if let Ok(txt) = std::fs::read_to_string(&ioc) {
let (cm7, cm4) = crate::bsp::ioc::detect_core_projects(&txt);
if cm7 && cm4 {
do_split = true;
}
}
}
if do_split {
for (subdir, csel) in [("cm7", CoreSel::Cm7), ("cm4", CoreSel::Cm4)] {
let odir = out.join(subdir);
std::fs::create_dir_all(&odir)?;
for kind in &kinds {
bsp_gen::from_ioc(
&ioc,
kind.clone(),
&odir,
grouped_writes,
with_deinit,
allow_reserved,
layout.clone(),
use_label_names,
label_prefix.as_deref(),
fail_on_duplicate_labels,
emit_label_consts,
Some(to_ir_core(csel)),
init_override,
overrides_ref,
)?;
}
}
} else {
let core_filter = core.map(to_ir_core);
for kind in kinds {
bsp_gen::from_ioc(
&ioc,
kind,
&out,
grouped_writes,
with_deinit,
allow_reserved,
layout.clone(),
use_label_names,
label_prefix.as_deref(),
fail_on_duplicate_labels,
emit_label_consts,
core_filter,
init_override,
overrides_ref,
)?;
}
}
if per_peripheral {
bsp_gen::emit_board_mod(&out, emit_hal, emit_pac, false, false)?;
}
}
BspCommand::FromC {
inputs,
out,
mcu,
package,
emit_hal,
emit_pac,
template,
grouped_writes,
one_file: _,
per_peripheral,
with_deinit,
use_label_names,
label_prefix,
fail_on_duplicate_labels,
emit_label_consts,
} => {
let mut files = Vec::new();
for p in inputs {
let ps = if p.is_dir() {
crate::ast::discover_c_sources(&p)
} else {
vec![p]
};
files.extend(ps);
}
if files.is_empty() {
return Err(anyhow!("no C sources found in inputs"));
}
let ir_tmp = crate::ast::extract_from_c_sources(
&files,
crate::ast::ExtractOptions {
mcu: &mcu,
package: &package,
},
)?;
// Normalize to the shared IR type to avoid cfg(test) path differences
let mut ir: crate::ir::Ir = serde_json::from_slice(&serde_json::to_vec(&ir_tmp)?)?;
// Apply environment overrides (STM32_* variables)
crate::bsp_gen::apply_env_overrides(&mut ir);
let mut kinds = Vec::new();
if emit_hal {
kinds.push(bsp_gen::TemplateKind::Hal);
}
if emit_pac {
kinds.push(bsp_gen::TemplateKind::Pac);
}
if let Some(t) = template {
kinds.push(bsp_gen::TemplateKind::Custom(t));
}
if kinds.is_empty() {
return Err(anyhow!("select --emit-hal, --emit-pac, or --template"));
}
let layout = if per_peripheral {
bsp_gen::Layout::PerPeripheral
} else {
bsp_gen::Layout::OneFile
};
for kind in kinds {
bsp_gen::render_from_ir(
&ir,
kind,
&out,
grouped_writes,
with_deinit,
layout.clone(),
use_label_names,
label_prefix.as_deref(),
fail_on_duplicate_labels,
emit_label_consts,
None,
)?;
}
if per_peripheral {
bsp_gen::emit_board_mod(&out, emit_hal, emit_pac, false, false)?;
}
}
},
Command::Ast { cmd } => match cmd {
AstCommand::FromC {
mcu,
package,
inputs,
out,
} => {
let mut files = Vec::new();
for p in inputs {
let ps = if p.is_dir() {
crate::ast::discover_c_sources(&p)
} else {
vec![p]
};
files.extend(ps);
}
let ir_tmp = crate::ast::extract_from_c_sources(
&files,
crate::ast::ExtractOptions {
mcu: &mcu,
package: &package,
},
)?;
let ir: crate::ir::Ir = serde_json::from_slice(&serde_json::to_vec(&ir_tmp)?)?;
let json = serde_json::to_string_pretty(&ir)?;
if let Some(path) = out {
std::fs::write(path, json)?;
} else {
println!("{}", json);
}
}
},
}
Ok(())
}