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