~ubuntu-branches/debian/sid/mame/sid

« back to all changes in this revision

Viewing changes to mess/src/build/makedep.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach, Jordi Mallach, Emmanuel Kasper
  • Date: 2011-12-19 22:56:27 UTC
  • mfrom: (0.1.2)
  • Revision ID: package-import@ubuntu.com-20111219225627-ub5oga1oys4ogqzm
Tags: 0.144-1
[ Jordi Mallach ]
* Fix syntax errors in DEP5 copyright file (lintian).
* Use a versioned copyright Format specification field.
* Update Vcs-* URLs.
* Move transitional packages to the new metapackages section, and make
  them priority extra.
* Remove references to GNU/Linux and MESS sources from copyright.
* Add build variables for s390x.
* Use .xz tarballs as it cuts 4MB for the upstream sources.
* Add nplayers.ini as a patch. Update copyright file to add CC-BY-SA-3.0.

[ Emmanuel Kasper ]
* New upstream release. Closes: #651538.
* Add Free Desktop compliant png icons of various sizes taken from
  the hydroxygen iconset
* Mess is now built from a new source package, to avoid possible source
  incompatibilities between mame and the mess overlay.
* Mame-tools are not built from the mame source package anymore, but
  from the mess source package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 
3
 
    MAME source code dependency generator
4
 
 
5
 
****************************************************************************
6
 
 
7
 
    Copyright Aaron Giles
8
 
    All rights reserved.
9
 
 
10
 
    Redistribution and use in source and binary forms, with or without
11
 
    modification, are permitted provided that the following conditions are
12
 
    met:
13
 
 
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
19
 
          distribution.
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.
23
 
 
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.
35
 
 
36
 
***************************************************************************/
37
 
 
38
 
#include <stdio.h>
39
 
#include <stdlib.h>
40
 
#include <string.h>
41
 
#include <ctype.h>
42
 
#include <zlib.h>
43
 
#include "osdcore.h"
44
 
#include "astring.h"
45
 
#include "corefile.h"
46
 
#include "tagmap.h"
47
 
 
48
 
 
49
 
/***************************************************************************
50
 
    CONSTANTS
51
 
***************************************************************************/
52
 
 
53
 
#define HASH_SIZE               193
54
 
 
55
 
 
56
 
/***************************************************************************
57
 
    TYPE DEFINITIONS
58
 
***************************************************************************/
59
 
 
60
 
typedef struct _include_path include_path;
61
 
struct _include_path
62
 
{
63
 
        include_path *  next;
64
 
        const astring * path;
65
 
};
66
 
 
67
 
 
68
 
typedef struct _exclude_path exclude_path;
69
 
struct _exclude_path
70
 
{
71
 
        exclude_path *  next;
72
 
        const astring * path;
73
 
        int                             pathlen;
74
 
        UINT8                   recursive;
75
 
};
76
 
 
77
 
 
78
 
typedef struct _list_entry list_entry;
79
 
struct _list_entry
80
 
{
81
 
        list_entry *    next;
82
 
        const astring * name;
83
 
};
84
 
 
85
 
 
86
 
typedef struct _file_entry file_entry;
87
 
 
88
 
typedef struct _dependency dependency;
89
 
struct _dependency
90
 
{
91
 
        dependency *    next;
92
 
        file_entry *    file;
93
 
};
94
 
 
95
 
 
96
 
struct _file_entry
97
 
{
98
 
        astring *               name;
99
 
        dependency *    deplist;
100
 
};
101
 
 
102
 
 
103
 
 
104
 
/***************************************************************************
105
 
    GLOBAL VARIABLES
106
 
***************************************************************************/
107
 
 
108
 
static include_path *incpaths;
109
 
static exclude_path *excpaths;
110
 
static tagmap *file_map;
111
 
 
112
 
 
113
 
 
114
 
/***************************************************************************
115
 
    PROTOTYPES
116
 
***************************************************************************/
117
 
 
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);
121
 
 
122
 
/* path helpers */
123
 
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename);
124
 
 
125
 
 
126
 
 
127
 
/***************************************************************************
128
 
    INLINE FUNCTIONS
129
 
***************************************************************************/
130
 
 
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);
134
 
 
135
 
/* path helpers */
136
 
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename);
137
 
 
138
 
 
139
 
 
140
 
/***************************************************************************
141
 
    MAIN
142
 
***************************************************************************/
143
 
 
144
 
/*-------------------------------------------------
145
 
    main - main entry point
146
 
-------------------------------------------------*/
147
 
 
148
 
