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