1
/***************************************************************************
3
MAME source code dependency generator
5
****************************************************************************
10
Redistribution and use in source and binary forms, with or without
11
modification, are permitted provided that the following conditions are
14
* Redistributions of source code must retain the above copyright
15
notice, this list of conditions and the following disclaimer.
16
* Redistributions in binary form must reproduce the above copyright
17
notice, this list of conditions and the following disclaimer in
18
the documentation and/or other materials provided with the
20
* Neither the name 'MAME' nor the names of its contributors may be
21
used to endorse or promote products derived from this software
22
without specific prior written permission.
24
THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
25
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
28
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
POSSIBILITY OF SUCH DAMAGE.
36
***************************************************************************/
49
/***************************************************************************
51
***************************************************************************/
56
/***************************************************************************
58
***************************************************************************/
60
typedef struct _include_path include_path;
68
typedef struct _exclude_path exclude_path;
78
typedef struct _list_entry list_entry;
86
typedef struct _file_entry file_entry;
88
typedef struct _dependency dependency;
104
/***************************************************************************
106
***************************************************************************/
108
static include_path *incpaths;
109
static exclude_path *excpaths;
110
static tagmap *file_map;
114
/***************************************************************************
116
***************************************************************************/
118
/* core output functions */
119
static int recurse_dir(int srcrootlen, const astring *srcdir);
120
static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile);
123
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename);
127
/***************************************************************************
129
***************************************************************************/
131
/* core output functions */
132
static int recurse_dir(int srcrootlen, const astring *srcdir);
133
static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile);
136
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename);
140
/***************************************************************************
142
***************************************************************************/
144
/*-------------------------------------------------
145
main - main entry point
146
-------------------------------------------------*/
148
int main(int argc, char *argv[])
150
include_path **incpathhead = &incpaths;
151
exclude_path **excpathhead = &excpaths;
152
astring *srcdir = NULL;
157
/* loop over arguments */
158
for (argnum = 1; argnum < argc; argnum++)
160
char *arg = argv[argnum];
163
if (arg[0] == '-' && arg[1] == 'I')
165
*incpathhead = (include_path *)malloc(sizeof(**incpathhead));
166
if (*incpathhead != NULL)
168
(*incpathhead)->next = NULL;
169
(*incpathhead)->path = astring_replacechr(astring_dupc(&arg[2]), '/', PATH_SEPARATOR[0]);
170
incpathhead = &(*incpathhead)->next;
175
else if (arg[0] == '-' && arg[1] == 'X')
177
*excpathhead = (exclude_path *)malloc(sizeof(**excpathhead));
178
if (*excpathhead != NULL)
181
(*excpathhead)->next = NULL;
182
path = astring_replacechr(astring_dupc(&arg[2]), PATH_SEPARATOR[0], '/');
183
(*excpathhead)->recursive = (astring_replacec(path, astring_len(path) - 4, "/...", "") != 0);
184
(*excpathhead)->path = path;
185
(*excpathhead)->pathlen = astring_len(path);
186
excpathhead = &(*excpathhead)->next;
190
/* ignore -include which is used by sdlmame to include sdlprefix.h before all other includes */
191
else if (strcmp(arg,"-include") == 0)
196
/* other parameter */
197
else if (arg[0] != '-' && unadorned == 0)
199
srcdir = astring_replacechr(astring_dupc(arg), '/', PATH_SEPARATOR[0]);
206
/* make sure we got 1 parameter */
210
/* create a tagmap for tracking files we've visited */
211
file_map = tagmap_alloc();
213
/* recurse over subdirectories */
214
result = recurse_dir(astring_len(srcdir), srcdir);
216
/* free source and destination directories */
217
tagmap_free(file_map);
218
astring_free(srcdir);
222
fprintf(stderr, "Usage:\n%s <srcroot> [-Iincpath [-Iincpath [...]]]\n", argv[0]);
228
/***************************************************************************
229
CORE OUTPUT FUNCTIONS
230
***************************************************************************/
232
static int compare_list_entries(const void *p1, const void *p2)
234
const list_entry *entry1 = *(const list_entry **)p1;
235
const list_entry *entry2 = *(const list_entry **)p2;
236
return strcmp(astring_c(entry1->name), astring_c(entry2->name));
240
/*-------------------------------------------------
241
recurse_dependencies - recurse through the
242
dependencies found, adding the mto the tagmap
243
unless we already exist in the map
244
-------------------------------------------------*/
246
static void recurse_dependencies(file_entry *file, tagmap *map)
248
int filelen = astring_len(file->name);
249
exclude_path *exclude;
252
/* skip if we're in an exclude path */
253
for (exclude = excpaths; exclude != NULL; exclude = exclude->next)
254
if (exclude->pathlen < filelen && strncmp(astring_c(file->name), astring_c(exclude->path), exclude->pathlen) == 0)
255
if (exclude->recursive || astring_chr(file->name, exclude->pathlen + 1, '/') == -1)
258
/* attempt to add; if we get an error, we're already present */
259
if (tagmap_add(map, astring_c(file->name), file->name, FALSE) != TMERR_NONE)
262
/* recurse the list from there */
263
for (dep = file->deplist; dep != NULL; dep = dep->next)
264
recurse_dependencies(dep->file, map);
268
/*-------------------------------------------------
269
recurse_dir - recurse through a directory
270
-------------------------------------------------*/
272
static int recurse_dir(int srcrootlen, const astring *srcdir)
274
static const osd_dir_entry_type typelist[] = { ENTTYPE_DIR, ENTTYPE_FILE };
278
/* iterate first over directories, then over files */
279
for (entindex = 0; entindex < ARRAY_LENGTH(typelist) && result == 0; entindex++)
281
osd_dir_entry_type entry_type = typelist[entindex];
282
const osd_directory_entry *entry;
283
list_entry **listarray = NULL;
284
list_entry *list = NULL;
289
/* open the directory and iterate through it */
290
dir = osd_opendir(astring_c(srcdir));
297
/* build up the list of files */
298
while ((entry = osd_readdir(dir)) != NULL)
299
if (entry->type == entry_type && entry->name[0] != '.')
301
list_entry *lentry = (list_entry *)malloc(sizeof(*lentry));
302
lentry->name = astring_dupc(entry->name);
308
/* close the directory */
311
/* skip if nothing found */
315
/* allocate memory for sorting */
316
listarray = (list_entry **)malloc(sizeof(list_entry *) * found);
318
for (curlist = list; curlist != NULL; curlist = curlist->next)
319
listarray[found++] = curlist;
322
qsort(listarray, found, sizeof(listarray[0]), compare_list_entries);
324
/* rebuild the list */
328
listarray[found]->next = list;
329
list = listarray[found];
333
/* iterate through each file */
334
for (curlist = list; curlist != NULL && result == 0; curlist = curlist->next)
338
/* build the source filename */
339
srcfile = astring_alloc();
340
astring_printf(srcfile, "%s%c%s", astring_c(srcdir), PATH_SEPARATOR[0], astring_c(curlist->name));
342
/* if we have a file, output it */
343
if (entry_type == ENTTYPE_FILE)
345
/* make sure we care, first */
346
if (core_filename_ends_with(astring_c(curlist->name), ".c"))
348
tagmap *depend_map = tagmap_alloc();
354
/* find dependencies */
355
file = compute_dependencies(srcrootlen, srcfile);
356
recurse_dependencies(file, depend_map);
358
/* convert the target from source to object (makes assumptions about rules) */
359
target = astring_dup(file->name);
360
astring_replacec(target, 0, "src/", "$(OBJ)/");
361
astring_replacec(target, 0, ".c", ".o");
362
printf("\n%s : \\\n", astring_c(target));
364
/* iterate over the hashed dependencies and output them as well */
365
for (taghash = 0; taghash < TAGMAP_HASH_SIZE; taghash++)
366
for (entry = depend_map->table[taghash]; entry != NULL; entry = entry->next)
367
printf("\t%s \\\n", astring_c((astring *)entry->object));
369
astring_free(target);
370
tagmap_free(depend_map);
374
/* if we have a directory, recurse */
376
result = recurse_dir(srcrootlen, srcfile);
378
/* free memory for the names */
379
astring_free(srcfile);
382
/* free all the allocated entries */
385
list_entry *next = list->next;
386
astring_free((astring *)list->name);
397
/*-------------------------------------------------
398
output_file - output a file, converting to
400
-------------------------------------------------*/
402
static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile)
410
/* see if we already have an entry */
411
normalfile = astring_dup(srcfile);
412
astring_replacechr(normalfile, PATH_SEPARATOR[0], '/');
413
file = (file_entry *)tagmap_find(file_map, astring_c(normalfile));
417
/* create a new header entry */
418
file = (file_entry *)malloc(sizeof(*file));
419
file->deplist = NULL;
420
file->name = normalfile;
421
tagmap_add(file_map, astring_c(file->name), file, FALSE);
423
/* read the source file */
424
if (core_fload(astring_c(srcfile), (void **)&filedata, &filelength) != FILERR_NONE)
426
fprintf(stderr, "Unable to read file '%s'\n", astring_c(srcfile));
430
/* find the #include directives in this file */
431
for (index = 0; index < filelength; index++)
432
if (filedata[index] == '#' && strncmp(&filedata[index + 1], "include", 7) == 0)
434
astring *filename, *target;
438
int just_continue = 0;
440
/* first make sure we're not commented or quoted */
441
for (scan = index; scan > 2 && filedata[scan] != 13 && filedata[scan] != 10; scan--)
442
if ((filedata[scan] == '/' && filedata[scan - 1] == '/') || filedata[scan] == '"')
450
/* scan forward to find the quotes or bracket */
452
for (scan = index; scan < filelength && filedata[scan] != '<' && filedata[scan] != '"' && filedata[scan] != 13 && filedata[scan] != 10; scan++) ;
454
/* ignore if not found or if it's bracketed */
455
if (scan >= filelength || filedata[scan] != '"')
459
/* find the closing quote */
460
while (scan < filelength && filedata[scan] != '"')
462
if (scan >= filelength)
465
/* find the include file */
466
filename = astring_dupch(&filedata[start], scan - start);
467
target = find_include_file(srcrootlen, srcfile, filename);
469
/* create a new dependency */
472
dep = (dependency *)malloc(sizeof(*dep));
473
dep->next = file->deplist;
475
dep->file = compute_dependencies(srcrootlen, target);
476
astring_free(target);
479
astring_free(filename);
488
/***************************************************************************
490
***************************************************************************/
492
/*-------------------------------------------------
493
find_include_file - find an include file
494
-------------------------------------------------*/
496
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename)
498
include_path *curpath;
500
/* iterate over include paths and find the file */
501
for (curpath = incpaths; curpath != NULL; curpath = curpath->next)
503
astring *srcincpath = astring_dup(curpath->path);
505
int lastsepindex = 0;
508
/* a '.' include path is specially treated */
509
if (astring_cmpc(curpath->path, ".") == 0)
510
astring_cpysubstr(srcincpath, srcfile, 0, astring_rchr(srcfile, 0, PATH_SEPARATOR[0]));
512
/* append the filename piecemeal to account for directories */
513
while ((sepindex = astring_chr(filename, lastsepindex, '/')) != -1)
515
astring *pathpart = astring_dupsubstr(filename, lastsepindex, sepindex - lastsepindex);
517
/* handle .. by removing a chunk from the incpath */
518
if (astring_cmpc(pathpart, "..") == 0)
520
int sepindex = astring_rchr(srcincpath, 0, PATH_SEPARATOR[0]);
522
astring_substr(srcincpath, 0, sepindex);
525
/* otherwise, append a path separator and the pathpart */
527
astring_cat(astring_catc(srcincpath, PATH_SEPARATOR), pathpart);
529
/* advance past the previous index */
530
lastsepindex = sepindex + 1;
532
/* free the path part we extracted */
533
astring_free(pathpart);
536
/* now append the filename */
537
astring_catsubstr(astring_catc(srcincpath, PATH_SEPARATOR), filename, lastsepindex, -1);
539
/* see if we can open it */
540
if (core_fopen(astring_c(srcincpath), OPEN_FLAG_READ, &testfile) == FILERR_NONE)
543
core_fclose(testfile);
547
/* free our include path */
548
astring_free(srcincpath);