1#!/usr/bin/env python3
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2018, Linaro Limited
5#
6
7def get_args():
8    import argparse
9
10    parser = argparse.ArgumentParser()
11    parser.add_argument('--name', required=True,
12                        help='Name of variable in C file')
13    parser.add_argument('--out', required=True, help='Name of C file')
14    parser.add_argument('--inf', required=True, help='Name of in file')
15
16    return parser.parse_args()
17
18def main():
19    import array
20
21    args = get_args()
22
23    inf = open(args.inf, 'r')
24    f = open(args.out, 'w')
25
26    f.write("/* automatically generated */\n")
27    f.write("#include <stdint.h>\n")
28    f.write("#include <stddef.h>\n\n")
29
30    f.write("const uint8_t " + args.name + "[] = {\n")
31    i = 0
32    for x in array.array("B", map(ord, (inf.read()))):
33        f.write("0x" + '{0:02x}'.format(x) + ",")
34        i = i + 1
35        if i % 8 == 0:
36            f.write("\n")
37        else:
38            f.write(" ")
39
40    f.write("'\\0'};\n")
41    f.write("const size_t " + args.name + "_size = sizeof(" +
42            args.name + ") - 1;\n")
43
44    f.close()
45    inf.close()
46
47if __name__ == "__main__":
48    main()
49