int main(int argc, char *argv[])
149
 
{
150
 
        include_path **incpathhead = &incpaths;
151
 
        exclude_path **excpathhead = &excpaths;
152
 
        astring *srcdir = NULL;
153
 
        int unadorned = 0;
154
 
        int result;
155
 
        int argnum;
156
 
 
157
 
        /* loop over arguments */
158
 
        for (argnum = 1; argnum < argc; argnum++)
159
 
        {
160
 
                char *arg = argv[argnum];
161
 
 
162
 
                /* include path? */
163
 
                if (arg[0] == '-' && arg[1] == 'I')
164
 
                {
165
 
                        *incpathhead = (include_path *)malloc(sizeof(**incpathhead));
166
 
                        if (*incpathhead != NULL)
167
 
                        {
168
 
                                (*incpathhead)->next = NULL;
169
 
                                (*incpathhead)->path = astring_replacechr(astring_dupc(&arg[2]), '/', PATH_SEPARATOR[0]);
170
 
                                incpathhead = &(*incpathhead)->next;
171
 
                        }
172
 
                }
173
 
 
174
 
                /* exclude path? */
175
 
                else if (arg[0] == '-' && arg[1] == 'X')
176
 
                {
177
 
                        *excpathhead = (exclude_path *)malloc(sizeof(**excpathhead));
178
 
                        if (*excpathhead != NULL)
179
 
                        {
180
 
                                astring *path;
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;
187
 
                        }
188
 
                }
189
 
 
190
 
                /* ignore -include which is used by sdlmame to include sdlprefix.h before all other includes */
191
 
                else if (strcmp(arg,"-include") == 0)
192
 
                {
193
 
                        argnum++;
194
 
                }
195
 
 
196
 
                /* other parameter */
197
 
                else if (arg[0] != '-' && unadorned == 0)
198
 
                {
199
 
                        srcdir = astring_replacechr(astring_dupc(arg), '/', PATH_SEPARATOR[0]);
200
 
                        unadorned++;
201
 
                }
202
 
                else
203
 
                        goto usage;
204
 
        }
205
 
 
206
 
        /* make sure we got 1 parameter */
207
 
        if (srcdir == NULL)
208
 
                goto usage;
209
 
 
210
 
        /* create a tagmap for tracking files we've visited */
211
 
        file_map = tagmap_alloc();
212
 
 
213
 
        /* recurse over subdirectories */
214
 
        result = recurse_dir(astring_len(srcdir), srcdir);
215
 
 
216
 
        /* free source and destination directories */
217
 
        tagmap_free(file_map);
218
 
        astring_free(srcdir);
219
 
        return result;
220
 
 
221
 
usage:
222
 
        fprintf(stderr, "Usage:\n%s <srcroot> [-Iincpath [-Iincpath [...]]]\n", argv[0]);
223
 
        return 1;
224
 
}
225
 
 
226
 
 
227
 
 
228
 
/***************************************************************************
229
 
    CORE OUTPUT FUNCTIONS
230
 
***************************************************************************/
231
 
 
232
 
static int compare_list_entries(const void *p1, const void *p2)
233
 
{
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));
237
 
}
238
 
 
239
 
 
240
 
/*-------------------------------------------------
241
 
    recurse_dependencies - recurse through the
242
 
    dependencies found, adding the mto the tagmap
243
 
    unless we already exist in the map
244
 
-------------------------------------------------*/
245
 
 
246
 
static void recurse_dependencies(file_entry *file, tagmap *map)
247
 
{
248
 
        int filelen = astring_len(file->name);
249
 
        exclude_path *exclude;
250
 
        dependency *dep;
251
 
 
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)
256
 
                                return;
257
 
 
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)
260
 
                return;
261
 
 
262
 
        /* recurse the list from there */
263
 
        for (dep = file->deplist; dep != NULL; dep = dep->next)
264
 
                recurse_dependencies(dep->file, map);
265
 
}
266
 
 
267
 
 
268
 
/*-------------------------------------------------
269
 
    recurse_dir - recurse through a directory
270
 
-------------------------------------------------*/
271
 
 
272
 
static int recurse_dir(int srcrootlen, const astring *srcdir)
273
 
