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