Cleanup: style, use braces for makesdna, datatoc
[blender.git] / source / blender / makesdna / intern / makesdna.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup DNA
22  *
23  * \brief Struct muncher for making SDNA.
24  *
25  * \section aboutmakesdnac About makesdna tool
26  * Originally by Ton, some mods by Frank, and some cleaning and
27  * extension by Nzc.
28  *
29  * Makesdna creates a .c file with a long string of numbers that
30  * encode the Blender file format. It is fast, because it is basically
31  * a binary dump. There are some details to mind when reconstructing
32  * the file (endianness and byte-alignment).
33  *
34  * This little program scans all structs that need to be serialized,
35  * and determined the names and types of all members. It calculates
36  * how much memory (on disk or in ram) is needed to store that struct,
37  * and the offsets for reaching a particular one.
38  *
39  * There is a facility to get verbose output from sdna. Search for
40  * \ref debugSDNA. This int can be set to 0 (no output) to some int. Higher
41  * numbers give more output.
42  */
43
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <assert.h>
48
49 #include "MEM_guardedalloc.h"
50
51 #include "BLI_utildefines.h"
52 #include "BLI_alloca.h"
53 #include "BLI_ghash.h"
54 #include "BLI_memarena.h"
55 #include "BLI_sys_types.h"  /* for intptr_t support */
56
57 #include "dna_utils.h"
58
59 #define SDNA_MAX_FILENAME_LENGTH 255
60
61 /* Included the path relative from /source/blender/ here, so we can move     */
62 /* headers around with more freedom.                                         */
63 static const char *includefiles[] = {
64
65         /* if you add files here, please add them at the end
66          * of makesdna.c (this file) as well */
67         "DNA_listBase.h",
68         "DNA_vec_types.h",
69         "DNA_ID.h",
70         "DNA_ipo_types.h",
71         "DNA_key_types.h",
72         "DNA_text_types.h",
73         "DNA_packedFile_types.h",
74         "DNA_gpu_types.h",
75         "DNA_camera_types.h",
76         "DNA_image_types.h",
77         "DNA_texture_types.h",
78         "DNA_light_types.h",
79         "DNA_material_types.h",
80         "DNA_vfont_types.h",
81         "DNA_meta_types.h",
82         "DNA_curve_types.h",
83         "DNA_mesh_types.h",
84         "DNA_meshdata_types.h",
85         "DNA_modifier_types.h",
86         "DNA_lattice_types.h",
87         "DNA_object_types.h",
88         "DNA_object_force_types.h",
89         "DNA_object_fluidsim_types.h",
90         "DNA_world_types.h",
91         "DNA_scene_types.h",
92         "DNA_view3d_types.h",
93         "DNA_view2d_types.h",
94         "DNA_space_types.h",
95         "DNA_userdef_types.h",
96         "DNA_screen_types.h",
97         "DNA_sdna_types.h",
98         "DNA_fileglobal_types.h",
99         "DNA_sequence_types.h",
100         "DNA_effect_types.h",
101         "DNA_outliner_types.h",
102         "DNA_sound_types.h",
103         "DNA_collection_types.h",
104         "DNA_armature_types.h",
105         "DNA_action_types.h",
106         "DNA_constraint_types.h",
107         "DNA_nla_types.h",
108         "DNA_node_types.h",
109         "DNA_color_types.h",
110         "DNA_brush_types.h",
111         "DNA_customdata_types.h",
112         "DNA_particle_types.h",
113         "DNA_cloth_types.h",
114         "DNA_gpencil_types.h",
115         "DNA_gpencil_modifier_types.h",
116         "DNA_shader_fx_types.h",
117         "DNA_windowmanager_types.h",
118         "DNA_anim_types.h",
119         "DNA_boid_types.h",
120         "DNA_smoke_types.h",
121         "DNA_speaker_types.h",
122         "DNA_movieclip_types.h",
123         "DNA_tracking_types.h",
124         "DNA_dynamicpaint_types.h",
125         "DNA_mask_types.h",
126         "DNA_rigidbody_types.h",
127         "DNA_freestyle_types.h",
128         "DNA_linestyle_types.h",
129         "DNA_cachefile_types.h",
130         "DNA_layer_types.h",
131         "DNA_workspace_types.h",
132         "DNA_lightprobe_types.h",
133
134         /* see comment above before editing! */
135
136         /* empty string to indicate end of includefiles */
137         "",
138 };
139
140 /* -------------------------------------------------------------------- */
141 /** \name Variables
142  * \{ */
143
144 static MemArena *mem_arena = NULL;
145
146 static int maxdata = 500000, maxnr = 50000;
147 static int nr_names = 0;
148 static int nr_types = 0;
149 static int nr_structs = 0;
150 /** At address `names[a]` is string `a`. */
151 static char **names;
152 /** At address `types[a]` is string `a`. */
153 static char **types;
154 /** At `types_size[a]` is the size of type `a` on this systems bitness (32 or 64). */
155 static short *types_size_native;
156 /** Contains sizes as they are calculated on 32 bit systems. */
157 static short *types_size_32;
158 /** Contains sizes as they are calculated on 64 bit systems. */
159 static short *types_size_64;
160 /** At `sp = structs[a]` is the first address of a struct definition:
161  * - `sp[0]` is type number.
162  * - `sp[1]` is the length of the element array (next).
163  * - `sp[2]` sp[3] is [(type_nr, name_nr), ..] (number of pairs is defined by `sp[1]`),
164  */
165 static short **structs, *structdata;
166
167 /** Versioning data */
168 static struct {
169         GHash *struct_map_alias_from_static;
170         GHash *struct_map_static_from_alias;
171         GHash *elem_map_static_from_alias;
172 } g_version_data = {NULL};
173
174 /**
175  * Variable to control debug output of makesdna.
176  * debugSDNA:
177  * - 0 = no output, except errors
178  * - 1 = detail actions
179  * - 2 = full trace, tell which names and types were found
180  * - 4 = full trace, plus all gritty details
181  */
182 static int debugSDNA = 0;
183 static int additional_slen_offset;
184
185 #define DEBUG_PRINTF(debug_level, ...) \
186         { if (debugSDNA > debug_level) { printf(__VA_ARGS__); } } ((void)0)
187
188
189 /* stub for BLI_abort() */
190 #ifndef NDEBUG
191 void BLI_system_backtrace(FILE *fp)
192 {
193        (void)fp;
194 }
195 #endif
196
197 /** \} */
198
199 /* -------------------------------------------------------------------- */
200 /** \name Function Declarations
201  * \{ */
202
203 /**
204  * Ensure type \c str to is in the #types array.
205  * \param str: Struct name without any qualifiers.
206  * \param len: The struct size in bytes.
207  * \return Index in the #types array.
208  */
209 static int add_type(const char *str, int size);
210
211 /**
212  * Ensure \c str is int the #names array.
213  * \param str: Struct member name which may include pointer prefix & array size.
214  * \return Index in the #names array.
215  */
216 static int add_name(const char *str);
217
218 /**
219  * Search whether this structure type was already found, and if not,
220  * add it.
221  */
222 static short *add_struct(int namecode);
223
224 /**
225  * Remove comments from this buffer. Assumes that the buffer refers to
226  * ascii-code text.
227  */
228 static int preprocess_include(char *maindata, const int maindata_len);
229
230 /**
231  * Scan this file for serializable types.
232  */
233 static int convert_include(const char *filename);
234
235 /**
236  * Determine how many bytes are needed for each struct.
237  */
238 static int calculate_struct_sizes(int);
239
240 /**
241  * Construct the DNA.c file
242  */
243 static void dna_write(FILE *file, const void *pntr, const int size);
244
245 /**
246  * Report all structures found so far, and print their lengths.
247  */
248 void print_struct_sizes(void);
249
250 /** \} */
251
252 /* -------------------------------------------------------------------- */
253 /** \name Implementation
254  *
255  * Make DNA string (write to file).
256  * \{ */
257
258
259 static const char *version_struct_static_from_alias(const char *str)
260 {
261         const char *str_test = BLI_ghash_lookup(g_version_data.struct_map_static_from_alias, str);
262         if (str_test != NULL) {
263                 return str_test;
264         }
265         return str;
266 }
267
268 static const char *version_struct_alias_from_static(const char *str)
269 {
270         const char *str_test = BLI_ghash_lookup(g_version_data.struct_map_alias_from_static, str);
271         if (str_test != NULL) {
272                 return str_test;
273         }
274         return str;
275 }
276
277 static const char *version_elem_static_from_alias(
278         const int strct, const char *elem_alias_full)
279 {
280         const uint elem_alias_full_len = strlen(elem_alias_full);
281         char *elem_alias = alloca(elem_alias_full_len + 1);
282         const int elem_alias_len = DNA_elem_id_strip_copy(elem_alias, elem_alias_full);
283         const char *str_pair[2] = {types[strct], elem_alias};
284         const char *elem_static = BLI_ghash_lookup(g_version_data.elem_map_static_from_alias, str_pair);
285         if (elem_static != NULL) {
286                 return DNA_elem_id_rename(
287                         mem_arena,
288                         elem_alias, elem_alias_len,
289                         elem_static, strlen(elem_static),
290                         elem_alias_full, elem_alias_full_len,
291                         DNA_elem_id_offset_start(elem_alias_full));
292         }
293         return elem_alias_full;
294 }
295
296 /**
297  * Enforce '_pad123' naming convention, disallow 'pad123' or 'pad_123',
298  * special exception for [a-z] after since there is a 'pad_rot_angle' preference.
299  */
300 static bool is_name_legal(const char *name)
301 {
302         const int name_size = strlen(name) + 1;
303         char *name_strip = alloca(name_size);
304         DNA_elem_id_strip_copy(name_strip, name);
305
306         const char prefix[] = {'p', 'a', 'd'};
307
308         if (name[0] == '_') {
309                 if (strncmp(&name_strip[1], prefix, sizeof(prefix)) != 0) {
310                         fprintf(stderr, "Error: only '_pad' variables can start with an underscore, found '%s'\n", name);
311                         return false;
312                 }
313         }
314         else if (strncmp(name_strip, prefix, sizeof(prefix)) == 0) {
315                 int i = sizeof(prefix);
316                 if (name_strip[i] >= 'a' && name_strip[i] <= 'z') {
317                         /* may be part of a word, allow that. */
318                         return true;
319                 }
320                 bool has_only_digit_or_none = true;
321                 for (; name_strip[i]; i++) {
322                         const char c = name_strip[i];
323                         if (!((c >= '0' && c <= '9') || c == '_')) {
324                                 has_only_digit_or_none = false;
325                                 break;
326                         }
327                 }
328                 if (has_only_digit_or_none) {
329                         /* found 'pad' or 'pad123'. */
330                         fprintf(stderr, "Error: padding variables must be formatted '_pad[number]', found '%s'\n", name);
331                         return false;
332                 }
333         }
334         return true;
335 }
336
337
338 static int add_type(const char *str, int size)
339 {
340         int nr;
341         char *cp;
342
343         /* first do validity check */
344         if (str[0] == 0) {
345                 return -1;
346         }
347         else if (strchr(str, '*')) {
348                 /* note: this is valid C syntax but we can't parse, complain!
349                  * 'struct SomeStruct* somevar;' <-- correct but we cant handle right now. */
350                 return -1;
351         }
352
353         str = version_struct_static_from_alias(str);
354
355         /* search through type array */
356         for (nr = 0; nr < nr_types; nr++) {
357                 if (strcmp(str, types[nr]) == 0) {
358                         if (size) {
359                                 types_size_native[nr] = size;
360                                 types_size_32[nr] = size;
361                                 types_size_64[nr] = size;
362                         }
363                         return nr;
364                 }
365         }
366
367         /* append new type */
368         const int str_size = strlen(str) + 1;
369         cp = BLI_memarena_alloc(mem_arena, str_size);
370         memcpy(cp, str, str_size);
371         types[nr_types] = cp;
372         types_size_native[nr_types] = size;
373         types_size_32[nr_types] = size;
374         types_size_64[nr_types] = size;
375
376         if (nr_types >= maxnr) {
377                 printf("too many types\n");
378                 return nr_types - 1;
379         }
380         nr_types++;
381
382         return nr_types - 1;
383 }
384
385
386 /**
387  *
388  * Because of the weird way of tokenizing, we have to 'cast' function
389  * pointers to ... (*f)(), whatever the original signature. In fact,
390  * we add name and type at the same time... There are two special
391  * cases, unfortunately. These are explicitly checked.
392  *
393  * */
394 static int add_name(const char *str)
395 {
396         int nr, i, j, k;
397         char *cp;
398         char buf[255]; /* stupid limit, change it :) */
399         const char *name;
400
401         additional_slen_offset = 0;
402
403         if (str[0] == 0 /*  || (str[1] == 0) */) {
404                 return -1;
405         }
406
407         if (str[0] == '(' && str[1] == '*') {
408                 /* we handle function pointer and special array cases here, e.g.
409                  * void (*function)(...) and float (*array)[..]. the array case
410                  * name is still converted to (array *)() though because it is that
411                  * way in old dna too, and works correct with elementsize() */
412                 int isfuncptr = (strchr(str + 1, '(')) != NULL;
413
414                 DEBUG_PRINTF(3, "\t\t\t\t*** Function pointer or multidim array pointer found\n");
415                 /* functionpointer: transform the type (sometimes) */
416                 i = 0;
417
418                 while (str[i] != ')') {
419                         buf[i] = str[i];
420                         i++;
421                 }
422
423                 /* Another number we need is the extra slen offset. This extra
424                  * offset is the overshoot after a space. If there is no
425                  * space, no overshoot should be calculated. */
426                 j = i; /* j at first closing brace */
427
428                 DEBUG_PRINTF(3, "first brace after offset %d\n", i);
429
430                 j++; /* j beyond closing brace ? */
431                 while ((str[j] != 0) && (str[j] != ')')) {
432                         DEBUG_PRINTF(3, "seen %c (%d)\n", str[j], str[j]);
433                         j++;
434                 }
435                 DEBUG_PRINTF(3, "seen %c (%d)\n" "special after offset%d\n", str[j], str[j], j);
436
437                 if (!isfuncptr) {
438                         /* multidimensional array pointer case */
439                         if (str[j] == 0) {
440                                 DEBUG_PRINTF(3, "offsetting for multidim array pointer\n");
441                         }
442                         else {
443                                 printf("Error during tokening multidim array pointer\n");
444                         }
445                 }
446                 else if (str[j] == 0) {
447                         DEBUG_PRINTF(3, "offsetting for space\n");
448                         /* get additional offset */
449                         k = 0;
450                         while (str[j] != ')') {
451                                 j++;
452                                 k++;
453                         }
454                         DEBUG_PRINTF(3, "extra offset %d\n", k);
455                         additional_slen_offset = k;
456                 }
457                 else if (str[j] == ')') {
458                         DEBUG_PRINTF(3, "offsetting for brace\n");
459                         ; /* don't get extra offset */
460                 }
461                 else {
462                         printf("Error during tokening function pointer argument list\n");
463                 }
464
465                 /*
466                  * Put )(void) at the end? Maybe )(). Should check this with
467                  * old sdna. Actually, sometimes )(), sometimes )(void...)
468                  * Alas.. such is the nature of braindamage :(
469                  *
470                  * Sorted it out: always do )(), except for headdraw and
471                  * windraw, part of ScrArea. This is important, because some
472                  * linkers will treat different fp's differently when called
473                  * !!! This has to do with interference in byte-alignment and
474                  * the way args are pushed on the stack.
475                  *
476                  * */
477                 buf[i] = 0;
478                 DEBUG_PRINTF(3, "Name before chomping: %s\n", buf);
479                 if ((strncmp(buf, "(*headdraw", 10) == 0) ||
480                     (strncmp(buf, "(*windraw", 9) == 0) )
481                 {
482                         buf[i] = ')';
483                         buf[i + 1] = '(';
484                         buf[i + 2] = 'v';
485                         buf[i + 3] = 'o';
486                         buf[i + 4] = 'i';
487                         buf[i + 5] = 'd';
488                         buf[i + 6] = ')';
489                         buf[i + 7] = 0;
490                 }
491                 else {
492                         buf[i] = ')';
493                         buf[i + 1] = '(';
494                         buf[i + 2] = ')';
495                         buf[i + 3] = 0;
496                 }
497                 /* now precede with buf*/
498                 DEBUG_PRINTF(3, "\t\t\t\t\tProposing fp name %s\n", buf);
499                 name = buf;
500         }
501         else {
502                 /* normal field: old code */
503                 name = str;
504         }
505
506         /* search name array */
507         for (nr = 0; nr < nr_names; nr++) {
508                 if (strcmp(name, names[nr]) == 0) {
509                         return nr;
510                 }
511         }
512
513         /* Sanity check the name. */
514         if (!is_name_legal(name)) {
515                 return -1;
516         }
517
518         /* Append new name. */
519         const int name_size = strlen(name) + 1;
520         cp = BLI_memarena_alloc(mem_arena, name_size);
521         memcpy(cp, name, name_size);
522         names[nr_names] = cp;
523
524         if (nr_names >= maxnr) {
525                 printf("too many names\n");
526                 return nr_names - 1;
527         }
528         nr_names++;
529
530         return nr_names - 1;
531 }
532
533 static short *add_struct(int namecode)
534 {
535         int len;
536         short *sp;
537
538         if (nr_structs == 0) {
539                 structs[0] = structdata;
540         }
541         else {
542                 sp = structs[nr_structs - 1];
543                 len = sp[1];
544                 structs[nr_structs] = sp + 2 * len + 2;
545         }
546
547         sp = structs[nr_structs];
548         sp[0] = namecode;
549
550         if (nr_structs >= maxnr) {
551                 printf("too many structs\n");
552                 return sp;
553         }
554         nr_structs++;
555
556         return sp;
557 }
558
559 static int preprocess_include(char *maindata, const int maindata_len)
560 {
561         int a, newlen, comment = 0;
562         char *cp, *temp, *md;
563
564         /* note: len + 1, last character is a dummy to prevent
565          * comparisons using uninitialized memory */
566         temp = MEM_mallocN(maindata_len + 1, "preprocess_include");
567         temp[maindata_len] = ' ';
568
569         memcpy(temp, maindata, maindata_len);
570
571         /* remove all c++ comments */
572         /* replace all enters/tabs/etc with spaces */
573         cp = temp;
574         a = maindata_len;
575         comment = 0;
576         while (a--) {
577                 if (cp[0] == '/' && cp[1] == '/') {
578                         comment = 1;
579                 }
580                 else if (*cp == '\n') {
581                         comment = 0;
582                 }
583                 if (comment || *cp < 32 || *cp > 128) *cp = 32;
584                 cp++;
585         }
586
587
588         /* data from temp copy to maindata, remove comments and double spaces */
589         cp = temp;
590         md = maindata;
591         newlen = 0;
592         comment = 0;
593         a = maindata_len;
594         while (a--) {
595
596                 if (cp[0] == '/' && cp[1] == '*') {
597                         comment = 1;
598                         cp[0] = cp[1] = 32;
599                 }
600                 if (cp[0] == '*' && cp[1] == '/') {
601                         comment = 0;
602                         cp[0] = cp[1] = 32;
603                 }
604
605                 /* do not copy when: */
606                 if (comment) {
607                         /* pass */
608                 }
609                 else if (cp[0] == ' ' && cp[1] == ' ') {
610                         /* pass */
611                 }
612                 else if (cp[-1] == '*' && cp[0] == ' ') {
613                         /* pointers with a space */
614                 }       /* skip special keywords */
615                 else if (strncmp("DNA_DEPRECATED", cp, 14) == 0) {
616                         /* single values are skipped already, so decrement 1 less */
617                         a -= 13;
618                         cp += 13;
619                 }
620                 else if (strncmp("DNA_PRIVATE_WORKSPACE", cp, 21) == 0) {
621                         /* Check for DNA_PRIVATE_WORKSPACE_READ_WRITE */
622                         if (strncmp("_READ_WRITE", cp + 21, 11) == 0) {
623                                 a -= 31;
624                                 cp += 31;
625                         }
626                         else {
627                                 a -= 20;
628                                 cp += 20;
629                         }
630                 }
631                 else {
632                         md[0] = cp[0];
633                         md++;
634                         newlen++;
635                 }
636                 cp++;
637         }
638
639         MEM_freeN(temp);
640         return newlen;
641 }
642
643 static void *read_file_data(const char *filename, int *r_len)
644 {
645 #ifdef WIN32
646         FILE *fp = fopen(filename, "rb");
647 #else
648         FILE *fp = fopen(filename, "r");
649 #endif
650         void *data;
651
652         if (!fp) {
653                 *r_len = -1;
654                 return NULL;
655         }
656
657         fseek(fp, 0L, SEEK_END);
658         *r_len = ftell(fp);
659         fseek(fp, 0L, SEEK_SET);
660
661         if (*r_len == -1) {
662                 fclose(fp);
663                 return NULL;
664         }
665
666         data = MEM_mallocN(*r_len, "read_file_data");
667         if (!data) {
668                 *r_len = -1;
669                 fclose(fp);
670                 return NULL;
671         }
672
673         if (fread(data, *r_len, 1, fp) != 1) {
674                 *r_len = -1;
675                 MEM_freeN(data);
676                 fclose(fp);
677                 return NULL;
678         }
679
680         fclose(fp);
681         return data;
682 }
683
684 static int convert_include(const char *filename)
685 {
686         /* read include file, skip structs with a '#' before it.
687          * store all data in temporal arrays.
688          */
689         int maindata_len, count, slen, type, name, strct;
690         short *structpoin, *sp;
691         char *maindata, *mainend, *md, *md1;
692         bool skip_struct;
693
694         md = maindata = read_file_data(filename, &maindata_len);
695         if (maindata_len == -1) {
696                 fprintf(stderr, "Can't read file %s\n", filename);
697                 return 1;
698         }
699
700         maindata_len = preprocess_include(maindata, maindata_len);
701         mainend = maindata + maindata_len - 1;
702
703         /* we look for '{' and then back to 'struct' */
704         count = 0;
705         skip_struct = false;
706         while (count < maindata_len) {
707
708                 /* code for skipping a struct: two hashes on 2 lines. (preprocess added a space) */
709                 if (md[0] == '#' && md[1] == ' ' && md[2] == '#') {
710                         skip_struct = true;
711                 }
712
713                 if (md[0] == '{') {
714                         md[0] = 0;
715                         if (skip_struct) {
716                                 skip_struct = false;
717                         }
718                         else {
719                                 if (md[-1] == ' ') {
720                                         md[-1] = 0;
721                                 }
722                                 md1 = md - 2;
723                                 while (*md1 != 32) {
724                                         /* to beginning of word */
725                                         md1--;
726                                 }
727                                 md1++;
728
729                                 /* we've got a struct name when... */
730                                 if (strncmp(md1 - 7, "struct", 6) == 0) {
731
732                                         strct = add_type(md1, 0);
733                                         if (strct == -1) {
734                                                 fprintf(stderr, "File '%s' contains struct we cant parse \"%s\"\n", filename, md1);
735                                                 return 1;
736                                         }
737
738                                         structpoin = add_struct(strct);
739                                         sp = structpoin + 2;
740
741                                         DEBUG_PRINTF(1, "\t|\t|-- detected struct %s\n", types[strct]);
742
743                                         /* first lets make it all nice strings */
744                                         md1 = md + 1;
745                                         while (*md1 != '}') {
746                                                 if (md1 > mainend) {
747                                                         break;
748                                                 }
749
750                                                 if (*md1 == ',' || *md1 == ' ') {
751                                                         *md1 = 0;
752                                                 }
753                                                 md1++;
754                                         }
755
756                                         /* read types and names until first character that is not '}' */
757                                         md1 = md + 1;
758                                         while (*md1 != '}') {
759                                                 if (md1 > mainend) {
760                                                         break;
761                                                 }
762
763                                                 /* skip when it says 'struct' or 'unsigned' or 'const' */
764                                                 if (*md1) {
765                                                         if (strncmp(md1, "struct", 6) == 0) { md1 += 7; }
766                                                         if (strncmp(md1, "unsigned", 8) == 0) { md1 += 9; }
767                                                         if (strncmp(md1, "const", 5) == 0) { md1 += 6; }
768
769                                                         /* we've got a type! */
770                                                         type = add_type(md1, 0);
771                                                         if (type == -1) {
772                                                                 fprintf(stderr, "File '%s' contains struct we can't parse \"%s\"\n", filename, md1);
773                                                                 return 1;
774                                                         }
775
776                                                         DEBUG_PRINTF(1, "\t|\t|\tfound type %s (", md1);
777
778                                                         md1 += strlen(md1);
779
780
781                                                         /* read until ';' */
782                                                         while (*md1 != ';') {
783                                                                 if (md1 > mainend) {
784                                                                         break;
785                                                                 }
786
787                                                                 if (*md1) {
788                                                                         /* We've got a name. slen needs
789                                                                          * correction for function
790                                                                          * pointers! */
791                                                                         slen = (int) strlen(md1);
792                                                                         if (md1[slen - 1] == ';') {
793                                                                                 md1[slen - 1] = 0;
794
795                                                                                 name = add_name(version_elem_static_from_alias(strct, md1));
796                                                                                 if (name == -1) {
797                                                                                         fprintf(stderr, "File '%s' contains struct with name that can't be added \"%s\"\n", filename, md1);
798                                                                                         return 1;
799                                                                                 }
800                                                                                 slen += additional_slen_offset;
801                                                                                 sp[0] = type;
802                                                                                 sp[1] = name;
803
804                                                                                 if (names[name] != NULL) {
805                                                                                         DEBUG_PRINTF(1, "%s |", names[name]);
806                                                                                 }
807
808                                                                                 structpoin[1]++;
809                                                                                 sp += 2;
810
811                                                                                 md1 += slen;
812                                                                                 break;
813                                                                         }
814
815                                                                         name = add_name(version_elem_static_from_alias(strct, md1));
816                                                                         if (name == -1) {
817                                                                                 fprintf(stderr, "File '%s' contains struct with name that can't be added \"%s\"\n", filename, md1);
818                                                                                 return 1;
819                                                                         }
820                                                                         slen += additional_slen_offset;
821
822                                                                         sp[0] = type;
823                                                                         sp[1] = name;
824                                                                         if (names[name] != NULL) {
825                                                                                 DEBUG_PRINTF(1, "%s ||", names[name]);
826                                                                         }
827
828                                                                         structpoin[1]++;
829                                                                         sp += 2;
830
831                                                                         md1 += slen;
832                                                                 }
833                                                                 md1++;
834                                                         }
835
836                                                         DEBUG_PRINTF(1, ")\n");
837
838                                                 }
839                                                 md1++;
840                                         }
841                                 }
842                         }
843                 }
844                 count++;
845                 md++;
846         }
847
848         MEM_freeN(maindata);
849
850         return 0;
851 }
852
853 static bool check_field_alignment(int firststruct, int structtype, int type, int len,
854                                   const char *name, const char *detail)
855 {
856         bool result = true;
857         if (type < firststruct && types_size_native[type] > 4 && (len % 8)) {
858                 fprintf(stderr, "Align 8 error (%s) in struct: %s %s (add %d padding bytes)\n",
859                         detail, types[structtype], name, len % 8);
860                 result = false;
861         }
862         if (types_size_native[type] > 3 && (len % 4) ) {
863                 fprintf(stderr, "Align 4 error (%s) in struct: %s %s (add %d padding bytes)\n",
864                         detail, types[structtype], name, len % 4);
865                 result = false;
866         }
867         if (types_size_native[type] == 2 && (len % 2) ) {
868                 fprintf(stderr, "Align 2 error (%s) in struct: %s %s (add %d padding bytes)\n",
869                         detail, types[structtype], name, len % 2);
870                 result = false;
871         }
872         return result;
873 }
874
875 static int calculate_struct_sizes(int firststruct)
876 {
877         int unknown = nr_structs, lastunknown;
878         bool dna_error = false;
879
880         while (unknown) {
881                 lastunknown = unknown;
882                 unknown = 0;
883
884                 /* check all structs... */
885                 for (int a = 0; a < nr_structs; a++) {
886                         const short *structpoin = structs[a];
887                         const int    structtype = structpoin[0];
888
889                         /* when length is not known... */
890                         if (types_size_native[structtype] == 0) {
891
892                                 const short *sp = structpoin + 2;
893                                 int size_native = 0;
894                                 int size_32 = 0;
895                                 int size_64 = 0;
896                                 bool has_pointer = false;
897
898                                 /* check all elements in struct */
899                                 for (int b = 0; b < structpoin[1]; b++, sp += 2) {
900                                         int type = sp[0];
901                                         const char *cp = names[sp[1]];
902
903                                         int namelen = (int)strlen(cp);
904                                         /* is it a pointer or function pointer? */
905                                         if (cp[0] == '*' || cp[1] == '*') {
906                                                 has_pointer = 1;
907                                                 /* has the name an extra length? (array) */
908                                                 int mul = 1;
909                                                 if (cp[namelen - 1] == ']') {
910                                                         mul = DNA_elem_array_size(cp);
911                                                 }
912
913                                                 if (mul == 0) {
914                                                         fprintf(stderr, "Zero array size found or could not parse %s: '%.*s'\n",
915                                                                 types[structtype], namelen + 1, cp);
916                                                         dna_error = 1;
917                                                 }
918
919                                                 /* 4-8 aligned/ */
920                                                 if (sizeof(void *) == 4) {
921                                                         if (size_native % 4) {
922                                                                 fprintf(stderr, "Align pointer error in struct (size_native 4): %s %s\n",
923                                                                         types[structtype], cp);
924                                                                 dna_error = 1;
925                                                         }
926                                                 }
927                                                 else {
928                                                         if (size_native % 8) {
929                                                                 fprintf(stderr, "Align pointer error in struct (size_native 8): %s %s\n",
930                                                                         types[structtype], cp);
931                                                                 dna_error = 1;
932                                                         }
933                                                 }
934
935                                                 if (size_64 % 8) {
936                                                         fprintf(stderr, "Align pointer error in struct (size_64 8): %s %s\n",
937                                                                 types[structtype], cp);
938                                                         dna_error = 1;
939                                                 }
940
941                                                 size_native += sizeof(void *) * mul;
942                                                 size_32 += 4 * mul;
943                                                 size_64 += 8 * mul;
944
945                                         }
946                                         else if (cp[0] == '[') {
947                                                 /* parsing can cause names "var" and "[3]"
948                                                  * to be found for "float var [3]" */
949                                                 fprintf(stderr, "Parse error in struct, invalid member name: %s %s\n",
950                                                         types[structtype], cp);
951                                                 dna_error = 1;
952                                         }
953                                         else if (types_size_native[type]) {
954                                                 /* has the name an extra length? (array) */
955                                                 int mul = 1;
956                                                 if (cp[namelen - 1] == ']') {
957                                                         mul = DNA_elem_array_size(cp);
958                                                 }
959
960                                                 if (mul == 0) {
961                                                         fprintf(stderr, "Zero array size found or could not parse %s: '%.*s'\n",
962                                                                 types[structtype], namelen + 1, cp);
963                                                         dna_error = 1;
964                                                 }
965
966                                                 /* struct alignment */
967                                                 if (type >= firststruct) {
968                                                         if (sizeof(void *) == 8 && (size_native % 8) ) {
969                                                                 fprintf(stderr, "Align struct error: %s %s\n",
970                                                                         types[structtype], cp);
971                                                                 dna_error = 1;
972                                                         }
973                                                 }
974
975                                                 /* Check 2-4-8 aligned. */
976                                                 if (!check_field_alignment(firststruct, structtype, type, size_32, cp, "32 bit")) {
977                                                         dna_error = 1;
978                                                 }
979                                                 if (!check_field_alignment(firststruct, structtype, type, size_64, cp, "64 bit")) {
980                                                         dna_error = 1;
981                                                 }
982
983                                                 size_native += mul * types_size_native[type];
984                                                 size_32 += mul * types_size_32[type];
985                                                 size_64 += mul * types_size_64[type];
986
987                                         }
988                                         else {
989                                                 size_native = 0;
990                                                 size_32 = 0;
991                                                 size_64 = 0;
992                                                 break;
993                                         }
994                                 }
995
996                                 if (size_native == 0) {
997                                         unknown++;
998                                 }
999                                 else {
1000                                         types_size_native[structtype] = size_native;
1001                                         types_size_32[structtype] = size_32;
1002                                         types_size_64[structtype] = size_64;
1003                                         /* two ways to detect if a struct contains a pointer:
1004                                          * has_pointer is set or size_native  doesn't match any of 32/64bit lengths*/
1005                                         if (has_pointer || size_64 != size_native || size_32 != size_native) {
1006                                                 if (size_64 % 8) {
1007                                                         fprintf(stderr, "Sizeerror 8 in struct: %s (add %d bytes)\n",
1008                                                                 types[structtype], size_64 % 8);
1009                                                         dna_error = 1;
1010                                                 }
1011                                         }
1012
1013                                         if (size_native % 4) {
1014                                                 fprintf(stderr, "Sizeerror 4 in struct: %s (add %d bytes)\n",
1015                                                        types[structtype], size_native % 4);
1016                                                 dna_error = 1;
1017                                         }
1018
1019                                 }
1020                         }
1021                 }
1022
1023                 if (unknown == lastunknown) {
1024                         break;
1025                 }
1026         }
1027
1028         if (unknown) {
1029                 fprintf(stderr, "ERROR: still %d structs unknown\n", unknown);
1030
1031                 if (debugSDNA) {
1032                         fprintf(stderr, "*** Known structs :\n");
1033
1034                         for (int a = 0; a < nr_structs; a++) {
1035                                 const short *structpoin = structs[a];
1036                                 const int    structtype = structpoin[0];
1037
1038                                 /* length unknown */
1039                                 if (types_size_native[structtype] != 0) {
1040                                         fprintf(stderr, "  %s\n", types[structtype]);
1041                                 }
1042                         }
1043                 }
1044
1045
1046                 fprintf(stderr, "*** Unknown structs :\n");
1047
1048                 for (int a = 0; a < nr_structs; a++) {
1049                         const short *structpoin = structs[a];
1050                         const int    structtype = structpoin[0];
1051
1052                         /* length unknown yet */
1053                         if (types_size_native[structtype] == 0) {
1054                                 fprintf(stderr, "  %s\n", types[structtype]);
1055                         }
1056                 }
1057
1058                 dna_error = 1;
1059         }
1060
1061         return dna_error;
1062 }
1063
1064 #define MAX_DNA_LINE_LENGTH 20
1065
1066 static void dna_write(FILE *file, const void *pntr, const int size)
1067 {
1068         static int linelength = 0;
1069         int i;
1070         const char *data;
1071
1072         data = (const char *)pntr;
1073
1074         for (i = 0; i < size; i++) {
1075                 fprintf(file, "%d, ", data[i]);
1076                 linelength++;
1077                 if (linelength >= MAX_DNA_LINE_LENGTH) {
1078                         fprintf(file, "\n");
1079                         linelength = 0;
1080                 }
1081         }
1082 }
1083
1084 void print_struct_sizes(void)
1085 {
1086         int a, unknown = nr_structs, structtype;
1087         /*int lastunknown;*/ /*UNUSED*/
1088         const short *structpoin;
1089         printf("\n\n*** All detected structs:\n");
1090
1091         while (unknown) {
1092                 /*lastunknown = unknown;*/ /*UNUSED*/
1093                 unknown = 0;
1094
1095                 /* check all structs... */
1096                 for (a = 0; a < nr_structs; a++) {
1097                         structpoin = structs[a];
1098                         structtype = structpoin[0];
1099                         printf("\t%s\t:%d\n", types[structtype], types_size_native[structtype]);
1100                 }
1101         }
1102
1103         printf("*** End of list\n");
1104
1105 }
1106
1107
1108 static int make_structDNA(const char *baseDirectory, FILE *file, FILE *file_offsets)
1109 {
1110         int i;
1111         const short *sp;
1112         /* str contains filenames. Since we now include paths, I stretched       */
1113         /* it a bit. Hope this is enough :) -nzc-                                */
1114         char str[SDNA_MAX_FILENAME_LENGTH];
1115         int firststruct;
1116
1117         if (debugSDNA > 0) {
1118                 fflush(stdout);
1119                 printf("Running makesdna at debug level %d\n", debugSDNA);
1120         }
1121
1122         mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
1123
1124         /* the longest known struct is 50k, so we assume 100k is sufficient! */
1125         structdata = MEM_callocN(maxdata, "structdata");
1126
1127         /* a maximum of 5000 variables, must be sufficient? */
1128         names = MEM_callocN(sizeof(char *) * maxnr, "names");
1129         types = MEM_callocN(sizeof(char *) * maxnr, "types");
1130         types_size_native = MEM_callocN(sizeof(short) * maxnr, "types_size_native");
1131         types_size_32 = MEM_callocN(sizeof(short) * maxnr, "types_size_32");
1132         types_size_64 = MEM_callocN(sizeof(short) * maxnr, "types_size_64");
1133         structs = MEM_callocN(sizeof(short *) * maxnr, "structs");
1134
1135         /* Build versioning data */
1136         DNA_alias_maps(
1137                 DNA_RENAME_ALIAS_FROM_STATIC,
1138                 &g_version_data.struct_map_alias_from_static,
1139                 NULL);
1140         DNA_alias_maps(
1141                 DNA_RENAME_STATIC_FROM_ALIAS,
1142                 &g_version_data.struct_map_static_from_alias,
1143                 &g_version_data.elem_map_static_from_alias);
1144
1145         /**
1146          * Insertion of all known types.
1147          *
1148          * \warning Order of function calls here must be aligned with #eSDNA_Type.
1149          * \warning uint is not allowed! use in structs an unsigned int.
1150          * \warning sizes must match #DNA_elem_type_size().
1151          */
1152         add_type("char", 1);     /* SDNA_TYPE_CHAR */
1153         add_type("uchar", 1);    /* SDNA_TYPE_UCHAR */
1154         add_type("short", 2);    /* SDNA_TYPE_SHORT */
1155         add_type("ushort", 2);   /* SDNA_TYPE_USHORT */
1156         add_type("int", 4);      /* SDNA_TYPE_INT */
1157
1158         /* note, long isn't supported,
1159          * these are place-holders to maintain alignment with eSDNA_Type*/
1160         add_type("long", 4);     /* SDNA_TYPE_LONG */
1161         add_type("ulong", 4);    /* SDNA_TYPE_ULONG */
1162
1163         add_type("float", 4);    /* SDNA_TYPE_FLOAT */
1164         add_type("double", 8);   /* SDNA_TYPE_DOUBLE */
1165         add_type("int64_t", 8);  /* SDNA_TYPE_INT64 */
1166         add_type("uint64_t", 8); /* SDNA_TYPE_UINT64 */
1167         add_type("void", 0);     /* SDNA_TYPE_VOID */
1168
1169         /* the defines above shouldn't be output in the padding file... */
1170         firststruct = nr_types;
1171
1172         /* add all include files defined in the global array                     */
1173         /* Since the internal file+path name buffer has limited length, I do a   */
1174         /* little test first...                                                  */
1175         /* Mind the breaking condition here!                                     */
1176         DEBUG_PRINTF(0, "\tStart of header scan:\n");
1177         for (i = 0; *(includefiles[i]) != '\0'; i++) {
1178                 sprintf(str, "%s%s", baseDirectory, includefiles[i]);
1179                 DEBUG_PRINTF(0, "\t|-- Converting %s\n", str);
1180                 if (convert_include(str)) {
1181                         return 1;
1182                 }
1183         }
1184         DEBUG_PRINTF(0, "\tFinished scanning %d headers.\n", i);
1185
1186         if (calculate_struct_sizes(firststruct)) {
1187                 /* error */
1188                 return 1;
1189         }
1190
1191         /* FOR DEBUG */
1192         if (debugSDNA > 1) {
1193                 int a, b;
1194                 /* short *elem; */
1195                 short num_types;
1196
1197                 printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
1198                 for (a = 0; a < nr_names; a++) {
1199                         printf(" %s\n", names[a]);
1200                 }
1201                 printf("\n");
1202
1203                 sp = types_size_native;
1204                 for (a = 0; a < nr_types; a++, sp++) {
1205                         printf(" %s %d\n", types[a], *sp);
1206                 }
1207                 printf("\n");
1208
1209                 for (a = 0; a < nr_structs; a++) {
1210                         sp = structs[a];
1211                         printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1], types_size_native[sp[0]]);
1212                         num_types  = sp[1];
1213                         sp += 2;
1214                         /* ? num_types was elem? */
1215                         for (b = 0; b < num_types; b++, sp += 2) {
1216                                 printf("   %s %s\n", types[sp[0]], names[sp[1]]);
1217                         }
1218                 }
1219         }
1220
1221         /* file writing */
1222
1223         DEBUG_PRINTF(0, "Writing file ... ");
1224
1225         if (nr_names == 0 || nr_structs == 0) {
1226                 /* pass */
1227         }
1228         else {
1229                 const char nil_bytes[4] = {0};
1230                 int len, len_align;
1231
1232                 dna_write(file, "SDNA", 4);
1233
1234                 /* write names */
1235                 dna_write(file, "NAME", 4);
1236                 len = nr_names;
1237                 dna_write(file, &len, 4);
1238                 /* write array */
1239                 len = 0;
1240                 for (int nr = 0; nr < nr_names; nr++) {
1241                         int name_size = strlen(names[nr]) + 1;
1242                         dna_write(file, names[nr], name_size);
1243                         len += name_size;
1244                 }
1245                 len_align = (len + 3) & ~3;
1246                 if (len != len_align) {
1247                         dna_write(file, nil_bytes, len_align - len);
1248                 }
1249
1250                 /* write TYPES */
1251                 dna_write(file, "TYPE", 4);
1252                 len = nr_types;
1253                 dna_write(file, &len, 4);
1254                 /* write array */
1255                 len = 0;
1256                 for (int nr = 0; nr < nr_types; nr++) {
1257                         int type_size = strlen(types[nr]) + 1;
1258                         dna_write(file, types[nr], type_size);
1259                         len += type_size;
1260                 }
1261                 len_align = (len + 3) & ~3;
1262                 if (len != len_align) {
1263                         dna_write(file, nil_bytes, len_align - len);
1264                 }
1265
1266                 /* WRITE TYPELENGTHS */
1267                 dna_write(file, "TLEN", 4);
1268
1269                 len = 2 * nr_types;
1270                 if (nr_types & 1) {
1271                         len += 2;
1272                 }
1273                 dna_write(file, types_size_native, len);
1274
1275                 /* WRITE STRUCTS */
1276                 dna_write(file, "STRC", 4);
1277                 len = nr_structs;
1278                 dna_write(file, &len, 4);
1279
1280                 /* calc datablock size */
1281                 sp = structs[nr_structs - 1];
1282                 sp += 2 + 2 * (sp[1]);
1283                 len = (intptr_t) ((char *) sp - (char *) structs[0]);
1284                 len = (len + 3) & ~3;
1285
1286                 dna_write(file, structs[0], len);
1287
1288                 /* a simple dna padding test */
1289                 if (0) {
1290                         FILE *fp;
1291                         int a;
1292
1293                         fp = fopen("padding.c", "w");
1294                         if (fp == NULL) {
1295                                 /* pass */
1296                         }
1297                         else {
1298
1299                                 /* add all include files defined in the global array */
1300                                 for (i = 0; *(includefiles[i]) != '\0'; i++) {
1301                                         fprintf(fp, "#include \"%s%s\"\n", baseDirectory, includefiles[i]);
1302                                 }
1303
1304                                 fprintf(fp, "main() {\n");
1305                                 sp = types_size_native;
1306                                 sp += firststruct;
1307                                 for (a = firststruct; a < nr_types; a++, sp++) {
1308                                         if (*sp) {
1309                                                 fprintf(fp, "\tif (sizeof(struct %s) - %d) printf(\"ALIGN ERROR:", types[a], *sp);
1310                                                 fprintf(fp, "%%d %s %d ", types[a], *sp);
1311                                                 fprintf(fp, "\\n\",  sizeof(struct %s) - %d);\n", types[a], *sp);
1312                                         }
1313                                 }
1314                                 fprintf(fp, "}\n");
1315                                 fclose(fp);
1316                         }
1317                 }
1318                 /*      end end padding test */
1319         }
1320
1321         /* write a simple enum with all structs offsets,
1322          * should only be accessed via SDNA_TYPE_FROM_STRUCT macro */
1323         {
1324                 fprintf(file_offsets, "#define SDNA_TYPE_FROM_STRUCT(id) _SDNA_TYPE_##id\n");
1325                 fprintf(file_offsets, "enum {\n");
1326                 for (i = 0; i < nr_structs; i++) {
1327                         const short *structpoin = structs[i];
1328                         const int    structtype = structpoin[0];
1329                         fprintf(file_offsets, "\t_SDNA_TYPE_%s = %d,\n", version_struct_alias_from_static(types[structtype]), i);
1330                 }
1331                 fprintf(file_offsets, "\tSDNA_TYPE_MAX = %d,\n", nr_structs);
1332                 fprintf(file_offsets, "};\n");
1333         }
1334
1335         /* Check versioning errors which could cause duplicate names,
1336          * do last because names are stripped. */
1337         {
1338                 GSet *names_unique = BLI_gset_str_new_ex(__func__, 512);
1339                 for (int struct_nr = 0; struct_nr < nr_structs; struct_nr++) {
1340                         sp = structs[struct_nr];
1341                         const char *struct_name = types[sp[0]];
1342                         const int len = sp[1];
1343                         sp += 2;
1344                         for (int a = 0; a < len; a++, sp += 2) {
1345                                 char *name = names[sp[1]];
1346                                 DNA_elem_id_strip(name);
1347                                 if (!BLI_gset_add(names_unique, name)) {
1348                                         fprintf(stderr, "Error: duplicate name found '%s.%s', "
1349                                                "likely cause is 'dna_rename_defs.h'\n",
1350                                                struct_name, name);
1351                                         return 1;
1352                                 }
1353                         }
1354                         BLI_gset_clear(names_unique, NULL);
1355                 }
1356                 BLI_gset_free(names_unique, NULL);
1357         }
1358
1359         MEM_freeN(structdata);
1360         MEM_freeN(names);
1361         MEM_freeN(types);
1362         MEM_freeN(types_size_native);
1363         MEM_freeN(types_size_32);
1364         MEM_freeN(types_size_64);
1365         MEM_freeN(structs);
1366
1367         BLI_memarena_free(mem_arena);
1368
1369         BLI_ghash_free(g_version_data.struct_map_alias_from_static, NULL, NULL);
1370         BLI_ghash_free(g_version_data.struct_map_static_from_alias, NULL, NULL);
1371         BLI_ghash_free(g_version_data.elem_map_static_from_alias, MEM_freeN, NULL);
1372
1373         DEBUG_PRINTF(0, "done.\n");
1374
1375         return 0;
1376 }
1377
1378 /** \} */
1379
1380 /* end make DNA. */
1381
1382 /* -------------------------------------------------------------------- */
1383 /** \name Main Function
1384  * \{ */
1385
1386 static void make_bad_file(const char *file, int line)
1387 {
1388         FILE *fp = fopen(file, "w");
1389         fprintf(fp,
1390                "#error \"Error! can't make correct DNA.c file from %s:%d, check alignment.\"\n",
1391                __FILE__, line);
1392         fclose(fp);
1393 }
1394
1395 #ifndef BASE_HEADER
1396 #define BASE_HEADER "../"
1397 #endif
1398
1399 int main(int argc, char **argv)
1400 {
1401         int return_status = 0;
1402
1403         if (argc != 3 && argc != 4) {
1404                 printf("Usage: %s dna.c dna_struct_offsets.h [base directory]\n", argv[0]);
1405                 return_status = 1;
1406         }
1407         else {
1408                 FILE *file_dna         = fopen(argv[1], "w");
1409                 FILE *file_dna_offsets = fopen(argv[2], "w");
1410                 if (!file_dna) {
1411                         printf("Unable to open file: %s\n", argv[1]);
1412                         return_status = 1;
1413                 }
1414                 else if (!file_dna_offsets) {
1415                         printf("Unable to open file: %s\n", argv[2]);
1416                         return_status = 1;
1417                 }
1418                 else {
1419                         const char *baseDirectory;
1420
1421                         if (argc == 4) {
1422                                 baseDirectory = argv[3];
1423                         }
1424                         else {
1425                                 baseDirectory = BASE_HEADER;
1426                         }
1427
1428                         fprintf(file_dna, "extern const unsigned char DNAstr[];\n");
1429                         fprintf(file_dna, "const unsigned char DNAstr[] = {\n");
1430                         if (make_structDNA(baseDirectory, file_dna, file_dna_offsets)) {
1431                                 /* error */
1432                                 fclose(file_dna);
1433                                 file_dna = NULL;
1434                                 make_bad_file(argv[1], __LINE__);
1435                                 return_status = 1;
1436                         }
1437                         else {
1438                                 fprintf(file_dna, "};\n");
1439                                 fprintf(file_dna, "extern const int DNAlen;\n");
1440                                 fprintf(file_dna, "const int DNAlen = sizeof(DNAstr);\n");
1441                         }
1442                 }
1443
1444                 if (file_dna) {
1445                         fclose(file_dna);
1446                 }
1447                 if (file_dna_offsets) {
1448                         fclose(file_dna_offsets);
1449                 }
1450         }
1451
1452
1453         return return_status;
1454 }
1455
1456 /* handy but fails on struct bounds which makesdna doesn't care about
1457  * with quite the same strictness as GCC does */
1458 #if 0
1459 /* include files for automatic dependencies */
1460
1461 /* extra safety check that we are aligned,
1462  * warnings here are easier to fix the makesdna's */
1463 #ifdef __GNUC__
1464 #  pragma GCC diagnostic error "-Wpadded"
1465 #endif
1466
1467 #endif /* if 0 */
1468
1469 /* even though DNA supports, 'long' shouldn't be used since it can be either 32 or 64bit,
1470  * use int or int64_t instead.
1471  * Only valid use would be as a runtime variable if an API expected a long,
1472  * but so far we dont have this happening. */
1473 #ifdef __GNUC__
1474 #  pragma GCC poison long
1475 #endif
1476
1477 #include "DNA_listBase.h"
1478 #include "DNA_vec_types.h"
1479 #include "DNA_ID.h"
1480 #include "DNA_ipo_types.h"
1481 #include "DNA_key_types.h"
1482 #include "DNA_text_types.h"
1483 #include "DNA_packedFile_types.h"
1484 #include "DNA_camera_types.h"
1485 #include "DNA_image_types.h"
1486 #include "DNA_texture_types.h"
1487 #include "DNA_light_types.h"
1488 #include "DNA_material_types.h"
1489 #include "DNA_vfont_types.h"
1490 #include "DNA_meta_types.h"
1491 #include "DNA_curve_types.h"
1492 #include "DNA_mesh_types.h"
1493 #include "DNA_meshdata_types.h"
1494 #include "DNA_modifier_types.h"
1495 #include "DNA_lattice_types.h"
1496 #include "DNA_object_types.h"
1497 #include "DNA_object_force_types.h"
1498 #include "DNA_object_fluidsim_types.h"
1499 #include "DNA_world_types.h"
1500 #include "DNA_scene_types.h"
1501 #include "DNA_view3d_types.h"
1502 #include "DNA_view2d_types.h"
1503 #include "DNA_space_types.h"
1504 #include "DNA_userdef_types.h"
1505 #include "DNA_screen_types.h"
1506 #include "DNA_sdna_types.h"
1507 #include "DNA_fileglobal_types.h"
1508 #include "DNA_sequence_types.h"
1509 #include "DNA_effect_types.h"
1510 #include "DNA_outliner_types.h"
1511 #include "DNA_sound_types.h"
1512 #include "DNA_collection_types.h"
1513 #include "DNA_armature_types.h"
1514 #include "DNA_action_types.h"
1515 #include "DNA_constraint_types.h"
1516 #include "DNA_nla_types.h"
1517 #include "DNA_node_types.h"
1518 #include "DNA_color_types.h"
1519 #include "DNA_brush_types.h"
1520 #include "DNA_customdata_types.h"
1521 #include "DNA_particle_types.h"
1522 #include "DNA_cloth_types.h"
1523 #include "DNA_gpencil_types.h"
1524 #include "DNA_gpencil_modifier_types.h"
1525 #include "DNA_shader_fx_types.h"
1526 #include "DNA_windowmanager_types.h"
1527 #include "DNA_anim_types.h"
1528 #include "DNA_boid_types.h"
1529 #include "DNA_smoke_types.h"
1530 #include "DNA_speaker_types.h"
1531 #include "DNA_movieclip_types.h"
1532 #include "DNA_tracking_types.h"
1533 #include "DNA_dynamicpaint_types.h"
1534 #include "DNA_mask_types.h"
1535 #include "DNA_rigidbody_types.h"
1536 #include "DNA_freestyle_types.h"
1537 #include "DNA_linestyle_types.h"
1538 #include "DNA_cachefile_types.h"
1539 #include "DNA_layer_types.h"
1540 #include "DNA_workspace_types.h"
1541 #include "DNA_lightprobe_types.h"
1542
1543 /* end of list */
1544
1545 /** \} */
1546
1547
1548 /* -------------------------------------------------------------------- */
1549 /** \name DNA Renaming Sanity Check
1550  *
1551  * Without this it's possible to reference struct members that don't exist,
1552  * breaking backward & forward compatibility.
1553  *
1554  * \{ */
1555
1556 static void UNUSED_FUNCTION(dna_rename_defs_ensure)(void)
1557 {
1558 #define DNA_STRUCT_RENAME(old, new) (void)sizeof(new);
1559 #define DNA_STRUCT_RENAME_ELEM(struct_name, old, new) (void)offsetof(struct_name, new);
1560 #include "dna_rename_defs.h"
1561 #undef DNA_STRUCT_RENAME
1562 #undef DNA_STRUCT_RENAME_ELEM
1563 }
1564
1565 /** \} */