{
274
 
        static const osd_dir_entry_type typelist[] = { ENTTYPE_DIR, ENTTYPE_FILE };
275
 
        int result = 0;
276
 
        int entindex;
277
 
 
278
 
        /* iterate first over directories, then over files */
279
 
        for (entindex = 0; entindex < ARRAY_LENGTH(typelist) && result == 0; entindex++)
280
 
        {
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;
285
 
                list_entry *curlist;
286
 
                osd_directory *dir;
287
 
                int found = 0;
288
 
 
289
 
                /* open the directory and iterate through it */
290
 
                dir = osd_opendir(astring_c(srcdir));
291
 
                if (dir == NULL)
292
 
                {
293
 
                        result = 1;
294
 
                        goto error;
295
 
                }
296
 
 
297
 
                /* build up the list of files */
298
 
                while ((entry = osd_readdir(dir)) != NULL)
299
 
                        if (entry->type == entry_type && entry->name[0] != '.')
300
 
                        {
301
 
                                list_entry *lentry = (list_entry *)malloc(sizeof(*lentry));
302
 
                                lentry->name = astring_dupc(entry->name);
303
 
                                lentry->next = list;
304
 
                                list = lentry;
305
 
                                found++;
306
 
                        }
307
 
 
308
 
                /* close the directory */
309
 
                osd_closedir(dir);
310
 
 
311
 
                /* skip if nothing found */
312
 
                if (found == 0)
313
 
                        continue;
314
 
 
315
 
                /* allocate memory for sorting */
316
 
                listarray = (list_entry **)malloc(sizeof(list_entry *) * found);
317
 
                found = 0;
318
 
                for (curlist = list; curlist != NULL; curlist = curlist->next)
319
 
                        listarray[found++] = curlist;
320
 
 
321
 
                /* sort the list */
322
 
                qsort(listarray, found, sizeof(listarray[0]), compare_list_entries);
323
 
 
324
 
                /* rebuild the list */
325
 
                list = NULL;
326
 
                while (--found >= 0)
327
 
                {
328
 
                        listarray[found]->next = list;
329
 
                        list = listarray[found];
330
 
                }
331
 
                free(listarray);
332
 
 
333
 
                /* iterate through each file */
334
 
                for (curlist = list; curlist != NULL && result == 0; curlist = curlist->next)
335
 
                {
336
 
                        astring *srcfile;
337
 
 
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));
341
 
 
342
 
                        /* if we have a file, output it */
343
 
                        if (entry_type == ENTTYPE_FILE)
344
 
                        {
345
 
                                /* make sure we care, first */
346
 
                                if (core_filename_ends_with(astring_c(curlist->name), ".c"))
347
 
                                {
348
 
                                        tagmap *depend_map = tagmap_alloc();
349
 
                                        tagmap_entry *entry;
350
 
                                        file_entry *file;
351
 
                                        astring *target;
352
 
                                        int taghash;
353
 
 
354
 
                                        /* find dependencies */
355
 
                                        file = compute_dependencies(srcrootlen, srcfile);
356
 
                                        recurse_dependencies(file, depend_map);
357
 
 
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));
363
 
 
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));
368
 
 
369
 
                                        astring_free(target);
370
 
                                        tagmap_free(depend_map);
371
 
                                }
372
 
                        }
373
 
 
374
 
                        /* if we have a directory, recurse */
375
 
                        else
376
 
                                result = recurse_dir(srcrootlen, srcfile);
377
 
 
378
 
                        /* free memory for the names */
379
 
                        astring_free(srcfile);
380
 
                }
381
 
 
382
 
                /* free all the allocated entries */
383
 
                while (list != NULL)
384
 
                {
385
 
                        list_entry *next = list->next;
386
 
                        astring_free((astring *)list->name);
387
 
                        free(list);
388
 
                        list = next;
389
 
                }
390
 
        }
391
 
 
392
 
error:
393
 
        return result;
394
 
}
395
 
 
396
 
 
397
 
/*-------------------------------------------------
398
 
    output_file - output a file, converting to
399
 
    HTML
400
 
-------------------------------------------------*/
401
 
 
402
 
static file_entry *compute_dependencies(int srcrootlen, const astring *srcfile)
403
 
{
404
 
        astring *normalfile;
405
 
        UINT32 filelength;
406
 
        file_entry *file;
407
 
        char *filedata;
408
 
        int index;
409
 
 
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));
414
 
        if (file != NULL)
415
 
                return file;
416
 
 
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);
422
 
 
423
 
        /* read the source file */
424
 
        if (core_fload(astring_c(srcfile), (void **)&filedata, &filelength) != FILERR_NONE)
425
 
        {
426
 
                fprintf(stderr, "Unable to read file '%s'\n", astring_c(srcfile));
427
 
                return file;
428
 
        }
