blob: 6fbef4967b3dcfe3952e67e0124b8176459a521b [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
Tim Edwards7519dfb2022-02-10 11:39:09 -050016import natural_sort
Tim Edwards51f81422020-07-26 12:49:48 -040017
Tim Edwards9be4ac22020-07-26 12:59:30 -040018#----------------------------------------------------------------------------
19
Tim Edwards51f81422020-07-26 12:49:48 -040020def usage():
21 print('')
22 print('Usage:')
23 print(' create_lib_library <destlibdir> <destlib> [-compile-only] ')
24 print(' [-excludelist="file1,file2,..."]')
25 print('')
26 print('Create a single liberty library from a set of individual liberty files.')
27 print('')
28 print('where:')
29 print(' <destlibdir> is the directory containing the individual liberty files')
30 print(' <destlib> is the root name of the library file')
31 print(' -compile-only remove the indidual files if specified')
32 print(' -excludelist= is a comma-separated list of files to ignore')
33 print('')
34
Tim Edwards9be4ac22020-07-26 12:59:30 -040035#----------------------------------------------------------------------------
Tim Edwards51f81422020-07-26 12:49:48 -040036# Warning: This script is unfinished. Needs to parse the library header
37# in each cell and generate a new library header combining the contents of
38# all cell headers. Also: The library name in the header needs to be
39# changed to the full library name. Also: There is no mechanism for
40# collecting all files belonging to a single process corner/temperature/
41# voltage.
Tim Edwards9be4ac22020-07-26 12:59:30 -040042#----------------------------------------------------------------------------
Tim Edwards51f81422020-07-26 12:49:48 -040043
Tim Edwards9be4ac22020-07-26 12:59:30 -040044def create_lib_library(destlibdir, destlib, do_compile_only=False, excludelist=[]):
Tim Edwards51f81422020-07-26 12:49:48 -040045
Tim Edwards05e66eb2020-09-24 13:11:59 -040046 # destlib should not have a file extension
47 destlibrooot = os.path.splitext(destlib)[0]
48
49 alllibname = destlibdir + '/' + destlibroot + '.lib'
Tim Edwards51f81422020-07-26 12:49:48 -040050 if os.path.isfile(alllibname):
51 os.remove(alllibname)
52
Tim Edwards05e66eb2020-09-24 13:11:59 -040053 print('Diagnostic: Creating consolidated liberty library ' + destlibroot + '.lib')
Tim Edwards51f81422020-07-26 12:49:48 -040054
Tim Edwards995c1332020-09-25 15:33:58 -040055 # If file "filelist.txt" exists in the directory, get the list of files from it
56 if os.path.exists(destlibdir + '/filelist.txt'):
57 with open(destlibdir + '/filelist.txt', 'r') as ifile:
58 rlist = ifile.read().splitlines()
59 llist = []
60 for rfile in rlist:
61 llist.append(destlibdir + '/' + rfile)
62 else:
63 llist = glob.glob(destlibdir + '/*.lib')
Tim Edwards7519dfb2022-02-10 11:39:09 -050064 llist = natural_sort.natural_sort(llist)
Tim Edwards995c1332020-09-25 15:33:58 -040065
Tim Edwards51f81422020-07-26 12:49:48 -040066 # Create exclude list with glob-style matching using fnmatch
67 if len(llist) > 0:
68 llistnames = list(os.path.split(item)[1] for item in llist)
69 notllist = []
70 for exclude in excludelist:
71 notllist.extend(fnmatch.filter(llistnames, exclude))
72
73 # Apply exclude list
74 if len(notllist) > 0:
75 for file in llist[:]:
76 if os.path.split(file)[1] in notllist:
77 llist.remove(file)
78
79 if len(llist) > 1:
80 print('New file is: ' + alllibname)
81 with open(alllibname, 'w') as ofile:
82 headerdone = False
83 for lfile in llist:
Tim Edwards4697a172021-11-05 22:47:07 -040084 if not os.path.exists(lfile):
85 print('Error: File ' + lfile + ' not found (skipping).')
86 continue
Tim Edwards51f81422020-07-26 12:49:48 -040087 with open(lfile, 'r') as ifile:
88 # print('Adding ' + lfile + ' to library.')
89 ltext = ifile.read()
90 llines = ltext.splitlines()
91 headerseen = False
92 for lline in llines:
93 if headerdone:
94 if not headerseen:
95 if not lline.split()[0] == 'cell':
96 continue
97 else:
98 headerseen = True
99 print(lline, file=ofile)
100 headerdone = True
101 print('/*--------EOF---------*/\n', file=ofile)
102
103 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) < 3:
150 print("Not enough arguments given to create_lib_library.py.")
151 usage()
152 sys.exit(1)
153
154 destlibdir = argumentlist[0]
155 destlib = argumentlist[1]
156 startup_script = argumentlist[2]
157
158 print('')
159 print('Create liberty library from files:')
160 print('')
161 print('Path to files: ' + destlibdir)
162 print('Name of compiled library: ' + destlib + '.lib')
163 print('Remove individual files: ' + 'Yes' if do_compile_only else 'No')
164 if len(excludelist) > 0:
165 print('List of files to exclude: ')
166 for file in excludelist:
167 print(file)
168 print('')
169
170 create_lib_library(destlibdir, destlib, do_compile_only, excludelist)
171 print('Done.')
172 sys.exit(0)
173
174#----------------------------------------------------------------------------