Cleanup: remove DNA_PRIVATE_WORKSPACE hacks.
[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 {
621                         md[0] = cp[0];
622                         md++;
623                         newlen++;
624                 }
625                 cp++;
626         }
627
628         MEM_freeN(temp);
629         return newlen;
630 }
631
632 static void *read_file_data(const char *filename, int *r_len)
633 {
634 #ifdef WIN32
635         FILE *fp = fopen(filename, "rb");
636 #else
637         FILE *fp = fopen(filename, "r");
638 #endif
639         void *data;
640
641         if (!fp) {
642                 *r_len = -1;
643                 return NULL;
644         }
645
646         fseek(fp, 0L, SEEK_END);
647         *r_len = ftell(fp);
648         fseek(fp, 0L, SEEK_SET);
649
650         if (*r_len == -1) {
651                 fclose(fp);
652                 return NULL;
653         }
654
655         data = MEM_mallocN(*r_len, "read_file_data");
656         if (!data) {
657                 *r_len = -1;
658                 fclose(fp);
659                 return NULL;
660         }
661
662         if (fread(data, *r_len, 1, fp) != 1) {
663                 *r_len = -1;
664                 MEM_freeN(data);
665                 fclose(fp);
666                 return NULL;
667         }
668
669         fclose(fp);
670         return data;
671 }
672
673 static int convert_include(const char *filename)
674 {
675         /* read include file, skip structs with a '#' before it.
676          * store all data in temporal arrays.
677          */
678         int maindata_len, count, slen, type, name, strct;
679         short *structpoin, *sp;
680         char *maindata, *mainend, *md, *md1;
681         bool skip_struct;
682
683         md = maindata = read_file_data(filename, &maindata_len);
684         if (maindata_len == -1) {
685                 fprintf(stderr, "Can't read file %s\n", filename);
686                 return 1;
687         }
688
689         maindata_len = preprocess_include(maindata, maindata_len);
690         mainend = maindata + maindata_len - 1;
691
692         /* we look for '{' and then back to 'struct' */
693         count = 0;
694         skip_struct = false;
695         while (count < maindata_len) {
696
697                 /* code for skipping a struct: two hashes on 2 lines. (preprocess added a space) */
698                 if (md[0] == '#' && md[1] == ' ' && md[2] == '#') {
699                         skip_struct = true;
700                 }
701
702                 if (md[0] == '{') {
703                         md[0] = 0;
704                         if (skip_struct) {
705                                 skip_struct = false;
706                         }
707                         else {
708                                 if (md[-1] == ' ') {
709                                         md[-1] = 0;
710                                 }
711                                 md1 = md - 2;
712                                 while (*md1 != 32) {
713                                         /* to beginning of word */
714                                         md1--;
715                                 }
716                                 md1++;
717
718                                 /* we've got a struct name when... */
719                                 if (strncmp(md1 - 7, "struct", 6) == 0) {
720
721                                         strct = add_type(md1, 0);
722                                         if (strct == -1) {
723                                                 fprintf(stderr, "File '%s' contains struct we cant parse \"%s\"\n", filename, md1);
724                                                 return 1;
725                                         }
726
727                                         structpoin = add_struct(strct);
728                                         sp = structpoin + 2;
729
730                                         DEBUG_PRINTF(1, "\t|\t|-- detected struct %s\n", types[strct]);
731
732                                         /* first lets make it all nice strings */
733                                         md1 = md + 1;
734                                         while (*md1 != '}') {
735                                                 if (md1 > mainend) {
736                                                         break;
737                                                 }
738
739                                                 if (*md1 == ',' || *md1 == ' ') {
740                                                         *md1 = 0;
741                                                 }
742                                                 md1++;
743                                         }
744
745                                         /* read types and names until first character that is not '}' */
746                                         md1 = md + 1;
747                                         while (*md1 != '}') {
748                                                 if (md1 > mainend) {
749                                                         break;
750                                                 }
751
752                                                 /* skip when it says 'struct' or 'unsigned' or 'const' */
753                                                 if (*md1) {
754                                                         if (strncmp(md1, "struct", 6) == 0) { md1 += 7; }
755                                                         if (strncmp(md1, "unsigned", 8) == 0) { md1 += 9; }
756                                                         if (strncmp(md1, "const", 5) == 0) { md1 += 6; }
757
758                                                         /* we've got a type! */
759                                                         type = add_type(md1, 0);
760                                                         if (type == -1) {
761                                                                 fprintf(stderr, "File '%s' contains struct we can't parse \"%s\"\n", filename, md1);
762                                                                 return 1;
763                                                         }
764
765                                                         DEBUG_PRINTF(1, "\t|\t|\tfound type %s (", md1);
766
767                                                         md1 += strlen(md1);
768
769
770                                                         /* read until ';' */
771                                                         while (*md1 != ';') {
772                                                                 if (md1 > mainend) {
773                                                                         break;
774                                                                 }
775
776                                                                 if (*md1) {
777                                                                         /* We've got a name. slen needs
778                                                                          * correction for function
779                                                                          * pointers! */
780                                                                         slen = (int) strlen(md1);
781                                                                         if (md1[slen - 1] == ';') {
782                                                                                 md1[slen - 1] = 0;
783
784                                                                                 name = add_name(version_elem_static_from_alias(strct, md1));
785                                                                                 if (name == -1) {
786                                                                                         fprintf(stderr, "File '%s' contains struct with name that can't be added \"%s\"\n", filename, md1);
787                                                                                         return 1;
788                                                                                 }
789                                                                                 slen += additional_slen_offset;
790                                                                                 sp[0] = type;
791                                                                                 sp[1] = name;
792
793                                                                                 if (names[name] != NULL) {
794                                                                                         DEBUG_PRINTF(1, "%s |", names[name]);
795                                                                                 }
796
797                                                                                 structpoin[1]++;
798                                                                                 sp += 2;
799
800                                                                                 md1 += slen;
801                                                                                 break;
802                                                                         }
803
804                                                                         name = add_name(version_elem_static_from_alias(strct, md1));
805                                                                         if (name == -1) {
806                                                                                 fprintf(stderr, "File '%s' contains struct with name that can't be added \"%s\"\n", filename, md1);
807                                                                                 return 1;
808                                                                         }
809                                                                         slen += additional_slen_offset;
810
811                                                                         sp[0] = type;
812                                                                         sp[1] = name;
813                                                                         if (names[name] != NULL) {
814                                                                                 DEBUG_PRINTF(1, "%s ||", names[name]);
815                                                                         }
816
817                                                                         structpoin[1]++;
818                                                                         sp += 2;
819
820                                                                         md1 += slen;
821                                                                 }
822                                                                 md1++;
823                                                         }
824
825                                                         DEBUG_PRINTF(1, ")\n");
826
827                                                 }
828                                                 md1++;
829                                         }
830                                 }
831                         }
832                 }
833                 count++;
834                 md++;
835         }
836
837         MEM_freeN(maindata);
838
839         return 0;
840 }
841
842 static bool check_field_alignment(int firststruct, int structtype, int type, int len,
843                                   const char *name, const char *detail)
844 {
845         bool result = true;
846         if (type < firststruct && types_size_native[type] > 4 && (len % 8)) {
847                 fprintf(stderr, "Align 8 error (%s) in struct: %s %s (add %d padding bytes)\n",
848                         detail, types[structtype], name, len % 8);
849                 result = false;
850         }
851         if (types_size_native[type] > 3 && (len % 4) ) {
852                 fprintf(stderr, "Align 4 error (%s) in struct: %s %s (add %d padding bytes)\n",
853                         detail, types[structtype], name, len % 4);
854                 result = false;
855         }
856         if (types_size_native[type] == 2 && (len % 2) ) {
857                 fprintf(stderr, "Align 2 error (%s) in struct: %s %s (add %d padding bytes)\n",
858                         detail, types[structtype], name, len % 2);
859                 result = false;
860         }
861         return result;
862 }
863
864 static int calculate_struct_sizes(int firststruct)
865 {
866         int unknown = nr_structs, lastunknown;
867         bool dna_error = false;
868
869         while (unknown) {
870                 lastunknown = unknown;
871                 unknown = 0;
872
873                 /* check all structs... */
874                 for (int a = 0; a < nr_structs; a++) {
875                         const short *structpoin = structs[a];
876                         const int    structtype = structpoin[0];
877
878                         /* when length is not known... */
879                         if (types_size_native[structtype] == 0) {
880
881                                 const short *sp = structpoin + 2;
882                                 int size_native = 0;
883                                 int size_32 = 0;
884                                 int size_64 = 0;
885                                 bool has_pointer = false;
886
887                                 /* check all elements in struct */
888                                 for (int b = 0; b < structpoin[1]; b++, sp += 2) {
889                                         int type = sp[0];
890                                         const char *cp = names[sp[1]];
891
892                                         int namelen = (int)strlen(cp);
893                                         /* is it a pointer or function pointer? */
894                                         if (cp[0] == '*' || cp[1] == '*') {
895                                                 has_pointer = 1;
896                                                 /* has the name an extra length? (array) */
897                                                 int mul = 1;
898                                                 if (cp[namelen - 1] == ']') {
899                                                         mul = DNA_elem_array_size(cp);
900                                                 }
901
902                                                 if (mul == 0) {
903                                                         fprintf(stderr, "Zero array size found or could not parse %s: '%.*s'\n",
904                                                                 types[structtype], namelen + 1, cp);
905                                                         dna_error = 1;
906                                                 }
907
908                                                 /* 4-8 aligned/ */
909                                                 if (sizeof(void *) == 4) {
910                                                         if (size_native % 4) {
911                                                                 fprintf(stderr, "Align pointer error in struct (size_native 4): %s %s\n",
912                                                                         types[structtype], cp);
913                                                                 dna_error = 1;
914                                                         }
915                                                 }
916                                                 else {
917                                                         if (size_native % 8) {
918                                                                 fprintf(stderr, "Align pointer error in struct (size_native 8): %s %s\n",
919                                                                         types[structtype], cp);
920                                                                 dna_error = 1;
921                                                         }
922                                                 }
923
924                                                 if (size_64 % 8) {
925                                                         fprintf(stderr, "Align pointer error in struct (size_64 8): %s %s\n",
926                                                                 types[structtype], cp);
927                                                         dna_error = 1;
928                                                 }
929
930                                                 size_native += sizeof(void *) * mul;
931                                                 size_32 += 4 * mul;
932                                                 size_64 += 8 * mul;
933
934                                         }
935                                         else if (cp[0] == '[') {
936                                                 /* parsing can cause names "var" and "[3]"
937                                                  * to be found for "float var [3]" */
938                                                 fprintf(stderr, "Parse error in struct, invalid member name: %s %s\n",
939                                                         types[structtype], cp);
940                                                 dna_error = 1;
941                                         }
942                                         else if (types_size_native[type]) {
943                                                 /* has the name an extra length? (array) */
944                                                 int mul = 1;
945                                                 if (cp[namelen - 1] == ']') {
946                                                         mul = DNA_elem_array_size(cp);
947                                                 }
948
949                                                 if (mul == 0) {
950                                                         fprintf(stderr, "Zero array size found or could not parse %s: '%.*s'\n",
951                                                                 types[structtype], namelen + 1, cp);
952                                                         dna_error = 1;
953                                                 }
954
955                                                 /* struct alignment */
956                                                 if (type >= firststruct) {
957                                                         if (sizeof(void *) == 8 && (size_native % 8) ) {
958                                                                 fprintf(stderr, "Align struct error: %s %s\n",
959                                                                         types[structtype], cp);
960                                                                 dna_error = 1;
961                                                         }
962                                                 }
963
964                                                 /* Check 2-4-8 aligned. */
965                                                 if (!check_field_alignment(firststruct, structtype, type, size_32, cp, "32 bit")) {
966                                                         dna_error = 1;
967                                                 }
968                                                 if (!check_field_alignment(firststruct, structtype, type, size_64, cp, "64 bit")) {
969                                                         dna_error = 1;
970                                                 }
971
972                                                 size_native += mul * types_size_native[type];
973                                                 size_32 += mul * types_size_32[type];
974                                                 size_64 += mul * types_size_64[type];
975
976                                         }
977                                         else {
978                                                 size_native = 0;
979                                                 size_32 = 0;
980                                                 size_64 = 0;
981                                                 break;
982                                         }
983                                 }
984
985                                 if (size_native == 0) {
986                                         unknown++;
987                                 }
988                                 else {
989                                         types_size_native[structtype] = size_native;
990                                         types_size_32[structtype] = size_32;
991                                         types_size_64[structtype] = size_64;
992                                         /* two ways to detect if a struct contains a pointer:
993                                          * has_pointer is set or size_native  doesn't match any of 32/64bit lengths*/
994                                         if (has_pointer || size_64 != size_native || size_32 != size_native) {
995                                                 if (size_64 % 8) {
996                                                         fprintf(stderr, "Sizeerror 8 in struct: %s (add %d bytes)\n",
997                                                                 types[structtype], size_64 % 8);
998                                                         dna_error = 1;
999                                                 }
1000                                         }
1001
1002                                         if (size_native % 4) {
1003                                                 fprintf(stderr, "Sizeerror 4 in struct: %s (add %d bytes)\n",
1004                                                        types[structtype], size_native % 4);
1005                                                 dna_error = 1;
1006                                         }
1007
1008                                 }
1009                         }
1010                 }
1011
1012                 if (unknown == lastunknown) {
1013                         break;
1014                 }
1015         }
1016
1017         if (unknown) {
1018                 fprintf(stderr, "ERROR: still %d structs unknown\n", unknown);
1019
1020                 if (debugSDNA) {
1021                         fprintf(stderr, "*** Known structs :\n");
1022
1023                         for (int a = 0; a < nr_structs; a++) {
1024                                 const short *structpoin = structs[a];
1025                                 const int    structtype = structpoin[0];
1026
1027                                 /* length unknown */
1028                                 if (types_size_native[structtype] != 0) {
1029                                         fprintf(stderr, "  %s\n", types[structtype]);
1030                                 }
1031                         }
1032                 }
1033
1034
1035                 fprintf(stderr, "*** Unknown structs :\n");
1036
1037                 for (int a = 0; a < nr_structs; a++) {
1038                         const short *structpoin = structs[a];
1039                         const int    structtype = structpoin[0];
1040
1041                         /* length unknown yet */
1042                         if (types_size_native[structtype] == 0) {
1043                                 fprintf(stderr, "  %s\n", types[structtype]);
1044                         }
1045                 }
1046
1047                 dna_error = 1;
1048         }
1049
1050         return dna_error;
1051 }
1052
1053 #define MAX_DNA_LINE_LENGTH 20
1054
1055 static void dna_write(FILE *file, const void *pntr, const int size)
1056 {
1057         static int linelength = 0;
1058         int i;
1059         const char *data;
1060
1061         data = (const char *)pntr;
1062
1063         for (i = 0; i < size; i++) {
1064                 fprintf(file, "%d, ", data[i]);
1065                 linelength++;
1066                 if (linelength >= MAX_DNA_LINE_LENGTH) {
1067                         fprintf(file, "\n");
1068                         linelength = 0;
1069                 }
1070         }
1071 }
1072
1073 void print_struct_sizes(void)
1074 {
1075         int a, unknown = nr_structs, structtype;
1076         /*int lastunknown;*/ /*UNUSED*/
1077         const short *structpoin;
1078         printf("\n\n*** All detected structs:\n");
1079
1080         while (unknown) {
1081                 /*lastunknown = unknown;*/ /*UNUSED*/
1082                 unknown = 0;
1083
1084                 /* check all structs... */
1085                 for (a = 0; a < nr_structs; a++) {
1086                         structpoin = structs[a];
1087                         structtype = structpoin[0];
1088                         printf("\t%s\t:%d\n", types[structtype], types_size_native[structtype]);
1089                 }
1090         }
1091
1092         printf("*** End of list\n");
1093
1094 }
1095
1096
1097 static int make_structDNA(const char *baseDirectory, FILE *file, FILE *file_offsets)
1098 {
1099         int i;
1100         const short *sp;
1101         /* str contains filenames. Since we now include paths, I stretched       */
1102         /* it a bit. Hope this is enough :) -nzc-                                */
1103         char str[SDNA_MAX_FILENAME_LENGTH];
1104         int firststruct;
1105
1106         if (debugSDNA > 0) {
1107                 fflush(stdout);
1108                 printf("Running makesdna at debug level %d\n", debugSDNA);
1109         }
1110
1111         mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
1112
1113         /* the longest known struct is 50k, so we assume 100k is sufficient! */
1114         structdata = MEM_callocN(maxdata, "structdata");
1115
1116         /* a maximum of 5000 variables, must be sufficient? */
1117         names = MEM_callocN(sizeof(char *) * maxnr, "names");
1118         types = MEM_callocN(sizeof(char *) * maxnr, "types");
1119         types_size_native = MEM_callocN(sizeof(short) * maxnr, "types_size_native");
1120         types_size_32 = MEM_callocN(sizeof(short) * maxnr, "types_size_32");
1121         types_size_64 = MEM_callocN(sizeof(short) * maxnr, "types_size_64");
1122         structs = MEM_callocN(sizeof(short *) * maxnr, "structs");
1123
1124         /* Build versioning data */
1125         DNA_alias_maps(
1126                 DNA_RENAME_ALIAS_FROM_STATIC,
1127                 &g_version_data.struct_map_alias_from_static,
1128                 NULL);
1129         DNA_alias_maps(
1130                 DNA_RENAME_STATIC_FROM_ALIAS,
1131                 &g_version_data.struct_map_static_from_alias,
1132                 &g_version_data.elem_map_static_from_alias);
1133
1134         /**
1135          * Insertion of all known types.
1136          *
1137          * \warning Order of function calls here must be aligned with #eSDNA_Type.
1138          * \warning uint is not allowed! use in structs an unsigned int.
1139          * \warning sizes must match #DNA_elem_type_size().
1140          */
1141         add_type("char", 1);     /* SDNA_TYPE_CHAR */
1142         add_type("uchar", 1);    /* SDNA_TYPE_UCHAR */
1143         add_type("short", 2);    /* SDNA_TYPE_SHORT */
1144         add_type("ushort", 2);   /* SDNA_TYPE_USHORT */
1145         add_type("int", 4);      /* SDNA_TYPE_INT */
1146
1147         /* note, long isn't supported,
1148          * these are place-holders to maintain alignment with eSDNA_Type*/
1149         add_type("long", 4);     /* SDNA_TYPE_LONG */
1150         add_type("ulong", 4);    /* SDNA_TYPE_ULONG */
1151
1152         add_type("float", 4);    /* SDNA_TYPE_FLOAT */
1153         add_type("double", 8);   /* SDNA_TYPE_DOUBLE */
1154         add_type("int64_t", 8);  /* SDNA_TYPE_INT64 */
1155         add_type("uint64_t", 8); /* SDNA_TYPE_UINT64 */
1156         add_type("void", 0);     /* SDNA_TYPE_VOID */
1157
1158         /* the defines above shouldn't be output in the padding file... */
1159         firststruct = nr_types;
1160
1161         /* add all include files defined in the global array                     */
1162         /* Since the internal file+path name buffer has limited length, I do a   */
1163         /* little test first...                                                  */
1164         /* Mind the breaking condition here!                                     */
1165         DEBUG_PRINTF(0, "\tStart of header scan:\n");
1166         for (i = 0; *(includefiles[i]) != '\0'; i++) {
1167                 sprintf(str, "%s%s", baseDirectory, includefiles[i]);
1168                 DEBUG_PRINTF(0, "\t|-- Converting %s\n", str);
1169                 if (convert_include(str)) {
1170                         return 1;
1171                 }
1172         }
1173         DEBUG_PRINTF(0, "\tFinished scanning %d headers.\n", i);
1174
1175         if (calculate_struct_sizes(firststruct)) {
1176                 /* error */
1177                 return 1;
1178         }
1179
1180         /* FOR DEBUG */
1181         if (debugSDNA > 1) {
1182                 int a, b;
1183                 /* short *elem; */
1184                 short num_types;
1185
1186                 printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
1187                 for (a = 0; a < nr_names; a++) {
1188                         printf(" %s\n", names[a]);
1189                 }
1190                 printf("\n");
1191
1192                 sp = types_size_native;
1193                 for (a = 0; a < nr_types; a++, sp++) {
1194                         printf(" %s %d\n", types[a], *sp);
1195                 }
1196                 printf("\n");
1197
1198                 for (a = 0; a < nr_structs; a++) {
1199                         sp = structs[a];
1200                         printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1], types_size_native[sp[0]]);
1201                         num_types  = sp[1];
1202                         sp += 2;
1203                         /* ? num_types was elem? */
1204                         for (b = 0; b < num_types; b++, sp += 2) {
1205                                 printf("   %s %s\n", types[sp[0]], names[sp[1]]);
1206                         }
1207                 }
1208         }
1209
1210         /* file writing */
1211
1212         DEBUG_PRINTF(0, "Writing file ... ");
1213
1214         if (nr_names == 0 || nr_structs == 0) {
1215                 /* pass */
1216         }
1217         else {
1218                 const char nil_bytes[4] = {0};
1219                 int len, len_align;
1220
1221                 dna_write(file, "SDNA", 4);
1222
1223                 /* write names */
1224                 dna_write(file, "NAME", 4);
1225                 len = nr_names;
1226                 dna_write(file, &len, 4);
1227                 /* write array */
1228                 len = 0;
1229                 for (int nr = 0; nr < nr_names; nr++) {
1230                         int name_size = strlen(names[nr]) + 1;
1231                         dna_write(file, names[nr], name_size);
1232                         len += name_size;
1233                 }
1234                 len_align = (len + 3) & ~3;
1235                 if (len != len_align) {
1236                         dna_write(file, nil_bytes, len_align - len);
1237                 }
1238
1239                 /* write TYPES */
1240                 dna_write(file, "TYPE", 4);
1241                 len = nr_types;
1242                 dna_write(file, &len, 4);
1243                 /* write array */
1244                 len = 0;
1245                 for (int nr = 0; nr < nr_types; nr++) {
1246                         int type_size = strlen(types[nr]) + 1;
1247                         dna_write(file, types[nr], type_size);
1248                         len += type_size;
1249                 }
1250                 len_align = (len + 3) & ~3;
1251                 if (len != len_align) {
1252                         dna_write(file, nil_bytes, len_align - len);
1253                 }
1254
1255                 /* WRITE TYPELENGTHS */
1256                 dna_write(file, "TLEN", 4);
1257
1258                 len = 2 * nr_types;
1259                 if (nr_types & 1) {
1260                         len += 2;
1261                 }
1262                 dna_write(file, types_size_native, len);
1263
1264                 /* WRITE STRUCTS */
1265                 dna_write(file, "STRC", 4);
1266                 len = nr_structs;
1267                 dna_write(file, &len, 4);
1268
1269                 /* calc datablock size */
1270                 sp = structs[nr_structs - 1];
1271                 sp += 2 + 2 * (sp[1]);
1272                 len = (intptr_t) ((char *) sp - (char *) structs[0]);
1273                 len = (len + 3) & ~3;
1274
1275                 dna_write(file, structs[0], len);
1276
1277                 /* a simple dna padding test */
1278                 if (0) {
1279                         FILE *fp;
1280                         int a;
1281
1282                         fp = fopen("padding.c", "w");
1283                         if (fp == NULL) {
1284                                 /* pass */
1285                         }
1286                         else {
1287
1288                                 /* add all include files defined in the global array */
1289                                 for (i = 0; *(includefiles[i]) != '\0'; i++) {
1290                                         fprintf(fp, "#include \"%s%s\"\n", baseDirectory, includefiles[i]);
1291                                 }
1292
1293                                 fprintf(fp, "main() {\n");
1294                                 sp = types_size_native;
1295                                 sp += firststruct;
1296                                 for (a = firststruct; a < nr_types; a++, sp++) {
1297                                         if (*sp) {
1298                                                 fprintf(fp, "\tif (sizeof(struct %s) - %d) printf(\"ALIGN ERROR:", types[a], *sp);
1299                                                 fprintf(fp, "%%d %s %d ", types[a], *sp);
1300                                                 fprintf(fp, "\\n\",  sizeof(struct %s) - %d);\n", types[a], *sp);
1301                                         }
1302                                 }
1303                                 fprintf(fp, "}\n");
1304                                 fclose(fp);
1305                         }
1306                 }
1307                 /*      end end padding test */
1308         }
1309
1310         /* write a simple enum with all structs offsets,
1311          * should only be accessed via SDNA_TYPE_FROM_STRUCT macro */
1312         {
1313                 fprintf(file_offsets, "#define SDNA_TYPE_FROM_STRUCT(id) _SDNA_TYPE_##id\n");
1314                 fprintf(file_offsets, "enum {\n");
1315                 for (i = 0; i < nr_structs; i++) {
1316                         const short *structpoin = structs[i];
1317                         const int    structtype = structpoin[0];
1318                         fprintf(file_offsets, "\t_SDNA_TYPE_%s = %d,\n", version_struct_alias_from_static(types[structtype]), i);
1319                 }
1320                 fprintf(file_offsets, "\tSDNA_TYPE_MAX = %d,\n", nr_structs);
1321                 fprintf(file_offsets, "};\n");
1322         }
1323
1324         /* Check versioning errors which could cause duplicate names,
1325          * do last because names are stripped. */
1326         {
1327                 GSet *names_unique = BLI_gset_str_new_ex(__func__, 512);
1328                 for (int struct_nr = 0; struct_nr < nr_structs; struct_nr++) {
1329                         sp = structs[struct_nr];
1330                         const char *struct_name = types[sp[0]];
1331                         const int len = sp[1];
1332                         sp += 2;
1333                         for (int a = 0; a < len; a++, sp += 2) {
1334                                 char *name = names[sp[1]];
1335                                 DNA_elem_id_strip(name);
1336                                 if (!BLI_gset_add(names_unique, name)) {
1337                                         fprintf(stderr, "Error: duplicate name found '%s.%s', "
1338                                                "likely cause is 'dna_rename_defs.h'\n",
1339                                                struct_name, name);
1340                                         return 1;
1341                                 }
1342                         }
1343                         BLI_gset_clear(names_unique, NULL);
1344                 }
1345                 BLI_gset_free(names_unique, NULL);
1346         }
1347
1348         MEM_freeN(structdata);
1349         MEM_freeN(names);
1350         MEM_freeN(types);
1351         MEM_freeN(types_size_native);
1352         MEM_freeN(types_size_32);
1353         MEM_freeN(types_size_64);
1354         MEM_freeN(structs);
1355
1356         BLI_memarena_free(mem_arena);
1357
1358         BLI_ghash_free(g_version_data.struct_map_alias_from_static, NULL, NULL);
1359         BLI_ghash_free(g_version_data.struct_map_static_from_alias, NULL, NULL);
1360         BLI_ghash_free(g_version_data.elem_map_static_from_alias, MEM_freeN, NULL);
1361
1362         DEBUG_PRINTF(0, "done.\n");
1363
1364         return 0;
1365 }
1366
1367 /** \} */
1368
1369 /* end make DNA. */
1370
1371 /* -------------------------------------------------------------------- */
1372 /** \name Main Function
1373  * \{ */
1374
1375 static void make_bad_file(const char *file, int line)
1376 {
1377         FILE *fp = fopen(file, "w");
1378         fprintf(fp,
1379                "#error \"Error! can't make correct DNA.c file from %s:%d, check alignment.\"\n",
1380                __FILE__, line);
1381         fclose(fp);
1382 }
1383
1384 #ifndef BASE_HEADER
1385 #define BASE_HEADER "../"
1386 #endif
1387
1388 int main(int argc, char **argv)
1389 {
1390         int return_status = 0;
1391
1392         if (argc != 3 && argc != 4) {
1393                 printf("Usage: %s dna.c dna_struct_offsets.h [base directory]\n", argv[0]);
1394                 return_status = 1;
1395         }
1396         else {
1397                 FILE *file_dna         = fopen(argv[1], "w");
1398                 FILE *file_dna_offsets = fopen(argv[2], "w");
1399                 if (!file_dna) {
1400                         printf("Unable to open file: %s\n", argv[1]);
1401                         return_status = 1;
1402                 }
1403                 else if (!file_dna_offsets) {
1404                         printf("Unable to open file: %s\n", argv[2]);
1405                         return_status = 1;
1406                 }
1407                 else {
1408                         const char *baseDirectory;
1409
1410                         if (argc == 4) {
1411                                 baseDirectory = argv[3];
1412                         }
1413                         else {
1414                                 baseDirectory = BASE_HEADER;
1415                         }
1416
1417                         fprintf(file_dna, "extern const unsigned char DNAstr[];\n");
1418                         fprintf(file_dna, "const unsigned char DNAstr[] = {\n");
1419                         if (make_structDNA(baseDirectory, file_dna, file_dna_offsets)) {
1420                                 /* error */
1421                                 fclose(file_dna);
1422                                 file_dna = NULL;
1423                                 make_bad_file(argv[1], __LINE__);
1424                                 return_status = 1;
1425                         }
1426                         else {
1427                                 fprintf(file_dna, "};\n");
1428                                 fprintf(file_dna, "extern const int DNAlen;\n");
1429                                 fprintf(file_dna, "const int DNAlen = sizeof(DNAstr);\n");
1430                         }
1431                 }
1432
1433                 if (file_dna) {
1434                         fclose(file_dna);
1435                 }
1436                 if (file_dna_offsets) {
1437                         fclose(file_dna_offsets);
1438                 }
1439         }
1440
1441
1442         return return_status;
1443 }
1444
1445 /* handy but fails on struct bounds which makesdna doesn't care about
1446  * with quite the same strictness as GCC does */
1447 #if 0
1448 /* include files for automatic dependencies */
1449
1450 /* extra safety check that we are aligned,
1451  * warnings here are easier to fix the makesdna's */
1452 #ifdef __GNUC__
1453 #  pragma GCC diagnostic error "-Wpadded"
1454 #endif
1455
1456 #endif /* if 0 */
1457
1458 /* even though DNA supports, 'long' shouldn't be used since it can be either 32 or 64bit,
1459  * use int or int64_t instead.
1460  * Only valid use would be as a runtime variable if an API expected a long,
1461  * but so far we dont have this happening. */
1462 #ifdef __GNUC__
1463 #  pragma GCC poison long
1464 #endif
1465
1466 #include "DNA_listBase.h"
1467 #include "DNA_vec_types.h"
1468 #include "DNA_ID.h"
1469 #include "DNA_ipo_types.h"
1470 #include "DNA_key_types.h"
1471 #include "DNA_text_types.h"
1472 #include "DNA_packedFile_types.h"
1473 #include "DNA_camera_types.h"
1474 #include "DNA_image_types.h"
1475 #include "DNA_texture_types.h"
1476 #include "DNA_light_types.h"
1477 #include "DNA_material_types.h"
1478 #include "DNA_vfont_types.h"
1479 #include "DNA_meta_types.h"
1480 #include "DNA_curve_types.h"
1481 #include "DNA_mesh_types.h"
1482 #include "DNA_meshdata_types.h"
1483 #include "DNA_modifier_types.h"
1484 #include "DNA_lattice_types.h"
1485 #include "DNA_object_types.h"
1486 #include "DNA_object_force_types.h"
1487 #include "DNA_object_fluidsim_types.h"
1488 #include "DNA_world_types.h"
1489 #include "DNA_scene_types.h"
1490 #include "DNA_view3d_types.h"
1491 #include "DNA_view2d_types.h"
1492 #include "DNA_space_types.h"
1493 #include "DNA_userdef_types.h"
1494 #include "DNA_screen_types.h"
1495 #include "DNA_sdna_types.h"
1496 #include "DNA_fileglobal_types.h"
1497 #include "DNA_sequence_types.h"
1498 #include "DNA_effect_types.h"
1499 #include "DNA_outliner_types.h"
1500 #include "DNA_sound_types.h"
1501 #include "DNA_collection_types.h"
1502 #include "DNA_armature_types.h"
1503 #include "DNA_action_types.h"
1504 #include "DNA_constraint_types.h"
1505 #include "DNA_nla_types.h"
1506 #include "DNA_node_types.h"
1507 #include "DNA_color_types.h"
1508 #include "DNA_brush_types.h"
1509 #include "DNA_customdata_types.h"
1510 #include "DNA_particle_types.h"
1511 #include "DNA_cloth_types.h"
1512 #include "DNA_gpencil_types.h"
1513 #include "DNA_gpencil_modifier_types.h"
1514 #include "DNA_shader_fx_types.h"
1515 #include "DNA_windowmanager_types.h"
1516 #include "DNA_anim_types.h"
1517 #include "DNA_boid_types.h"
1518 #include "DNA_smoke_types.h"
1519 #include "DNA_speaker_types.h"
1520 #include "DNA_movieclip_types.h"
1521 #include "DNA_tracking_types.h"
1522 #include "DNA_dynamicpaint_types.h"
1523 #include "DNA_mask_types.h"
1524 #include "DNA_rigidbody_types.h"
1525 #include "DNA_freestyle_types.h"
1526 #include "DNA_linestyle_types.h"
1527 #include "DNA_cachefile_types.h"
1528 #include "DNA_layer_types.h"
1529 #include "DNA_workspace_types.h"
1530 #include "DNA_lightprobe_types.h"
1531
1532 /* end of list */
1533
1534 /** \} */
1535
1536
1537 /* -------------------------------------------------------------------- */
1538 /** \name DNA Renaming Sanity Check
1539  *
1540  * Without this it's possible to reference struct members that don't exist,
1541  * breaking backward & forward compatibility.
1542  *
1543  * \{ */
1544
1545 static void UNUSED_FUNCTION(dna_rename_defs_ensure)(void)
1546 {
1547 #define DNA_STRUCT_RENAME(old, new) (void)sizeof(new);
1548 #define DNA_STRUCT_RENAME_ELEM(struct_name, old, new) (void)offsetof(struct_name, new);
1549 #include "dna_rename_defs.h"
1550 #undef DNA_STRUCT_RENAME
1551 #undef DNA_STRUCT_RENAME_ELEM
1552 }
1553
1554 /** \} */