1// Defaults for rust backend generation.
2genrule_defaults {
3    name: "pdl_rust_generator_defaults",
4    cmd: "$(location :pdlc) --output-format rust $(in) > $(out)",
5    tools: [":pdlc"],
6    defaults_visibility: [
7        "//system/nfc:__subpackages__",
8        "//external/rust/pica",
9        "//external/uwb/src",
10        "//packages/modules/Bluetooth:__subpackages__",
11        "//tools/netsim:__subpackages__",
12        "//vendor:__subpackages__",
13    ],
14}
15
16// Defaults for legacy rust backend generation.
17genrule_defaults {
18    name: "pdl_rust_legacy_generator_defaults",
19    cmd: "$(location :pdlc) --output-format rust_legacy $(in) > $(out)",
20    tools: [":pdlc"],
21    defaults_visibility: [
22        "//system/nfc:__subpackages__",
23        "//external/rust/pica",
24        "//external/uwb/src",
25        "//packages/modules/Bluetooth:__subpackages__",
26        "//tools/netsim:__subpackages__",
27    ],
28}
29
30// Defaults for rust_noalloc backend generation.
31genrule_defaults {
32    name: "pdl_rust_noalloc_generator_defaults",
33    cmd: "$(location :pdlc) --output-format rust_no_alloc $(in) > $(out)",
34    tools: [":pdlc"],
35}
36
37// Defaults for PDL python backend generation.
38genrule_defaults {
39    name: "pdl_python_generator_defaults",
40    tools: [
41        ":pdlc",
42        ":pdl_python_generator",
43    ],
44}
45
46// Defaults for PDL C++ backend generation.
47genrule_defaults {
48    name: "pdl_cxx_generator_defaults",
49    tools: [
50        ":pdlc",
51        ":pdl_cxx_generator",
52    ],
53}
54
55filegroup {
56    name: "pdl_generated_files",
57    srcs: [
58        "tests/generated/rust/**/*.rs",
59    ],
60}
61
62rust_test_host {
63    name: "pdl_tests",
64    host_cross_supported: false,
65    srcs: ["src/main.rs"],
66    rustlibs: [
67        "libargh",
68        "libcodespan_reporting",
69        "libheck",
70        "libpdl_compiler",
71        "libpest",
72        "libprettyplease",
73        "libproc_macro2",
74        "libquote",
75        "libserde",
76        "libserde_json",
77        "libsyn",
78        "libtempfile",
79    ],
80    proc_macros: [
81        "libpest_derive",
82        "libpaste",
83    ],
84    test_suites: ["general-tests"],
85    data: [
86        ":pdl_generated_files",
87    ],
88}
89
90genrule {
91    name: "pdl_generated_files_compile_rs",
92    cmd: "$(location tests/generated_files_compile.sh) $(in) > $(out)",
93    srcs: [":pdl_generated_files"],
94    out: ["generated_files_compile.rs"],
95    tool_files: ["tests/generated_files_compile.sh"],
96}
97
98rust_test_host {
99    name: "pdl_generated_files_compile",
100    srcs: [":pdl_generated_files_compile_rs"],
101    features: ["serde"],
102    rustlibs: [
103        "libbytes",
104        "libserde",
105        "libtempfile",
106        "libpdl_runtime",
107    ],
108    test_suites: ["general-tests"],
109    clippy_lints: "none",
110    lints: "none",
111}
112
113// The generators support more features for LE packets than for BE
114// packets. We use a single input written for LE packets and remove
115// the parts that don't work for BE packets. We do this by removing
116// everything between
117//
118// // Start: little_endian_only
119//
120// and
121//
122// // End: little_endian_only
123//
124// from the LE packet input.
125genrule_defaults {
126    name: "pdl_be_test_file_defaults",
127    cmd: "sed -e 's/little_endian_packets/big_endian_packets/' " +
128        "     -e '/Start: little_endian_only/,/End: little_endian_only/d' " +
129        " < $(in) > $(out)",
130}
131
132genrule {
133    name: "pdl_be_test_file",
134    defaults: ["pdl_be_test_file_defaults"],
135    srcs: ["tests/canonical/le_test_file.pdl"],
136    out: ["be_test_file.pdl"],
137}
138
139genrule {
140    name: "pdl_rust_generator_tests_le_src",
141    cmd: "$(location :pdlc)" +
142        " --output-format rust" +
143        " --exclude-declaration UnsizedCustomField" +
144        " --exclude-declaration Packet_Custom_Field_VariableSize" +
145        " --exclude-declaration Struct_Custom_Field_VariableSize_" +
146        " --exclude-declaration Struct_Custom_Field_VariableSize" +
147        " --exclude-declaration Checksum" +
148        " --exclude-declaration Packet_Checksum_Field_FromStart" +
149        " --exclude-declaration Packet_Checksum_Field_FromEnd" +
150        " --exclude-declaration Struct_Checksum_Field_FromStart_" +
151        " --exclude-declaration Struct_Checksum_Field_FromStart" +
152        " --exclude-declaration Struct_Checksum_Field_FromEnd_" +
153        " --exclude-declaration Struct_Checksum_Field_FromEnd" +
154        " --exclude-declaration PartialParent5" +
155        " --exclude-declaration PartialParent12" +
156        " --exclude-declaration PartialChild5_A" +
157        " --exclude-declaration PartialChild5_B" +
158        " --exclude-declaration PartialChild12_A" +
159        " --exclude-declaration PartialChild12_B" +
160        " --exclude-declaration Packet_Array_Field_UnsizedElement_SizeModifier" +
161        " --exclude-declaration Struct_Array_Field_UnsizedElement_SizeModifier_" +
162        " --exclude-declaration Struct_Array_Field_UnsizedElement_SizeModifier" +
163        " $(location tests/canonical/le_test_file.pdl) > $(out);" +
164        "$(location :pdlc) $(location tests/canonical/le_test_vectors.json) --output-format rust --tests >> $(out)",
165    srcs: [
166        "tests/canonical/le_test_file.pdl",
167        "tests/canonical/le_test_vectors.json",
168    ],
169    out: ["le_canonical.rs"],
170    tools: [":pdlc"],
171}
172
173genrule {
174    name: "pdl_rust_generator_tests_be_src",
175    cmd: "$(location :pdlc)" +
176        " --output-format rust" +
177        " --exclude-declaration UnsizedCustomField" +
178        " --exclude-declaration Packet_Custom_Field_VariableSize" +
179        " --exclude-declaration Struct_Custom_Field_VariableSize_" +
180        " --exclude-declaration Struct_Custom_Field_VariableSize" +
181        " --exclude-declaration Checksum" +
182        " --exclude-declaration Packet_Checksum_Field_FromStart" +
183        " --exclude-declaration Packet_Checksum_Field_FromEnd" +
184        " --exclude-declaration Struct_Checksum_Field_FromStart_" +
185        " --exclude-declaration Struct_Checksum_Field_FromStart" +
186        " --exclude-declaration Struct_Checksum_Field_FromEnd_" +
187        " --exclude-declaration Struct_Checksum_Field_FromEnd" +
188        " --exclude-declaration Packet_Array_Field_UnsizedElement_SizeModifier" +
189        " --exclude-declaration Struct_Array_Field_UnsizedElement_SizeModifier_" +
190        " --exclude-declaration Struct_Array_Field_UnsizedElement_SizeModifier" +
191        " $(location :pdl_be_test_file) > $(out);" +
192        "$(location :pdlc) $(location tests/canonical/be_test_vectors.json) --output-format rust --tests >> $(out)",
193    srcs: [
194        ":pdl_be_test_file",
195        "tests/canonical/be_test_vectors.json",
196    ],
197    out: ["be_canonical.rs"],
198    tools: [":pdlc"],
199}
200
201rust_test_host {
202    name: "pdl_rust_generator_tests_le",
203    srcs: [":pdl_rust_generator_tests_le_src"],
204    test_suites: ["general-tests"],
205    features: ["serde"],
206    rustlibs: [
207        "libbytes",
208        "libnum_traits",
209        "libpdl_runtime",
210        "libserde",
211        "libserde_json",
212    ],
213    clippy_lints: "none",
214    lints: "none",
215}
216
217rust_test_host {
218    name: "pdl_rust_generator_tests_be",
219    srcs: [":pdl_rust_generator_tests_be_src"],
220    test_suites: ["general-tests"],
221    features: ["serde"],
222    rustlibs: [
223        "libbytes",
224        "libnum_traits",
225        "libpdl_runtime",
226        "libserde",
227        "libserde_json",
228    ],
229    clippy_lints: "none",
230    lints: "none",
231}
232
233// Generate the python parser+serializer backend for the
234// little endian test file located at tests/canonical/le_test_file.pdl.
235genrule {
236    name: "pdl_python_generator_le_test_gen",
237    defaults: ["pdl_python_generator_defaults"],
238    cmd: "set -o pipefail;" +
239        " $(location :pdlc) $(in) |" +
240        " $(location :pdl_python_generator)" +
241        " --exclude-declaration Packet_Array_Field_VariableElementSize_ConstantSize" +
242        " --exclude-declaration Packet_Array_Field_VariableElementSize_VariableSize" +
243        " --exclude-declaration Packet_Array_Field_VariableElementSize_VariableCount" +
244        " --exclude-declaration Packet_Array_Field_VariableElementSize_UnknownSize" +
245        " --output $(out) --custom-type-location tests.custom_types",
246    tool_files: [
247        "tests/custom_types.py",
248    ],
249    srcs: [
250        "tests/canonical/le_test_file.pdl",
251    ],
252    out: [
253        "le_backend.py",
254    ],
255}
256
257// Generate the python parser+serializer backend for a big endian test
258// file derived from tests/canonical/le_test_file.pdl.
259genrule {
260    name: "pdl_python_generator_be_test_gen",
261    defaults: ["pdl_python_generator_defaults"],
262    cmd: "set -o pipefail;" +
263        " $(location :pdlc) $(in) |" +
264        " $(location :pdl_python_generator)" +
265        " --exclude-declaration Packet_Array_Field_VariableElementSize_ConstantSize" +
266        " --exclude-declaration Packet_Array_Field_VariableElementSize_VariableSize" +
267        " --exclude-declaration Packet_Array_Field_VariableElementSize_VariableCount" +
268        " --exclude-declaration Packet_Array_Field_VariableElementSize_UnknownSize" +
269        " --output $(out) --custom-type-location tests.custom_types",
270    tool_files: [
271        "tests/custom_types.py",
272    ],
273    srcs: [
274        ":pdl_be_test_file",
275    ],
276    out: [
277        "be_backend.py",
278    ],
279}
280
281// Test the generated python parser+serializer against
282// pre-generated binary inputs.
283python_test_host {
284    name: "pdl_python_generator_test",
285    main: "tests/python_generator_test.py",
286    srcs: [
287        ":pdl_python_generator_be_test_gen",
288        ":pdl_python_generator_le_test_gen",
289        "tests/custom_types.py",
290        "tests/python_generator_test.py",
291    ],
292    data: [
293        "tests/canonical/be_test_vectors.json",
294        "tests/canonical/le_test_vectors.json",
295    ],
296    libs: [
297        "typing_extensions",
298    ],
299    test_options: {
300        unit_test: true,
301    },
302    version: {
303        py3: {
304            embedded_launcher: true,
305        },
306    },
307}
308
309// Generate the rust_noalloc backend srcs against the little-endian test vectors
310genrule {
311    name: "pdl_rust_noalloc_le_test_backend_srcs",
312    defaults: ["pdl_rust_noalloc_generator_defaults"],
313    srcs: ["tests/canonical/le_rust_noalloc_test_file.pdl"],
314    out: ["_packets.rs"],
315}
316
317// Generate the C++ parser+serializer backend for the
318// little endian test file located at tests/canonical/le_test_file.pdl.
319genrule {
320    name: "pdl_cxx_canonical_le_src_gen",
321    defaults: ["pdl_cxx_generator_defaults"],
322    cmd: "set -o pipefail;" +
323        " $(location :pdlc) $(in) |" +
324        " $(location :pdl_cxx_generator)" +
325        " --exclude-declaration Packet_Custom_Field_ConstantSize" +
326        " --exclude-declaration Packet_Custom_Field_VariableSize" +
327        " --exclude-declaration Packet_Checksum_Field_FromStart" +
328        " --exclude-declaration Packet_Checksum_Field_FromEnd" +
329        " --exclude-declaration Struct_Custom_Field_ConstantSize" +
330        " --exclude-declaration Struct_Custom_Field_VariableSize" +
331        " --exclude-declaration Struct_Checksum_Field_FromStart" +
332        " --exclude-declaration Struct_Checksum_Field_FromEnd" +
333        " --exclude-declaration Packet_Array_Field_VariableElementSize_ConstantSize" +
334        " --exclude-declaration Packet_Array_Field_VariableElementSize_VariableSize" +
335        " --exclude-declaration Packet_Array_Field_VariableElementSize_VariableCount" +
336        " --exclude-declaration Packet_Array_Field_VariableElementSize_UnknownSize" +
337        " --exclude-declaration Struct_Custom_Field_ConstantSize_" +
338        " --exclude-declaration Struct_Custom_Field_VariableSize_" +
339        " --exclude-declaration Struct_Checksum_Field_FromStart_" +
340        " --exclude-declaration Struct_Checksum_Field_FromEnd_" +
341        " --exclude-declaration PartialParent5" +
342        " --exclude-declaration PartialChild5_A" +
343        " --exclude-declaration PartialChild5_B" +
344        " --exclude-declaration PartialParent12" +
345        " --exclude-declaration PartialChild12_A" +
346        " --exclude-declaration PartialChild12_B" +
347        " --namespace le_test" +
348        " --output $(out)",
349    srcs: [
350        "tests/canonical/le_test_file.pdl",
351    ],
352    out: [
353        "canonical_le_test_file.h",
354    ],
355}
356
357// Generate the C++ parser+serializer backend tests for the
358// little endian test file located at tests/canonical/le_test_file.pdl.
359genrule {
360    name: "pdl_cxx_canonical_le_test_gen",
361    cmd: "set -o pipefail;" +
362        " inputs=( $(in) ) &&" +
363        " $(location :pdlc) $${inputs[0]} |" +
364        " $(location :pdl_cxx_unittest_generator)" +
365        " --output $(out)" +
366        " --test-vectors $${inputs[1]}" +
367        " --include-header $$(basename $${inputs[2]})" +
368        " --using-namespace le_test" +
369        " --namespace le_test" +
370        " --parser-test-suite LeParserTest" +
371        " --serializer-test-suite LeSerializerTest",
372    tools: [
373        ":pdlc",
374        ":pdl_cxx_unittest_generator",
375    ],
376    srcs: [
377        "tests/canonical/le_test_file.pdl",
378
379        "tests/canonical/le_test_vectors.json",
380
381        ":pdl_cxx_canonical_le_src_gen",
382    ],
383    out: [
384        "canonical_le_test.cc",
385    ],
386}
387
388// Generate the C++ parser+serializer backend for the
389// big endian test file.
390genrule {
391    name: "pdl_cxx_canonical_be_src_gen",
392    defaults: ["pdl_cxx_generator_defaults"],
393    cmd: "set -o pipefail;" +
394        " $(location :pdlc) $(in) |" +
395        " $(location :pdl_cxx_generator)" +
396        " --exclude-declaration Packet_Custom_Field_ConstantSize" +
397        " --exclude-declaration Packet_Custom_Field_VariableSize" +
398        " --exclude-declaration Packet_Checksum_Field_FromStart" +
399        " --exclude-declaration Packet_Checksum_Field_FromEnd" +
400        " --exclude-declaration Struct_Custom_Field_ConstantSize" +
401        " --exclude-declaration Struct_Custom_Field_VariableSize" +
402        " --exclude-declaration Struct_Checksum_Field_FromStart" +
403        " --exclude-declaration Struct_Checksum_Field_FromEnd" +
404        " --exclude-declaration Packet_Array_Field_VariableElementSize_ConstantSize" +
405        " --exclude-declaration Packet_Array_Field_VariableElementSize_VariableSize" +
406        " --exclude-declaration Packet_Array_Field_VariableElementSize_VariableCount" +
407        " --exclude-declaration Packet_Array_Field_VariableElementSize_UnknownSize" +
408        " --exclude-declaration Struct_Custom_Field_ConstantSize_" +
409        " --exclude-declaration Struct_Custom_Field_VariableSize_" +
410        " --exclude-declaration Struct_Checksum_Field_FromStart_" +
411        " --exclude-declaration Struct_Checksum_Field_FromEnd_" +
412        " --exclude-declaration PartialParent5" +
413        " --exclude-declaration PartialChild5_A" +
414        " --exclude-declaration PartialChild5_B" +
415        " --exclude-declaration PartialParent12" +
416        " --exclude-declaration PartialChild12_A" +
417        " --exclude-declaration PartialChild12_B" +
418        " --namespace be_test" +
419        " --output $(out)",
420    srcs: [
421        ":pdl_be_test_file",
422    ],
423    out: [
424        "canonical_be_test_file.h",
425    ],
426}
427
428// Generate the C++ parser+serializer backend tests for the
429// big endian test file.
430genrule {
431    name: "pdl_cxx_canonical_be_test_gen",
432    cmd: "set -o pipefail;" +
433        " inputs=( $(in) ) &&" +
434        " $(location :pdlc) $${inputs[0]} |" +
435        " $(location :pdl_cxx_unittest_generator)" +
436        " --output $(out)" +
437        " --test-vectors $${inputs[1]}" +
438        " --include-header $$(basename $${inputs[2]})" +
439        " --using-namespace be_test" +
440        " --namespace be_test" +
441        " --parser-test-suite BeParserTest" +
442        " --serializer-test-suite BeSerializerTest",
443    tools: [
444        ":pdlc",
445        ":pdl_cxx_unittest_generator",
446    ],
447    srcs: [
448        ":pdl_be_test_file",
449
450        "tests/canonical/be_test_vectors.json",
451
452        ":pdl_cxx_canonical_be_src_gen",
453    ],
454    out: [
455        "canonical_be_test.cc",
456    ],
457}
458
459// Test the generated C++ parser+serializer against
460// pre-generated binary inputs.
461cc_test_host {
462    name: "pdl_cxx_generator_test",
463    local_include_dirs: [
464        "scripts",
465    ],
466    generated_headers: [
467        "pdl_cxx_canonical_be_src_gen",
468        "pdl_cxx_canonical_le_src_gen",
469    ],
470    generated_sources: [
471        "pdl_cxx_canonical_be_test_gen",
472        "pdl_cxx_canonical_le_test_gen",
473    ],
474    static_libs: [
475        "libgtest",
476    ],
477}
478