blob: 4a46e5f4db73185ec7d2b326dff014d4c21f573a [file] [log] [blame]
Tim Edwards51f81422020-07-26 12:49:48 -04001#!/usr/bin/env python3
2#
3# create_lef_library.py
4#
5#----------------------------------------------------------------------------
6# Given a destination directory holding individual LEF files of a number
7# of cells, create a single LEF library file named <alllibname> and place
8# it in the same directory. This is done for the option "compile" if specified
9# for the "-lef" install.
10#----------------------------------------------------------------------------
11
12import sys
13import os
14import glob
15import fnmatch
16
Tim Edwards9be4ac22020-07-26 12:59:30 -040017#----------------------------------------------------------------------------
18
Tim Edwards51f81422020-07-26 12:49:48 -040019def usage():
20 print('')
21 print('Usage:')
22 print(' create_lef_library <destlibdir> <destlib> [-compile-only]')
23 print(' [-excludelist="file1,file2,..."]')
24 print('')
25 print('Create a single LEF library from a set of individual LEF files.')
26 print('')
27 print('where:')
28 print(' <destlibdir> is the directory containing the individual LEF files')
29 print(' <destlib> is the root name of the library file')
30 print(' -compile-only remove the indidual files if specified')
31 print(' -excludelist= is a comma-separated list of files to ignore')
32 print('')
33
Tim Edwards9be4ac22020-07-26 12:59:30 -040034#----------------------------------------------------------------------------
35
36def create_lef_library(destlibdir, destlib, do_compile_only=False, excludelist=[]):
Tim Edwards51f81422020-07-26 12:49:48 -040037
Tim Edwards05e66eb2020-09-24 13:11:59 -040038 # destlib should not have a file extension
39 destlibroot = os.path.splitext(destlib)[0]
40
41 alllibname = destlibdir + '/' + destlibroot + '.lef'
Tim Edwards51f81422020-07-26 12:49:48 -040042 if os.path.isfile(alllibname):
43 os.remove(alllibname)
44
Tim Edwards05e66eb2020-09-24 13:11:59 -040045 print('Diagnostic: Creating consolidated LEF library ' + destlibroot + '.lef')
Tim Edwards995c1332020-09-25 15:33:58 -040046
47 # If file "filelist.txt" exists in the directory, get the list of files from it
48 if os.path.exists(destlibdir + '/filelist.txt'):
49 with open(destlibdir + '/filelist.txt', 'r') as ifile:
50 rlist = ifile.read().splitlines()
51 llist = []
52 for rfile in rlist:
53 llist.append(destlibdir + '/' + rfile)
54 else:
55 llist = glob.glob(destlibdir + '/*.lef')
56
Tim Edwards51f81422020-07-26 12:49:48 -040057 if alllibname in llist:
58 llist.remove(alllibname)
59
60 # Create exclude list with glob-style matching using fnmatch
61 if len(llist) > 0:
62 llistnames = list(os.path.split(item)[1] for item in llist)
63 notllist = []
64 for exclude in excludelist:
65 notllist.extend(fnmatch.filter(llistnames, exclude))
66
67 # Apply exclude list
68 if len(notllist) > 0:
69 for file in llist[:]:
70 if os.path.split(file)[1] in notllist:
71 llist.remove(file)
72
73 if len(llist) > 1:
74 print('New file is: ' + alllibname)
75 with open(alllibname, 'w') as ofile:
76 headerdone = False
77 for lfile in llist:
78 with open(lfile, 'r') as ifile:
79 # print('Adding ' + lfile + ' to library.')
80 ltext = ifile.read()
81 llines = ltext.splitlines()
82 headerseen = False
83 for lline in llines:
84 if headerdone:
85 if not headerseen:
86 if not lline.startswith('MACRO'):
87 continue
88 else:
89 headerseen = True
90 ltok = lline.split()
Tim Edwards019ddf22020-07-26 13:25:16 -040091 if len(ltok) > 1 and ltok[0] == 'END' and ltok[1] == 'LIBRARY':
Tim Edwards51f81422020-07-26 12:49:48 -040092 # Remove "END LIBRARY" line from individual files
93 pass
94 else:
95 print(lline, file=ofile)
96 headerdone = True
97 print('#--------EOF---------\n', file=ofile)
98
Tim Edwards019ddf22020-07-26 13:25:16 -040099 # Add "END LIBRARY" to the end of the library file
100 print('', file=ofile)
101 print('END LIBRARY', file=ofile)
102
Tim Edwards51f81422020-07-26 12:49:48 -0400103 if do_compile_only == True:
104 print('Compile-only: Removing individual LEF files')
105 for lfile in llist:
106 if os.path.isfile(lfile):
107 os.remove(lfile)
Tim Edwards51f81422020-07-26 12:49:48 -0400108 else:
109 print('Only one file (' + str(llist) + '); ignoring "compile" option.')
110
111#----------------------------------------------------------------------------
112
113if __name__ == '__main__':
114
115 if len(sys.argv) == 1:
116 usage()
117 sys.exit(0)
118
119 argumentlist = []
120
121 # Defaults
122 do_compile_only = False
123 excludelist = []
124
125 # Break arguments into groups where the first word begins with "-".
126 # All following words not beginning with "-" are appended to the
127 # same list (optionlist). Then each optionlist is processed.
128 # Note that the first entry in optionlist has the '-' removed.
129
130 for option in sys.argv[1:]:
131 if option.find('-', 0) == 0:
132 keyval = option[1:].split('=')
133 if keyval[0] == 'compile-only':
134 if len(keyval) > 0:
Tim Edwards9be4ac22020-07-26 12:59:30 -0400135 if keyval[1].tolower() == 'true' or keyval[1].tolower() == 'yes' or keyval[1] == '1':
Tim Edwards51f81422020-07-26 12:49:48 -0400136 do_compile_only = True
137 else:
138 do_compile_only = True
139 elif keyval[1] == 'exclude' or key == 'excludelist':
140 if len(keyval) > 0:
141 excludelist = keyval[1].trim('"').split(',')
142 else:
143 print("No items in exclude list (ignoring).")
144 else:
145 print("Unknown option '" + keyval[0] + "' (ignoring).")
146 else:
147 argumentlist.append(option)
148
149 if len(argumentlist) < 2:
150 print("Not enough arguments given to create_lef_library.py.")
151 usage()
152 sys.exit(1)
153
154 destlibdir = argumentlist[0]
155 destlib = argumentlist[1]
156
157 print('')
158 print('Create LEF library from files:')
159 print('')
160 print('Path to files: ' + destlibdir)
161 print('Name of compiled library: ' + destlib + '.lef')
162 print('Remove individual files: ' + 'Yes' if do_compile_only else 'No')
163 if len(excludelist) > 0:
164 print('List of files to exclude: ')
165 for file in excludelist:
166 print(file)
167 print('')
168
169 create_lef_library(destlibdir, destlib, do_compile_only, excludelist)
170 print('Done.')
171 sys.exit(0)
172
173#----------------------------------------------------------------------------