aboutsummaryrefslogtreecommitdiffstats
path: root/meson/test cases/common/168 preserve gendir/genprog.py
blob: 681c43a84490f57a7da173ed9d82920b081d5f14 (plain)
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
#!/usr/bin/env python3

import os, sys, argparse

h_templ = '''#pragma once

int %s(void);
'''

c_templ = '''#include"%s.h"

int %s(void) {
    return 0;
}
'''

parser = argparse.ArgumentParser()
parser.add_argument('--searchdir', required=True)
parser.add_argument('--outdir', required=True)
parser.add_argument('ifiles', nargs='+')

options = parser.parse_args()

searchdir = options.searchdir
outdir = options.outdir
ifiles = options.ifiles

rel_ofiles = []

for ifile in ifiles:
    if not ifile.startswith(options.searchdir):
        sys.exit(f'Input file {ifile} does not start with search dir {searchdir}.')
    rel_ofile = ifile[len(searchdir):]
    if rel_ofile[0] == '/' or rel_ofile[0] == '\\':
        rel_ofile = rel_ofile[1:]
    rel_ofiles.append(os.path.splitext(rel_ofile)[0])

ofile_bases = [os.path.join(outdir, i) for i in rel_ofiles]

for i, ifile_name in enumerate(ifiles):
    proto_name = open(ifile_name).readline().strip()
    h_out = ofile_bases[i] + '.h'
    c_out = ofile_bases[i] + '.c'
    os.makedirs(os.path.split(ofile_bases[i])[0], exist_ok=True)
    open(h_out, 'w').write(h_templ % (proto_name))
    open(c_out, 'w').write(c_templ % (proto_name, proto_name))