forked from notaz/picodrive
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathamalgamate.c
246 lines (214 loc) · 5.53 KB
/
amalgamate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#define OUT_FILE "PicoAll.c"
// files to amalgamate, in order
static const char *files[] =
{
"Pico/Pico.h",
// PicoInt.h includes some CD stuff, so start with them
"Pico/cd/cd_file.h",
"Pico/cd/cd_sys.h",
"Pico/cd/LC89510.h",
"Pico/cd/gfx_cd.h",
"Pico/cd/pcm.h",
"Pico/PicoInt.h",
"Pico/Patch.h",
"Pico/sound/mix.h",
// source
"Pico/Area.c",
"Pico/Cart.c",
"Pico/Draw2.c",
"Pico/Draw.c",
"Pico/VideoPort.c",
"Pico/sound/sound.c",
"Pico/MemoryCmn.c",
"Pico/Memory.c",
"Pico/Misc.c",
"Pico/Patch.c",
"Pico/Sek.c",
"Pico/cd/Area.c",
"Pico/cd/buffering.c",
"Pico/cd/cd_file.c",
"Pico/cd/cd_sys.c",
"Pico/cd/cell_map.c",
"Pico/cd/gfx_cd.c",
"Pico/cd/LC89510.c",
"Pico/cd/Memory.c",
"Pico/cd/Misc.c",
"Pico/cd/pcm.c",
"Pico/cd/Sek.c",
"Pico/cd/Pico.c",
"Pico/Pico.c",
};
static char *includes[128];
static void eprintf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
exit(1);
}
static void emit_header(FILE *f, const char *fname)
{
char tmp[128] = "/* */";
memcpy(tmp + 3, fname, strlen(fname));
fprintf(f, "\n\n");
fprintf(f, "/**************************************************************/\n");
fprintf(f, "/**************************************************************/\n");
fprintf(f, "%s\n", tmp);
fprintf(f, "/**************************************************************/\n");
}
static const char *add_include(const char *include)
{
int i;
char processed_inc[128+4];
// must first quote relative includes
snprintf(processed_inc, sizeof(processed_inc), (include[0] != '<') ? "\"%s\"" : "%s", include);
// find in include list
for (i = 0; includes[i] && i < 128; i++)
{
if (strcmp(processed_inc, includes[i]) == 0) break;
}
if (i == 128) eprintf("add_include: includes overflowed\n");
if (includes[i] != NULL)
{
printf("already have: %s\n", processed_inc);
return NULL;
}
else
{
printf("adding: %s\n", processed_inc);
includes[i] = strdup(processed_inc);
if (includes[i] == NULL) eprintf("add_include: OOM\n");
return includes[i];
}
}
static const char *add_raw_include(const char *include, const char *base)
{
const char *ps, *pe;
char processed_inc[128];
for (ps = include; *ps && isspace(*ps); ps++);
if (*ps == '<')
{
int len = 1;
// system include, search for '>'
for (pe = ps; *pe && *pe != '>'; pe++, len++);
if (*pe == 0 || len > 127) eprintf("add_raw_include: failed sysinclude, len=%i\n", len);
strncpy(processed_inc, ps, len);
processed_inc[len] = 0;
}
else if (*ps == '\"')
{
int len, pos;
// relative include, make path absolute (or relative to base dir)
strcpy(processed_inc, base);
ps++;
while (*ps == '.')
{
if (strncmp(ps, "../", 3) == 0)
{
char *p;
if (processed_inc[0] == 0)
eprintf("add_raw_include: already in root, can't go down: %s | %s\n", ps, include);
p = strrchr(processed_inc, '/');
if (p == NULL) eprintf("add_raw_include: can't happen\n");
*p = 0;
p = strrchr(processed_inc, '/');
if (p != NULL) p[1] = 0;
else processed_inc[0] = 0;
ps += 3;
}
else if (strncmp(ps, "./", 2) == 0)
{
ps += 2; // just skip
}
while (*ps == '/') ps++;
}
if (*ps == 0) eprintf("add_raw_include: failed with %s\n", include);
len = pos = strlen(processed_inc);
for (pe = ps; *pe && *pe != '\"'; pe++, len++);
if (*pe == 0 || len > 127) eprintf("add_raw_include: failed with %s, len=%i\n", include, len);
strncpy(processed_inc + pos, ps, len - pos);
processed_inc[len] = 0;
}
else
eprintf("add_raw_include: unhandled include: %s\n", ps);
return add_include(processed_inc);
}
// returns pointer to location after part in string
static const char *substr_end(const char *string, const char *part)
{
const char *p = string;
int len = strlen(part);
while (*p && isspace(*p)) p++;
return (strncmp(p, part, len) == 0) ? (p + len) : NULL;
}
static void strip_cr(char *str)
{
int len = strlen(str);
char *p = str;
while ((p = strchr(p, '\r')))
{
memmove(p, p + 1, len - (p - str) + 1);
}
if (strlen(str) > 0)
{
p = str + strlen(str) - 1;
while (p >= str && isspace(*p)) { *p = 0; p--; } // strip spaces on line ends
}
strcat(str, "\n"); // re-add newline
}
int main(void)
{
char buff[512]; // tmp buffer
char path[128]; // path to file being included, with ending slash
int i, ifile;
FILE *fo;
memset(includes, 0, sizeof(includes));
fo = fopen(OUT_FILE, "w");
if (fo == NULL) return 1;
// special header
fprintf(fo, "#define PICO_INTERNAL static\n");
fprintf(fo, "#define PICO_INTERNAL_ASM\n");
for (ifile = 0; ifile < sizeof(files) / sizeof(files[0]); ifile++)
{
FILE *fi;
const char *file = files[ifile], *p;
p = strrchr(file, '/');
if (p == NULL) eprintf("main: file in root? %s\n", file);
strncpy(path, file, p - file + 1);
path[p - file + 1] = 0;
fi = fopen(file, "r");
if (fi == NULL) eprintf("main: failed to open %s\n", file);
// if (strcmp(file + strlen(file) - 2, ".h") == 0)
add_include(file);
emit_header(fo, file);
while (!feof(fi))
{
p = fgets(buff, sizeof(buff), fi);
if (p == NULL) break;
strip_cr(buff);
// include?
p = substr_end(buff, "#include");
if (p != NULL)
{
p = add_raw_include(p, path);
if (p != NULL) fprintf(fo, "#include %s\n", p);
continue;
}
// passthrough
fputs(buff, fo);
}
}
emit_header(fo, "EOF");
for (i = 0; includes[i] && i < 128; i++)
{
free(includes[i]);
}
fclose(fo);
return 0;
}