|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# ---------------------------------------------------------------------------- |
| 4 | +# "THE BEER-WARE LICENSE" (Revision 42): |
| 5 | +# <[email protected]> wrote this file. As long as you retain this notice you |
| 6 | +# can do whatever you want with this stuff. If we meet some day, and you think |
| 7 | +# this stuff is worth it, you can buy me a beer in return. Joerg Wunsch |
| 8 | +# ---------------------------------------------------------------------------- |
| 9 | + |
| 10 | +# Tool to extract some data out of a bunch of GCC specs files, |
| 11 | +# providing snippets for gen-avr-lib-tree.sh, and <avr/io.h>. |
| 12 | +# |
| 13 | +# Intended to run on specs files from Microchip "device packs", to |
| 14 | +# ease integration of new devices. |
| 15 | + |
| 16 | +import sys |
| 17 | +import re |
| 18 | + |
| 19 | +def usage(): |
| 20 | + print('usage: spec2libtree.py <specfiles>', file = sys.stderr) |
| 21 | + sys.exit(64) |
| 22 | + |
| 23 | +def parse_specs(name): |
| 24 | + mcuclass = '' |
| 25 | + flashsize = '' |
| 26 | + crtfile = '' |
| 27 | + devname = '' |
| 28 | + macro = '' |
| 29 | + header = '' |
| 30 | + # pre-compile regexps |
| 31 | + mcu_re = re.compile('.*-mmcu=([^ ]+) ') |
| 32 | + flash_re = re.compile('.*--pmem-wrap-around=([\d]+)') |
| 33 | + crt_re = re.compile('.*(crt[a-z0-9]+\.o)') |
| 34 | + devname_re = re.compile('.*-D(__AVR[^ ]+__).*-D__AVR_DEVICE_NAME__=([a-z0-9]+).*-D__AVR_DEV_LIB_NAME__=([A-Za-z0-9]+)') |
| 35 | + |
| 36 | + with open(name) as f: |
| 37 | + l = f.readlines() |
| 38 | + i = 0 |
| 39 | + for line in l: |
| 40 | + x = line.find('self_spec') |
| 41 | + if x >= 0: |
| 42 | + # next line is supposed to have the MCU group |
| 43 | + m = mcu_re.match(l[i + 1]) |
| 44 | + if m: |
| 45 | + mcuclass = m.group(1) |
| 46 | + x = line.find('link_pmem_wrap') |
| 47 | + if x >= 0: |
| 48 | + # if next line has --pmem-wrap-around=64k this gets |
| 49 | + # us the flash size; if empty, assume 128k; |
| 50 | + # flash size is only used to distinguish between |
| 51 | + # ${CFLAGS_SPACE} and ${CFLAGS_BIG_MEMORY} |
| 52 | + m = flash_re.match(l[i + 1]) |
| 53 | + if m: |
| 54 | + flashsize = m.group(1) |
| 55 | + else: |
| 56 | + flashsize = 128 |
| 57 | + x = line.find('avrlibc_startfile') |
| 58 | + if x >= 0: |
| 59 | + m = crt_re.match(l[i + 1]) |
| 60 | + if m: |
| 61 | + crtfile = m.group(1) |
| 62 | + x = line.find('cpp') |
| 63 | + if x >= 0: |
| 64 | + # next line has CPP macros, among them the device name |
| 65 | + m = devname_re.match(l[i + 1]) |
| 66 | + if m: |
| 67 | + devname = m.group(2) |
| 68 | + macro = m.group(1) |
| 69 | + header = m.group(3) |
| 70 | + i = i + 1 |
| 71 | + return (mcuclass, flashsize, crtfile, devname, macro, header) |
| 72 | + return None |
| 73 | + |
| 74 | +classes = {} |
| 75 | + |
| 76 | +if len(sys.argv) <= 1: |
| 77 | + usage() |
| 78 | + |
| 79 | +# parse all input files |
| 80 | +for arg in sys.argv[1:]: |
| 81 | + result = parse_specs(arg) |
| 82 | + if result != None: |
| 83 | + (mcuclass, flashsize, crtfile, devname, macro, header) = result |
| 84 | + key = mcuclass.upper() + '_DEV_INFO' |
| 85 | + data = devname + ':' + crtfile + ':${DEV_DEFS}:' |
| 86 | + if int(flashsize) >= 64: |
| 87 | + data += '${CFLAGS_BIG_MEMORY}:${DEV_ASFLAGS};\\' |
| 88 | + else: |
| 89 | + data += '${CFLAGS_SPACE}:${DEV_ASFLAGS};\\' |
| 90 | + try: |
| 91 | + classes[key].append((data, macro, header)) |
| 92 | + except KeyError: |
| 93 | + classes[key] = [(data, macro, header)] |
| 94 | + |
| 95 | +k = list(classes.keys()) |
| 96 | +k.sort() |
| 97 | + |
| 98 | +# output #1: gen-avr-lib-tree.sh snippets |
| 99 | +for key in k: |
| 100 | + d = classes[key] |
| 101 | + print(key + '="\\') |
| 102 | + i = 0 |
| 103 | + for data in d: |
| 104 | + data = data[0] # remove macro and header |
| 105 | + if i == len(d) - 1: |
| 106 | + # drop semicolon |
| 107 | + print(data[:-2] + data[-1:]) |
| 108 | + else: |
| 109 | + print(data) |
| 110 | + i += 1 |
| 111 | + print('"') |
| 112 | + |
| 113 | +# output #2: <avr/io.h> addition |
| 114 | +for key in k: |
| 115 | + d = classes[key] |
| 116 | + for data in d: |
| 117 | + print('#elif defined (' + data[1] + ')') |
| 118 | + print('# include <avr/io' + data[2] + '.h>') |
0 commit comments