aboutsummaryrefslogtreecommitdiffstats
path: root/tools/update_configs.py
blob: 6bc69b9aa31de7393e5bc5e057f81354048c0ffa (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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'Fabien Poussin'
__version__ = '0.1'

import os
import re
from argparse import ArgumentParser
from traceback import print_exc
from shutil import copy

parser = ArgumentParser(description='Generate ChibiOS-Contrib config and make files from ChibiOS')
parser.add_argument('-s', '--src', default='../../ChibiOS-RT', type=str, help="ChibiOS folder")
parser.add_argument('-d', '--dst', default='..', type=str, help='ChibiOS-Contrib folder')


def makefile(lines):

    for l in range(len(lines)):

        if 'CHIBIOS =' in lines[l]:
            lines[l] = lines[l][:-1] + '/../ChibiOS-RT\n'
            lines.insert(l + 1, 'CHIBIOS_CONTRIB = $(CHIBIOS)/../ChibiOS-Contrib\n')

        if '$(CHIBIOS)/os/hal/hal.mk' in lines[l] \
                or '$(CHIBIOS)/os/hal/ports/' in lines[l] \
                or '$(CHIBIOS)/os/various' in lines[l]            :
            lines[l] = lines[l].replace('CHIBIOS', 'CHIBIOS_CONTRIB')

    return "".join(lines)


def halconf(lines):

    idx_end = lines.index('#endif /* HALCONF_H */\n')
    lines.insert(idx_end - 1, '\n')
    lines.insert(idx_end - 1, '#include "halconf_community.h"')
    lines.insert(idx_end - 1, '\n')

    return "".join(lines)


def mcuconf(lines):

    idx_end = lines.index('#endif /* MCUCONF_H */\n')
    lines.insert(idx_end - 1, '\n')
    lines.insert(idx_end - 1, '#include "mcuconf_community.h"')
    lines.insert(idx_end - 1, '\n')

    return "".join(lines)


if __name__ == '__main__':

    args = parser.parse_args()
    sources = {}

    for folder in ['testhal']:

        for family in os.scandir(args.src + '/{}/STM32/'.format(folder)):
            if not family.name[0].isupper() or not family.is_dir():
                continue

            for test in os.scandir(family.path):
                try:
                    sources[family.name] = {'makefile': None, 'halconf': None, 'mcuconf': None}

                    with open(test.path + '/Makefile', 'r') as file:
                        sources[family.name]['makefile'] = makefile(file.readlines())

                    with open(test.path + '/halconf.h', 'r') as file:
                        sources[family.name]['halconf'] = halconf(file.readlines())

                    with open(test.path + '/mcuconf.h', 'r') as file:
                        sources[family.name]['mcuconf'] = mcuconf(file.readlines())

                except Exception as e:
                    print(test.path, e)
                    del sources[family.name]
                    continue

                break

        for family in os.scandir(args.dst + '/{}/STM32/'.format(folder)):
            if not family.name[0].isupper() or not family.is_dir():
                continue

            for test in os.scandir(family.path):
                copy('templates/halconf_community.h', test.path)
                copy('templates/mcuconf_community.h', test.path)

                try:
                    with open(test.path + '/Makefile', 'w') as file:
                        file.write(sources[family.name]['makefile'])

                    with open(test.path + '/halconf.h', 'w') as file:
                        file.write(sources[family.name]['halconf'])

                    with open(test.path + '/mcuconf.h', 'w') as file:
                        file.write(sources[family.name]['mcuconf'])

                    print('updated', test.path)
                except KeyError as e:
                    print('Missing family data', e)