429
 
 
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)
433
 
                {
434
 
                        astring *filename, *target;
435
 
                        int scan = index;
436
 
                        dependency *dep;
437
 
                        int start;
438
 
                        int just_continue = 0;
439
 
 
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] == '"')
443
 
                                {
444
 
                                        just_continue = 1;
445
 
                                        break;
446
 
                                }
447
 
                        if (just_continue)
448
 
                                continue;
449
 
 
450
 
                        /* scan forward to find the quotes or bracket */
451
 
                        index += 7;
452
 
                        for (scan = index; scan < filelength && filedata[scan] != '<' && filedata[scan] != '"' && filedata[scan] != 13 && filedata[scan] != 10; scan++) ;
453
 
 
454
 
                        /* ignore if not found or if it's bracketed */
455
 
                        if (scan >= filelength || filedata[scan] != '"')
456
 
                                continue;
457
 
                        start = ++scan;
458
 
 
459
 
                        /* find the closing quote */
460
 
                        while (scan < filelength && filedata[scan] != '"')
461
 
                                scan++;
462
 
                        if (scan >= filelength)
463
 
                                continue;
464
 
 
465
 
                        /* find the include file */
466
 
                        filename = astring_dupch(&filedata[start], scan - start);
467
 
                        target = find_include_file(srcrootlen, srcfile, filename);
468
 
 
469
 
                        /* create a new dependency */
470
 
                        if (target != NULL)
471
 
                        {
472
 
                                dep = (dependency *)malloc(sizeof(*dep));
473
 
                                dep->next = file->deplist;
474
 
                                file->deplist = dep;
475
 
                                dep->file = compute_dependencies(srcrootlen, target);
476
 
                                astring_free(target);
477
 
                        }
478
 
 
479
 
                        astring_free(filename);
480
 
                }
481
 
 
482
 
        free(filedata);
483
 
        return file;
484
 
}
485
 
 
486
 
 
487
 
 
488
 
/***************************************************************************
489
 
    HELPERS
490
 
***************************************************************************/
491
 
 
492
 
/*-------------------------------------------------
493
 
    find_include_file - find an include file
494
 
-------------------------------------------------*/
495
 
 
496
 
static astring *find_include_file(int srcrootlen, const astring *srcfile, const astring *filename)
497
 
{
498
 
        include_path *curpath;
499
 
 
500
 
        /* iterate over include paths and find the file */
501
 
        for (curpath = incpaths; curpath != NULL; curpath = curpath->next)
502
 
        {
503
 
                astring *srcincpath = astring_dup(curpath->path);
504
 
                core_file *testfile;
505
 
                int lastsepindex = 0;
506
 
                int sepindex;
507
 
 
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]));
511
 
 
512
 
                /* append the filename piecemeal to account for directories */
513
 
                while ((sepindex = astring_chr(filename, lastsepindex, '/')) != -1)
514
 
                {
515
 
                        astring *pathpart = astring_dupsubstr(filename, lastsepindex, sepindex - lastsepindex);
516
 
 
517
 
                        /* handle .. by removing a chunk from the incpath */
518
 
                        if (astring_cmpc(pathpart, "..") == 0)
519
 
                        {
520
 
                                int sepindex = astring_rchr(srcincpath, 0, PATH_SEPARATOR[0]);
521
 
                                if (sepindex != -1)
522
 
                                        astring_substr(srcincpath, 0, sepindex);
523
 
                        }
524
 
 
525
 
                        /* otherwise, append a path separator and the pathpart */
526
 
                        else
527
 
                                astring_cat(astring_catc(srcincpath, PATH_SEPARATOR), pathpart);
528
 
 
529
 
                        /* advance past the previous index */
530
 
                        lastsepindex = sepindex + 1;
531
 
 
532
 
                        /* free the path part we extracted */
533
 
                        astring_free(pathpart);
534
 
                }
535
 
 
536
 
                /* now append the filename */
537
 
                astring_catsubstr(astring_catc(srcincpath, PATH_SEPARATOR), filename, lastsepindex, -1);
538
 
 
539
 
                /* see if we can open it */
540
 
                if (core_fopen(astring_c(srcincpath), OPEN_FLAG_READ, &testfile) == FILERR_NONE)
541
 
                {
542
 
                        /* close the file */
543
 
                        core_fclose(testfile);
544
 
                        return srcincpath;
545
 
                }
546
 
 
547
 
                /* free our include path */
548
 
                astring_free(srcincpath);
549
 
        }
550
 
        return NULL;
551
 
}