summaryrefslogtreecommitdiffstatshomepage
path: root/src/build/makedep.c
blob: 7f0e41b34c8b86b06917c61322430f82f975fb4c (plain) (blame)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/***************************************************************************

    MAME source code dependency generator

****************************************************************************

    Copyright Aaron Giles
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are
    met:

        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
          notice, this list of conditions and the following disclaimer in
          the documentation and/or other materials provided with the
          distribution.
        * Neither the name 'MAME' nor the names of its contributors may be
          used to endorse or promote products derived from this software
          without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <zlib.h>
#include "osdcore.h"
#include "astring.h"
#include "corefile.h"
#include "tagmap.h"


/***************************************************************************
    CONSTANTS
***************************************************************************/

#define HASH_SIZE		193


/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

typedef struct _include_path include_path;
struct _include_path
{
	include_path *	next;
	const astring *	path;
};


typedef struct _exclude_path exclude_path;
struct _exclude_path
{
	exclude_path *	next;
	const astring *	path;
	int				pathlen;
	UINT8			recursive;
};


typedef struct _list_entry list_entry;
struct _list_entry
{
	list_entry *	next;
	const astring *	name;
};


typedef struct _file_entry file_entry;

typedef struct _dependency dependency;
struct _dependency
{
	dependency *	next;
	file_entry *	file;
};


struct _file_entry
{
	astring *		name;
	dependency *	deplist;
};



/***************************************************************************
    GLOBAL VARIABLES
***************************************************************************/

static include_path *incpaths;
static exclude_path *excpaths;
static tagmap *file_map;



/***************************************************************************
    PROTOTYPES
***************************************************************************/

/* core output functions */
static int recurse_dir(int srcrootlen, const astring *srcdir);
static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile);

/* path helpers */
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename);



/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

/* core output functions */
static int recurse_dir(int srcrootlen, const astring *srcdir);
static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile);

/* path helpers */
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename);



/***************************************************************************
    MAIN
***************************************************************************/

/*-------------------------------------------------
    main - main entry point
-------------------------------------------------*/

int main(int argc, char *argv[])
{
	include_path **incpathhead = &incpaths;
	exclude_path **excpathhead = &excpaths;
	astring *srcdir = NULL;
	int unadorned = 0;
	int result;
	int argnum;

	/* loop over arguments */
	for (argnum = 1; argnum < argc; argnum++)
	{
		char *arg = argv[argnum];

		/* include path? */
		if (arg[0] == '-' && arg[1] == 'I')
		{
			*incpathhead = (include_path *)malloc(sizeof(**incpathhead));
			if (*incpathhead != NULL)
			{
				(*incpathhead)->next = NULL;
				(*incpathhead)->path = astring_replacechr(astring_dupc(&arg[2]), '/', PATH_SEPARATOR[0]);
				incpathhead = &(*incpathhead)->next;
			}
		}

		/* exclude path? */
		else if (arg[0] == '-' && arg[1] == 'X')
		{
			*excpathhead = (exclude_path *)malloc(sizeof(**excpathhead));
			if (*excpathhead != NULL)
			{
				astring *path;
				(*excpathhead)->next = NULL;
				path = astring_replacechr(astring_dupc(&arg[2]), PATH_SEPARATOR[0], '/');
				(*excpathhead)->recursive = (astring_replacec(path, astring_len(path) - 4, "/...", "") != 0);
				(*excpathhead)->path = path;
				(*excpathhead)->pathlen = astring_len(path);
				excpathhead = &(*excpathhead)->next;
			}
		}

		/* other parameter */
		else if (arg[0] != '-' && unadorned == 0)
		{
			srcdir = astring_replacechr(astring_dupc(arg), '/', PATH_SEPARATOR[0]);
			unadorned++;
		}
		else
			goto usage;
	}

	/* make sure we got 1 parameter */
	if (srcdir == NULL)
		goto usage;

	/* create a tagmap for tracking files we've visited */
	file_map = tagmap_alloc();

	/* recurse over subdirectories */
	result = recurse_dir(astring_len(srcdir), srcdir);

	/* free source and destination directories */
	tagmap_free(file_map);
	astring_free(srcdir);
	return result;

usage:
	fprintf(stderr, "Usage:\n%s <srcroot> [-Iincpath [-Iincpath [...]]]\n", argv[0]);
	return 1;
}



/***************************************************************************
    CORE OUTPUT FUNCTIONS
***************************************************************************/

static int compare_list_entries(const void *p1, const void *p2)
{
	const list_entry *entry1 = *(const list_entry **)p1;
	const list_entry *entry2 = *(const list_entry **)p2;
	return strcmp(astring_c(entry1->name), astring_c(entry2->name));
}


