blob: 247f5418dadb1304bfcc4325ab25553af130f677 [file] [log] [blame]
Tim Edwards51f81422020-07-26 12:49:48 -04001#!/usr/bin/env python3
2#
3# create_lib_library.py
4#
5#----------------------------------------------------------------------------
6# Given a destination directory holding individual liberty files of a number
7# of cells, create a single liberty library file named <alllibname> and place
8# it in the same directory. This is done for the option "compile" if specified
9# for the "-lib" 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_lib_library <destlibdir> <destlib> [-compile-only] ')
23 print(' [-excludelist="file1,file2,..."]')
24 print('')
25 print('Create a single liberty library from a set of individual liberty files.')
26 print('')
27 print('where:')
28 print(' <destlibdir> is the directory containing the individual liberty 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#----------------------------------------------------------------------------
Tim Edwards51f81422020-07-26 12:49:48 -040035# Warning: This script is unfinished. Needs to parse the library header
36# in each cell and generate a new library header combining the contents of
37# all cell headers. Also: The library name in the header needs to be
38# changed to the full library name. Also: There is no mechanism for
39# collecting all files belonging to a single process corner/temperature/
40# voltage.
Tim Edwards9be4ac22020-07-26 12:59:30 -040041#----------------------------------------------------------------------------
Tim Edwards51f81422020-07-26 12:49:48 -040042
Tim Edwards9be4ac22020-07-26 12:59:30 -040043def create_lib_library(destlibdir, destlib, do_compile_only=False, excludelist=[]):
Tim Edwards51f81422020-07-26 12:49:48 -040044
Tim Edwards05e66eb2020-09-24 13:11:59 -040045 # destlib should not have a file extension
46 destlibrooot = os.path.splitext(destlib)[0]
47
48 alllibname = destlibdir + '/' + destlibroot + '.lib'
Tim Edwards51f81422020-07-26 12:49:48 -040049 if os.path.isfile(alllibname):
50 os.remove(alllibname)
51
Tim Edwards05e66eb2020-09-24 13:11:59 -040052 print('Diagnostic: Creating consolidated liberty library ' + destlibroot + '.lib')
Tim Edwards51f81422020-07-26 12:49:48 -040053
Tim Edwards995c1332020-09-25 15:33:58 -040054 # If file "filelist.txt" exists in the directory, get the list of files from it
55 if os.path.exists(destlibdir + '/filelist.txt'):
56 with open(destlibdir + '/filelist.txt', 'r') as ifile:
57 rlist = ifile.read().splitlines()
58 llist = []
59 for rfile in rlist:
60 llist.append(destlibdir + '/' + rfile)
61 else:
62 llist = glob.glob(destlibdir + '/*.lib')
63
Tim Edwards51f81422020-07-26 12:49:48 -040064 # Create exclude list with glob-style matching using fnmatch
65 if len(llist) > 0:
66 llistnames = list(os.path.split(item)[1] for item in llist)
67 notllist = []
68 for exclude in excludelist:
69 notllist.extend(fnmatch.filter(llistnames, exclude))
70
71 # Apply exclude list
72 if len(notllist) > 0:
73 for file in llist[:]:
74 if os.path.split(file)[1] in notllist:
75 llist.remove(file)
76
77 if len(llist) > 1:
78 print('New file is: ' + alllibname)
79 with open(alllibname, 'w') as ofile:
80 headerdone = False
81 for lfile in llist:
Tim Edwards4697a172021-11-05 22:47:07 -040082 if not os.path.exists(lfile):
83 print('Error: File ' + lfile + ' not found (skipping).')
84 continue
Tim Edwards51f81422020-07-26 12:49:48 -040085 with open(lfile, 'r') as ifile:
86 # print('Adding ' + lfile + ' to library.')
87 ltext = ifile.read()
88 llines = ltext.splitlines()
89 headerseen = False
90 for lline in llines:
91 if headerdone:
92 if not headerseen:
93 if not lline.split()[0] == 'cell':
94 continue
95 else:
96 headerseen = True
97 print(lline, file=ofile)
98 headerdone = True
99 print('/*--------EOF---------*/\n', file=ofile)
100
101 if do_compile_only == True:
102 print('Compile-only: Removing individual LEF files')
103 for lfile in llist:
104 if os.path.isfile(lfile):
105 os.remove(lfile)
Tim Edwards51f81422020-07-26 12:49:48 -0400106 else:
107 print('Only one file (' + str(llist) + '); ignoring "compile" option.')
108
109#----------------------------------------------------------------------------
110
111if __name__ == '__main__':
112
113 if len(sys.argv) == 1:
114 usage()
115 sys.exit(0)
116
117 argumentlist = []
118
119 # Defaults
120 do_compile_only = False
121 excludelist = []
122
123 # Break arguments into groups where the first word begins with "-".
124 # All following words not beginning with "-" are appended to the
125 # same list (optionlist). Then each optionlist is processed.
126 # Note that the first entry in optionlist has the '-' removed.
127
128 for option in sys.argv[1:]:
129 if option.find('-', 0) == 0:
130 keyval = option[1:].split('=')
131 if keyval[0] == 'compile-only':
132 if len(keyval) > 0:
Tim Edwards9be4ac22020-07-26 12:59:30 -0400133 if keyval[1].tolower() == 'true' or keyval[1].tolower() == 'yes' or keyval[1] == '1':
Tim Edwards51f81422020-07-26 12:49:48 -0400134 do_compile_only = True
135 else:
136 do_compile_only = True
137 elif keyval[1] == 'exclude' or key == 'excludelist':
138 if len(keyval) > 0:
139 excludelist = keyval[1].trim('"').split(',')
140 else:
141 print("No items in exclude list (ignoring).")
142 else:
143 print("Unknown option '" + keyval[0] + "' (ignoring).")
144 else:
145 argumentlist.append(option)
146
147 if len(argumentlist) < 3:
148 print("Not enough arguments given to create_lib_library.py.")
149 usage()
150 sys.exit(1)
151
152 destlibdir = argumentlist[0]
153 destlib = argumentlist[1]
154 startup_script = argumentlist[2]
155
156 print('')
157 print('Create liberty library from files:')
158 print('')
159 print('Path to files: ' + destlibdir)
160 print('Name of compiled library: ' + destlib + '.lib')
161 print('Remove individual files: ' + 'Yes' if do_compile_only else 'No')
162 if len(excludelist) > 0:
163 print('List of files to exclude: ')
164 for file in excludelist:
165 print(file)
166 print('')
167
168 create_lib_library(destlibdir, destlib, do_compile_only, excludelist)
169 print('Done.')
170 sys.exit(0)
171
172#----------------------------------------------------------------------------