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