summaryrefslogtreecommitdiffstatshomepage
path: root/scripts/build/makedep.py
blob: 6867a6cb34ac6baa23fd38794ede06409fcb032b (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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#!/usr/bin/python
##
## license:BSD-3-Clause
## copyright-holders:Vas Crabb

import argparse
import io
import os.path
import sys


class ParserBase(object):
    def process_lines(self, f):
        self.input_line = 1
        for line in f:
            pos = 0
            start = 0
            if line.endswith('\n'):
                line = line[:-1]
            used = 0
            while used is not None:
                start += used
                used = self.processors[self.parse_state](line[start:])
            self.input_line += 1


class CppParser(ParserBase):
    TOKEN_LEAD = frozenset(
            [chr(x) for x in range(ord('A'), ord('Z') + 1)] +
            [chr(x) for x in range(ord('a'), ord('z') + 1)] +
            ['_'])
    TOKEN_CONTINUATION = frozenset(
            [chr(x) for x in range(ord('0'), ord('9') + 1)] +
            [chr(x) for x in range(ord('A'), ord('Z') + 1)] +
            [chr(x) for x in range(ord('a'), ord('z') + 1)] +
            ['_'])
    HEXADECIMAL_DIGIT = frozenset(
            [chr(x) for x in range(ord('0'), ord('9') + 1)] +
            [chr(x) for x in range(ord('A'), ord('F') + 1)] +
            [chr(x) for x in range(ord('a'), ord('f') + 1)])

    class Handler(object):
        def line(self, text):
            pass

        def comment(self, text):
            pass

        def line_comment(self, text):
            pass

    class ParseState(object):
        DEFAULT = 0
        COMMENT = 1
        LINE_COMMENT = 2
        TOKEN = 3
        STRING_CONSTANT = 4
        CHARACTER_CONSTANT = 5
        NUMERIC_CONSTANT = 6

    def __init__(self, handler, **kwargs):
        super(CppParser, self).__init__(**kwargs)
        self.handler = handler
        self.processors = {
                self.ParseState.DEFAULT: self.process_default,
                self.ParseState.COMMENT: self.process_comment,
                self.ParseState.LINE_COMMENT: self.process_line_comment,
                self.ParseState.TOKEN: self.process_token,
                self.ParseState.STRING_CONSTANT: self.process_text,
                self.ParseState.CHARACTER_CONSTANT: self.process_text,
                self.ParseState.NUMERIC_CONSTANT: self.process_numeric }

    def parse(self, f):
        self.parse_state = self.ParseState.DEFAULT
        self.comment_line = None
        self.lead_digit = None
        self.radix = None
        self.line_buffer = ''
        self.comment_buffer = ''
        self.process_lines(f)
        if self.parse_state == self.ParseState.COMMENT:
            raise Exception('unterminated multi-line comment beginning on line %d' % (self.comment_line, ))
        elif self.parse_state == self.ParseState.CHARACTER_CONSTANT:
            raise Exception('unterminated character literal on line %d' % (self.input_line, ))
        elif self.parse_state == self.ParseState.STRING_CONSTANT:
            raise Exception('unterminated string literal on line %d' % (self.input_line, ))

    def process_default(self, line):
        escape = False
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if (ch == '"') or (ch == "'"):
                self.parse_state = self.ParseState.STRING_CONSTANT if ch == '"' else self.ParseState.CHARACTER_CONSTANT
                self.line_buffer += line[:pos + 1]
                return pos + 1
            elif ch == '*':
                if escape:
                    self.parse_state = self.ParseState.COMMENT
                    self.comment_line = self.input_line
                    self.line_buffer += line[:pos - 1] + ' '
                    return pos + 1
            elif ch == '/':
                if escape:
                    self.parse_state = self.ParseState.LINE_COMMENT
                    self.handler.line(self.line_buffer + line[:pos - 1] + ' ')
                    self.line_buffer = ''
                    return pos + 1
            elif ch in self.TOKEN_LEAD:
                self.parse_state = self.ParseState.TOKEN
                self.line_buffer += line[:pos]
                return pos
            elif (ch >= '0') and (ch <= '9'):
                self.parse_state = self.ParseState.NUMERIC_CONSTANT
                self.line_buffer += line[:pos]
                return pos
            escape = ch == '/'
            pos += 1
        if line.endswith('\\'):
            self.line_buffer += line[:-1]
        else:
            self.handler.line(self.line_buffer + line)
            self.line_buffer = ''

    def process_comment(self, line):
        escape = False
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if escape and (ch == '/'):
                self.parse_state = self.ParseState.DEFAULT
                self.comment_line = None
                self.handler.comment(self.comment_buffer + line[:pos - 1])
                self.comment_buffer = ''
                return pos + 1
            escape = ch == '*'
            pos += 1
        if line.endswith('\\'):
            self.comment_buffer += line[:-1]
        else:
            self.comment_buffer += line + '\n'

    def process_line_comment(self, line):
        self.parse_state = self.ParseState.DEFAULT
        self.handler.line_comment(self.comment_buffer + line)
        self.comment_buffer = ''

    def process_token(self, line):
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if ch not in self.TOKEN_CONTINUATION:
                self.parse_state = self.ParseState.DEFAULT
                self.line_buffer += line[:pos]
                return pos
            pos += 1
        self.parse_state = self.ParseState.DEFAULT
        self.handler.line(self.line_buffer + line)
        self.line_buffer = ''

    def process_text(self, line):
        quote = '"' if self.parse_state == self.ParseState.STRING_CONSTANT else "'"
        escape = False
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if (ch == quote) and not escape:
                self.parse_state = self.ParseState.DEFAULT
                self.line_buffer += line[:pos + 1]
                return pos + 1
            escape = (ch == '\\') and not escape
            pos += 1
        if line.endswith('\\'):
            self.line_buffer += line[:-1]
        else:
            t = 'string' if self.ParseState == self.ParseState.STRING_CONSTANT else 'character'
            raise Exception('unterminated %s literal on line %d' % (t, self.input_line))

    def process_numeric(self, line):
        escape = False
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if self.lead_digit is None:
                self.lead_digit = ch
                if ch != '0':
                    self.radix = 10
            elif self.radix is None:
                if ch == "'":
                    if escape:
                        raise Exception('adjacent digit separators on line %d' % (self.input_line, ))
                    else:
                        escape = True
                elif (ch == 'B') or (ch == 'b'):
                    self.radix = 2
                elif (ch == 'X') or (ch == 'x'):
                    self.radix = 16
                elif (ch >= '0') and (ch <= '7'):
                    self.radix = 8
                else:
                    self.parse_state = self.ParseState.DEFAULT # probably an argument to a token-pasting or stringifying macro
            else:
                if ch == "'":
                    if escape:
                        raise Exception('adjacent digit separators on line %d' % (self.input_line, ))
                    else:
                        escape = True
                else:
                    escape = False
                    if self.radix == 2:
                        if (ch < '0') or (ch > '1'):
                            self.parse_state = self.ParseState.DEFAULT
                    elif self.radix == 8:
                        if (ch < '0') or (ch > '7'):
                            self.parse_state = self.ParseState.DEFAULT
                    elif self.radix == 10:
                        if (ch < '0') or (ch > '9'):
                            self.parse_state = self.ParseState.DEFAULT
                    elif self.radix == 16:
                        if ch not in self.HEXADECIMAL_DIGIT:
                            self.parse_state = self.ParseState.DEFAULT
            if self.parse_state == self.ParseState.DEFAULT:
                self.lead_digit = None
                self.radix = None
                self.line_buffer += line[:pos]
                return pos
            pos += 1
        self.parse_state = self.ParseState.DEFAULT
        self.lead_digit = None
        self.radix = None
        self.handler.line(self.line_buffer + line)
        self.line_buffer = ''


class LuaParser(ParserBase):
    class Handler(object):
        def short_comment(self, text):
            pass

        def long_comment_start(self, level):
            pass

        def long_comment_line(self, text):
            pass

        def long_comment_end(self):
            pass

    class ParseState(object):
        DEFAULT = 0
        SHORT_COMMENT = 1
        LONG_COMMENT = 2
        STRING_CONSTANT = 3
        LONG_STRING_CONSTANT = 4

    def __init__(self, handler, **kwargs):
        super(LuaParser, self).__init__(**kwargs)
        self.handler = handler
        self.processors = {
                self.ParseState.DEFAULT: self.process_default,
                self.ParseState.SHORT_COMMENT: self.process_short_comment,
                self.ParseState.LONG_COMMENT: self.process_long_comment,
                self.ParseState.STRING_CONSTANT: self.process_string_constant,
                self.ParseState.LONG_STRING_CONSTANT: self.process_long_string_constant }

    def parse(self, f):
        self.parse_state = self.ParseState.DEFAULT
        self.long_bracket_level = None
        self.escape = False
        self.block_line = None
        self.block_level = None
        self.string_quote = None
        self.process_lines(f)
        if self.parse_state == self.ParseState.LONG_COMMENT:
            raise Exception('unterminated long comment beginning on line %d' % (self.block_line, ));
        elif self.parse_state == self.ParseState.STRING_CONSTANT:
            raise Exception('unterminated string literal on line %d' % (self.input_line, ));
        elif self.parse_state == self.ParseState.LONG_STRING_CONSTANT:
            raise Exception('unterminated long string literal beginning on line %d' % (self.block_line, ));

    def process_default(self, line):
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if (ch == '"') or (ch == "'"):
                self.string_quote = ch
                self.parse_state = self.ParseState.STRING_CONSTANT
                self.long_bracket_level = None
                self.escape = False
                return pos + 1;
            elif (ch == '-') and self.escape:
                self.parse_state = self.ParseState.SHORT_COMMENT
                self.long_bracket_level = None
                self.escape = False
                return pos + 1
            elif self.long_bracket_level is not None:
                if ch == '=':
                    self.long_bracket_level += 1
                elif ch == '[':
                    self.block_line = self.input_line
                    self.block_level = self.long_bracket_level
                    self.parse_state = self.ParseState.LONG_STRING_CONSTANT
                    self.long_bracket_level = None
                    self.escape = False
                    return pos + 1
                else:
                    self.long_bracket_level = None
            elif ch == '[':
                self.long_bracket_level = 0
            self.escape = ch == '-'
            pos += 1
        self.escape = False

    def process_short_comment(self, line):
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if self.long_bracket_level is not None:
                if ch == '=':
                    self.long_bracket_level += 1
                elif ch == '[':
                    self.block_line = self.input_line
                    self.block_level = self.long_bracket_level
                    self.parse_state = self.ParseState.LONG_COMMENT
                    self.long_bracket_level = None
                    self.handler.long_comment_start(self.block_level)
                    return pos + 1
                else:
                    self.long_bracket_level = None
            elif ch == '[':
                self.long_bracket_level = 0
            if self.long_bracket_level is None:
                self.handler.short_comment(line[pos:])
                self.parse_state = self.ParseState.DEFAULT
                return None
            pos += 1
        self.handler.short_comment(line)
        self.parse_state = self.ParseState.DEFAULT

    def process_long_comment(self, line):
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if self.long_bracket_level is not None:
                if ch == '=':
                    self.long_bracket_level += 1
                elif ch == ']':
                    if self.long_bracket_level == self.block_level:
                        if self.parse_state == self.ParseState.LONG_COMMENT:
                            self.handler.long_comment_line(line[:endpos])
                            self.handler.long_comment_end()
                        self.parse_state = self.ParseState.DEFAULT
                        return pos + 1
                    else:
                        self.long_bracket_level = 0
                else:
                    self.long_bracket_level = None
            elif ch == ']':
                endpos = pos
                self.long_bracket_level = 0
            pos += 1
        self.long_bracket_level = None
        self.handler.long_comment_line(line)

    def process_string_constant(self, line):
        pos = 0
        length = len(line)
        while pos < length:
            ch = line[pos]
            if (ch == self.string_quote) and not self.escape:
                self.parse_state = self.ParseState.DEFAULT
                return pos + 1
            self.escape = (ch == '\\') and not self.escape
            pos += 1
        if not self.escape:
            raise Exception('unterminated string literal on line %d' % (self.input_line, ));

    def process_long_string_constant(self, line):
        self.process_long_comment(line) # this works because they're both closed by a matching long bracket


class DriverFilter(object):
    DRIVER_CHARS = frozenset(
            [chr(x) for x in range(ord('0'), ord('9') + 1)] +
            [chr(x) for x in range(ord('a'), ord('z') + 1)] +
            ['_'])

    def __init__(self, options, **kwargs):
        super(DriverFilter, self).__init__(**kwargs)
        self.parse_filter(options.filter)
        self.parse_list(options.list)

    def write_source(self, f):
        f.write(
                '#include "emu.h"\n' \
                '\n' \
                '#include "drivenum.h"\n' \
                '\n')
        for driver in self.drivers:
            f.write('GAME_EXTERN(%s);\n' % driver)
        f.write(
                '\n' \
                'game_driver const *const driver_list::s_drivers_sorted[%d] =\n' \
                '{\n' % (len(self.drivers), ))
        for driver in self.drivers:
            f.write('\t&GAME_NAME(%s),\n' % driver)
        f.write(
                '};\n' \
                '\n' \
                'std::size_t const driver_list::s_driver_count = %d;\n' % (len(self.drivers), ))

    def parse_filter(self, path):
        def do_parse(p):
            def line_hook(text):
                text = text.strip()
                if text.startswith('#'):
                    do_parse(os.path.join(os.path.dirname(n), text[1:].lstrip()))
                elif text.startswith('+'):
                    text = text[1:].lstrip()
                    if not text:
                        sys.stderr.write('%s:%s: Empty driver name\n' % (p, parser.input_line, text))
                        sys.exit(1)
                    elif not all(x in self.DRIVER_CHARS for x in text):
                        sys.stderr.write('%s:%s: Invalid character in driver name "%s"\n' % (p, parser.input_line, text))
                        sys.exit(1)
                    includes.add(text)
                    excludes.discard(text)
                elif text.startswith('-'):
                    text = text[1:].lstrip()
                    if not text:
                        sys.stderr.write('%s:%s: Empty driver name\n' % (p, parser.input_line, text))
                        sys.exit(1)
                    elif not all(x in self.DRIVER_CHARS for x in text):
                        sys.stderr.write('%s:%s: Invalid character in driver name "%s"\n' % (p, parser.input_line, text))
                        sys.exit(1)
                    includes.discard(text)
                    excludes.add(text)
                elif text:
                    sources.add(text)

            n = os.path.normpath(p)
            if n not in filters:
                filters.add(n)
                try:
                    f = io.open(n, 'r', encoding='utf-8')
                except IOError:
                    sys.stderr.write('Unable to open filter file "%s"\n' % (p, ))
                    sys.exit(1)
                with f:
                    handler = CppParser.Handler()
                    handler.line = line_hook
                    parser = CppParser(handler)
                    try:
                        parser.parse(f)
                    except IOError:
                        sys.stderr.write('Error reading filter file "%s"\n' % (p, ))
                        sys.exit(1)
                    except Exception as e:
                        sys.stderr.write('Error parsing filter file "%s": %s\n' % (p, e))
                        sys.exit(1)

        sources = set()
        includes = set()
        excludes = set()
        filters = set()
        if path is not None:
            do_parse(path)
            sys.stderr.write('%d source file(s) found\n' % (len(sources), ))
        self.sources = frozenset(sources)
        self.includes = frozenset(includes)
        self.excludes = frozenset(excludes)

    def parse_list(self, path):
        def do_parse(p):
            def line_hook(text):
                text = text.strip()
                if text.startswith('#'):
                    do_parse(os.path.join(os.path.dirname(n), text[1:].lstrip()))
                elif text.startswith('@'):
                    parts = text[1:].lstrip().split(':', 1)
                    parts[0] = parts[0].strip()
                    if (parts[0] == 'source') and (len(parts) == 2):
                        parts[1] = parts[1].strip()
                        if not parts[1]:
                            sys.stderr.write('%s:%s: Empty source file name "%s"\n' % (p, parser.input_line, text))
                            sys.exit(1)
                        elif self.sources:
                            state['includesrc'] = parts[1] in self.sources
                    else:
                        sys.stderr.write('%s:%s: Unsupported directive "%s"\n' % (p, parser.input_line, text))
                        sys.exit(1)
                elif text:
                    if not all(x in self.DRIVER_CHARS for x in text):
                        sys.stderr.write('%s:%s: Invalid character in driver name "%s"\n' % (p, parser.input_line, text))
                        sys.exit(1)
                    elif state['includesrc'] and (text not in self.excludes):
                        drivers.add(text)

            n = os.path.normpath(p)
            if n not in lists:
                lists.add(n)
                try:
                    f = io.open(n, 'r', encoding='utf-8')
                except IOError:
                    sys.stderr.write('Unable to open list file "%s"\n' % (p, ))
                    sys.exit(1)
                with f:
                    handler = CppParser.Handler()
                    handler.line = line_hook
                    parser = CppParser(handler)
                    try:
                        parser.parse(f)
                    except IOError:
                        sys.stderr.write('Error reading list file "%s"\n' % (p, ))
                        sys.exit(1)
                    except Exception as e:
                        sys.stderr.write('Error parsing list file "%s": %s\n' % (p, e))
                        sys.exit(1)

        lists = set()
        drivers = set()
        state = object()
        state = { 'includesrc': True }
        do_parse(path)
        for driver in self.includes:
            drivers.add(driver)
        sys.stderr.write('%d driver(s) found\n' % (len(drivers), ))
        drivers.add('___empty')
        self.drivers = sorted(drivers)


def split_path(path):
    path = os.path.normpath(path)
    result = [ ]
    while True:
        dirname, basename = os.path.split(path)
        if dirname == path:
            result.insert(0, dirname)
            return result
        elif basename == path:
            result.insert(0, basename)
            return result
        else:
            result.insert(0, basename)
            path = dirname


def parse_command_line():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(title='commands', dest='command', metavar='<command>')

    subparser = subparsers.add_parser('sourcesproject', help='generate project directives for source files')
    subparser.add_argument('-r', '--root', metavar='<srcroot>', default='.', help='path to emulator source root (defaults to working directory)')
    subparser.add_argument('-t', '--target', metavar='<target>', required=True, help='generated emulator target name')
    subparser.add_argument('sources', metavar='<srcfile>', nargs='+', help='source files to include')

    subparser = subparsers.add_parser('sourcesfilter', help='generate driver filter for source files')
    subparser.add_argument('sources', metavar='<srcfile>', nargs='+', help='source files to include')

    subparser = subparsers.add_parser('driverlist', help='generate driver list source')
    subparser.add_argument('-f', '--filter', metavar='<fltfile>', help='input filter file')
    subparser.add_argument('list', metavar='<lstfile>', help='input list file')

    return parser.parse_args()


def collect_lua_directives(options):
    def short_comment_hook(text):
        if text.startswith('@'):
            name, action = text[1:].rstrip().rsplit(',', 1)
            if name not in result:
                result[name] = [ ]
            result[name].append(action)

    base = os.path.join(options.root, 'scripts', 'src')
    result = { }
    handler = LuaParser.Handler()
    handler.short_comment = short_comment_hook
    parser = LuaParser(handler)
    for name in ('bus', 'cpu', 'machine', 'sound', 'video', 'formats'):
        path = os.path.join(base, name + '.lua')
        try:
            f = io.open(path, 'r', encoding='utf-8')
        except IOError:
            sys.stderr.write('Unable to open source file "%s"\n' % (path, ))
            sys.exit(1)
        try:
            with f:
                parser.parse(f)
        except IOError:
            sys.stderr.write('Error reading source file "%s"\n' % (path, ))
            sys.exit(1)
        except Exception as e:
            sys.stderr.write('Error parsing source file "%s": %s\n' % (path, e))
            sys.exit(1)
    return result


def scan_source_dependencies(options):
    def locate_include(path):
        split = [ ]
        forward = 0
        reverse = 0
        for part in path.split('/'):
            if part and (part != '.'):
                if part != '..':
                    forward += 1
                    split.append(part)
                elif forward:
                    split.pop()
                    forward -= 1
                else:
                    split.append(part)
                    reverse += 1
        split = tuple(split)
        for incdir, depth in roots:
            if (not depth) or (not reverse):
                components = incdir + split
                depth = depth + forward - 1
            elif depth >= reverse:
                components = incdir[:-reverse] + split[reverse:]
                depth = depth + forward - reverse - 1
            else:
                components = incdir[:-depth] + split[depth:]
                depth = forward - 1
            if os.path.isfile(os.path.join(options.root, *components)):
                return components, depth
        return None, 0

    def test_siblings(relative, basename, depth):
        pathbase = '/'.join(relative) + '/'
        dirname = os.path.join(options.root, *relative)
        for ext in ('.cpp', '.ipp', '.hxx'):
            path = pathbase + basename + ext
            if (path not in seen) and os.path.isfile(os.path.join(dirname, basename + ext)):
                remaining.append((path, depth))
                seen.add(path)

    def line_hook(text):
        text = text.lstrip()
        if text.startswith('#'):
            text = text[1:].lstrip()
            if text.startswith('include'):
                text = text[7:]
                if text[:1].isspace():
                    text = text.strip()
                    if (len(text) > 2) and (text[0] == '"') and (text[-1] == '"'):
                        components, depth = locate_include(text[1:-1])
                        if components:
                            path = '/'.join(components)
                            if path not in seen:
                                remaining.append((path, depth))
                                seen.add(path)
                                base, ext = os.path.splitext(components[-1])
                                if ext.lower().startswith('.h'):
                                    components = components[:-1]
                                    test_siblings(components, base, depth)
                                    if components == ('src', 'mame', 'includes'):
                                        for aspect in ('audio', 'drivers', 'video', 'machine'):
                                            test_siblings(('src', 'mame', aspect), base, depth)

    handler = CppParser.Handler()
    handler.line = line_hook
    parser = CppParser(handler)
    seen = set('/'.join(x for x in split_path(source) if x) for source in options.sources)
    remaining = list([(x, 0) for x in seen])
    default_roots = ((('src', 'devices'), 0), (('src', 'mame'), 0), (('src', 'lib'), 0))
    while remaining:
        source, depth = remaining.pop()
        components = tuple(source.split('/'))
        roots = ((components[:-1], depth), ) + default_roots
        try:
            f = io.open(os.path.join(options.root, *components), 'r', encoding='utf-8')
        except IOError:
            sys.stderr.write('Unable to open source file "%s"\n' % (source, ))
            sys.exit(1)
        try:
            with f:
                parser.parse(f)
        except IOError:
            sys.stderr.write('Error reading source file "%s"\n' % (source, ))
            sys.exit(1)
        except Exception as e:
            sys.stderr.write('Error parsing source file "%s": %s\n' % (source, e))
            sys.exit(1)
    return seen


def write_project(options, f, mappings, sources):
    targetsrc = ''
    for source in sorted(sources):
        action = mappings.get(source)
        if action:
            for line in action:
                f.write(line + '\n')
        if source.startswith('src/mame/'):
            targetsrc += '        MAME_DIR .. "%s",\n' % (source, )
    f.write(
            '\n' \
            'function createProjects_mame_%s(_target, _subtarget)\n' \
            '    project ("mame_%s")\n' \
            '    targetsubdir(_target .."_" .. _subtarget)\n' \
            '    kind (LIBTYPE)\n' \
            '    uuid (os.uuid("drv-mame-%s"))\n' \
            '    addprojectflags()\n' \
            '    \n' \
            '    includedirs {\n' \
            '        MAME_DIR .. "src/osd",\n' \
            '        MAME_DIR .. "src/emu",\n' \
            '        MAME_DIR .. "src/devices",\n' \
            '        MAME_DIR .. "src/mame",\n' \
            '        MAME_DIR .. "src/lib",\n' \
            '        MAME_DIR .. "src/lib/util",\n' \
            '        MAME_DIR .. "src/lib/netlist",\n' \
            '        MAME_DIR .. "3rdparty",\n' \
            '        GEN_DIR  .. "mame/layout",\n' \
            '        ext_includedir("flac"),\n' \
            '        ext_includedir("glm"),\n' \
            '        ext_includedir("jpeg"),\n' \
            '        ext_includedir("rapidjson"),\n' \
            '        ext_includedir("zlib"),\n' \
            '    }\n' \
            '\n' \
            '    files{\n%s' \
            '    }\n' \
            'end\n' \
            '\n' \
            'function linkProjects_mame_%s(_target, _subtarget)\n' \
            '    links {\n' \
            '        "mame_%s",\n' \
            '    }\n' \
            'end\n' % (options.target, options.target, options.target, targetsrc, options.target, options.target))


def write_filter(options, f):
    drivers = set()
    for source in options.sources:
        components = tuple(x for x in split_path(source) if x)
        if (len(components) > 3) and (components[:3] == ('src', 'mame', 'drivers')):
            ext = os.path.splitext(components[-1])[1].lower()
            if ext.startswith('.c'):
                drivers.add('/'.join(components[3:]))
    for driver in sorted(drivers):
        f.write(driver + '\n')


if __name__ == '__main__':
    options = parse_command_line()
    if options.command == 'sourcesproject':
        header_to_optional = collect_lua_directives(options)
        source_dependencies = scan_source_dependencies(options)
        write_project(options, sys.stdout, header_to_optional, source_dependencies)
    elif options.command == 'sourcesfilter':
        write_filter(options, sys.stdout)
    elif options.command == 'driverlist':
        DriverFilter(options).write_source(sys.stdout)