4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
32 * Struct muncher for making SDNA
34 * Originally by Ton, some mods by Frank, and some cleaning and
37 * Makesdna creates a .c file with a long string of numbers that
38 * encode the Blender file format. It is fast, because it is basically
39 * a binary dump. There are some details to mind when reconstructing
40 * the file (endianness and byte-alignment).
42 * This little program scans all structs that need to be serialized,
43 * and determined the names and types of all members. It calculates
44 * how much memory (on disk or in ram) is needed to store that struct,
45 * and the offsets for reaching a particular one.
47 * There is a facility to get verbose output from sdna. Search for
48 * debugSDNA. This int can be set to 0 (no output) to some int. Higher
49 * numbers give more output.
52 #define DNA_VERSION_DATE "$Id$"
58 #include "MEM_guardedalloc.h"
59 #include "DNA_sdna_types.h"
65 #define SDNA_MAX_FILENAME_LENGTH 255
68 /* Included the path relative from /source/blender/ here, so we can move */
69 /* headers around with more freedom. */
70 char *includefiles[] = {
72 // if you add files here, please add them at the end
73 // of makesdna.c (this file) as well
80 "DNA_scriptlink_types.h",
82 "DNA_packedFile_types.h",
85 "DNA_texture_types.h",
88 "DNA_material_types.h",
90 // if you add files here, please add them at the end
91 // of makesdna.c (this file) as well
95 "DNA_meshdata_types.h",
96 "DNA_lattice_types.h",
101 "DNA_view3d_types.h",
102 "DNA_view2d_types.h",
104 "DNA_userdef_types.h",
105 "DNA_screen_types.h",
107 // if you add files here, please add them at the end
108 // of makesdna.c (this file) as well
109 "DNA_fileglobal_types.h",
110 "DNA_sequence_types.h",
111 "DNA_effect_types.h",
114 "DNA_property_types.h",
115 "DNA_sensor_types.h",
116 "DNA_controller_types.h",
117 "DNA_actuator_types.h",
120 "DNA_armature_types.h",
121 "DNA_action_types.h",
122 "DNA_constraint_types.h",
124 // if you add files here, please add them at the end
125 // of makesdna.c (this file) as well
127 // empty string to indicate end of includefiles
131 int maxdata= 500000, maxnr= 50000;
135 char **names, *namedata; /* at adress names[a] is string a */
136 char **types, *typedata; /* at adress types[a] is string a */
137 short *typelens; /* at typelens[a] is de length of type a */
138 short *alphalens; /* contains sizes as they are calculated on the alpha */
139 short **structs, *structdata; /* at sp= structs[a] is the first adress of a struct definition
141 sp[1] is amount of elements
142 sp[2] sp[3] is typenr, namenr (etc) */
145 * - 0 = no output, except errors
146 * - 1 = detail actions
147 * - 2 = full trace, tell which names and types were found
148 * - 4 = full trace, plus all gritty details
151 int additional_slen_offset;
153 /* ************************************************************************** */
155 /* ************************************************************************** */
158 * Add type <str> to struct indexed by <len>, if it was not yet found.
160 int add_type(char *str, int len);
163 * Add variable <str> to
165 int add_name(char *str);
168 * Search whether this structure type was already found, and if not,
171 short *add_struct(int namecode);
174 * Remove comments from this buffer. Assumes that the buffer refers to
177 int preprocess_include(char *maindata, int len);
180 * Scan this file for serializable types.
182 int convert_include(char *filename);
185 * Determine how many bytes are needed for an array.
187 int arraysize(char *astr, int len);
190 * Determine how many bytes are needed for each struct.
192 int calculate_structlens(void);
195 * Construct the DNA.c file
197 void dna_write(FILE *file, void *pntr, int size);
200 * Report all structures found so far, and print their lenghts.
202 void printStructLenghts(void);
207 int make_structDNA(FILE *file);
212 int main(int argc, char ** argv);
216 /* ************************************************************************** */
218 /* ************************************************************************** */
220 /* ************************* MAKEN DNA ********************** */
222 int add_type(char *str, int len)
227 if(str[0]==0) return -1;
229 /* search through type array */
230 for(nr=0; nr<nr_types; nr++) {
231 if(strcmp(str, types[nr])==0) {
240 /* append new type */
241 if(nr_types==0) cp= typedata;
243 cp= types[nr_types-1]+strlen(types[nr_types-1])+1;
247 typelens[nr_types]= len;
248 alphalens[nr_types]= len;
250 if(nr_types>=maxnr) {
251 printf("too many types\n");
262 * Because of the weird way of tokenizing, we have to 'cast' function
263 * pointers to ... (*f)(), whatever the original signature. In fact,
264 * we add name and type at the same time... There are two special
265 * cases, unfortunately. These are explicitly checked.
268 int add_name(char *str)
272 char buf[255]; /* stupid limit, change it :) */
275 additional_slen_offset = 0;
277 if((str[0]==0) /* || (str[1]==0) */) return -1;
279 if (str[0] == '(' && str[1] == '*') {
280 if (debugSDNA > 3) printf("\t\t\t\t*** Function pointer found\n");
281 /* functionpointer: transform the type (sometimes) */
285 while (str[i] != ')') {
290 /* Another number we need is the extra slen offset. This extra
291 * offset is the overshoot after a space. If there is no
292 * space, no overshoot should be calculated. */
293 j = i; /* j at first closing brace */
295 if (debugSDNA > 3) printf("first brace after offset %d\n", i);
297 j++; /* j beyond closing brace ? */
298 while ((str[j] != 0) && (str[j] != ')' )) {
299 if (debugSDNA > 3) printf("seen %c ( %d) \n", str[j], str[j]);
302 if (debugSDNA > 3) printf("seen %c ( %d) \n", str[j], str[j]);
303 if (debugSDNA > 3) printf("special after offset %d\n", j);
306 if (debugSDNA > 3) printf("offsetting for space\n");
307 /* get additional offset */
309 while (str[j] != ')') {
313 if (debugSDNA > 3) printf("extra offset %d\n", k);
314 additional_slen_offset = k;
315 } else if (str[j] == ')' ) {
316 if (debugSDNA > 3) printf("offsetting for brace\n");
317 ; /* don't get extra offset */
319 printf("Error during tokening function pointer argument list\n");
323 * Put )(void) at the end? Maybe )(). Should check this with
324 * old sdna. Actually, sometimes )(), sometimes )(void...)
325 * Alas.. such is the nature of braindamage :(
327 * Sorted it out: always do )(), except for headdraw and
328 * windraw, part of ScrArea. This is important, because some
329 * linkers will treat different fp's differently when called
330 * !!! This has to do with interference in byte-alignment and
331 * the way args are pushed on the stack.
335 if (debugSDNA > 3) printf("Name before chomping: %s\n", buf);
336 if ( (strncmp(buf,"(*headdraw", 10) == 0)
337 || (strncmp(buf,"(*windraw", 9) == 0) ) {
352 /* now precede with buf*/
353 if (debugSDNA > 3) printf("\t\t\t\t\tProposing fp name %s\n", buf);
356 /* normal field: old code */
360 /* search name array */
361 for(nr=0; nr<nr_names; nr++) {
362 if(strcmp(name, names[nr])==0) {
367 /* append new type */
368 if(nr_names==0) cp= namedata;
370 cp= names[nr_names-1]+strlen(names[nr_names-1])+1;
375 if(nr_names>=maxnr) {
376 printf("too many names\n");
384 short *add_struct(int namecode)
390 structs[0]= structdata;
393 sp= structs[nr_structs-1];
395 structs[nr_structs]= sp+ 2*len+2;
398 sp= structs[nr_structs];
401 if(nr_structs>=maxnr) {
402 printf("too many structs\n");
410 int preprocess_include(char *maindata, int len)
412 int a, newlen, comment = 0;
413 char *cp, *temp, *md;
415 temp= MEM_mallocN(len, "preprocess_include");
416 memcpy(temp, maindata, len);
418 // remove all c++ comments
419 /* replace all enters/tabs/etc with spaces */
424 if(cp[0]=='/' && cp[1]=='/') {
429 if (comment || *cp<32 || *cp>128 ) *cp= 32;
434 /* data from temp copy to maindata, remove comments and double spaces */
442 if(cp[0]=='/' && cp[1]=='*') {
446 if(cp[0]=='*' && cp[1]=='/') {
451 /* do not copy when: */
453 else if( cp[0]==' ' && cp[1]==' ' );
454 else if( cp[-1]=='*' && cp[0]==' ' ); /* pointers with a space */
467 static void *read_file_data(char *filename, int *len_r)
470 FILE *fp= fopen(filename, "rb");
472 FILE *fp= fopen(filename, "r");
481 fseek(fp, 0L, SEEK_END);
483 fseek(fp, 0L, SEEK_SET);
485 data= MEM_mallocN(*len_r, "read_file_data");
491 if (fread(data, *len_r, 1, fp)!=1) {
500 int convert_include(char *filename)
502 /* read include file, skip structs with a '#' before it.
503 store all data in temporal arrays.
505 int filelen, count, overslaan, slen, type, name, strct;
506 short *structpoin, *sp;
507 char *maindata, *mainend, *md, *md1;
509 md= maindata= read_file_data(filename, &filelen);
511 printf("Can't read file %s\n", filename);
515 filelen= preprocess_include(maindata, filelen);
516 mainend= maindata+filelen-1;
518 /* we look for '{' and then back to 'struct' */
521 while(count<filelen) {
523 /* code for skipping a struct: two hashes. (preprocess added a space) */
524 if(md[0]=='#' && md[1]==' ' && md[2]=='#') {
534 if(md[-1]==' ') md[-1]= 0;
536 while( *md1!=32) md1--; /* to beginning of word */
539 /* we've got a struct name when... */
540 if( strncmp(md1-7, "struct", 6)==0 ) {
543 strct= add_type(md1, 0);
544 structpoin= add_struct(strct);
547 if (debugSDNA > 1) printf("\t|\t|-- detected struct %s\n", types[strct]);
549 /* first lets make it all nice strings */
552 if( ((long)md1) > ((long)mainend) ) break;
554 if(*md1==',' || *md1==' ') *md1= 0;
558 /* read types and names until first character that is not '}' */
560 while( *md1 != '}' ) {
561 if( ((long)md1) > ((long)mainend) ) break;
563 /* skip when it says 'struct' or 'unsigned' */
565 if( strncmp(md1, "struct", 6)==0 ) md1+= 7;
566 if( strncmp(md1, "unsigned", 6)==0 ) md1+= 9;
568 /* we've got a type! */
569 type= add_type(md1, 0);
571 if (debugSDNA > 1) printf("\t|\t|\tfound type %s (", md1);
577 while( *md1 != ';' ) {
578 if( ((long)md1) > ((long)mainend) ) break;
581 /* We've got a name. slen needs
582 * correction for function
585 if( md1[slen-1]==';' ) {
590 slen += additional_slen_offset;
594 if ((debugSDNA>1) && (names[name] != 0 )) printf("%s |", names[name]);
605 slen += additional_slen_offset;
609 if ((debugSDNA > 1) && (names[name] != 0 )) printf("%s ||", names[name]);
619 if (debugSDNA > 1) printf(")\n");
636 int arraysize(char *astr, int len)
639 char str[100], *cp=0;
641 memcpy(str, astr, len+1);
643 for(a=0; a<len; a++) {
647 else if( str[a]==']' && cp) {
656 int calculate_structlens(void)
658 int a, b, len, alphalen, unknown= nr_structs, lastunknown, structtype, type, mul, namelen;
659 short *sp, *structpoin;
661 int has_pointer, dna_error = 0;
664 lastunknown= unknown;
667 /* check all structs... */
668 for(a=0; a<nr_structs; a++) {
669 structpoin= structs[a];
670 structtype= structpoin[0];
672 /* when length is not known... */
673 if(typelens[structtype]==0) {
680 /* check all elements in struct */
681 for(b=0; b<structpoin[1]; b++, sp+=2) {
686 /* is it a pointer or function pointer? */
687 if(cp[0]=='*' || cp[1]=='*') {
689 /* has the name an extra length? (array) */
691 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
694 if(sizeof(void *) == 4) {
696 printf("Align pointer error in struct: %s %s\n", types[structtype], cp);
701 printf("Align pointer error in struct: %s %s\n", types[structtype], cp);
707 printf("Align pointer error in struct: %s %s\n", types[structtype],cp);
711 len += sizeof(void *) * mul;
714 } else if( typelens[type] ) {
715 /* has the name an extra length? (array) */
717 if( cp[namelen-1]==']') mul= arraysize(cp, namelen);
720 if(typelens[type]>3 && (len % 4) ) {
721 printf("Align 4 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%4);
724 else if(typelens[type]==2 && (len % 2) ) {
725 printf("Align 2 error in struct: %s %s (add %d padding bytes)\n", types[structtype], cp, len%2);
729 len += mul*typelens[type];
730 alphalen += mul * alphalens[type];
742 typelens[structtype]= len;
743 alphalens[structtype]= alphalen;
744 // two ways to detect if a struct contains a pointer:
745 // has_pointer is set or alphalen != len
746 if (has_pointer || alphalen != len) {
748 printf("Sizeerror in struct: %s (add %d bytes)\n", types[structtype], alphalen%8);
754 printf("Sizeerror in struct: %s (add %d bytes)\n", types[structtype], len%4);
762 if(unknown==lastunknown) break;
766 printf("ERROR: still %d structs unknown\n", unknown);
769 printf("*** Known structs : \n");
771 for(a=0; a<nr_structs; a++) {
772 structpoin= structs[a];
773 structtype= structpoin[0];
776 if(typelens[structtype]!=0) {
777 printf(" %s\n", types[structtype]);
783 printf("*** Unknown structs : \n");
785 for(a=0; a<nr_structs; a++) {
786 structpoin= structs[a];
787 structtype= structpoin[0];
789 /* length unkown yet */
790 if(typelens[structtype]==0) {
791 printf(" %s\n", types[structtype]);
799 #define MAX_DNA_LINE_LENGTH 20
801 void dna_write(FILE *file, void *pntr, int size)
803 static int linelength = 0;
807 data = (char *) pntr;
809 for (i = 0 ; i < size ; i++)
811 fprintf(file, "%d,", data[i]);
813 if (linelength >= MAX_DNA_LINE_LENGTH) {
820 void printStructLenghts(void)
822 int a, unknown= nr_structs, lastunknown, structtype;
824 printf("\n\n*** All detected structs:\n");
827 lastunknown= unknown;
830 /* check all structs... */
831 for(a=0; a<nr_structs; a++) {
832 structpoin= structs[a];
833 structtype= structpoin[0];
834 printf("\t%s\t:%d\n", types[structtype], typelens[structtype]);
838 printf("*** End of list\n");
843 int make_structDNA(FILE *file)
847 /* str contains filenames. Since we now include paths, I stretched */
848 /* it a bit. Hope this is enough :) -nzc- */
849 char str[SDNA_MAX_FILENAME_LENGTH], *cp;
852 if (debugSDNA > -1) {
854 printf("Running makesdna at debug level %d\n", debugSDNA);
855 printf("\tProgram version: %s\n", DNA_VERSION_DATE);
858 /* the longest known struct is 50k, so we assume 100k is sufficent! */
859 namedata= MEM_callocN(maxdata, "namedata");
860 typedata= MEM_callocN(maxdata, "typedata");
861 structdata= MEM_callocN(maxdata, "structdata");
863 /* a maximum of 5000 variables, must be sufficient? */
864 names= MEM_callocN(sizeof(char *)*maxnr, "names");
865 types= MEM_callocN(sizeof(char *)*maxnr, "types");
866 typelens= MEM_callocN(sizeof(short)*maxnr, "typelens");
867 alphalens= MEM_callocN(sizeof(short)*maxnr, "alphalens");
868 structs= MEM_callocN(sizeof(short)*maxnr, "structs");
870 /* insertion of all known types */
871 /* watch it: uint is not allowed! use in structs an unsigned int */
872 add_type("char", 1); /* 0 */
873 add_type("uchar", 1); /* 1 */
874 add_type("short", 2); /* 2 */
875 add_type("ushort", 2); /* 3 */
876 add_type("int", 4); /* 4 */
877 add_type("long", 4); /* 5 */
878 add_type("ulong", 4); /* 6 */
879 add_type("float", 4); /* 7 */
880 add_type("double", 8); /* 8 */
881 add_type("void", 0); /* 9 */
883 // the defines above shouldn't be output in the padding file...
884 firststruct = nr_types;
887 #define BASE_HEADER "../"
890 /* add all include files defined in the global array */
891 /* Since the internal file+path name buffer has limited length, I do a */
892 /* little test first... */
893 /* Mind the breaking condition here! */
894 if (debugSDNA) printf("\tStart of header scan:\n");
895 for (i = 0; strlen(includefiles[i]); i++) {
896 if (debugSDNA) printf("\t|-- Converting %s%s\n", BASE_HEADER, includefiles[i]);
897 if (strlen(includefiles[i]) > SDNA_MAX_FILENAME_LENGTH) {
898 /* this would cause coredumps*/
899 printf("*** \tError in makesdna: the specified filenames is too long "
900 "for parsing.\n\tFile: %s%s\n", BASE_HEADER, includefiles[i]);
902 sprintf(str, "%s%s", BASE_HEADER, includefiles[i]);
903 if (convert_include(str)) {
908 if (debugSDNA) printf("\tFinished scanning %d headers.\n", i);
910 if (calculate_structlens()) {
922 printf("nr_names %d nr_types %d nr_structs %d\n", nr_names, nr_types, nr_structs);
923 for(a=0; a<nr_names; a++) {
924 printf(" %s \n", names[a]);
929 for(a=0; a<nr_types; a++, sp++) {
930 printf(" %s %d\n", types[a], *sp);
934 for(a=0; a<nr_structs; a++) {
936 printf(" struct %s elems: %d size: %d\n", types[sp[0]], sp[1],typelens[sp[0]]);
939 /* ? num_types was elem? */
940 for(b=0; b< num_types; b++, sp+= 2) {
941 printf(" %s %s\n", types[sp[0]], names[sp[1]]);
948 if (debugSDNA > -1) printf("Writing file ... ");
950 if(nr_names==0 || nr_structs==0);
953 dna_write(file, str, 4);
957 dna_write(file, str, 4);
959 dna_write(file, &len, 4);
961 /* calculate size of datablock with strings */
962 cp= names[nr_names-1];
963 cp+= strlen(names[nr_names-1]) + 1; /* +1: null-terminator */
964 len= (long)cp - (long)(names[0]);
966 dna_write(file, names[0], len);
970 dna_write(file, str, 4);
972 dna_write(file, &len, 4);
974 /* calculate datablock size */
975 cp= types[nr_types-1];
976 cp+= strlen(types[nr_types-1]) + 1; /* +1: null-terminator */
977 len= (long)cp - (long)(types[0]);
980 dna_write(file, types[0], len);
982 /* WRITE TYPELENGTHS */
984 dna_write(file, str, 4);
987 if(nr_types & 1) len+= 2;
988 dna_write(file, typelens, len);
992 dna_write(file, str, 4);
994 dna_write(file, &len, 4);
996 /* calc datablock size */
997 sp= structs[nr_structs-1];
999 len= (long)sp - (long)(structs[0]);
1002 dna_write(file, structs[0], len);
1004 /* a simple dna padding test */
1010 fp= fopen("padding.c", "w");
1014 // add all include files defined in the global array
1015 for (i = 0; strlen(includefiles[i]); i++) {
1016 fprintf(fp, "#include \"%s\"\n", includefiles[i]);
1019 fprintf(fp, "main(){\n");
1022 for(a=firststruct; a<nr_types; a++, sp++) {
1023 fprintf(fp, "\tprintf(\" ");
1024 fprintf(fp, "%%d %s %d ", types[a], *sp);
1025 fprintf(fp, "\\n\", sizeof(struct %s) - %d);\n", types[a], *sp);
1031 /* end end padding test */
1035 MEM_freeN(namedata);
1036 MEM_freeN(typedata);
1037 MEM_freeN(structdata);
1040 MEM_freeN(typelens);
1043 if (debugSDNA > -1) printf("done.\n");
1048 /* ************************* END MAKE DNA ********************** */
1050 static void make_bad_file(char *file)
1052 FILE *fp= fopen(file, "w");
1053 fprintf(fp, "ERROR! Cannot make correct DNA.c file, STUPID!\n");
1057 int main(int argc, char ** argv)
1060 int return_status = 0;
1063 printf("Usage: %s outfile.c\n", argv[0]);
1066 file = fopen(argv[1], "w");
1068 printf ("Unable to open file: %s\n", argv[1]);
1071 fprintf (file, "unsigned char DNAstr[]= {\n");
1072 if (make_structDNA(file)) {
1075 make_bad_file(argv[1]);
1078 fprintf(file, "};\n");
1079 fprintf(file, "int DNAlen= sizeof(DNAstr);\n");
1087 return(return_status);
1090 // include files for automatic dependancies
1091 #include "DNA_listBase.h"
1092 #include "DNA_vec_types.h"
1094 #include "DNA_ipo_types.h"
1095 #include "DNA_key_types.h"
1096 #include "DNA_scriptlink_types.h"
1097 #include "DNA_text_types.h"
1098 #include "DNA_packedFile_types.h"
1099 #include "DNA_camera_types.h"
1100 #include "DNA_image_types.h"
1101 #include "DNA_texture_types.h"
1102 #include "DNA_lamp_types.h"
1103 #include "DNA_wave_types.h"
1104 #include "DNA_material_types.h"
1105 #include "DNA_vfont_types.h"
1106 #include "DNA_meta_types.h"
1107 #include "DNA_curve_types.h"
1108 #include "DNA_mesh_types.h"
1109 #include "DNA_meshdata_types.h"
1110 #include "DNA_lattice_types.h"
1111 #include "DNA_object_types.h"
1112 #include "DNA_world_types.h"
1113 #include "DNA_radio_types.h"
1114 #include "DNA_scene_types.h"
1115 #include "DNA_view3d_types.h"
1116 #include "DNA_view2d_types.h"
1117 #include "DNA_space_types.h"
1118 #include "DNA_userdef_types.h"
1119 #include "DNA_screen_types.h"
1120 #include "DNA_sdna_types.h"
1121 #include "DNA_fileglobal_types.h"
1122 #include "DNA_sequence_types.h"
1123 #include "DNA_effect_types.h"
1124 #include "DNA_ika_types.h"
1125 #include "DNA_oops_types.h"
1126 #include "DNA_property_types.h"
1127 #include "DNA_sensor_types.h"
1128 #include "DNA_controller_types.h"
1129 #include "DNA_actuator_types.h"
1130 #include "DNA_sound_types.h"
1131 #include "DNA_group_types.h"
1132 #include "DNA_armature_types.h"
1133 #include "DNA_action_types.h"
1134 #include "DNA_constraint_types.h"
1135 #include "DNA_nla_types.h"