Blender V5.3
makesdna.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 * SPDX-FileCopyrightText: 2002-2026 Blender Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later */
5
27
28#define DNA_DEPRECATED_ALLOW
29
30#include <algorithm>
31#include <cctype>
32#include <climits>
33#include <cstdio>
34#include <cstdlib>
35#include <cstring>
36
37#include "BLI_set.hh"
38#include "BLI_string_ref.hh"
39#include "BLI_sys_types.hh" /* For `intptr_t` support. */
40#include "BLI_system.hh" /* For #BLI_system_backtrace stub. */
41#include "BLI_utildefines.hh"
42#include "BLI_vector.hh"
43#include "BLI_vector_set.hh"
44
45#include "DNA_genfile.h"
46#include "DNA_sdna_types.h"
47#include "dna_parse.h"
48#include "dna_utils.h"
49
50namespace blender {
51
52/* Include files that will be used to generate makesdna output.
53 * By default, they match the blender_includefiles, but could be overridden via a command line
54 * argument for the purposes of regression testing. */
56
57/* -------------------------------------------------------------------- */
60
69extern int debugSDNA; /* Defined in `dna_parse.cc`. */
70
71#define DEBUG_PRINTF(debug_level, ...) \
72 { \
73 if (debugSDNA > debug_level) { \
74 printf(__VA_ARGS__); \
75 } \
76 } \
77 ((void)0)
78
79/* stub for BLI_abort() */
80#ifndef NDEBUG
81void BLI_system_backtrace(FILE *fp)
82{
83 (void)fp;
84}
85#endif
86
88
89/* -------------------------------------------------------------------- */
93
95struct TypeInfo {
96 const std::string name;
97 /* Native, 32 bit and 64 bit platform sizes. */
99 short size_32;
100 short size_64;
101 /* Alignment requirements for 32 and 64 bit platforms. */
102 short align_32;
103 short align_64;
104 /* Struct or built-in type? */
106};
107
109struct TypeTable {
110 /* Using a vector set with guaranteed insertion order, so that resulting SDNA
111 * types are always in the same order. */
112 class GetIDFn {
113 public:
114 StringRef operator()(const TypeInfo &info) const
115 {
116 return info.name;
117 }
118 };
120
121 void add_builtin(const StringRef name, const short size, const int expected_type_index)
122 {
123 BLI_assert(this->types.size() == expected_type_index);
124 UNUSED_VARS_NDEBUG(expected_type_index);
125 this->types.add_new({.name = name,
126 .size_native = size,
127 .size_32 = size,
128 .size_64 = size,
129 .align_32 = size,
130 .align_64 = size,
131 .is_struct = false});
132 }
133
136 {
137 this->types.add({.name = name,
138 .size_native = 0,
139 .size_32 = 0,
140 .size_64 = 0,
141 .align_32 = 0,
142 .align_64 = 0,
143 .is_struct = true});
144 }
145
148 {
149 return this->types.index_of_as(name);
150 }
151
154 {
155 /* Const cast is okay because only TypeInfo::name is used for #VectorSet hash and equality. */
156 return const_cast<TypeInfo &>(this->types[this->lookup_index(name)]);
157 }
158 const TypeInfo &lookup(StringRefNull name) const
159 {
160 return this->types[this->lookup_index(name)];
161 }
162};
163
165{
166 TypeTable table;
167
168 /* Insert built-in types.
169 *
170 * \warning Order of function calls here must be aligned with #eSDNA_Type.
171 * \warning uint is not allowed! use in structs an unsigned int.
172 * \warning sizes must match #DNA_elem_type_size().
173 */
174 table.add_builtin("char", 1, SDNA_TYPE_CHAR);
175 table.add_builtin("uchar", 1, SDNA_TYPE_UCHAR);
176 table.add_builtin("short", 2, SDNA_TYPE_SHORT);
177 table.add_builtin("ushort", 2, SDNA_TYPE_USHORT);
178 table.add_builtin("int", 4, SDNA_TYPE_INT);
179
180 /* NOTE: long isn't supported, these are place-holders to maintain alignment with #eSDNA_Type. */
181 table.add_builtin("long", 4, 5 /* SDNA_TYPE_LONG */);
182 table.add_builtin("ulong", 4, 6 /* SDNA_TYPE_ULONG */);
183
184 table.add_builtin("float", 4, SDNA_TYPE_FLOAT);
185 table.add_builtin("double", 8, SDNA_TYPE_DOUBLE);
186 table.add_builtin("int64_t", 8, SDNA_TYPE_INT64);
187 table.add_builtin("uint64_t", 8, SDNA_TYPE_UINT64);
188 table.add_builtin("void", 0, SDNA_TYPE_VOID);
189 table.add_builtin("int8_t", 1, SDNA_TYPE_INT8);
190
191 /* Fake place-holder struct definition used to get an identifier for raw, untyped bytes buffers
192 * in blend-files.
193 *
194 * It will be written into the blend-file's SDNA, but it must never be used in the source code.
195 * Trying to declare `struct raw_data` in DNA headers will cause a build error.
196 *
197 * NOTE: While not critical, since all blend-files before introduction of this 'raw_data'
198 * type/struct have been using the `0` value for raw data #BHead.SDNAnr, it's best to reserve
199 * that first struct index to this raw data explicitly. */
200 table.add_struct("raw_data"); /* SDNA_TYPE_RAW_DATA */
201 BLI_STATIC_ASSERT(SDNA_RAW_DATA_STRUCT_INDEX == 0, "'raw data' SDNA struct index should be 0")
202
203 /* Insert struct and member types. */
204 for (const dna::ParsedStruct &parsed_struct : parsed_structs) {
205 table.add_struct(parsed_struct.type_name);
206 for (const dna::ParsedMember &parsed_member : parsed_struct.members) {
207 table.add_struct(parsed_member.type_name);
208 }
209 }
210
211 return table;
212}
213
215
216/* -------------------------------------------------------------------- */
219
221static int member_size_native(const TypeInfo &member_type, const dna::ParsedMember &parsed_member)
222{
223 const char *cp = parsed_member.member_name.c_str();
224 const int namelen = int(parsed_member.member_name.size());
225 const int array_num = (cp[namelen - 1] == ']') ? DNA_member_array_num(cp) : 1;
226 /* Pointer size. */
227 if (cp[0] == '*' || cp[1] == '*') {
228 return sizeof(void *) * array_num;
229 }
230 return member_type.size_native * array_num;
231}
232
233static bool check_member_alignment(const TypeInfo &struct_info,
234 const TypeInfo &member_type,
235 const int len,
236 const int member_align_override,
237 const StringRefNull name,
238 const StringRefNull detail)
239{
240 bool result = true;
241 const char *struct_name = struct_info.name.c_str();
242 const short type_size = member_type.size_native;
243 if (member_align_override > 0 && (len % member_align_override)) {
244 fprintf(stderr,
245 "Align %d error (%s) in struct: %s %s (add %d padding bytes)\n",
246 member_align_override,
247 detail.c_str(),
248 struct_name,
249 name.c_str(),
250 member_align_override - (len % member_align_override));
251 result = false;
252 }
253 if (!member_type.is_struct && type_size > 4 && (len % 8)) {
254 fprintf(stderr,
255 "Align 8 error (%s) in struct: %s %s (add %d padding bytes)\n",
256 detail.c_str(),
257 struct_name,
258 name.c_str(),
259 len % 8);
260 result = false;
261 }
262 if (type_size > 3 && (len % 4)) {
263 fprintf(stderr,
264 "Align 4 error (%s) in struct: %s %s (add %d padding bytes)\n",
265 detail.c_str(),
266 struct_name,
267 name.c_str(),
268 len % 4);
269 result = false;
270 }
271 if (type_size == 2 && (len % 2)) {
272 fprintf(stderr,
273 "Align 2 error (%s) in struct: %s %s (add %d padding bytes)\n",
274 detail.c_str(),
275 struct_name,
276 name.c_str(),
277 len % 2);
278 result = false;
279 }
280 return result;
281}
282
285 Span<dna::ParsedStruct> parsed_structs)
286{
287 bool dna_error = false;
288
289 /* Multiple iterations to handle nested structs. */
290 int unknown = int(parsed_structs.size());
291 while (unknown) {
292 const int lastunknown = unknown;
293 unknown = 0;
294
295 for (const dna::ParsedStruct &parsed_struct : parsed_structs) {
296 TypeInfo &struct_info = table.lookup(parsed_struct.type_name);
297
298 /* When size is not known yet. */
299 if (struct_info.size_native == 0) {
300 int size_native = 0;
301 int size_32 = 0;
302 int size_64 = 0;
303 /* Sizes of the largest field in a struct. */
304 int max_align_32 = 0;
305 int max_align_64 = 0;
306
307 /* check all members in struct */
308 for (const dna::ParsedMember &parsed_member : parsed_struct.members) {
309 const TypeInfo &member_type = table.lookup(parsed_member.type_name);
310 const char *cp = parsed_member.member_name.c_str();
311 const int namelen = int(parsed_member.member_name.size());
312
313 /* is it a pointer or function pointer? */
314 if (cp[0] == '*' || cp[1] == '*') {
315 /* has the name an extra length? (array) */
316 const int array_num = (cp[namelen - 1] == ']') ? DNA_member_array_num(cp) : 1;
317
318 if (array_num == 0) {
319 fprintf(stderr,
320 "Zero array size found or could not parse %s: '%.*s'\n",
321 struct_info.name.c_str(),
322 namelen + 1,
323 cp);
324 dna_error = true;
325 }
326
327 /* 4-8 aligned/ */
328 if (sizeof(void *) == 4) {
329 if (size_native % 4) {
330 fprintf(stderr,
331 "Align pointer error in struct (size_native 4): %s %s\n",
332 struct_info.name.c_str(),
333 cp);
334 dna_error = true;
335 }
336 }
337 else {
338 if (size_native % 8) {
339 fprintf(stderr,
340 "Align pointer error in struct (size_native 8): %s %s\n",
341 struct_info.name.c_str(),
342 cp);
343 dna_error = true;
344 }
345 }
346
347 if (size_64 % 8) {
348 fprintf(stderr,
349 "Align pointer error in struct (size_64 8): %s %s\n",
350 struct_info.name.c_str(),
351 cp);
352 dna_error = true;
353 }
354
355 size_native += member_size_native(member_type, parsed_member);
356 size_32 += 4 * array_num;
357 size_64 += 8 * array_num;
358 max_align_32 = std::max(max_align_32, 4);
359 max_align_64 = std::max(max_align_64, 8);
360 }
361 else if (cp[0] == '[') {
362 /* parsing can cause names "var" and "[3]"
363 * to be found for "float var [3]" */
364 fprintf(stderr,
365 "Parse error in struct, invalid member name: %s %s\n",
366 struct_info.name.c_str(),
367 cp);
368 dna_error = true;
369 }
370 else if (member_type.size_native) {
371 /* has the name an extra length? (array) */
372 const int array_num = (cp[namelen - 1] == ']') ? DNA_member_array_num(cp) : 1;
373
374 if (array_num == 0) {
375 fprintf(stderr,
376 "Zero array size found or could not parse %s: '%.*s'\n",
377 struct_info.name.c_str(),
378 namelen + 1,
379 cp);
380 dna_error = true;
381 }
382
383 /* struct alignment */
384 if (member_type.is_struct) {
385 if (sizeof(void *) == 8 && (size_native % 8)) {
386 fprintf(stderr,
387 "Align struct error: %s::%s (starts at %d on the native platform; "
388 "%d %% %zu = %d bytes)\n",
389 struct_info.name.c_str(),
390 cp,
391 size_native,
392 size_native,
393 sizeof(void *),
394 size_native % 8);
395 dna_error = true;
396 }
397 }
398
399 /* Per-member C++ alignment override from the parser. */
400 const int member_align = parsed_member.alignment;
401
402 /* Check 2-4-8 aligned, plus any stricter C++ alignment. */
404 struct_info, member_type, size_32, member_align, cp, "32 bit"))
405 {
406 dna_error = true;
407 }
409 struct_info, member_type, size_64, member_align, cp, "64 bit"))
410 {
411 dna_error = true;
412 }
413
414 size_native += member_size_native(member_type, parsed_member);
415 size_32 += array_num * member_type.size_32;
416 size_64 += array_num * member_type.size_64;
417 max_align_32 = std::max<int>(max_align_32, member_type.align_32);
418 max_align_64 = std::max<int>(max_align_64, member_type.align_64);
419 max_align_32 = std::max<int>(max_align_32, member_align);
420 max_align_64 = std::max<int>(max_align_64, member_align);
421 }
422 else {
423 size_native = 0;
424 size_32 = 0;
425 size_64 = 0;
426 break;
427 }
428 }
429
430 if (size_native == 0) {
431 unknown++;
432 }
433 else {
434 /* Sanity check: struct sizes are written as #short to the SDNA blob. */
435 BLI_assert(size_native <= SHRT_MAX);
436 BLI_assert(size_32 <= SHRT_MAX);
437 BLI_assert(size_64 <= SHRT_MAX);
438
439 struct_info.size_native = short(size_native);
440 struct_info.size_32 = short(size_32);
441 struct_info.size_64 = short(size_64);
442 struct_info.align_32 = short(max_align_32);
443 struct_info.align_64 = short(max_align_64);
444
445 /* Sanity check 1: alignment should never be 0. */
446 BLI_assert(max_align_32);
447 BLI_assert(max_align_64);
448
449 /* Sanity check 2: alignment should always be equal or smaller than the maximum
450 * alignment we support. 8 bytes for built-in types (e.g. `int64_t`, `double`),
451 * up to 16 bytes for C++ over-aligned types like `float4x4`. */
452 BLI_assert(max_align_32 <= 16);
453 BLI_assert(max_align_64 <= 16);
454
455 if (size_32 % max_align_32) {
456 /* There is an one odd case where only the 32 bit struct has alignment issues
457 * and the 64 bit does not, that can only be fixed by adding a padding pointer
458 * to the struct to resolve the problem. */
459 if ((size_64 % max_align_64 == 0) && (size_32 % max_align_32 == 4)) {
460 fprintf(stderr,
461 "Sizeerror in 32 bit struct: %s (add padding pointer)\n",
462 struct_info.name.c_str());
463 }
464 else {
465 fprintf(stderr,
466 "Sizeerror in 32 bit struct: %s (add %d bytes)\n",
467 struct_info.name.c_str(),
468 max_align_32 - (size_32 % max_align_32));
469 }
470 dna_error = true;
471 }
472
473 if (size_64 % max_align_64) {
474 fprintf(stderr,
475 "Sizeerror in 64 bit struct: %s (add %d bytes)\n",
476 struct_info.name.c_str(),
477 max_align_64 - (size_64 % max_align_64));
478 dna_error = true;
479 }
480
481 if (size_native % 4 && !ELEM(size_native, 1, 2)) {
482 fprintf(stderr,
483 "Sizeerror 4 in struct: %s (add %d bytes)\n",
484 struct_info.name.c_str(),
485 size_native % 4);
486 dna_error = true;
487 }
488 }
489 }
490 }
491
492 if (unknown == lastunknown) {
493 break;
494 }
495 }
496
497 if (unknown) {
498 fprintf(stderr, "ERROR: still %d structs unknown\n", unknown);
499
500 if (debugSDNA) {
501 fprintf(stderr, "*** Known structs :\n");
502 for (const dna::ParsedStruct &ps : parsed_structs) {
503 const TypeInfo &info = table.lookup(ps.type_name);
504 if (info.size_native != 0) {
505 fprintf(stderr, " %s\n", info.name.c_str());
506 }
507 }
508 }
509
510 fprintf(stderr, "*** Unknown structs :\n");
511 for (const dna::ParsedStruct &ps : parsed_structs) {
512 const TypeInfo &info = table.lookup(ps.type_name);
513 if (info.size_native == 0) {
514 fprintf(stderr, " %s\n", info.name.c_str());
515 }
516 }
517
518 dna_error = true;
519 }
520
521 return dna_error;
522}
523
525
526/* -------------------------------------------------------------------- */
529
531static void dna_write(FILE *file, const void *pntr, const int size)
532{
533 constexpr int MAX_DNA_LINE_LENGTH = 20;
534 static int linelength = 0;
535 const char *data = static_cast<const char *>(pntr);
536
537 for (int i = 0; i < size; i++) {
538 fprintf(file, "%d, ", data[i]);
539 linelength++;
540 if (linelength >= MAX_DNA_LINE_LENGTH) {
541 fprintf(file, "\n");
542 linelength = 0;
543 }
544 }
545}
546
548static void write_sdna_blob(FILE *file,
549 const TypeTable &table,
550 const Span<dna::ParsedStruct> parsed_structs)
551{
552 /* `raw_data` plus parsed structs. */
553 const int num_structs = 1 + int(parsed_structs.size());
554
555 /* Deduplicated member names in source order. */
556 VectorSet<StringRefNull> member_names;
557 for (const dna::ParsedStruct &ps : parsed_structs) {
558 for (const dna::ParsedMember &pm : ps.members) {
559 member_names.add(pm.member_name);
560 }
561 }
562
563 /* FOR DEBUG. */
564 if (debugSDNA > 1) {
565 printf("names_len %d types_len %d structs_len %d\n",
566 int(member_names.size()),
567 int(table.types.size()),
568 num_structs);
569 for (const StringRefNull name : member_names) {
570 printf(" %s\n", name.c_str());
571 }
572 printf("\n");
573
574 for (const TypeInfo &type : table.types) {
575 printf(" %s %d\n", type.name.c_str(), type.size_native);
576 }
577 printf("\n");
578
579 for (const dna::ParsedStruct &ps : parsed_structs) {
580 const TypeInfo &info = table.lookup(ps.type_name);
581 printf(" struct %s elems: %d size: %d\n",
582 info.name.c_str(),
583 int(ps.members.size()),
584 info.size_native);
585 for (const dna::ParsedMember &pm : ps.members) {
586 const TypeInfo &member_type = table.lookup(pm.type_name);
587 printf(" %s %s allign32:%d, allign64:%d\n",
588 member_type.name.c_str(),
589 pm.member_name.c_str(),
590 member_type.align_32,
591 member_type.align_64);
592 }
593 }
594 }
595
596 if (member_names.is_empty() || parsed_structs.is_empty()) {
597 return;
598 }
599
600 const char nil_bytes[4] = {0};
601
602 dna_write(file, "SDNA", 4);
603
604 /* Write NAME: member names. */
605 dna_write(file, "NAME", 4);
606 int len = int(member_names.size());
607 dna_write(file, &len, 4);
608 len = 0;
609 for (const StringRefNull member_name : member_names) {
610 const int member_len = int(member_name.size()) + 1;
611 dna_write(file, member_name.c_str(), member_len);
612 len += member_len;
613 }
614 int len_align = (len + 3) & ~3;
615 if (len != len_align) {
616 dna_write(file, nil_bytes, len_align - len);
617 }
618
619 /* Write TYPES: built-in and struct table. */
620 dna_write(file, "TYPE", 4);
621 len = int(table.types.size());
622 dna_write(file, &len, 4);
623 len = 0;
624 for (const TypeInfo &type : table.types) {
625 const int type_len = int(type.name.size()) + 1;
626 dna_write(file, type.name.c_str(), type_len);
627 len += type_len;
628 }
629 len_align = (len + 3) & ~3;
630 if (len != len_align) {
631 dna_write(file, nil_bytes, len_align - len);
632 }
633
634 /* WRITE TLEN: size of each type. */
635 dna_write(file, "TLEN", 4);
636 for (const TypeInfo &type : table.types) {
637 dna_write(file, &type.size_native, 2);
638 }
639
640 /* Pad to multiple of 4 bytes when types count is odd. */
641 if (table.types.size() & 1) {
642 dna_write(file, nil_bytes, 2);
643 }
644
645 /* WRITE STRUCTS */
646 dna_write(file, "STRC", 4);
647 dna_write(file, &num_structs, 4);
648
649 BLI_assert_msg(table.types.size() < SHRT_MAX, "SDNA only supports up to SHRT_MAX types");
650
651 /* Synthetic `raw_data` struct: type index, zero members. */
652 const short raw_data_header[2] = {short(table.lookup_index("raw_data")), 0};
653 dna_write(file, raw_data_header, 4);
654
655 for (const dna::ParsedStruct &ps : parsed_structs) {
656 const short header[2] = {short(table.lookup_index(ps.type_name)), short(ps.members.size())};
657 dna_write(file, header, 4);
658 for (const dna::ParsedMember &pm : ps.members) {
659 const short pair[2] = {short(table.lookup_index(pm.type_name)),
660 short(member_names.index_of_as(pm.member_name))};
661 dna_write(file, pair, 4);
662 }
663 }
664
665 /* No padding needed as each entry is 4 bytes already. */
666}
667
669
670/* -------------------------------------------------------------------- */
673
675static void write_sdna_type_offsets(FILE *file, const Span<dna::ParsedStruct> parsed_structs)
676{
677 fprintf(file, "#pragma once\n");
678 fprintf(file, "#define SDNA_TYPE_FROM_STRUCT(id) _SDNA_TYPE_##id\n");
679 fprintf(file, "enum {\n");
680 fprintf(file, "\t_SDNA_TYPE_raw_data = %d,\n", SDNA_RAW_DATA_STRUCT_INDEX);
681 for (const int64_t i : parsed_structs.index_range()) {
682 const int sdna_index = int(i) + 1;
683 fprintf(
684 file, "\t_SDNA_TYPE_%s = %d,\n", parsed_structs[i].alias_type_name.c_str(), sdna_index);
685 }
686 fprintf(file, "\tSDNA_TYPE_MAX = %d,\n", 1 + int(parsed_structs.size()));
687 fprintf(file, "};\n\n");
688}
689
691static void write_sdna_struct_ids(FILE *file, const Span<dna::ParsedStruct> parsed_structs)
692{
693 fprintf(file, "#include \"DNA_sdna_type_ids.hh\"\n\n");
694 fprintf(file, "namespace blender {\n");
695 fprintf(file, "namespace dna {\n\n");
696 fprintf(file, "int sdna_struct_id_get_max() { return %d; }\n", int(parsed_structs.size()));
697 fprintf(file, "\n}\n");
698
699 for (const int64_t i : parsed_structs.index_range()) {
700 const char *name = parsed_structs[i].alias_type_name.c_str();
701 const int sdna_index = int(i) + 1;
702 fprintf(file, "struct %s;\n", name);
703 fprintf(
704 file, "template<> int dna::sdna_struct_id_get<%s>() { return %d; }\n", name, sdna_index);
705 }
706
707 fprintf(file, "\n}\n");
708}
709
711static void write_rna_defaults(FILE *file,
712 const StringRefNull base_directory,
713 const Span<dna::ParsedStruct> parsed_structs)
714{
715 fprintf(file, "/* Default struct member values for RNA. */\n");
716 fprintf(file, "#define DNA_DEPRECATED_ALLOW\n");
717 fprintf(file, "#define DNA_NO_EXTERNAL_CONSTRUCTORS\n");
718 fprintf(file, "#include \"DNA_sdna_type_ids.hh\"\n\n");
719
720 for (const char *filename : includefiles) {
721 fprintf(file, "#include \"%s%s\"\n", base_directory.c_str(), filename);
722 }
723
724 fprintf(file, "namespace blender {\n\n");
725
726 /* Define an instance of each struct. */
727 for (const dna::ParsedStruct &parsed_struct : parsed_structs) {
728 const StringRefNull name = parsed_struct.type_name.c_str();
729 std::string default_var;
730
731 if (name == "bTheme") {
732 /* Exception for bTheme which is auto-generated. */
733 fprintf(file, "extern \"C\" const bTheme U_theme_default;\n");
734 default_var = "U_theme_default";
735 }
736 else {
737 fprintf(file, "static const %s DNA_DEFAULT_%s = {};\n", name.c_str(), name.c_str());
738 default_var = "DNA_DEFAULT_" + name;
739 }
740
741 /* Table with pointer to each member. */
742 fprintf(file, "static const void *const member_defaults_%s[] = {\n", name.c_str());
743 for (const dna::ParsedMember &pm : parsed_struct.members) {
745 fprintf(file, " &%s.%.*s,\n", default_var.c_str(), int(bare_id.size()), bare_id.data());
746 }
747 fprintf(file, "};\n");
748 }
749
750 /* Table with all structs. */
751 fprintf(file, "\nextern const void *const *const DNA_member_default_table[] = {\n");
752 for (const dna::ParsedStruct &parsed_struct : parsed_structs) {
753 fprintf(file, " member_defaults_%s,\n", parsed_struct.type_name.c_str());
754 }
755 fprintf(file, "};\n\n");
756
757 fprintf(file, "} // namespace blender\n");
758}
759
761static void write_sdna_verify(FILE *file,
762 const TypeTable &table,
763 const Span<dna::ParsedStruct> parsed_structs,
764 const StringRefNull base_directory)
765{
766 fprintf(file, "/* Verify struct sizes and member offsets are as expected by DNA. */\n");
767 fprintf(file, "#include \"BLI_assert.hh\"\n\n");
768 /* Needed so we can find offsets of deprecated structs. */
769 fprintf(file, "#define DNA_DEPRECATED_ALLOW\n");
770 /* Workaround enum naming collision in static asserts
771 * (ideally this included a unique name/id per file). */
772 fprintf(file, "#define assert_line_ assert_line_DNA_\n");
773 for (const char *filename : includefiles) {
774 fprintf(file, "#include \"%s%s\"\n", base_directory.c_str(), filename);
775 }
776 fprintf(file, "#undef assert_line_\n");
777 fprintf(file, "\n");
778 fprintf(file, "using namespace blender;\n");
779 fprintf(file, "\n");
780
781 for (const dna::ParsedStruct &parsed_struct : parsed_structs) {
782 int offset = 0;
783 for (const dna::ParsedMember &parsed_member : parsed_struct.members) {
784 const TypeInfo &member_type = table.lookup(parsed_member.type_name);
785 const StringRef alias_id = DNA_member_id_string_ref(parsed_member.alias_member_name);
786 fprintf(file,
787 "BLI_STATIC_ASSERT(offsetof(struct %s, %.*s) == %d, \"DNA member offset "
788 "verify\");\n",
789 parsed_struct.alias_type_name.c_str(),
790 int(alias_id.size()),
791 alias_id.data(),
792 offset);
793 offset += member_size_native(member_type, parsed_member);
794 }
795 const TypeInfo &struct_info = table.lookup(parsed_struct.type_name);
796 fprintf(file,
797 "BLI_STATIC_ASSERT(sizeof(struct %s) == %d, \"DNA struct size verify\");\n\n",
798 parsed_struct.alias_type_name.c_str(),
799 struct_info.size_native);
800 }
801}
802
804
805/* -------------------------------------------------------------------- */
810
811static bool apply_renames(Vector<dna::ParsedStruct> &parsed_structs)
812{
814
815 for (dna::ParsedStruct &parsed_struct : parsed_structs) {
816 /* Save the original C++ name and rewrite. */
817 parsed_struct.alias_type_name = parsed_struct.type_name;
818 parsed_struct.type_name = rename_maps.types.lookup_default_as(parsed_struct.type_name,
819 parsed_struct.type_name);
820
821 Set<StringRef> members_unique;
822 members_unique.reserve(parsed_struct.members.size());
823
824 for (dna::ParsedMember &parsed_member : parsed_struct.members) {
825 /* Save the original C++ type and rewrite. */
826 parsed_member.alias_member_name = parsed_member.member_name;
827 parsed_member.type_name = rename_maps.types.lookup_default_as(parsed_member.type_name,
828 parsed_member.type_name);
829
830 /* Rewrite the member name. */
831 std::string &member_name = parsed_member.member_name;
832 StringRef stripped = DNA_member_id_string_ref(member_name);
833 const StringRef *member_static = rename_maps.members.lookup_ptr(
834 {parsed_struct.type_name, std::string(stripped)});
835 if (member_static != nullptr) {
836 const size_t prefix_len = stripped.data() - member_name.data();
837 member_name.replace(prefix_len, stripped.size(), *member_static);
838 stripped = DNA_member_id_string_ref(member_name);
839 }
840
841 /* Sanity check to ensure all member names are still unique. */
842 if (!members_unique.add(stripped)) {
843 fprintf(stderr,
844 "Error: duplicate name found '%s.%.*s', "
845 "likely cause is 'dna_rename_defs.h'\n",
846 parsed_struct.alias_type_name.c_str(),
847 int(stripped.size()),
848 stripped.data());
849 return false;
850 }
851 }
852 }
853 return true;
854}
855
857
858/* -------------------------------------------------------------------- */
863
864static bool make_structDNA(const StringRefNull base_directory,
865 FILE *file,
866 FILE *file_offsets,
867 FILE *file_verify,
868 FILE *file_ids,
869 FILE *file_defaults)
870{
871 if (debugSDNA > 0) {
872 fflush(stdout);
873 printf("Running makesdna at debug level %d\n", debugSDNA);
874 }
875
876 /* Parse structs and enums from all DNA header files. */
877 Vector<dna::ParsedStruct> parsed_structs;
878 Vector<dna::ParsedEnum> parsed_enums;
879
880 DEBUG_PRINTF(0, "\tStart of header scan:\n");
881 if (!dna::parse_dna_headers(base_directory, parsed_structs, parsed_enums, includefiles)) {
882 return false;
883 }
884 DEBUG_PRINTF(0, "\tFinished scanning headers.\n");
885
886 /* Write default values for RNA before any substitution or renaming, as RNA binds
887 * to the actual C++ data structures rather than SDNA. */
888 write_rna_defaults(file_defaults, base_directory, parsed_structs);
889
890 /* Substitute C++ types with C types known to SDNA. */
891 if (!dna::substitute_cpp_types(parsed_structs, parsed_enums, false)) {
892 return false;
893 }
894
895 /* Apply renames from `dna_rename_defs.h`. */
896 if (!apply_renames(parsed_structs)) {
897 return false;
898 }
899
900 /* Build type table. */
901 TypeTable table = build_type_table(parsed_structs);
902
903 /* Compute type sizes and check alignment. */
904 if (compute_type_size_and_alignment(table, parsed_structs)) {
905 return false;
906 }
907
908 /* Write SDNA blob. */
909 DEBUG_PRINTF(0, "Writing file ... ");
910 write_sdna_blob(file, table, parsed_structs);
911
912 /* Write auxiliary files. */
913 write_sdna_type_offsets(file_offsets, parsed_structs);
914 write_sdna_struct_ids(file_ids, parsed_structs);
915 write_sdna_verify(file_verify, table, parsed_structs, base_directory);
916
917 DEBUG_PRINTF(0, "done.\n");
918
919 return true;
920}
921
923
924/* -------------------------------------------------------------------- */
927
928static void make_bad_file(const StringRefNull filename, int line)
929{
930 FILE *fp = fopen(filename.c_str(), "w");
931 fprintf(fp,
932 "#error \"Error! can't make correct DNA.c file from %s:%d, check alignment.\"\n",
933 __FILE__,
934 line);
935 fclose(fp);
936}
937
938#ifndef BASE_HEADER
939# define BASE_HEADER "../"
940#endif
941
942static void print_usage(const char *argv0)
943{
944 printf(
945 "Usage: %s [--include-file <file>, ...] "
946 "dna.cc dna_type_offsets.h dna_verify.cc dna_struct_ids.cc dna_defaults.cc "
947 "[base directory]\n",
948 argv0);
949}
950
951} // namespace blender
952
953int main(int argc, char **argv)
954{
955 using namespace blender;
956 Vector<const char *> cli_include_files;
957
958 /* There is a number of non-optional arguments that must be provided to the executable. */
959 if (argc < 6) {
960 print_usage(argv[0]);
961 return 1;
962 }
963
964 /* Parse optional arguments. */
965 int arg_index = 1; /* Skip the argv0. */
966 while (arg_index < argc) {
967 if (STREQ(argv[arg_index], "--include-file")) {
968 ++arg_index;
969 if (arg_index == argc) {
970 printf("Missing argument for --include-file\n");
971 print_usage(argv[0]);
972 return 1;
973 }
974 cli_include_files.append(argv[arg_index]);
975 ++arg_index;
976 continue;
977 }
978 break;
979 }
980
981 if (!cli_include_files.is_empty()) {
982 includefiles = Span<const char *>(cli_include_files.data(), cli_include_files.size());
983 }
984
985 /* Check the number of non-optional positional arguments. */
986 const int num_arguments = argc - arg_index;
987 if (!ELEM(num_arguments, 5, 6)) {
988 print_usage(argv[0]);
989 return 0;
990 }
991
992 int return_status = 0;
993
994 FILE *file_dna = fopen(argv[arg_index], "w");
995 FILE *file_dna_offsets = fopen(argv[arg_index + 1], "w");
996 FILE *file_dna_verify = fopen(argv[arg_index + 2], "w");
997 FILE *file_dna_ids = fopen(argv[arg_index + 3], "w");
998 FILE *file_dna_defaults = fopen(argv[arg_index + 4], "w");
999 if (!file_dna) {
1000 printf("Unable to open file: %s\n", argv[arg_index]);
1001 return_status = 1;
1002 }
1003 else if (!file_dna_offsets) {
1004 printf("Unable to open file: %s\n", argv[arg_index + 1]);
1005 return_status = 1;
1006 }
1007 else if (!file_dna_verify) {
1008 printf("Unable to open file: %s\n", argv[arg_index + 2]);
1009 return_status = 1;
1010 }
1011 else if (!file_dna_ids) {
1012 printf("Unable to open file: %s\n", argv[arg_index + 3]);
1013 return_status = 1;
1014 }
1015 else if (!file_dna_defaults) {
1016 printf("Unable to open file: %s\n", argv[arg_index + 4]);
1017 return_status = 1;
1018 }
1019 else {
1020 const char *base_directory;
1021
1022 if (num_arguments == 6) {
1023 base_directory = argv[arg_index + 5];
1024 }
1025 else {
1026 base_directory = BASE_HEADER;
1027 }
1028
1029 /* NOTE: #init_structDNA() in dna_genfile.cc expects `sdna->data` is 4-bytes aligned.
1030 * `DNAstr[]` buffer written by `makesdna` is used for this data, so make `DNAstr` forcefully
1031 * 4-bytes aligned. */
1032#ifdef __GNUC__
1033# define FORCE_ALIGN_4 " __attribute__((aligned(4))) "
1034#else
1035# define FORCE_ALIGN_4 " "
1036#endif
1037 fprintf(file_dna, "extern const unsigned char DNAstr[];\n");
1038 fprintf(file_dna, "const unsigned char" FORCE_ALIGN_4 "DNAstr[] = {\n");
1039#undef FORCE_ALIGN_4
1040
1041 if (!make_structDNA(base_directory,
1042 file_dna,
1043 file_dna_offsets,
1044 file_dna_verify,
1045 file_dna_ids,
1046 file_dna_defaults))
1047 {
1048 /* error */
1049 fclose(file_dna);
1050 file_dna = nullptr;
1051 make_bad_file(argv[1], __LINE__);
1052 return_status = 1;
1053 }
1054 else {
1055 fprintf(file_dna, "};\n");
1056 fprintf(file_dna, "extern const int DNAlen;\n");
1057 fprintf(file_dna, "const int DNAlen = sizeof(DNAstr);\n");
1058 }
1059 }
1060
1061 if (file_dna) {
1062 fclose(file_dna);
1063 }
1064 if (file_dna_offsets) {
1065 fclose(file_dna_offsets);
1066 }
1067 if (file_dna_verify) {
1068 fclose(file_dna_verify);
1069 }
1070 if (file_dna_ids) {
1071 fclose(file_dna_ids);
1072 }
1073 if (file_dna_defaults) {
1074 fclose(file_dna_defaults);
1075 }
1076
1077 return return_status;
1078}
1079
1080/* handy but fails on struct bounds which makesdna doesn't care about
1081 * with quite the same strictness as GCC does */
1082#if 0
1083/* include files for automatic dependencies */
1084
1085/* extra safety check that we are aligned,
1086 * warnings here are easier to fix the makesdna's */
1087# ifdef __GNUC__
1088# pragma GCC diagnostic error "-Wpadded"
1089# endif
1090
1091#endif /* if 0 */
1092
1093/* The include file below is automatically generated from the `SRC_DNA_INC`
1094 * variable in 'source/blender/CMakeLists.txt'. */
1095#include "dna_includes_all.h"
1096
1097/* end of list */
1098
1100
1101/* -------------------------------------------------------------------- */
1108
1109static void UNUSED_FUNCTION(dna_rename_defs_ensure)()
1110{
1111 using namespace blender;
1112#define DNA_STRUCT_RENAME(old, new) (void)sizeof(new);
1113#define DNA_STRUCT_RENAME_MEMBER(new_struct_name, old, new) (void)offsetof(new_struct_name, new);
1114#include "dna_rename_defs.h"
1115
1116#undef DNA_STRUCT_RENAME
1117#undef DNA_STRUCT_RENAME_MEMBER
1118}
1119
blenloader genfile private function prototypes
#define SDNA_RAW_DATA_STRUCT_INDEX
#define UNUSED_FUNCTION(x)
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
constexpr Span()=default
void reserve(const int64_t n)
Definition BLI_set.hh:656
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr const char * c_str() const
StringRef operator()(const TypeInfo &info) const
Definition makesdna.cc:114
bool add(const Key &key)
int64_t index_of_as(const ForwardKey &key) const
int64_t size() const
void add_new(const Key &key)
int64_t size() const
void append(const T &value)
bool is_empty() const
constexpr int64_t size() const
constexpr const char * data() const
#define printf
#define DEBUG_PRINTF(debug_level,...)
Definition dna_parse.cc:37
#define SHRT_MAX
#define main()
#define BASE_HEADER
Definition makesdna.cc:939
#define FORCE_ALIGN_4
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
bool substitute_cpp_types(Vector< ParsedStruct > &structs, const Span< ParsedEnum > enums, bool)
Definition dna_parse.cc:833
bool parse_dna_headers(const StringRefNull base_directory, Vector< ParsedStruct > &r_structs, Vector< ParsedEnum > &r_enums, Span< const char * > include_files)
Definition dna_parse.cc:704
Span< const char * > default_dna_header_filenames()
Definition dna_parse.cc:49
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int debugSDNA
Definition dna_parse.cc:33
__int64 int64_t
Definition BKE_text.h:90
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
static void write_sdna_type_offsets(FILE *file, const Span< dna::ParsedStruct > parsed_structs)
Definition makesdna.cc:675
static TypeTable build_type_table(const Span< dna::ParsedStruct > parsed_structs)
Definition makesdna.cc:164
static bool apply_renames(Vector< dna::ParsedStruct > &parsed_structs)
Definition makesdna.cc:811
static int compute_type_size_and_alignment(TypeTable &table, Span< dna::ParsedStruct > parsed_structs)
Definition makesdna.cc:284
BMesh const char void * data
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet
StringRef DNA_member_id_string_ref(const StringRef member_full)
Definition dna_utils.cc:95
static void dna_write(FILE *file, const void *pntr, const int size)
Definition makesdna.cc:531
DnaRenameMaps DNA_rename_maps_alias_to_static()
Definition dna_utils.cc:183
static bool check_member_alignment(const TypeInfo &struct_info, const TypeInfo &member_type, const int len, const int member_align_override, const StringRefNull name, const StringRefNull detail)
Definition makesdna.cc:233
void BLI_system_backtrace(FILE *fp)
Definition system.cc:112
static Span< const char * > includefiles
Definition makesdna.cc:55
static void write_rna_defaults(FILE *file, const StringRefNull base_directory, const Span< dna::ParsedStruct > parsed_structs)
Definition makesdna.cc:711
static void print_usage(const char *argv0)
Definition makesdna.cc:942
int DNA_member_array_num(const StringRef str)
Definition dna_utils.cc:28
static void write_sdna_verify(FILE *file, const TypeTable &table, const Span< dna::ParsedStruct > parsed_structs, const StringRefNull base_directory)
Definition makesdna.cc:761
@ SDNA_TYPE_INT64
Definition DNA_genfile.h:45
@ SDNA_TYPE_SHORT
Definition DNA_genfile.h:38
@ SDNA_TYPE_UINT64
Definition DNA_genfile.h:46
@ SDNA_TYPE_FLOAT
Definition DNA_genfile.h:43
@ SDNA_TYPE_USHORT
Definition DNA_genfile.h:39
@ SDNA_TYPE_UCHAR
Definition DNA_genfile.h:37
@ SDNA_TYPE_INT
Definition DNA_genfile.h:40
@ SDNA_TYPE_CHAR
Definition DNA_genfile.h:36
@ SDNA_TYPE_DOUBLE
Definition DNA_genfile.h:44
@ SDNA_TYPE_VOID
Definition DNA_genfile.h:47
@ SDNA_TYPE_INT8
Definition DNA_genfile.h:48
static int member_size_native(const TypeInfo &member_type, const dna::ParsedMember &parsed_member)
Definition makesdna.cc:221
static void make_bad_file(const StringRefNull filename, int line)
Definition makesdna.cc:928
static void write_sdna_struct_ids(FILE *file, const Span< dna::ParsedStruct > parsed_structs)
Definition makesdna.cc:691
static bool make_structDNA(const StringRefNull base_directory, FILE *file, FILE *file_offsets, FILE *file_verify, FILE *file_ids, FILE *file_defaults)
Definition makesdna.cc:864
static void write_sdna_blob(FILE *file, const TypeTable &table, const Span< dna::ParsedStruct > parsed_structs)
Definition makesdna.cc:548
Map< std::pair< StringRef, StringRef >, StringRef > members
Definition dna_utils.h:76
Map< StringRef, StringRef > types
Definition dna_utils.h:74
const std::string name
Definition makesdna.cc:96
int lookup_index(StringRefNull name) const
Definition makesdna.cc:147
const TypeInfo & lookup(StringRefNull name) const
Definition makesdna.cc:158
void add_struct(StringRefNull name)
Definition makesdna.cc:135
CustomIDVectorSet< TypeInfo, GetIDFn > types
Definition makesdna.cc:119
void add_builtin(const StringRef name, const short size, const int expected_type_index)
Definition makesdna.cc:121
TypeInfo & lookup(StringRefNull name)
Definition makesdna.cc:153
std::string alias_member_name
Definition dna_parse.h:25