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