Tim Edwards | 55f4d0e | 2020-07-05 15:41:02 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # insert_property.py: For the given install path, library name, and cellname, |
| 4 | # find the Magic layout of the cell, and add the specified property string. |
| 5 | # If the property exists and is the same as specified, then it remains the |
| 6 | # same. If the property exists but has a different value, it is replaced. |
| 7 | # The property is added to the layout in both the mag/ (full) and maglef/ |
| 8 | # (abstract) directories. Option "-maglef" or "-mag" will restrict the |
| 9 | # use to only the view indicated by the option. |
| 10 | # |
| 11 | # e.g.: |
| 12 | # |
| 13 | # insert_property.py /home/tim/projects/efabless/tech/SkyWater/EFS8A \ |
| 14 | # s8iom0 s8iom0s8_top_gpio "FIXED_BBOX 0 607 15000 40200" |
| 15 | |
| 16 | import os |
| 17 | import re |
| 18 | import sys |
| 19 | |
| 20 | def addprop(filename, propstring, noupdate): |
| 21 | with open(filename, 'r') as ifile: |
| 22 | magtext = ifile.read().splitlines() |
| 23 | |
| 24 | propname = propstring.split()[0] |
| 25 | proprex = re.compile('<< properties >>') |
| 26 | endrex = re.compile('<< end >>') |
| 27 | |
| 28 | in_props = False |
| 29 | printed = False |
| 30 | done = False |
| 31 | |
| 32 | with open(filename, 'w') as ofile: |
| 33 | for line in magtext: |
| 34 | pmatch = proprex.match(line) |
| 35 | if pmatch: |
| 36 | in_props = True |
| 37 | elif in_props: |
| 38 | linetok = line.split() |
| 39 | if linetok[0] == 'string': |
| 40 | testname = linetok[1] |
| 41 | testval = linetok[2] |
| 42 | if testname == propname: |
| 43 | if noupdate == False: |
| 44 | print('string ' + propstring, file=ofile) |
| 45 | printed = True |
| 46 | done = True |
| 47 | |
| 48 | ematch = endrex.match(line) |
| 49 | if ematch: |
| 50 | if in_props == False: |
| 51 | print('<< properties >>', file=ofile) |
| 52 | if done == False: |
| 53 | print('string ' + propstring, file=ofile) |
| 54 | |
| 55 | if not printed: |
| 56 | print(line, file=ofile) |
| 57 | printed = False |
| 58 | |
| 59 | def usage(): |
| 60 | print("insert_property.py <path_to_pdk> <libname> <cellname> <prop_string> [option]") |
| 61 | print(" options:") |
| 62 | print(" -mag do only for the view in the mag/ directory") |
| 63 | print(" -maglef do only for the view in the maglef/ directory") |
| 64 | print(" -noupdate do not replace the property if it already exists in the file") |
| 65 | return 0 |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | |
| 69 | options = [] |
| 70 | arguments = [] |
| 71 | for item in sys.argv[1:]: |
| 72 | if item.find('-', 0) == 0: |
| 73 | options.append(item) |
| 74 | else: |
| 75 | arguments.append(item) |
| 76 | |
| 77 | if len(arguments) < 4: |
| 78 | print("Not enough options given to insert_property.py.") |
| 79 | usage() |
| 80 | sys.exit(0) |
| 81 | |
| 82 | source = arguments[0] |
| 83 | libname = arguments[1] |
| 84 | cellname = arguments[2] |
| 85 | propstring = arguments[3] |
| 86 | |
| 87 | noupdate = True if '-noupdate' in options else False |
| 88 | fail = 0 |
| 89 | |
| 90 | efformat = True if '-ef_format' in options else False |
| 91 | |
| 92 | domag = True |
| 93 | domaglef = True |
| 94 | if '-mag' in options and '-maglef' not in options: |
| 95 | domaglef = False |
| 96 | if '-maglef' in options and '-mag' not in options: |
| 97 | domag = False |
| 98 | |
| 99 | if domag: |
| 100 | if efformat: |
| 101 | filename = source + '/libs.ref/mag/' + libname + '/' + cellname + '.mag' |
| 102 | else: |
| 103 | filename = source + '/libs.ref/' + libname + '/mag/' + cellname + '.mag' |
| 104 | |
| 105 | if os.path.isfile(filename): |
| 106 | addprop(filename, propstring, noupdate) |
| 107 | else: |
| 108 | fail += 1 |
| 109 | else: |
| 110 | fail += 1 |
| 111 | |
| 112 | if domaglef: |
| 113 | if efformat: |
| 114 | filename = source + '/libs.ref/maglef/' + libname + '/' + cellname + '.mag' |
| 115 | else: |
| 116 | filename = source + '/libs.ref/' + libname + '/maglef/' + cellname + '.mag' |
| 117 | |
| 118 | if os.path.isfile(filename): |
| 119 | addprop(filename, propstring, noupdate) |
| 120 | else: |
| 121 | fail += 1 |
| 122 | else: |
| 123 | fail += 1 |
| 124 | |
| 125 | if fail == 2: |
| 126 | print('Error: No layout file in either mag/ or maglef/', file=sys.stderr) |
| 127 | if efformat: |
| 128 | print('(' + source + '/libs.ref/mag[lef]/' + libname + |
| 129 | '/' + cellname + '.mag)', file=sys.stderr) |
| 130 | else: |
| 131 | print('(' + source + '/libs.ref/' + libname + '/mag[lef]/' |
| 132 | + cellname + '.mag)', file=sys.stderr) |
| 133 | |