blob: b883be832d38f45a5b15fef71a9037e9275a2977 [file] [log] [blame]
Tim Edwards9d3debb2020-10-20 20:52:18 -04001#!/usr/bin/env python3
Tim Edwardsc6202ef2020-09-20 17:16:33 -04002#
Tim Edwardsbfc82692020-09-20 21:33:08 -04003# rename_models ---
Tim Edwardsc6202ef2020-09-20 17:16:33 -04004#
5# This script renames include paths from SPICE files in the models
6# (libs.tech/ngspice) directory that point to the original ../cells/
7# directory, to point to the correct location in libs.ref/sky130_fd_pr/spice/
8#
9#
10# This script is a filter to be run by setting the name of this script as
11# the value to "filter=" for the model install in the sky130 Makefile.
12
13import re
14import os
15import sys
16
Tim Edwards8ae22612021-08-27 20:54:41 -040017def filter(inname, outname, ef_format = True):
18
19 if ef_format:
20 libpath = 'spi/sky130_fd_pr/'
21 else:
22 libpath = 'sky130_fd_pr/spice/'
Tim Edwardsc6202ef2020-09-20 17:16:33 -040023
24 # Read input
25 try:
26 with open(inname, 'r') as inFile:
27 spitext = inFile.read()
28 spilines = spitext.splitlines()
29 except:
Tim Edwardsbfc82692020-09-20 21:33:08 -040030 print('rename_models.py: failed to open ' + fnmIn + ' for reading.', file=sys.stderr)
Tim Edwardsc6202ef2020-09-20 17:16:33 -040031 return 1
32
33 # Process input with regexp
34
35 fixedlines = []
36 modified = False
37
38 for line in spilines:
39
40 # Fix: Replace "../cells/<name>/" with "../../libs.ref/sky130_fd_pr/spice/"
Tim Edwards8ae22612021-08-27 20:54:41 -040041 fixedline = re.sub('\.\./cells/[^/]+/', '../../libs.ref/' + libpath, line)
Tim Edwards1c74f852021-06-09 16:46:50 -040042
43 # This subsitution makes SPICE files compatible with Xyce without
44 # breaking ngspice compatibility ('$' comments changed to ';')
45 fixedline = re.sub('(.*[ \t]+)\$([ \t+].*)', '\g<1>;\g<2>', fixedline)
46
Tim Edwardsc6202ef2020-09-20 17:16:33 -040047 fixedlines.append(fixedline)
48 if fixedline != line:
49 modified = True
50
51 # Write output
52 if outname == None:
53 for i in fixedlines:
54 print(i)
55 else:
56 # If the output is a symbolic link but no modifications have been made,
57 # then leave it alone. If it was modified, then remove the symbolic
58 # link before writing.
59 if os.path.islink(outname):
60 if not modified:
61 return 0
62 else:
63 os.unlink(outname)
64 try:
65 with open(outname, 'w') as outFile:
66 for i in fixedlines:
67 print(i, file=outFile)
68 except:
Tim Edwardsbfc82692020-09-20 21:33:08 -040069 print('rename_models.py: failed to open ' + outname + ' for writing.', file=sys.stderr)
Tim Edwardsc6202ef2020-09-20 17:16:33 -040070 return 1
71
72
73if __name__ == '__main__':
74
75 # This script expects to get one or two arguments. One argument is
76 # mandatory and is the input file. The other argument is optional and
77 # is the output file. The output file and input file may be the same
78 # name, in which case the original input is overwritten.
79
80 options = []
81 arguments = []
82 for item in sys.argv[1:]:
83 if item.find('-', 0) == 0:
84 options.append(item[1:])
85 else:
86 arguments.append(item)
87
88 if len(arguments) > 0:
89 infilename = arguments[0]
Tim Edwards8ae22612021-08-27 20:54:41 -040090 else:
91 print('Usage: rename_models.py <filename> [<outfilename>] [-ef_format]')
92 sys.exit(1)
Tim Edwardsc6202ef2020-09-20 17:16:33 -040093
94 if len(arguments) > 1:
95 outfilename = arguments[1]
96 else:
97 outfilename = None
98
Tim Edwards8ae22612021-08-27 20:54:41 -040099 ef_format = False
100 if len(options) > 0:
101 if options[0] == 'ef_format':
102 ef_format = True
103
104 result = filter(infilename, outfilename, ef_format)
Tim Edwardsc6202ef2020-09-20 17:16:33 -0400105 sys.exit(result)