aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/rodata2cpp.py
blob: be67d3af53aa3a20035c34649290e247a5fcd36b (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
import argparse


def main():
    parser = argparse.ArgumentParser(description='Convert rodata into C++ source with embedded file content')
    parser.add_argument('symbol', help='symbol name exported from generated file')
    parser.add_argument('rodata', type=argparse.FileType('rb'), help='input .rodata file path')
    parser.add_argument('cpp', type=argparse.FileType('w', encoding='UTF-8'), help='destination .cpp file path')

    args = parser.parse_args()
    args.cpp.write('static_assert(sizeof(unsigned int) == 4, "ups, something gone wrong");\n\n')
    args.cpp.write('extern "C" {\n')
    args.cpp.write('    extern const unsigned char ' + args.symbol + '[] = {\n')

    cnt = 0

    for ch in args.rodata.read():
        args.cpp.write('0x%02x, ' % ch)

        cnt += 1

        if cnt % 50 == 1:
            args.cpp.write('\n')

    args.cpp.write('    };\n')
    args.cpp.write('    extern const unsigned int ' + args.symbol + 'Size = sizeof(' + args.symbol + ');\n')
    args.cpp.write('}\n')

    args.rodata.close()
    args.cpp.close()


if __name__ == '__main__':
    main()