6 // Bootstrap for themes.
8 // By default, concatenates all the resources and a table of contents
9 // into an object file with objcopy.
11 // Run nm <object> to get the symbols created by bootstrap.
13 // The user must make extern variable declarations like
14 // extern unsigned char _binary_myobject_data_start[];
15 // and link the object to get at the symbols.
17 // Pass the symbol like
18 // _binary_destname_o_start
19 // to BC_Theme::set_data to set an image table for a theme.
22 // Because of the need for runtime compilation of source files for OpenGL,
23 // it now supports loading a single resource and omitting the table of contents.
28 // Usage: bootstrap <dest object> <resource> * n
33 void append_contents(char *path,
41 for(i = strlen(path) - 1;
42 i > 0 && path[i] && path[i] != '/';
46 if(path[i] == '/') i++;
48 for(j = 0; path[i] != 0; i++, j++)
53 strcpy(buffer + *buffer_size, string);
55 *buffer_size += strlen(string) + 1;
57 *(int*)(buffer + *buffer_size) = data_offset;
58 *buffer_size += sizeof(int);
61 int main(int argc, char *argv[])
66 char *contents_buffer;
67 int contents_size = 0;
74 // TODO 2100 to get rid of compiler warning, but better solution needed:
75 // temp_path and out_path parsing have potential buffer overflow.
76 char system_command[2100];
83 fprintf(stderr, "Usage: bootstrap [-b] <dest object> <resource> * n\n");
84 fprintf(stderr, "-b - omit table of contents just store resource.\n");
85 fprintf(stderr, "-s - omit table of contents but append 0 to resource.\n");
89 if(!strcmp(argv[current_arg], "-b"))
95 if(!strcmp(argv[current_arg], "-s"))
101 // Make object filename
102 strcpy(temp_path, argv[current_arg]);
103 strcpy(out_path, argv[current_arg++]);
104 ptr = strchr(temp_path, '.');
111 fprintf(stderr, "Dest path must end in .o\n");
115 if(!(dest = fopen(temp_path, "w")))
117 fprintf(stderr, "Couldn't open dest file %s. %s\n",
123 if(!(data_buffer = malloc(0x1000000)))
125 fprintf(stderr, "Not enough memory to allocate data buffer.\n");
129 if(!(contents_buffer = malloc(0x100000)))
131 fprintf(stderr, "Not enough memory to allocate contents buffer.\n");
135 // Leave space for offset to data
136 contents_size = sizeof(int);
138 // Read through all the resources, concatenate to dest file,
139 // and record the contents.
140 while(current_arg < argc)
142 char *path = argv[current_arg++];
143 if(!(src = fopen(path, "r")))
145 fprintf(stderr, "%s while opening %s\n", strerror(errno), path);
150 fseek(src, 0, SEEK_END);
152 fseek(src, 0, SEEK_SET);
154 data_offset = data_size;
156 if(!binary_mode && !string_mode)
158 // Write size of image in data buffer
159 *(data_buffer + data_size) = (size & 0xff000000) >> 24;
161 *(data_buffer + data_size) = (size & 0xff0000) >> 16;
163 *(data_buffer + data_size) = (size & 0xff00) >> 8;
165 *(data_buffer + data_size) = size & 0xff;
169 int temp = fread(data_buffer + data_size, 1, size, src);
172 if(string_mode) data_buffer[data_size++] = 0;
175 if(!binary_mode && !string_mode)
178 append_contents(path,
186 if(!binary_mode && !string_mode)
188 // Finish off size of contents
189 *(int*)(contents_buffer) = contents_size;
191 fwrite(contents_buffer, 1, contents_size, dest);
195 fwrite(data_buffer, 1, data_size, dest);
198 // Run system command on it
199 sprintf(system_command, "%s %s %s", BOOTSTRAP, temp_path, out_path);
200 int temp = system(system_command);