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