Browse Source

Added ISWINDOWS constant in basefuncs.py, reworked compilationFiles function

ijon 4 years ago
parent
commit
c64a1c9b6d
1 changed files with 54 additions and 39 deletions
  1. 54 39
      inc/basefuncs.py

+ 54 - 39
inc/basefuncs.py

@@ -3,6 +3,8 @@
 
 import sys, os
 
+ISWINDOWS = sys.platform.startswith("win")
+
 def printCommand(command):
     newCommand = []
 
@@ -14,101 +16,114 @@ def printCommand(command):
 
     return " ".join(newCommand)
 
+
+
 def adjustBinaryName(name):
-    if sys.platform.startswith("win"):
+    if ISWINDOWS:
         return name.lower() + ".exe"
-    
+
     return name
 
+
+
 def getPathList():
     path = os.environ.get("PATH", "")
-    
-    if sys.platform.startswith("win"):
+
+    if ISWINDOWS:
         return path.split(";")
-    
+
     return path.split(":")
 
+
+
 def findBinary(binaryName, extraPath=None):
     path = []
     if extraPath: path.extend(extraPath)
     path.extend(getPathList())
-    
+
     adjustedName = adjustBinaryName(binaryName)
-    
+
     for dir in path:
         realdir   = os.path.realpath(dir)
         checkpath = realdir + os.sep + adjustedName
-        
+
         if os.path.isfile(checkpath) and os.access(checkpath, os.X_OK):
             return checkpath
-    
+
     return None
 
-def compilationFiles(srcDir, srcExts=(".c",), hdrExts=(".h",), objExt=".o"):
+
+
+def compilationFiles(srcDir, objDir=None, srcExts=(".c",), hdrExts=(".h",), objExt=".o"):
     sources = []
     headers = []
     objects = []
-    
-    if sys.platform.startswith("win"):
-        adjustName = lambda x: x.lower()
-    else:
-        adjustName = lambda x: x
-    
+
+
+    if ISWINDOWS:   adjustName = lambda x: x.lower()
+    else:           adjustName = lambda x: x
+
     srcDir = os.path.realpath(srcDir)
-    
+    objDir = os.path.realpath(objDir or srcDir)
+
     if not os.path.isdir(srcDir):
         return ([], [], [])
-    
+
     for dir, _, files in os.walk(srcDir):
         for file in files:
-            afile = adjustName(file)
-            pfile = dir + os.sep + file
-            
+            adjName = adjustName(file)
+            objName = adjName.rsplit(".", 1)[0] + objExt
+
+            srcPath = os.path.join(dir, file)
+            objPath = os.path.join(dir, objName).replace(srcDir, objDir, 1)
+
             for i in srcExts:
-                if afile.endswith(i):
-                    sources.append(pfile)
-                    objects.append(objExt.join(pfile.rsplit(i, 1)))
-            
+                if adjName.endswith(i):
+                    sources.append(srcPath)
+                    objects.append(objPath)
+
             for i in hdrExts:
-                if afile.endswith(i):  headers.append(pfile)
-    
+                if adjName.endswith(i):
+                    headers.append(srcPath)
+
     return (sources, headers, objects)
 
+
 def toRecompile(sources, headers, objects):
     latestHeaderMod = -1
     latestObjectMod = -1
-    
+
     for h in headers:
         if not os.path.isfile(h): continue
         mtime = os.stat(h).st_mtime
         latestHeaderMod = max(latestHeaderMod, mtime)
-    
+
     objectMtimes = {}
-    
+
     for o in objects:
         if not os.path.isfile(o): continue
         mtime = os.stat(o).st_mtime
         latestObjectMod = max(latestObjectMod, mtime)
-        
+
         objectMtimes[o] = mtime
-    
+
     if latestHeaderMod >= latestObjectMod:
         return {sources[i]: objects[i] for i in range(len(sources))}
-    
-    
+
+
     ret = {}
-    
+
     for i, c in enumerate(sources):
         if not os.path.isfile(c): continue
-        
+
         o = objects[i]
         if not os.path.isfile(o):
             ret[c] = o
             continue
-        
+
         c_mtime = os.stat(c).st_mtime
         o_mtime = objectMtimes[o]
-        
+
         if c_mtime >= o_mtime: ret[c] = o
-    
+
     return ret