Merge branch 'blender2.7'
[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 "BLI_sys_types.h"  /* for intptr_t support */
50 #include "BLI_memarena.h"
51
52 #include "dna_utils.h"
53
54 #define SDNA_MAX_FILENAME_LENGTH 255
55
56 /* Included the path relative from /source/blender/ here, so we can move     */
57 /* headers around with more freedom.                                         */
58 static const char *includefiles[] = {
59
60         /* if you add files here, please add them at the end
61          * of makesdna.c (this file) as well */
62         "DNA_listBase.h",
63         "DNA_vec_types.h",
64         "DNA_ID.h",
65         "DNA_ipo_types.h",
66         "DNA_key_types.h",
67         "DNA_text_types.h",
68         "DNA_packedFile_types.h",
69         "DNA_gpu_types.h",
70         "DNA_camera_types.h",
71         "DNA_image_types.h",
72         "DNA_texture_types.h",
73         "DNA_lamp_types.h",
74         "DNA_material_types.h",
75         "DNA_vfont_types.h",
76         "DNA_meta_types.h",
77         "DNA_curve_types.h",
78         "DNA_mesh_types.h",
79         "DNA_meshdata_types.h",
80         "DNA_modifier_types.h",
81         "DNA_lattice_types.h",
82         "DNA_object_types.h",
83         "DNA_object_force_types.h",
84         "DNA_object_fluidsim_types.h",
85         "DNA_world_types.h",
86         "DNA_scene_types.h",
87         "DNA_view3d_types.h",
88         "DNA_view2d_types.h",
89         "DNA_space_types.h",
90         "DNA_userdef_types.h",
91         "DNA_screen_types.h",
92         "DNA_sdna_types.h",
93         "DNA_fileglobal_types.h",
94         "DNA_sequence_types.h",
95         "DNA_effect_types.h",
96         "DNA_outliner_types.h",
97         "DNA_sound_types.h",
98         "DNA_collection_types.h",
99         "DNA_armature_types.h",
100         "DNA_action_types.h",
101         "DNA_constraint_types.h",
102         "DNA_nla_types.h",
103         "DNA_node_types.h",
104         "DNA_color_types.h",
105         "DNA_brush_types.h",
106         "DNA_customdata_types.h",
107         "DNA_particle_types.h",
108         "DNA_cloth_types.h",
109         "DNA_gpencil_types.h",
110         "DNA_gpencil_modifier_types.h",
111         "DNA_shader_fx_types.h",
112         "DNA_windowmanager_types.h",
113         "DNA_anim_types.h",
114         "DNA_boid_types.h",
115         "DNA_smoke_types.h",
116         "DNA_speaker_types.h",
117         "DNA_movieclip_types.h",
118         "DNA_tracking_types.h",
119         "DNA_dynamicpaint_types.h",
120         "DNA_mask_types.h",
121         "DNA_rigidbody_types.h",
122         "DNA_freestyle_types.h",
123         "DNA_linestyle_types.h",
124         "DNA_cachefile_types.h",
125         "DNA_layer_types.h",
126         "DNA_workspace_types.h",
127         "DNA_lightprobe_types.h",
128
129         /* see comment above before editing! */
130
131         /* empty string to indicate end of includefiles */
132         "",
133 };
134
135 MemArena *mem_arena = NULL;
136
137 static int maxdata = 500000, maxnr = 50000;
138 static int nr_names = 0;
139 static int nr_types = 0;
140 static int nr_structs = 0;
141 /** at address names[a] is string a */
142 static char **names;
143 /** at address types[a] is string a */
144 static char **types;
145 /** at typelens[a] is the length of type 'a' on this systems bitness (32 or 64) */
146 static short *typelens_native;
147 /** contains sizes as they are calculated on 32 bit systems */
148 static short *typelens_32;
149 /** contains sizes as they are calculated on 64 bit systems */
150 static short *typelens_64;
151 /** at sp = structs[a] is the first address of a struct definition
152  * sp[0] is type number
153  * sp[1] is amount of elements
154  * sp[2] sp[3] is typenr,  namenr (etc) */
155 static short **structs, *structdata;
156 /**
157  * Variable to control debug output of makesdna.
158  * debugSDNA:
159  * - 0 = no output, except errors
160  * - 1 = detail actions
161  * - 2 = full trace, tell which names and types were found
162  * - 4 = full trace, plus all gritty details
163  */
164 static int debugSDNA = 0;
165 static int additional_slen_offset;
166
167 #define DEBUG_PRINTF(debug_level, ...) \
168         { if (debugSDNA > debug_level) { printf(__VA_ARGS__); } } ((void)0)
169
170
171 /* stub for BLI_abort() */
172 #ifndef NDEBUG
173 void BLI_system_backtrace(FILE *fp);
174 void BLI_system_backtrace(FILE *fp)
175 {
176        (void)fp;
177 }
178 #endif
179
180 /* ************************************************************************** */
181 /* Functions                                                                  */
182 /* ************************************************************************** */
183
184 /**
185  * Add type \c str to struct indexed by \c len, if it was not yet found.
186  * \param str: char
187  * \param len: int
188  */
189 static int add_type(const char *str, int len);
190
191 /**
192  * Add variable \c str to
193  * \param str:
194  */
195 static int add_name(const char *str);
196
197 /**
198  * Search whether this structure type was already found, and if not,
199  * add it.
200  */
201 static short *add_struct(int namecode);
202
203 /**
204  * Remove comments from this buffer. Assumes that the buffer refers to
205  * ascii-code text.
206  */
207 static int preprocess_include(char *maindata, int len);
208
209 /**
210  * Scan this file for serializable types.
211  */
212 static int convert_include(const char *filename);
213
214 /**
215  * Determine how many bytes are needed for each struct.
216  */
217 static int calculate_structlens(int);
218
219 /**
220  * Construct the DNA.c file
221  */
222 static void dna_write(FILE *file, const void *pntr, const int size);
223
224 /**
225  * Report all structures found so far, and print their lengths.
226  */
227 void printStructLengths(void);
228
229
230
231 /* ************************************************************************** */
232 /* Implementation                                                             */
233 /* ************************************************************************** */
234
235 /* ************************* MAKEN DNA ********************** */
236
237 static int add_type(const char *str, int len)
238 {
239         int nr;
240         char *cp;
241
242         /* first do validity check */
243         if (str[0] == 0) {
244                 return -1;
245         }
246         else if (strchr(str, '*')) {
247                 /* note: this is valid C syntax but we can't parse, complain!
248                  * 'struct SomeStruct* somevar;' <-- correct but we cant handle right now. */
249                 return -1;
250         }
251
252         /* search through type array */
253         for (nr = 0; nr < nr_types; nr++) {
254                 if (strcmp(str, types[nr]) == 0) {
255                         if (len) {
256                                 typelens_native[nr] = len;
257                                 typelens_32[nr] = len;
258                                 typelens_64[nr] = len;
259                         }
260                         return nr;
261                 }
262         }
263
264         /* append new type */
265         const int str_size = strlen(str) + 1;
266         cp = BLI_memarena_alloc(mem_arena, str_size);
267         memcpy(cp, str, str_size);
268         types[nr_types] = cp;
269         typelens_native[nr_types] = len;
270         typelens_32[nr_types] = len;
271         typelens_64[nr_types] = len;
272
273         if (nr_types >= maxnr) {
274                 printf("too many types\n");
275                 return nr_types - 1;
276         }
277         nr_types++;
278
279         return nr_types - 1;
280 }
281
282
283 /**
284  *
285  * Because of the weird way of tokenizing, we have to 'cast' function
286  * pointers to ... (*f)(), whatever the original signature. In fact,
287  * we add name and type at the same time... There are two special
288  * cases, unfortunately. These are explicitly checked.
289  *
290  * */
291 static int add_name(const char *str)
292 {
293         int nr, i, j, k;
294         char *cp;
295         char buf[255]; /* stupid limit, change it :) */
296         const char *name;
297
298         additional_slen_offset = 0;
299
300         if (str[0] == 0 /*  || (str[1] == 0) */) return -1;
301
302         if (str[0] == '(' && str[1] == '*') {
303                 /* we handle function pointer and special array cases here, e.g.
304                  * void (*function)(...) and float (*array)[..]. the array case
305                  * name is still converted to (array *)() though because it is that
306                  * way in old dna too, and works correct with elementsize() */
307                 int isfuncptr = (strchr(str + 1, '(')) != NULL;
308
309                 DEBUG_PRINTF(3, "\t\t\t\t*** Function pointer or multidim array pointer found\n");
310                 /* functionpointer: transform the type (sometimes) */
311                 i = 0;
312
313                 while (str[i] != ')') {
314                         buf[i] = str[i];
315                         i++;
316                 }
317
318                 /* Another number we need is the extra slen offset. This extra
319                  * offset is the overshoot after a space. If there is no
320                  * space, no overshoot should be calculated. */
321                 j = i; /* j at first closing brace */
322
323                 DEBUG_PRINTF(3, "first brace after offset %d\n", i);
324
325                 j++; /* j beyond closing brace ? */
326                 while ((str[j] != 0) && (str[j] != ')')) {
327                         DEBUG_PRINTF(3, "seen %c (%d)\n", str[j], str[j]);
328                         j++;
329                 }
330                 DEBUG_PRINTF(3, "seen %c (%d)\n" "special after offset%d\n", str[j], str[j], j);
331
332                 if (!isfuncptr) {
333                         /* multidimensional array pointer case */
334                         if (str[j] == 0) {
335                                 DEBUG_PRINTF(3, "offsetting for multidim array pointer\n");
336                         }
337                         else
338                                 printf("Error during tokening multidim array pointer\n");
339                 }
340                 else if (str[j] == 0) {
341                         DEBUG_PRINTF(3, "offsetting for space\n");
342                         /* get additional offset */
343                         k = 0;
344                         while (str[j] != ')') {
345                                 j++;
346                                 k++;
347                         }
348                         DEBUG_PRINTF(3, "extra offset %d\n", k);
349                         additional_slen_offset = k;
350                 }
351                 else if (str[j] == ')') {
352                         DEBUG_PRINTF(3, "offsetting for brace\n");
353                         ; /* don't get extra offset */
354                 }
355                 else {
356                         printf("Error during tokening function pointer argument list\n");
357                 }
358
359                 /*
360                  * Put )(void) at the end? Maybe )(). Should check this with
361                  * old sdna. Actually, sometimes )(), sometimes )(void...)
362                  * Alas.. such is the nature of braindamage :(
363                  *
364                  * Sorted it out: always do )(), except for headdraw and
365                  * windraw, part of ScrArea. This is important, because some
366                  * linkers will treat different fp's differently when called
367                  * !!! This has to do with interference in byte-alignment and
368                  * the way args are pushed on the stack.
369                  *
370                  * */
371                 buf[i] = 0;
372                 DEBUG_PRINTF(3, "Name before chomping: %s\n", buf);
373                 if ((strncmp(buf, "(*headdraw", 10) == 0) ||
374                     (strncmp(buf, "(*windraw", 9) == 0) )
375                 {
376                         buf[i] = ')';
377                         buf[i + 1] = '(';
378                         buf[i + 2] = 'v';
379                         buf[i + 3] = 'o';
380                         buf[i + 4] = 'i';
381                         buf[i + 5] = 'd';
382                         buf[i + 6] = ')';
383                         buf[i + 7] = 0;
384                 }
385                 else {
386                         buf[i] = ')';
387                         buf[i + 1] = '(';
388                         buf[i + 2] = ')';
389                         buf[i + 3] = 0;
390                 }
391                 /* now precede with buf*/
392                 DEBUG_PRINTF(3, "\t\t\t\t\tProposing fp name %s\n", buf);
393                 name = buf;
394         }
395         else {
396                 /* normal field: old code */
397                 name = str;
398         }
399
400         /* search name array */
401         for (nr = 0; nr < nr_names; nr++) {
402                 if (strcmp(name, names[nr]) == 0) {
403                         return nr;
404                 }
405         }
406
407         /* append new type */
408         const int name_size = strlen(name) + 1;
409         cp = BLI_memarena_alloc(mem_arena, name_size);
410         memcpy(cp, name, name_size);
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                                         DEBUG_PRINTF(1, "\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                                                         DEBUG_PRINTF(1, "\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 (names[name] != NULL) {
678                                                                                         DEBUG_PRINTF(1, "%s |", names[name]);
679                                                                                 }
680
681                                                                                 structpoin[1]++;
682                                                                                 sp += 2;
683
684                                                                                 md1 += slen;
685                                                                                 break;
686                                                                         }
687
688
689                                                                         name = add_name(md1);
690                                                                         slen += additional_slen_offset;
691
692                                                                         sp[0] = type;
693                                                                         sp[1] = name;
694                                                                         if (names[name] != NULL) {
695                                                                                 DEBUG_PRINTF(1, "%s ||", names[name]);
696                                                                         }
697
698                                                                         structpoin[1]++;
699                                                                         sp += 2;
700
701                                                                         md1 += slen;
702                                                                 }
703                                                                 md1++;
704                                                         }
705
706                                                         DEBUG_PRINTF(1, ")\n");
707
708                                                 }
709                                                 md1++;
710                                         }
711                                 }
712                         }
713                 }
714                 count++;
715                 md++;
716         }
717
718         MEM_freeN(maindata);
719
720         return 0;
721 }
722
723 static bool check_field_alignment(int firststruct, int structtype, int type, int len,
724                                   const char *name, const char *detail)
725 {
726         bool result = true;
727         if (type < firststruct && typelens_native[type] > 4 && (len % 8)) {
728                 fprintf(stderr, "Align 8 error (%s) in struct: %s %s (add %d padding bytes)\n",
729                         detail, types[structtype], name, len % 8);
730                 result = false;
731         }
732         if (typelens_native[type] > 3 && (len % 4) ) {
733                 fprintf(stderr, "Align 4 error (%s) in struct: %s %s (add %d padding bytes)\n",
734                         detail, types[structtype], name, len % 4);
735                 result = false;
736         }
737         if (typelens_native[type] == 2 && (len % 2) ) {
738                 fprintf(stderr, "Align 2 error (%s) in struct: %s %s (add %d padding bytes)\n",
739                         detail, types[structtype], name, len % 2);
740                 result = false;
741         }
742         return result;
743 }
744
745 static int calculate_structlens(int firststruct)
746 {
747         int unknown = nr_structs, lastunknown;
748         bool dna_error = false;
749
750         while (unknown) {
751                 lastunknown = unknown;
752                 unknown = 0;
753
754                 /* check all structs... */
755                 for (int a = 0; a < nr_structs; a++) {
756                         const short *structpoin = structs[a];
757                         const int    structtype = structpoin[0];
758
759                         /* when length is not known... */
760                         if (typelens_native[structtype] == 0) {
761
762                                 const short *sp = structpoin + 2;
763                                 int len_native = 0;
764                                 int len_32 = 0;
765                                 int len_64 = 0;
766                                 bool has_pointer = false;
767
768                                 /* check all elements in struct */
769                                 for (int b = 0; b < structpoin[1]; b++, sp += 2) {
770                                         int type = sp[0];
771                                         const char *cp = names[sp[1]];
772
773                                         int namelen = (int)strlen(cp);
774                                         /* is it a pointer or function pointer? */
775                                         if (cp[0] == '*' || cp[1] == '*') {
776                                                 has_pointer = 1;
777                                                 /* has the name an extra length? (array) */
778                                                 int mul = 1;
779                                                 if (cp[namelen - 1] == ']') {
780                                                         mul = DNA_elem_array_size(cp);
781                                                 }
782
783                                                 if (mul == 0) {
784                                                         fprintf(stderr, "Zero array size found or could not parse %s: '%.*s'\n",
785                                                                 types[structtype], namelen + 1, cp);
786                                                         dna_error = 1;
787                                                 }
788
789                                                 /* 4-8 aligned/ */
790                                                 if (sizeof(void *) == 4) {
791                                                         if (len_native % 4) {
792                                                                 fprintf(stderr, "Align pointer error in struct (len_native 4): %s %s\n",
793                                                                         types[structtype], cp);
794                                                                 dna_error = 1;
795                                                         }
796                                                 }
797                                                 else {
798                                                         if (len_native % 8) {
799                                                                 fprintf(stderr, "Align pointer error in struct (len_native 8): %s %s\n",
800                                                                         types[structtype], cp);
801                                                                 dna_error = 1;
802                                                         }
803                                                 }
804
805                                                 if (len_64 % 8) {
806                                                         fprintf(stderr, "Align pointer error in struct (len_64 8): %s %s\n",
807                                                                 types[structtype], cp);
808                                                         dna_error = 1;
809                                                 }
810
811                                                 len_native += sizeof(void *) * mul;
812                                                 len_32 += 4 * mul;
813                                                 len_64 += 8 * mul;
814
815                                         }
816                                         else if (cp[0] == '[') {
817                                                 /* parsing can cause names "var" and "[3]"
818                                                  * to be found for "float var [3]" */
819                                                 fprintf(stderr, "Parse error in struct, invalid member name: %s %s\n",
820                                                         types[structtype], cp);
821                                                 dna_error = 1;
822                                         }
823                                         else if (typelens_native[type]) {
824                                                 /* has the name an extra length? (array) */
825                                                 int mul = 1;
826                                                 if (cp[namelen - 1] == ']') {
827                                                         mul = DNA_elem_array_size(cp);
828                                                 }
829
830                                                 if (mul == 0) {
831                                                         fprintf(stderr, "Zero array size found or could not parse %s: '%.*s'\n",
832                                                                 types[structtype], namelen + 1, cp);
833                                                         dna_error = 1;
834                                                 }
835
836                                                 /* struct alignment */
837                                                 if (type >= firststruct) {
838                                                         if (sizeof(void *) == 8 && (len_native % 8) ) {
839                                                                 fprintf(stderr, "Align struct error: %s %s\n",
840                                                                         types[structtype], cp);
841                                                                 dna_error = 1;
842                                                         }
843                                                 }
844
845                                                 /* Check 2-4-8 aligned. */
846                                                 if (!check_field_alignment(firststruct, structtype, type, len_32, cp, "32 bit")) {
847                                                         dna_error = 1;
848                                                 }
849                                                 if (!check_field_alignment(firststruct, structtype, type, len_64, cp, "64 bit")) {
850                                                         dna_error = 1;
851                                                 }
852
853                                                 len_native += mul * typelens_native[type];
854                                                 len_32 += mul * typelens_32[type];
855                                                 len_64 += mul * typelens_64[type];
856
857                                         }
858                                         else {
859                                                 len_native = 0;
860                                                 len_32 = 0;
861                                                 len_64 = 0;
862                                                 break;
863                                         }
864                                 }
865
866                                 if (len_native == 0) {
867                                         unknown++;
868                                 }
869                                 else {
870                                         typelens_native[structtype] = len_native;
871                                         typelens_32[structtype] = len_32;
872                                         typelens_64[structtype] = len_64;
873                                         /* two ways to detect if a struct contains a pointer:
874                                          * has_pointer is set or len_native  doesn't match any of 32/64bit lengths*/
875                                         if (has_pointer || len_64 != len_native || len_32 != len_native) {
876                                                 if (len_64 % 8) {
877                                                         fprintf(stderr, "Sizeerror 8 in struct: %s (add %d bytes)\n",
878                                                                 types[structtype], len_64 % 8);
879                                                         dna_error = 1;
880                                                 }
881                                         }
882
883                                         if (len_native % 4) {
884                                                 fprintf(stderr, "Sizeerror 4 in struct: %s (add %d bytes)\n",
885                                                        types[structtype], len_native % 4);
886                                                 dna_error = 1;
887                                         }
888
889                                 }
890                         }
891                 }
892
893                 if (unknown == lastunknown) break;
894         }
895
896         if (unknown) {
897                 fprintf(stderr, "ERROR: still %d structs unknown\n", unknown);
898
899                 if (debugSDNA) {
900                         fprintf(stderr, "*** Known structs :\n");
901
902                         for (int a = 0; a < nr_structs; a++) {
903                                 const short *structpoin = structs[a];
904                                 const int    structtype = structpoin[0];
905
906                                 /* length unknown */
907                                 if (typelens_native[structtype] != 0) {
908                                         fprintf(stderr, "  %s\n", types[structtype]);
909                                 }
910                         }
911                 }
912
913
914                 fprintf(stderr, "*** Unknown 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 yet */
921                         if (typelens_native[structtype] == 0) {
922                                 fprintf(stderr, "  %s\n", types[structtype]);
923                         }
924                 }
925
926                 dna_error = 1;
927         }
928
929         return(dna_error);
930 }
931
932 #define MAX_DNA_LINE_LENGTH 20
933
934 static void dna_write(FILE *file, const void *pntr, const int size)
935 {
936         static int linelength = 0;
937         int i;
938         const char *data;
939
940         data = (const char *)pntr;
941
942         for (i = 0; i < size; i++) {
943                 fprintf(file, "%d, ", data[i]);
944                 linelength++;
945                 if (linelength >= MAX_DNA_LINE_LENGTH) {
946                         fprintf(file, "\n");
947                         linelength = 0;
948                 }
949         }
950 }
951
952 void printStructLengths(void)
953 {
954         int a, unknown = nr_structs, structtype;
955         /*int lastunknown;*/ /*UNUSED*/
956         const short *structpoin;
957         printf("\n\n*** All detected structs:\n");
958
959         while (unknown) {
960                 /*lastunknown = unknown;*/ /*UNUSED*/
961                 unknown = 0;
962
963                 /* check all structs... */
964                 for (a = 0; a < nr_structs; a++) {
965                         structpoin = structs[a];
966                         structtype = structpoin[0];
967                         printf("\t%s\t:%d\n", types[structtype], typelens_native[structtype]);
968                 }
969         }
970
971         printf("*** End of list\n");
972
973 }
974
975
976 static int make_structDNA(const char *baseDirectory, FILE *file, FILE *file_offsets)
977 {
978         int i;
979         const short *sp;
980         /* str contains filenames. Since we now include paths, I stretched       */
981         /* it a bit. Hope this is enough :) -nzc-                                */
982         char str[SDNA_MAX_FILENAME_LENGTH];
983         int firststruct;
984
985         if (debugSDNA > 0) {
986                 fflush(stdout);
987                 printf("Running makesdna at debug level %d\n", debugSDNA);
988         }
989
990         mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
991
992         /* the longest known struct is 50k, so we assume 100k is sufficient! */
993         structdata = MEM_callocN(maxdata, "structdata");
994
995         /* a maximum of 5000 variables, must be sufficient? */
996         names = MEM_callocN(sizeof(char *) * maxnr, "names");
997         types = MEM_callocN(sizeof(char *) * maxnr, "types");
998         typelens_native = MEM_callocN(sizeof(short) * maxnr, "typelens_native");
999         typelens_32 = MEM_callocN(sizeof(short) * maxnr, "typelens_32");
1000         typelens_64 = MEM_callocN(sizeof(short) * maxnr, "typelens_64");
1001         structs = MEM_callocN(sizeof(short *) * maxnr, "structs");
1002
1003         /**
1004          * Insertion of all known types.
1005          *
1006          * \warning Order of function calls here must be aligned with #eSDNA_Type.
1007          * \warning uint is not allowed! use in structs an unsigned int.
1008          * \warning sizes must match #DNA_elem_type_size().
1009          */
1010         add_type("char", 1);     /* SDNA_TYPE_CHAR */
1011         add_type("uchar", 1);    /* SDNA_TYPE_UCHAR */
1012         add_type("short", 2);    /* SDNA_TYPE_SHORT */
1013         add_type("ushort", 2);   /* SDNA_TYPE_USHORT */
1014         add_type("int", 4);      /* SDNA_TYPE_INT */
1015
1016         /* note, long isn't supported,
1017          * these are place-holders to maintain alignment with eSDNA_Type*/
1018         add_type("long", 4);     /* SDNA_TYPE_LONG */
1019         add_type("ulong", 4);    /* SDNA_TYPE_ULONG */
1020
1021         add_type("float", 4);    /* SDNA_TYPE_FLOAT */
1022         add_type("double", 8);   /* SDNA_TYPE_DOUBLE */
1023         add_type("int64_t", 8);  /* SDNA_TYPE_INT64 */
1024         add_type("uint64_t", 8); /* SDNA_TYPE_UINT64 */
1025         add_type("void", 0);     /* SDNA_TYPE_VOID */
1026
1027         /* the defines above shouldn't be output in the padding file... */
1028         firststruct = nr_types;
1029
1030         /* add all include files defined in the global array                     */
1031         /* Since the internal file+path name buffer has limited length, I do a   */
1032         /* little test first...                                                  */
1033         /* Mind the breaking condition here!                                     */
1034         DEBUG_PRINTF(0, "\tStart of header scan:\n");
1035         for (i = 0; *(includefiles[i]) != '\0'; i++) {
1036                 sprintf(str, "%s%s", baseDirectory, includefiles[i]);
1037                 DEBUG_PRINTF(0, "\t|-- Converting %s\n", str);
1038                 if (convert_include(str)) {
1039                         return (1);
1040                 }
1041         }
1042         DEBUG_PRINTF(0, "\tFinished scanning %d headers.\n", i);
1043
1044         if (calculate_structlens(firststruct)) {
1045                 /* error */
1046                 return(1);
1047         }
1048
1049         /* FOR DEBUG */
1050         if (debugSDNA > 1) {
1051                 int a, b;
1052                 /* short *elem; */
1053                 short num_types;
1054
1055                 printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
1056                 for (a = 0; a < nr_names; a++) {
1057                         printf(" %s\n", names[a]);
1058                 }
1059                 printf("\n");
1060
1061                 sp = typelens_native;
1062                 for (a = 0; a < nr_types; a++, sp++) {
1063                         printf(" %s %d\n", types[a], *sp);
1064                 }
1065                 printf("\n");
1066
1067                 for (a = 0; a < nr_structs; a++) {
1068                         sp = structs[a];
1069                         printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1], typelens_native[sp[0]]);
1070                         num_types  = sp[1];
1071                         sp += 2;
1072                         /* ? num_types was elem? */
1073                         for (b = 0; b < num_types; b++, sp += 2) {
1074                                 printf("   %s %s\n", types[sp[0]], names[sp[1]]);
1075                         }
1076                 }
1077         }
1078
1079         /* file writing */
1080
1081         DEBUG_PRINTF(0, "Writing file ... ");
1082
1083         if (nr_names == 0 || nr_structs == 0) {
1084                 /* pass */
1085         }
1086         else {
1087                 const char nil_bytes[4] = {0};
1088                 int len, len_align;
1089
1090                 dna_write(file, "SDNA", 4);
1091
1092                 /* write names */
1093                 dna_write(file, "NAME", 4);
1094                 len = nr_names;
1095                 dna_write(file, &len, 4);
1096                 /* write array */
1097                 len = 0;
1098                 for (int nr = 0; nr < nr_names; nr++) {
1099                         int name_size = strlen(names[nr]) + 1;
1100                         dna_write(file, names[nr], name_size);
1101                         len += name_size;
1102                 }
1103                 len_align = (len + 3) & ~3;
1104                 if (len != len_align) {
1105                         dna_write(file, nil_bytes, len_align - len);
1106                 }
1107
1108                 /* write TYPES */
1109                 dna_write(file, "TYPE", 4);
1110                 len = nr_types;
1111                 dna_write(file, &len, 4);
1112                 /* write array */
1113                 len = 0;
1114                 for (int nr = 0; nr < nr_types; nr++) {
1115                         int type_size = strlen(types[nr]) + 1;
1116                         dna_write(file, types[nr], type_size);
1117                         len += type_size;
1118                 }
1119                 len_align = (len + 3) & ~3;
1120                 if (len != len_align) {
1121                         dna_write(file, nil_bytes, len_align - len);
1122                 }
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(structdata);
1192         MEM_freeN(names);
1193         MEM_freeN(types);
1194         MEM_freeN(typelens_native);
1195         MEM_freeN(typelens_32);
1196         MEM_freeN(typelens_64);
1197         MEM_freeN(structs);
1198
1199         BLI_memarena_free(mem_arena);
1200
1201         DEBUG_PRINTF(0, "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 */