Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 1 | #!/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 | |
| 12 | import sys |
| 13 | import os |
| 14 | import glob |
| 15 | import fnmatch |
| 16 | |
Tim Edwards | 9be4ac2 | 2020-07-26 12:59:30 -0400 | [diff] [blame] | 17 | #---------------------------------------------------------------------------- |
| 18 | |
Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 19 | def 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 Edwards | 9be4ac2 | 2020-07-26 12:59:30 -0400 | [diff] [blame] | 34 | #---------------------------------------------------------------------------- |
Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 35 | # 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 Edwards | 9be4ac2 | 2020-07-26 12:59:30 -0400 | [diff] [blame] | 41 | #---------------------------------------------------------------------------- |
Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 42 | |
Tim Edwards | 9be4ac2 | 2020-07-26 12:59:30 -0400 | [diff] [blame] | 43 | def create_lib_library(destlibdir, destlib, do_compile_only=False, excludelist=[]): |
Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 44 | |
Tim Edwards | 05e66eb | 2020-09-24 13:11:59 -0400 | [diff] [blame] | 45 | # destlib should not have a file extension |
| 46 | destlibrooot = os.path.splitext(destlib)[0] |
| 47 | |
| 48 | alllibname = destlibdir + '/' + destlibroot + '.lib' |
Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 49 | if os.path.isfile(alllibname): |
| 50 | os.remove(alllibname) |
| 51 | |
Tim Edwards | 05e66eb | 2020-09-24 13:11:59 -0400 | [diff] [blame] | 52 | print('Diagnostic: Creating consolidated liberty library ' + destlibroot + '.lib') |
Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 53 | |
Tim Edwards | 995c133 | 2020-09-25 15:33:58 -0400 | [diff] [blame] | 54 | # 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 Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 64 | # 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: |
| 82 | with open(lfile, 'r') as ifile: |
| 83 | # print('Adding ' + lfile + ' to library.') |
| 84 | ltext = ifile.read() |
| 85 | llines = ltext.splitlines() |
| 86 | headerseen = False |
| 87 | for lline in llines: |
| 88 | if headerdone: |
| 89 | if not headerseen: |
| 90 | if not lline.split()[0] == 'cell': |
| 91 | continue |
| 92 | else: |
| 93 | headerseen = True |
| 94 | print(lline, file=ofile) |
| 95 | headerdone = True |
| 96 | print('/*--------EOF---------*/\n', file=ofile) |
| 97 | |
| 98 | if do_compile_only == True: |
| 99 | print('Compile-only: Removing individual LEF files') |
| 100 | for lfile in llist: |
| 101 | if os.path.isfile(lfile): |
| 102 | os.remove(lfile) |
Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 103 | else: |
| 104 | print('Only one file (' + str(llist) + '); ignoring "compile" option.') |
| 105 | |
| 106 | #---------------------------------------------------------------------------- |
| 107 | |
| 108 | if __name__ == '__main__': |
| 109 | |
| 110 | if len(sys.argv) == 1: |
| 111 | usage() |
| 112 | sys.exit(0) |
| 113 | |
| 114 | argumentlist = [] |
| 115 | |
| 116 | # Defaults |
| 117 | do_compile_only = False |
| 118 | excludelist = [] |
| 119 | |
| 120 | # Break arguments into groups where the first word begins with "-". |
| 121 | # All following words not beginning with "-" are appended to the |
| 122 | # same list (optionlist). Then each optionlist is processed. |
| 123 | # Note that the first entry in optionlist has the '-' removed. |
| 124 | |
| 125 | for option in sys.argv[1:]: |
| 126 | if option.find('-', 0) == 0: |
| 127 | keyval = option[1:].split('=') |
| 128 | if keyval[0] == 'compile-only': |
| 129 | if len(keyval) > 0: |
Tim Edwards | 9be4ac2 | 2020-07-26 12:59:30 -0400 | [diff] [blame] | 130 | if keyval[1].tolower() == 'true' or keyval[1].tolower() == 'yes' or keyval[1] == '1': |
Tim Edwards | 51f8142 | 2020-07-26 12:49:48 -0400 | [diff] [blame] | 131 | do_compile_only = True |
| 132 | else: |
| 133 | do_compile_only = True |
| 134 | elif keyval[1] == 'exclude' or key == 'excludelist': |
| 135 | if len(keyval) > 0: |
| 136 | excludelist = keyval[1].trim('"').split(',') |
| 137 | else: |
| 138 | print("No items in exclude list (ignoring).") |
| 139 | else: |
| 140 | print("Unknown option '" + keyval[0] + "' (ignoring).") |
| 141 | else: |
| 142 | argumentlist.append(option) |
| 143 | |
| 144 | if len(argumentlist) < 3: |
| 145 | print("Not enough arguments given to create_lib_library.py.") |
| 146 | usage() |
| 147 | sys.exit(1) |
| 148 | |
| 149 | destlibdir = argumentlist[0] |
| 150 | destlib = argumentlist[1] |
| 151 | startup_script = argumentlist[2] |
| 152 | |
| 153 | print('') |
| 154 | print('Create liberty library from files:') |
| 155 | print('') |
| 156 | print('Path to files: ' + destlibdir) |
| 157 | print('Name of compiled library: ' + destlib + '.lib') |
| 158 | print('Remove individual files: ' + 'Yes' if do_compile_only else 'No') |
| 159 | if len(excludelist) > 0: |
| 160 | print('List of files to exclude: ') |
| 161 | for file in excludelist: |
| 162 | print(file) |
| 163 | print('') |
| 164 | |
| 165 | create_lib_library(destlibdir, destlib, do_compile_only, excludelist) |
| 166 | print('Done.') |
| 167 | sys.exit(0) |
| 168 | |
| 169 | #---------------------------------------------------------------------------- |