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