/*-------------------------------------------------
    recurse_dependencies - recurse through the
    dependencies found, adding the mto the tagmap
    unless we already exist in the map
-------------------------------------------------*/

static void recurse_dependencies(file_entry *file, tagmap *map)
{
	int filelen = astring_len(file->name);
	exclude_path *exclude;
	dependency *dep;

	/* skip if we're in an exclude path */
	for (exclude = excpaths; exclude != NULL; exclude = exclude->next)
		if (exclude->pathlen < filelen && strncmp(astring_c(file->name), astring_c(exclude->path), exclude->pathlen) == 0)
			if (exclude->recursive || astring_chr(file->name, exclude->pathlen + 1, '/') == -1)
				return;

	/* attempt to add; if we get an error, we're already present */
	if (tagmap_add(map, astring_c(file->name), file->name, FALSE) != TMERR_NONE)
		return;

	/* recurse the list from there */
	for (dep = file->deplist; dep != NULL; dep = dep->next)
		recurse_dependencies(dep->file, map);
}


/*-------------------------------------------------
    recurse_dir - recurse through a directory
-------------------------------------------------*/

static int recurse_dir(int srcrootlen, const astring *srcdir)
{
	static const osd_dir_entry_type typelist[] = { ENTTYPE_DIR, ENTTYPE_FILE };
	int result = 0;
	int entindex;

	/* iterate first over directories, then over files */
	for (entindex = 0; entindex < ARRAY_LENGTH(typelist) && result == 0; entindex++)
	{
		osd_dir_entry_type entry_type = typelist[entindex];
		const osd_directory_entry *entry;
		list_entry **listarray = NULL;
		list_entry *list = NULL;
		list_entry *curlist;
		osd_directory *dir;
		int found = 0;

		/* open the directory and iterate through it */
		dir = osd_opendir(astring_c(srcdir));
		if (dir == NULL)
		{
			result = 1;
			goto error;
		}

		/* build up the list of files */
		while ((entry = osd_readdir(dir)) != NULL)
			if (entry->type == entry_type && entry->name[0] != '.')
			{
				list_entry *lentry = (list_entry *)malloc(sizeof(*lentry));
				lentry->name = astring_dupc(entry->name);
				lentry->next = list;
				list = lentry;
				found++;
			}

		/* close the directory */
		osd_closedir(dir);

		/* skip if nothing found */
		if (found == 0)
			continue;

		/* allocate memory for sorting */
		listarray = (list_entry **)malloc(sizeof(list_entry *) * found);
		found = 0;
		for (curlist = list; curlist != NULL; curlist = curlist->next)
			listarray[found++] = curlist;

		/* sort the list */
		qsort(listarray, found, sizeof(listarray[0]), compare_list_entries);

		/* rebuild the list */
		list = NULL;
		while (--found >= 0)
		{
			listarray[found]->next = list;
			list = listarray[found];
		}
		free(listarray);

		/* iterate through each file */
		for (curlist = list; curlist != NULL && result == 0; curlist = curlist->next)
		{
			astring *srcfile;

			/* build the source filename */
			srcfile = astring_alloc();
			astring_printf(srcfile, "%s%c%s", astring_c(srcdir), PATH_SEPARATOR[0], astring_c(curlist->name));

			/* if we have a file, output it */
			if (entry_type == ENTTYPE_FILE)
			{
				/* make sure we care, first */
				if (core_filename_ends_with(astring_c(curlist->name), ".c"))
				{
					tagmap *depend_map = tagmap_alloc();
					tagmap_entry *entry;
					file_entry *file;
					astring *target;
					int taghash;

					/* find dependencies */
					file = compute_dependencies(srcrootlen, srcfile);
					recurse_dependencies(file, depend_map);

					/* convert the target from source to object (makes assumptions about rules) */
					target = astring_dup(file->name);
					astring_replacec(target, 0, "src/", "$(OBJ)/");
					astring_replacec(target, 0, ".c", ".o");
					printf("\n%s : \\\n", astring_c(target));

					/* iterate over the hashed dependencies and output them as well */
					for (taghash = 0; taghash < TAGMAP_HASH_SIZE; taghash++)
						for (entry = depend_map->table[taghash]; entry != NULL; entry = entry->next)
							printf("\t%s \\\n", astring_c((astring *)entry->object));

					astring_free(target);
					tagmap_free(depend_map);
				}
			}

			/* if we have a directory, recurse */
			else
				result = recurse_dir(srcrootlen, srcfile);

			/* free memory for the names */
			astring_free(srcfile);
		}

		/* free all the allocated entries */
		while (list != NULL)
		{
			list_entry *next = list->next;
			astring_free((astring *)list->name);
			free(list);
			list = next;
		}
	}

error:
	return result;
}


