Added additional option "sort" to supply a sorting script to specify
the order of files when compiled into a library;  this allows the
Makefile to enforce natural sort order and/or put dependent entries
at the top.  Also added a custom script for sky130 to handle the
assortment of "include" statements in the standard cell verilog
before generating the library files.
diff --git a/common/foundry_install.py b/common/foundry_install.py
index 7028956..3fa7f1c 100755
--- a/common/foundry_install.py
+++ b/common/foundry_install.py
@@ -79,6 +79,10 @@
 #		    when a foundry library has inconveniently split
 #		    an IP library (LEF, CDL, verilog, etc.) into
 #		    individual files.
+#	compile-only:	Like "compile" except that the individual
+#		    files are removed after the library file has been
+#		    created.
+#
 #	stub :	   Remove contents of subcircuits from CDL or SPICE
 #		    netlist files.
 #
@@ -98,6 +102,20 @@
 #		    "compile" or "compile-only", this refers to the
 # 		    name of the target compiled file.
 #
+#	filter:	   Followed by "=" and the name of a script.
+#		    Each file is passed through the filter script
+#		    before writing into the staging area.
+#
+#	sort:	   Optionally followed by "=" and the name of a script.
+#		    The list of files to process (after applying items
+#		    from "exclude") will be written to a file
+#		    "filelist.txt", which will be used by the
+#		    library compile routines, if present.  If a script
+#		    name is specified, then the sort script will rewrite
+#		    the file with the order in which entries should
+#		    appear in the compiled library.  Only useful when
+#		    used with "compile" or "compile-only".
+#
 #	noconvert : Install only; do not attempt to convert to other
 #		    formats (applies only to GDS, CDL, and LEF).
 #
@@ -669,16 +687,11 @@
         # the source should be copied (or linked) from <number> levels up
         # in the hierarchy (see below).
 
-        if 'up' in option:
-            uparg = option.index('up') 
-            try:
-                hier_up = int(option[uparg + 1])
-            except:
-                print("Non-numeric option to 'up': " + option[uparg + 1])
-                print("Ignoring 'up' option.")
-                hier_up = 0
-        else:
-            hier_up = 0
+        hier_up = 0
+        for item in option:
+            if item.split('=')[0] == 'up':
+                hier_up = int(item.split('=')[1])
+                break
 
         filter_scripts = []
         for item in option:
@@ -713,6 +726,17 @@
         else:
             print('Renaming file to: ' + newname)
 
+        # Option 'sort' may have an argument. . .
+        try:
+            sortscript = list(item.split('=')[1] for item in option if item.startswith('sort'))[0]
+        except IndexError:
+            sortscript = None
+        else:
+            print('Sorting files with script ' + sortscript)
+        # . . . or not.
+        if 'sort' in option:
+            sortscript = True
+
         # 'anno' may be specified for LEF, in which case the LEF is used only
         # to annotate GDS and is not itself installed;  this allows LEF to
         # be generated from Magic and avoids quirky use of obstruction layers.
@@ -791,6 +815,7 @@
                     print('   ' + item)
                 print('(' + str(len(liblist)) + ' files total)')
 
+            destfilelist = []
             for libname in liblist:
                 # Note that there may be a hierarchy to the files in option[1],
                 # say for liberty timing files under different conditions, so
@@ -877,6 +902,19 @@
                     # Apply filter script to all files in the target directory
                     tfilter(targname, filter_script)
 
+                destfilelist.append(os.path.split(targname)[1])
+
+            if sortscript:
+                print('Diagnostic:  Sorting files to compile.')
+                with open(destlibdir + '/filelist.txt', 'w') as ofile:
+                    for destfile in destfilelist:
+                        print(destfile, file=ofile)
+                if os.path.isfile(sortscript):
+                    print('Diagnostic:  Sorting files with ' + sortscript)
+                    subprocess.run([sortscript, destlibdir],
+				stdout = subprocess.DEVNULL,
+				stderr = subprocess.DEVNULL)
+
             if do_compile == True or do_compile_only == True:
                 # NOTE:  The purpose of "rename" is to put a destlib-named
                 # library elsewhere so that it can be merged with another
@@ -946,6 +984,11 @@
                     targrename = destlibdir + destpath + '/' + newname
                     if os.path.isfile(origname):
                         os.rename(origname, targrename)
+      
+            # If "filelist.txt" was created, remove it
+            if sortscript:
+                if os.path.isfile(destlibdir + '/filelist.txt'):
+                    os.remove(destlibdir + '/filelist.txt')
 
         # Find any libraries/options marked as "privileged" (or "private") and
         # move the files from libs.tech or libs.ref to libs.priv, leaving a