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