blob: 4b8ecf381abfe07c90a7d7db82988df2f10b9320 [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
38 alllibname = destlibdir + '/' + destlib + '.lef'
39 if os.path.isfile(alllibname):
40 os.remove(alllibname)
41
42 print('Diagnostic: Creating consolidated LEF library ' + destlib + '.lef')
43 llist = glob.glob(destlibdir + '/*.lef')
44 if alllibname in llist:
45 llist.remove(alllibname)
46
47 # Create exclude list with glob-style matching using fnmatch
48 if len(llist) > 0:
49 llistnames = list(os.path.split(item)[1] for item in llist)
50 notllist = []
51 for exclude in excludelist:
52 notllist.extend(fnmatch.filter(llistnames, exclude))
53
54 # Apply exclude list
55 if len(notllist) > 0:
56 for file in llist[:]:
57 if os.path.split(file)[1] in notllist:
58 llist.remove(file)
59
60 if len(llist) > 1:
61 print('New file is: ' + alllibname)
62 with open(alllibname, 'w') as ofile:
63 headerdone = False
64 for lfile in llist:
65 with open(lfile, 'r') as ifile:
66 # print('Adding ' + lfile + ' to library.')
67 ltext = ifile.read()
68 llines = ltext.splitlines()
69 headerseen = False
70 for lline in llines:
71 if headerdone:
72 if not headerseen:
73 if not lline.startswith('MACRO'):
74 continue
75 else:
76 headerseen = True
77 ltok = lline.split()
Tim Edwards019ddf22020-07-26 13:25:16 -040078 if len(ltok) > 1 and ltok[0] == 'END' and ltok[1] == 'LIBRARY':
Tim Edwards51f81422020-07-26 12:49:48 -040079 # Remove "END LIBRARY" line from individual files
80 pass
81 else:
82 print(lline, file=ofile)
83 headerdone = True
84 print('#--------EOF---------\n', file=ofile)
85
Tim Edwards019ddf22020-07-26 13:25:16 -040086 # Add "END LIBRARY" to the end of the library file
87 print('', file=ofile)
88 print('END LIBRARY', file=ofile)
89
Tim Edwards51f81422020-07-26 12:49:48 -040090 if do_compile_only == True:
91 print('Compile-only: Removing individual LEF files')
92 for lfile in llist:
93 if os.path.isfile(lfile):
94 os.remove(lfile)
Tim Edwards51f81422020-07-26 12:49:48 -040095 else:
96 print('Only one file (' + str(llist) + '); ignoring "compile" option.')
97
98#----------------------------------------------------------------------------
99
100if __name__ == '__main__':
101
102 if len(sys.argv) == 1:
103 usage()
104 sys.exit(0)
105
106 argumentlist = []
107
108 # Defaults
109 do_compile_only = False
110 excludelist = []
111
112 # Break arguments into groups where the first word begins with "-".
113 # All following words not beginning with "-" are appended to the
114 # same list (optionlist). Then each optionlist is processed.
115 # Note that the first entry in optionlist has the '-' removed.
116
117 for option in sys.argv[1:]:
118 if option.find('-', 0) == 0:
119 keyval = option[1:].split('=')
120 if keyval[0] == 'compile-only':
121 if len(keyval) > 0:
Tim Edwards9be4ac22020-07-26 12:59:30 -0400122 if keyval[1].tolower() == 'true' or keyval[1].tolower() == 'yes' or keyval[1] == '1':
Tim Edwards51f81422020-07-26 12:49:48 -0400123 do_compile_only = True
124 else:
125 do_compile_only = True
126 elif keyval[1] == 'exclude' or key == 'excludelist':
127 if len(keyval) > 0:
128 excludelist = keyval[1].trim('"').split(',')
129 else:
130 print("No items in exclude list (ignoring).")
131 else:
132 print("Unknown option '" + keyval[0] + "' (ignoring).")
133 else:
134 argumentlist.append(option)
135
136 if len(argumentlist) < 2:
137 print("Not enough arguments given to create_lef_library.py.")
138 usage()
139 sys.exit(1)
140
141 destlibdir = argumentlist[0]
142 destlib = argumentlist[1]
143
144 print('')
145 print('Create LEF library from files:')
146 print('')
147 print('Path to files: ' + destlibdir)
148 print('Name of compiled library: ' + destlib + '.lef')
149 print('Remove individual files: ' + 'Yes' if do_compile_only else 'No')
150 if len(excludelist) > 0:
151 print('List of files to exclude: ')
152 for file in excludelist:
153 print(file)
154 print('')
155
156 create_lef_library(destlibdir, destlib, do_compile_only, excludelist)
157 print('Done.')
158 sys.exit(0)
159
160#----------------------------------------------------------------------------