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