-
Notifications
You must be signed in to change notification settings - Fork 624
/
cbindgen.rs
1051 lines (985 loc) · 37.6 KB
/
cbindgen.rs
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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
use anyhow::Context;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
// cSpell: ignore compat constexpr corelib deps sharedvector pathdata
fn enums(path: &Path) -> anyhow::Result<()> {
let mut enums_priv = BufWriter::new(
std::fs::File::create(path.join("slint_enums_internal.h"))
.context("Error creating slint_enums_internal.h file")?,
);
writeln!(enums_priv, "#pragma once")?;
writeln!(enums_priv, "// This file is auto-generated from {}", file!())?;
writeln!(enums_priv, "#include \"slint_enums.h\"")?;
writeln!(enums_priv, "namespace slint::cbindgen_private {{")?;
let mut enums_pub = BufWriter::new(
std::fs::File::create(path.join("slint_enums.h"))
.context("Error creating slint_enums.h file")?,
);
writeln!(enums_pub, "#pragma once")?;
writeln!(enums_pub, "// This file is auto-generated from {}", file!())?;
writeln!(enums_pub, "namespace slint {{")?;
macro_rules! enum_file {
(PointerEventButton) => {{
writeln!(enums_priv, "using slint::PointerEventButton;")?;
&mut enums_pub
}};
(AccessibleRole) => {{
writeln!(enums_priv, "using slint::testing::AccessibleRole;")?;
&mut enums_pub
}};
($_:ident) => {
&mut enums_priv
};
}
macro_rules! enum_sub_namespace {
(AccessibleRole) => {{
Some("testing")
}};
($_:ident) => {
None
};
}
macro_rules! print_enums {
($( $(#[doc = $enum_doc:literal])* $(#[non_exhaustive])? enum $Name:ident { $( $(#[doc = $value_doc:literal])* $Value:ident,)* })*) => {
$(
let file = enum_file!($Name);
let namespace: Option<&'static str> = enum_sub_namespace!($Name);
if let Some(ns) = namespace {
writeln!(file, "namespace {} {{", ns)?;
}
$(writeln!(file, "///{}", $enum_doc)?;)*
writeln!(file, "enum class {} {{", stringify!($Name))?;
$(
$(writeln!(file, " ///{}", $value_doc)?;)*
writeln!(file, " {},", stringify!($Value).trim_start_matches("r#"))?;
)*
writeln!(file, "}};")?;
if namespace.is_some() {
writeln!(file, "}}")?;
}
)*
}
}
i_slint_common::for_each_enums!(print_enums);
writeln!(enums_pub, "}}")?;
writeln!(enums_priv, "}}")?;
// Print the key codes constants
// This is not an enum, but fits well in that file
writeln!(
enums_pub,
r#"
/// This namespace contains constants for each special non-printable key.
///
/// Each constant can be converted to SharedString.
/// The constants are meant to be used with the slint::Window::dispatch_key_press_event() and
/// slint::Window::dispatch_key_release_event() functions.
///
/// Example:
/// ```
/// window.dispatch_key_press_event(slint::platform::key_codes::Tab);
/// ```
namespace slint::platform::key_codes {{
"#
)?;
macro_rules! print_key_codes {
($($char:literal # $name:ident # $($qt:ident)|* # $($winit:ident $(($_pos:ident))?)|* # $($_xkb:ident)|*;)*) => {
$(
writeln!(enums_pub, "/// A constant that represents the key code to be used in slint::Window::dispatch_key_press_event()")?;
writeln!(enums_pub, r#"constexpr std::u8string_view {} = u8"\u{:04x}";"#, stringify!($name), $char as u32)?;
)*
};
}
i_slint_common::for_each_special_keys!(print_key_codes);
writeln!(enums_pub, "}}")?;
Ok(())
}
fn builtin_structs(path: &Path) -> anyhow::Result<()> {
let mut structs_pub = BufWriter::new(
std::fs::File::create(path.join("slint_builtin_structs.h"))
.context("Error creating slint_builtin_structs.h file")?,
);
writeln!(structs_pub, "#pragma once")?;
writeln!(structs_pub, "// This file is auto-generated from {}", file!())?;
writeln!(structs_pub, "namespace slint {{")?;
let mut structs_priv = BufWriter::new(
std::fs::File::create(path.join("slint_builtin_structs_internal.h"))
.context("Error creating slint_builtin_structs_internal.h file")?,
);
writeln!(structs_priv, "#pragma once")?;
writeln!(structs_priv, "// This file is auto-generated from {}", file!())?;
writeln!(structs_priv, "#include \"slint_builtin_structs.h\"")?;
writeln!(structs_priv, "#include \"slint_enums_internal.h\"")?;
writeln!(structs_priv, "namespace slint::cbindgen_private {{")?;
writeln!(structs_priv, "enum class KeyEventType : uint8_t;")?;
macro_rules! struct_file {
(StandardListViewItem) => {{
writeln!(structs_priv, "using slint::StandardListViewItem;")?;
&mut structs_pub
}};
($_:ident) => {
&mut structs_priv
};
}
macro_rules! print_structs {
($(
$(#[doc = $struct_doc:literal])*
$(#[non_exhaustive])?
$(#[derive(Copy, Eq)])?
struct $Name:ident {
@name = $inner_name:literal
export {
$( $(#[doc = $pub_doc:literal])* $pub_field:ident : $pub_type:ty, )*
}
private {
$( $(#[doc = $pri_doc:literal])* $pri_field:ident : $pri_type:ty, )*
}
}
)*) => {
$(
let file = struct_file!($Name);
$(writeln!(file, "///{}", $struct_doc)?;)*
writeln!(file, "struct {} {{", stringify!($Name))?;
$(
$(writeln!(file, " ///{}", $pub_doc)?;)*
let pub_type = match stringify!($pub_type) {
"i32" => "int32_t",
"f32" | "Coord" => "float",
other => other,
};
writeln!(file, " {} {};", pub_type, stringify!($pub_field))?;
)*
$(
$(writeln!(file, " ///{}", $pri_doc)?;)*
let pri_type = stringify!($pri_type).replace(' ', "");
let pri_type = match pri_type.as_str() {
"usize" => "uintptr_t",
"crate::animations::Instant" => "uint64_t",
// This shouldn't be accessed by the C++ anyway, just need to have the same ABI in a struct
"Option<i32>" => "std::pair<int32_t, int32_t>",
"Option<core::ops::Range<i32>>" => "std::tuple<int32_t, int32_t, int32_t>",
other => other,
};
writeln!(file, " {} {};", pri_type, stringify!($pri_field))?;
)*
writeln!(file, " /// \\private")?;
writeln!(file, " {}", format!("friend bool operator==(const {name}&, const {name}&) = default;", name = stringify!($Name)))?;
writeln!(file, " /// \\private")?;
writeln!(file, " {}", format!("friend bool operator!=(const {name}&, const {name}&) = default;", name = stringify!($Name)))?;
writeln!(file, "}};")?;
)*
};
}
i_slint_common::for_each_builtin_structs!(print_structs);
writeln!(structs_priv, "}}")?;
writeln!(structs_pub, "}}")?;
Ok(())
}
fn ensure_cargo_rerun_for_crate(
crate_dir: &Path,
dependencies: &mut Vec<PathBuf>,
) -> anyhow::Result<()> {
dependencies.push(crate_dir.to_path_buf());
for entry in std::fs::read_dir(crate_dir)? {
let entry = entry?;
if entry.path().extension().map_or(false, |e| e == "rs") {
dependencies.push(entry.path());
}
}
Ok(())
}
fn default_config() -> cbindgen::Config {
let mut config = cbindgen::Config::default();
config.macro_expansion.bitflags = true;
config.pragma_once = true;
config.include_version = true;
config.namespaces = Some(vec!["slint".into(), "cbindgen_private".into()]);
config.line_length = 100;
config.tab_width = 4;
// Note: we might need to switch to C if we need to generate bindings for language that needs C headers
config.language = cbindgen::Language::Cxx;
config.cpp_compat = true;
config.documentation = true;
config.export = cbindgen::ExportConfig {
rename: [
("Callback".into(), "private_api::CallbackHelper".into()),
("VoidArg".into(), "void".into()),
("KeyEventArg".into(), "KeyEvent".into()),
("PointerEventArg".into(), "PointerEvent".into()),
("PointerScrollEventArg".into(), "PointerScrollEvent".into()),
("PointArg".into(), "slint::LogicalPosition".into()),
("FloatArg".into(), "float".into()),
("IntArg".into(), "int".into()),
("MenuEntryArg".into(), "MenuEntry".into()),
// Note: these types are not the same, but they are only used in callback return types that are only used in C++ (set and called)
// therefore it is ok to reinterpret_cast
("MenuEntryModel".into(), "std::shared_ptr<slint::Model<MenuEntry>>".into()),
("Coord".into(), "float".into()),
]
.iter()
.cloned()
.collect(),
..Default::default()
};
config.defines = [
("target_pointer_width = 64".into(), "SLINT_TARGET_64".into()),
("target_pointer_width = 32".into(), "SLINT_TARGET_32".into()),
("target_arch = wasm32".into(), "SLINT_TARGET_WASM".into()), // Disable any wasm guarded code in C++, too - so that there are no gaps in enums.
]
.iter()
.cloned()
.collect();
config.structure.associated_constants_in_body = true;
config.constant.allow_constexpr = true;
config
}
fn gen_item_declarations(items: &[&str]) -> String {
format!(
r#"
namespace slint::private_api {{
#define SLINT_DECL_ITEM(ItemName) \
extern const cbindgen_private::ItemVTable ItemName##VTable; \
extern SLINT_DLL_IMPORT const cbindgen_private::ItemVTable* slint_get_##ItemName##VTable();
extern "C" {{
{}
}}
#undef SLINT_DECL_ITEM
}}
"#,
items
.iter()
.map(|item_name| format!("SLINT_DECL_ITEM({});", item_name))
.collect::<Vec<_>>()
.join("\n")
)
}
fn gen_corelib(
root_dir: &Path,
include_dir: &Path,
dependencies: &mut Vec<PathBuf>,
enabled_features: EnabledFeatures,
) -> anyhow::Result<()> {
let mut config = default_config();
let items = [
"Empty",
"Rectangle",
"BasicBorderRectangle",
"BorderRectangle",
"ImageItem",
"ClippedImage",
"TouchArea",
"FocusScope",
"SwipeGestureHandler",
"Flickable",
"SimpleText",
"ComplexText",
"Path",
"WindowItem",
"TextInput",
"Clip",
"BoxShadow",
"Rotate",
"Opacity",
"Layer",
"ContextMenu",
];
config.export.include = [
"Clipboard",
"ItemTreeVTable",
"Slice",
"WindowAdapterRcOpaque",
"PropertyAnimation",
"AnimationDirection",
"EasingCurve",
"TextHorizontalAlignment",
"TextVerticalAlignment",
"TextOverflow",
"TextWrap",
"ImageFit",
"FillRule",
"MouseCursor",
"InputType",
"StandardButtonKind",
"DialogButtonRole",
"PointerEventKind",
"PointerEventButton",
"PointerEvent",
"PointerScrollEvent",
"Rect",
"SortOrder",
"BitmapFont",
"PhysicalRegion",
]
.iter()
.chain(items.iter())
.map(|x| x.to_string())
.collect();
let mut private_exported_types: std::collections::HashSet<String> =
config.export.include.iter().cloned().collect();
// included in generated_public.h
let public_exported_types = [
"RenderingState",
"SetRenderingNotifierError",
"GraphicsAPI",
"CloseRequestResponse",
"StandardListViewItem",
"Rgb8Pixel",
"Rgba8Pixel",
];
config.export.exclude = [
"SharedString",
"SharedVector",
"ImageInner",
"ImageCacheKey",
"Image",
"Color",
"PathData",
"PathElement",
"Brush",
"slint_new_path_elements",
"slint_new_path_events",
"Property",
"Slice",
"Timer",
"TimerMode",
"PropertyHandleOpaque",
"Callback",
"slint_property_listener_scope_evaluate",
"slint_property_listener_scope_is_dirty",
"PropertyTrackerOpaque",
"CallbackOpaque",
"WindowAdapterRc",
"VoidArg",
"KeyEventArg",
"PointerEventArg",
"PointerScrollEventArg",
"PointArg",
"Point",
"MenuEntryModel",
"MenuEntryArg",
"slint_color_brighter",
"slint_color_darker",
"slint_color_transparentize",
"slint_color_mix",
"slint_color_with_alpha",
"slint_color_to_hsva",
"slint_color_from_hsva",
"slint_image_size",
"slint_image_path",
"slint_image_load_from_path",
"slint_image_load_from_embedded_data",
"slint_image_from_embedded_textures",
"slint_image_compare_equal",
"slint_image_set_nine_slice_edges",
"slint_image_to_rgb8",
"slint_image_to_rgba8",
"slint_image_to_rgba8_premultiplied",
"slint_timer_start",
"slint_timer_singleshot",
"slint_timer_destroy",
"slint_timer_stop",
"slint_timer_restart",
"slint_timer_running",
"Coord",
"LogicalRect",
"LogicalPoint",
"LogicalPosition",
"LogicalLength",
]
.iter()
.chain(public_exported_types.iter())
.map(|x| x.to_string())
.collect();
let mut crate_dir = root_dir.to_owned();
crate_dir.extend(["internal", "core"].iter());
ensure_cargo_rerun_for_crate(&crate_dir, dependencies)?;
let mut string_config = config.clone();
string_config.export.exclude = vec!["SharedString".into()];
string_config.export.body.insert(
"Slice".to_owned(),
" const T &operator[](int i) const { return ptr[i]; }
/// Note: this doesn't initialize Slice properly, but we need to keep the struct as compatible with C
constexpr Slice() = default;
/// Rust uses a NonNull, so even empty slices shouldn't use nullptr
constexpr Slice(const T *ptr, uintptr_t len) : ptr(ptr ? const_cast<T*>(ptr) : reinterpret_cast<T*>(sizeof(T))), len(len) {}
"
.to_owned(),
);
cbindgen::Builder::new()
.with_config(string_config)
.with_src(crate_dir.join("string.rs"))
.with_src(crate_dir.join("slice.rs"))
.with_after_include("namespace slint { struct SharedString; }")
.generate()
.context("Unable to generate bindings for slint_string_internal.h")?
.write_to_file(include_dir.join("slint_string_internal.h"));
cbindgen::Builder::new()
.with_config(config.clone())
.with_src(crate_dir.join("sharedvector.rs"))
.with_after_include("namespace slint { template<typename T> struct SharedVector; }")
.generate()
.context("Unable to generate bindings for slint_sharedvector_internal.h")?
.write_to_file(include_dir.join("slint_sharedvector_internal.h"));
let mut properties_config = config.clone();
properties_config.export.exclude.clear();
properties_config.structure.derive_eq = true;
properties_config.structure.derive_neq = true;
private_exported_types.extend(properties_config.export.include.iter().cloned());
cbindgen::Builder::new()
.with_config(properties_config)
.with_src(crate_dir.join("properties.rs"))
.with_src(crate_dir.join("properties/ffi.rs"))
.with_src(crate_dir.join("callbacks.rs"))
.with_after_include("namespace slint { class Color; class Brush; }")
.generate()
.context("Unable to generate bindings for slint_properties_internal.h")?
.write_to_file(include_dir.join("slint_properties_internal.h"));
// slint_timer_internal.h:
let timer_config = {
let mut tmp = config.clone();
tmp.export.include = vec![
"TimerMode",
"slint_timer_start",
"slint_timer_singleshot",
"slint_timer_destroy",
"slint_timer_stop",
"slint_timer_restart",
"slint_timer_running",
]
.iter()
.map(|s| s.to_string())
.collect();
tmp.export.exclude = config
.export
.exclude
.iter()
.filter(|exclusion| !tmp.export.include.iter().any(|inclusion| inclusion == *exclusion))
.cloned()
.collect();
tmp
};
cbindgen::Builder::new()
.with_config(timer_config)
.with_src(crate_dir.join("timers.rs"))
.generate()
.context("Unable to generate bindings for slint_timer_internal.h")?
.write_to_file(include_dir.join("slint_timer_internal.h"));
for (rust_types, extra_excluded_types, internal_header, prelude) in [
(
vec![
"ImageInner",
"Image",
"ImageCacheKey",
"Size",
"slint_image_size",
"slint_image_path",
"slint_image_load_from_path",
"slint_image_load_from_embedded_data",
"slint_image_from_embedded_textures",
"slint_image_compare_equal",
"slint_image_set_nine_slice_edges",
"slint_image_to_rgb8",
"slint_image_to_rgba8",
"slint_image_to_rgba8_premultiplied",
"SharedPixelBuffer",
"SharedImageBuffer",
"StaticTextures",
"BorrowedOpenGLTextureOrigin"
],
vec!["Color"],
"slint_image_internal.h",
"namespace slint::cbindgen_private { struct ParsedSVG{}; struct HTMLImage{}; using namespace vtable; namespace types{ struct NineSliceImage{}; } }",
),
(
vec!["Color", "slint_color_brighter", "slint_color_darker",
"slint_color_transparentize",
"slint_color_mix",
"slint_color_with_alpha",
"slint_color_to_hsva",
"slint_color_from_hsva",],
vec![],
"slint_color_internal.h",
"",
),
(
vec!["PathData", "PathElement", "slint_new_path_elements", "slint_new_path_events"],
vec![],
"slint_pathdata_internal.h",
"",
),
(
vec!["Brush", "LinearGradient", "GradientStop", "RadialGradient"],
vec!["Color"],
"slint_brush_internal.h",
"",
),
]
.iter()
{
let mut special_config = config.clone();
special_config.export.include = rust_types.iter().map(|s| s.to_string()).collect();
special_config.export.exclude = [
"slint_visit_item_tree",
"slint_windowrc_drop",
"slint_windowrc_clone",
"slint_windowrc_show",
"slint_windowrc_hide",
"slint_windowrc_is_visible",
"slint_windowrc_get_scale_factor",
"slint_windowrc_set_scale_factor",
"slint_windowrc_get_text_input_focused",
"slint_windowrc_set_text_input_focused",
"slint_windowrc_set_focus_item",
"slint_windowrc_set_component",
"slint_windowrc_show_popup",
"slint_windowrc_close_popup",
"slint_windowrc_set_rendering_notifier",
"slint_windowrc_request_redraw",
"slint_windowrc_on_close_requested",
"slint_windowrc_position",
"slint_windowrc_set_logical_position",
"slint_windowrc_set_physical_position",
"slint_windowrc_size",
"slint_windowrc_set_logical_size",
"slint_windowrc_set_physical_size",
"slint_windowrc_color_scheme",
"slint_windowrc_default_font_size",
"slint_windowrc_dispatch_pointer_event",
"slint_windowrc_dispatch_key_event",
"slint_windowrc_dispatch_event",
"slint_windowrc_set_fullscreen",
"slint_windowrc_set_minimized",
"slint_windowrc_set_maximized",
"slint_windowrc_is_fullscreen",
"slint_windowrc_is_minimized",
"slint_windowrc_is_maximized",
"slint_windowrc_take_snapshot",
"slint_new_path_elements",
"slint_new_path_events",
"slint_color_brighter",
"slint_color_darker",
"slint_color_transparentize",
"slint_color_mix",
"slint_color_with_alpha",
"slint_color_to_hsva",
"slint_color_from_hsva",
"slint_image_size",
"slint_image_path",
"slint_image_load_from_path",
"slint_image_load_from_embedded_data",
"slint_image_set_nine_slice_edges",
"slint_image_to_rgb8",
"slint_image_to_rgba8",
"slint_image_to_rgba8_premultiplied",
"slint_image_from_embedded_textures",
"slint_image_compare_equal",
]
.iter()
.filter(|exclusion| !rust_types.iter().any(|inclusion| inclusion == *exclusion))
.chain(extra_excluded_types.iter())
.chain(public_exported_types.iter())
.map(|s| s.to_string())
.collect();
special_config.enumeration = cbindgen::EnumConfig {
derive_tagged_enum_copy_assignment: true,
derive_tagged_enum_copy_constructor: true,
derive_tagged_enum_destructor: true,
derive_helper_methods: true,
private_default_tagged_enum_constructor: true,
..Default::default()
};
special_config.structure.derive_eq = true;
special_config.structure.derive_neq = true;
// Put the rust type in a deeper "types" namespace, so the use of same type in for example generated
// Property<> fields uses the public `slint::Blah` type
special_config.namespaces =
Some(vec!["slint".into(), "cbindgen_private".into(), "types".into()]);
private_exported_types.extend(special_config.export.include.iter().cloned());
special_config.after_includes = (!prelude.is_empty()).then(|| prelude.to_string());
cbindgen::Builder::new()
.with_config(special_config)
.with_src(crate_dir.join("graphics.rs"))
.with_src(crate_dir.join("graphics/color.rs"))
.with_src(crate_dir.join("graphics/path.rs"))
.with_src(crate_dir.join("graphics/brush.rs"))
.with_src(crate_dir.join("graphics/image.rs"))
.with_src(crate_dir.join("graphics/image/cache.rs"))
.with_src(crate_dir.join("animations.rs"))
// .with_src(crate_dir.join("input.rs"))
.with_src(crate_dir.join("item_rendering.rs"))
.with_src(crate_dir.join("window.rs"))
.with_include("slint_enums_internal.h")
.generate()
.with_context(|| format!("Unable to generate bindings for {}", internal_header))?
.write_to_file(include_dir.join(internal_header));
}
// Generate a header file with some public API (enums, etc.)
let mut public_config = config.clone();
public_config.namespaces = Some(vec!["slint".into()]);
public_config.export.item_types = vec![cbindgen::ItemType::Enums, cbindgen::ItemType::Structs];
// Previously included types are now excluded (to avoid duplicates)
public_config.export.exclude = private_exported_types.into_iter().collect();
public_config.export.exclude.push("LogicalPosition".into());
public_config.export.include = public_exported_types.into_iter().map(str::to_string).collect();
public_config.export.body.insert(
"Rgb8Pixel".to_owned(),
"/// \\private\nfriend bool operator==(const Rgb8Pixel&, const Rgb8Pixel&) = default;"
.into(),
);
public_config.export.body.insert(
"Rgba8Pixel".to_owned(),
"/// \\private\nfriend bool operator==(const Rgba8Pixel&, const Rgba8Pixel&) = default;"
.into(),
);
cbindgen::Builder::new()
.with_config(public_config)
.with_src(crate_dir.join("graphics.rs"))
.with_src(crate_dir.join("window.rs"))
.with_src(crate_dir.join("api.rs"))
.with_src(crate_dir.join("model.rs"))
.with_src(crate_dir.join("graphics/image.rs"))
.with_include("slint_string.h")
.with_after_include(format!(
r#"
/// This macro expands to the to the numeric value of the major version of Slint you're
/// developing against. For example if you're using version 1.5.2, this macro will expand to 1.
#define SLINT_VERSION_MAJOR {x}
/// This macro expands to the to the numeric value of the minor version of Slint you're
/// developing against. For example if you're using version 1.5.2, this macro will expand to 5.
#define SLINT_VERSION_MINOR {y}
/// This macro expands to the to the numeric value of the patch version of Slint you're
/// developing against. For example if you're using version 1.5.2, this macro will expand to 2.
#define SLINT_VERSION_PATCH {z}
/// This macro expands to the string representation of the version of Slint you're developing against.
/// For example if you're using version 1.5.2, this macro will expand to "1.5.2".
#define SLINT_VERSION_STRING "{x}.{y}.{z}"
{features}
"#,
x = env!("CARGO_PKG_VERSION_MAJOR"),
y = env!("CARGO_PKG_VERSION_MINOR"),
z = env!("CARGO_PKG_VERSION_PATCH"),
features = enabled_features.defines()
))
.generate()
.context("Unable to generate bindings for slint_generated_public.h")?
.write_to_file(include_dir.join("slint_generated_public.h"));
config.export.body.insert(
"ItemTreeNode".to_owned(),
" constexpr ItemTreeNode(Item_Body x) : item {x} {}
constexpr ItemTreeNode(DynamicTree_Body x) : dynamic_tree{x} {}"
.to_owned(),
);
config.export.body.insert(
"CachedRenderingData".to_owned(),
" constexpr CachedRenderingData() : cache_index{}, cache_generation{} {}".to_owned(),
);
config.export.body.insert(
"EasingCurve".to_owned(),
" constexpr EasingCurve(EasingCurve::Tag tag = Tag::Linear, float a = 0, float b = 0, float c = 1, float d = 1) : tag(tag), cubic_bezier{{a,b,c,d}} {}".into()
);
config.export.body.insert(
"LayoutInfo".to_owned(),
" inline LayoutInfo merge(const LayoutInfo &other) const;
friend inline LayoutInfo operator+(const LayoutInfo &a, const LayoutInfo &b) { return a.merge(b); }
friend bool operator==(const LayoutInfo&, const LayoutInfo&) = default;".into(),
);
config.export.body.insert(
"WindowEvent".to_owned(),
"/* Some members of the WindowEvent enum have destructors (with SharedString), but thankfully we don't use these so we can have an empty constructor */
~WindowEvent() {}"
.into(),
);
config
.export
.body
.insert("Flickable".to_owned(), " inline Flickable(); inline ~Flickable();".into());
config.export.pre_body.insert("FlickableDataBox".to_owned(), "struct FlickableData;".into());
cbindgen::Builder::new()
.with_config(config)
.with_src(crate_dir.join("lib.rs"))
.with_include("slint_config.h")
.with_include("vtable.h")
.with_include("slint_string.h")
.with_include("slint_sharedvector.h")
.with_include("slint_properties.h")
.with_include("slint_callbacks.h")
.with_include("slint_color.h")
.with_include("slint_image.h")
.with_include("slint_pathdata.h")
.with_include("slint_brush.h")
.with_include("slint_generated_public.h")
.with_include("slint_enums_internal.h")
.with_include("slint_point.h")
.with_include("slint_timer.h")
.with_include("slint_builtin_structs_internal.h")
.with_after_include(
r"
namespace slint {
namespace private_api { class WindowAdapterRc; }
namespace cbindgen_private {
using slint::private_api::WindowAdapterRc;
using namespace vtable;
struct KeyEvent; struct PointerEvent;
using private_api::Property;
using private_api::PathData;
using private_api::Point;
struct Rect;
using LogicalRect = Rect;
using LogicalPoint = Point2D<float>;
using LogicalLength = float;
struct ItemTreeVTable;
struct ItemVTable;
using types::IntRect;
}
template<typename ModelData> class Model;
}",
)
.with_trailer(gen_item_declarations(&items))
.generate()
.expect("Unable to generate bindings")
.write_to_file(include_dir.join("slint_internal.h"));
Ok(())
}
fn gen_backend_qt(
root_dir: &Path,
include_dir: &Path,
dependencies: &mut Vec<PathBuf>,
) -> anyhow::Result<()> {
let mut config = default_config();
let items = [
"NativeButton",
"NativeSpinBox",
"NativeCheckBox",
"NativeSlider",
"NativeProgressIndicator",
"NativeGroupBox",
"NativeLineEdit",
"NativeScrollView",
"NativeStandardListViewItem",
"NativeTableHeaderSection",
"NativeComboBox",
"NativeComboBoxPopup",
"NativeTabWidget",
"NativeTab",
"NativeStyleMetrics",
"NativePalette",
];
config.export.include = items.iter().map(|x| x.to_string()).collect();
config.export.exclude = vec!["FloatArg".into(), "IntArg".into()];
config.export.body.insert(
"NativeStyleMetrics".to_owned(),
" inline explicit NativeStyleMetrics(void* = nullptr); inline ~NativeStyleMetrics();"
.to_owned(),
);
config.export.body.insert(
"NativePalette".to_owned(),
" inline explicit NativePalette(void* = nullptr); inline ~NativePalette();".to_owned(),
);
let mut crate_dir = root_dir.to_owned();
crate_dir.extend(["internal", "backends", "qt"].iter());
ensure_cargo_rerun_for_crate(&crate_dir, dependencies)?;
cbindgen::Builder::new()
.with_config(config)
.with_crate(crate_dir)
.with_include("slint_internal.h")
.with_after_include(
r"
namespace slint::cbindgen_private {
// HACK ALERT: This struct declaration is duplicated in internal/backend/qt/qt_widgets.rs - keep in sync.
struct SlintTypeErasedWidget
{
virtual ~SlintTypeErasedWidget() = 0;
SlintTypeErasedWidget(const SlintTypeErasedWidget&) = delete;
SlintTypeErasedWidget& operator=(const SlintTypeErasedWidget&) = delete;
virtual void *qwidget() const = 0;
};
using SlintTypeErasedWidgetPtr = std::unique_ptr<SlintTypeErasedWidget>;
}
",
)
.with_trailer(gen_item_declarations(&items))
.generate()
.context("Unable to generate bindings for slint_qt_internal.h")?
.write_to_file(include_dir.join("slint_qt_internal.h"));
Ok(())
}
fn gen_testing(
root_dir: &Path,
include_dir: &Path,
dependencies: &mut Vec<PathBuf>,
) -> anyhow::Result<()> {
let config = default_config();
let mut crate_dir = root_dir.to_owned();
crate_dir.extend(["internal", "backends", "testing"].iter());
ensure_cargo_rerun_for_crate(&crate_dir, dependencies)?;
cbindgen::Builder::new()
.with_config(config)
.with_crate(crate_dir)
.with_include("slint_testing_internal.h")
.generate()
.context("Unable to generate bindings for slint_testing_internal.h")?
.write_to_file(include_dir.join("slint_testing_internal.h"));
Ok(())
}
fn gen_platform(
root_dir: &Path,
include_dir: &Path,
dependencies: &mut Vec<PathBuf>,
) -> anyhow::Result<()> {
let config = default_config();
let mut crate_dir = root_dir.to_owned();
crate_dir.extend(["api", "cpp"].iter());
ensure_cargo_rerun_for_crate(&crate_dir, dependencies)?;
cbindgen::Builder::new()
.with_config(config)
.with_crate(crate_dir)
.with_include("slint_image_internal.h")
.with_include("slint_internal.h")
.with_after_include(
r"
namespace slint::cbindgen_private { struct WindowProperties; }
",
)
.generate()
.context("Unable to generate bindings for slint_platform_internal.h")?
.write_to_file(include_dir.join("slint_platform_internal.h"));
Ok(())
}
fn gen_interpreter(
root_dir: &Path,
include_dir: &Path,
dependencies: &mut Vec<PathBuf>,
) -> anyhow::Result<()> {
let mut config = default_config();
config.export.exclude = IntoIterator::into_iter([
"Value",
"ValueType",
"PropertyDescriptor",
"Diagnostic",
"PropertyDescriptor",
"Box",
])
.map(String::from)
.collect();
let mut crate_dir = root_dir.to_owned();
crate_dir.extend(["internal", "interpreter"].iter());
ensure_cargo_rerun_for_crate(&crate_dir, dependencies)?;
// Generate a header file with some public API (enums, etc.)
let mut public_config = config.clone();
public_config.namespaces = Some(vec!["slint".into(), "interpreter".into()]);
public_config.export.item_types = vec![cbindgen::ItemType::Enums, cbindgen::ItemType::Structs];
public_config.export.exclude = IntoIterator::into_iter([
"ComponentCompilerOpaque",
"ComponentDefinitionOpaque",
"ModelAdaptorVTable",
"StructIteratorOpaque",
"ComponentInstance",
"StructIteratorResult",
"Value",
"StructOpaque",
"ModelNotifyOpaque",
])
.map(String::from)
.collect();
cbindgen::Builder::new()
.with_config(public_config)
.with_crate(crate_dir.clone())
.generate()
.context("Unable to generate bindings for slint_interpreter_generated_public.h")?
.write_to_file(include_dir.join("slint_interpreter_generated_public.h"));
cbindgen::Builder::new()
.with_config(config)
.with_crate(crate_dir)
.with_include("slint_internal.h")
.with_include("slint_interpreter_generated_public.h")
.with_after_include(
r"
namespace slint::cbindgen_private {
struct Value;
using slint::interpreter::ValueType;
using slint::interpreter::PropertyDescriptor;
using slint::interpreter::Diagnostic;
template <typename T> using Box = T*;
}",
)
.generate()
.context("Unable to generate bindings for slint_interpreter_internal.h")?
.write_to_file(include_dir.join("slint_interpreter_internal.h"));
Ok(())
}
macro_rules! declare_features {
($($f:ident)+) => {
#[derive(Clone, Copy)]
pub struct EnabledFeatures {
$(pub $f: bool,)*
}
impl EnabledFeatures {
/// Generate the `#define`
pub fn defines(self) -> String {
let mut defines = String::new();
$(
if self.$f {
defines = format!("{defines}///This macro is defined when Slint was configured with the SLINT_FEATURE_{0} flag enabled\n#define SLINT_FEATURE_{0}\n", stringify!($f).to_ascii_uppercase());
};
)*
defines
}
/// Get the feature from the environment variable set by cargo when building running the slint-cpp's build script
#[allow(unused)]
pub fn from_env() -> Self {
Self {
$(
$f: std::env::var(format!("CARGO_FEATURE_{}", stringify!($f).to_ascii_uppercase())).is_ok(),
)*
}
}