blob: 53fdba9d9fafd8ceca21646739c738ea38abb1c3 [file] [log] [blame]
Tim Edwards55f4d0e2020-07-05 15:41:02 -04001#!/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#
Tim Edwards4e5bf212021-01-06 13:11:31 -050013# insert_property.py /path/to/sky130A \
14# sky130_fd_io sky130_fd_io__top_gpiov2 "MASKHINTS_HVI 0 607 15000 40200"
Tim Edwards55f4d0e2020-07-05 15:41:02 -040015
16import os
17import re
18import sys
19
20def 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
59def 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
67if __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
Tim Edwards4e5bf212021-01-06 13:11:31 -050087 # Diagnostic
88 print('insert_property.py:')
89 print(' source = ' + source)
90 print(' library = ' + libname)
91 print(' cell = ' + cellname)
92 print(' property = ' + propstring)
93
Tim Edwards55f4d0e2020-07-05 15:41:02 -040094 noupdate = True if '-noupdate' in options else False
95 fail = 0
96
97 efformat = True if '-ef_format' in options else False
98
99 domag = True
100 domaglef = True
101 if '-mag' in options and '-maglef' not in options:
102 domaglef = False
103 if '-maglef' in options and '-mag' not in options:
104 domag = False
105
106 if domag:
107 if efformat:
108 filename = source + '/libs.ref/mag/' + libname + '/' + cellname + '.mag'
109 else:
110 filename = source + '/libs.ref/' + libname + '/mag/' + cellname + '.mag'
111
112 if os.path.isfile(filename):
113 addprop(filename, propstring, noupdate)
114 else:
115 fail += 1
116 else:
117 fail += 1
118
119 if domaglef:
120 if efformat:
121 filename = source + '/libs.ref/maglef/' + libname + '/' + cellname + '.mag'
122 else:
123 filename = source + '/libs.ref/' + libname + '/maglef/' + cellname + '.mag'
124
125 if os.path.isfile(filename):
126 addprop(filename, propstring, noupdate)
127 else:
128 fail += 1
129 else:
130 fail += 1
131
132 if fail == 2:
133 print('Error: No layout file in either mag/ or maglef/', file=sys.stderr)
134 if efformat:
135 print('(' + source + '/libs.ref/mag[lef]/' + libname +
136 '/' + cellname + '.mag)', file=sys.stderr)
137 else:
138 print('(' + source + '/libs.ref/' + libname + '/mag[lef]/'
139 + cellname + '.mag)', file=sys.stderr)
140