/*-------------------------------------------------
    output_file - output a file, converting to
    HTML
-------------------------------------------------*/

static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile)
{
	astring *normalfile;
	UINT32 filelength;
	file_entry *file;
	char *filedata;
	int index;

	/* see if we already have an entry */
	normalfile = astring_dup(srcfile);
	astring_replacechr(normalfile, PATH_SEPARATOR[0], '/');
	file = (file_entry *)tagmap_find(file_map, astring_c(normalfile));
	if (file != NULL)
		return file;

	/* create a new header entry */
	file = (file_entry *)malloc(sizeof(*file));
	file->deplist = NULL;
	file->name = normalfile;
	tagmap_add(file_map, astring_c(file->name), file, FALSE);

	/* read the source file */
	if (core_fload(astring_c(srcfile), (void **)&filedata, &filelength) != FILERR_NONE)
	{
		fprintf(stderr, "Unable to read file '%s'\n", astring_c(srcfile));
		return file;
	}

	/* find the #include directives in this file */
	for (index = 0; index < filelength; index++)
		if (filedata[index] == '#' && strncmp(&filedata[index + 1], "include", 7) == 0)
		{
			astring *filename, *target;
			int scan = index;
			dependency *dep;
			int start;

			/* first make sure we're not commented or quoted */
			for (scan = index; scan > 2 && filedata[scan] != 13 && filedata[scan] != 10; scan--)
				if ((filedata[scan] == '/' && filedata[scan - 1] == '/') || filedata[scan] == '"')
					break;
			if (filedata[scan] != 13 && filedata[scan] != 10)
				continue;

			/* scan forward to find the quotes or bracket */
			index += 7;
			for (scan = index; scan < filelength && filedata[scan] != '<' && filedata[scan] != '"' && filedata[scan] != 13 && filedata[scan] != 10; scan++) ;

			/* ignore if not found or if it's bracketed */
			if (scan >= filelength || filedata[scan] != '"')
				continue;
			start = ++scan;

			/* find the closing quote */
			while (scan < filelength && filedata[scan] != '"')
				scan++;
			if (scan >= filelength)
				continue;

			/* find the include file */
			filename = astring_dupch(&filedata[start], scan - start);
			target = find_include_file(srcrootlen, srcfile, filename);

			/* create a new dependency */
			if (target != NULL)
			{
				dep = (dependency *)malloc(sizeof(*dep));
				dep->next = file->deplist;
				file->deplist = dep;
				dep->file = compute_dependencies(srcrootlen, target);
				astring_free(target);
			}

			astring_free(filename);
		}

	free(filedata);
	return file;
}



/***************************************************************************
    HELPERS
***************************************************************************/

/*-------------------------------------------------
    find_include_file - find an include file
-------------------------------------------------*/

static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename)
{
	include_path *curpath;

	/* iterate over include paths and find the file */
	for (curpath = incpaths; curpath != NULL; curpath = curpath->next)
	{
		astring *srcincpath = astring_dup(curpath->path);
		core_file *testfile;
		int lastsepindex = 0;
		int sepindex;

		/* a '.' include path is specially treated */
		if (astring_cmpc(curpath->path, ".") == 0)
			astring_cpysubstr(srcincpath, srcfile, 0, astring_rchr(srcfile, 0, PATH_SEPARATOR[0]));

		/* append the filename piecemeal to account for directories */
		while ((sepindex = astring_chr(filename, lastsepindex, '/')) != -1)
		{
			astring *pathpart = astring_dupsubstr(filename, lastsepindex, sepindex - lastsepindex);

			/* handle .. by removing a chunk from the incpath */
			if (astring_cmpc(pathpart, "..") == 0)
			{
				int sepindex = astring_rchr(srcincpath, 0, PATH_SEPARATOR[0]);
				if (sepindex != -1)
					astring_substr(srcincpath, 0, sepindex);
			}

			/* otherwise, append a path separator and the pathpart */
			else
				astring_cat(astring_catc(srcincpath, PATH_SEPARATOR), pathpart);

			/* advance past the previous index */
			lastsepindex = sepindex + 1;

			/* free the path part we extracted */
			astring_free(pathpart);
		}

		/* now append the filename */
		astring_catsubstr(astring_catc(srcincpath, PATH_SEPARATOR), filename, lastsepindex, -1);

		/* see if we can open it */
		if (core_fopen(astring_c(srcincpath), OPEN_FLAG_READ, &testfile) == FILERR_NONE)
		{
			/* close the file */
			core_fclose(testfile);
			return srcincpath;
		}

		/* free our include path */
		astring_free(srcincpath);
	}
	return NULL;
}