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