]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Scripts/ConvertMasmToNasm.py
BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / BaseTools / Scripts / ConvertMasmToNasm.py
index 6343fbd94c6df4c9ff4725ef6a47a47707408e44..5b83724b3124f84eb6566efaa9da46c90d1538eb 100755 (executable)
@@ -53,6 +53,7 @@ class CommonUtils:
 \r
         self.unsupportedSyntaxSeen = False\r
         self.src = self.args.source\r
+        self.keep = self.args.keep\r
         assert(os.path.exists(self.src))\r
         self.dirmode = os.path.isdir(self.src)\r
         srcExt = os.path.splitext(self.src)[1]\r
@@ -78,6 +79,9 @@ class CommonUtils:
                             help="Disable all messages except FATAL ERRORS.")\r
         parser.add_argument("--git", action="store_true",\r
                             help="Use git to create commits for each file converted")\r
+        parser.add_argument("--keep", action="append", choices=('asm', 's'),\r
+                            default=[],\r
+                            help="Don't remove files with this extension")\r
         parser.add_argument("--diff", action="store_true",\r
                             help="Show diff of conversion")\r
         parser.add_argument("-f", "--force", action="store_true",\r
@@ -175,9 +179,18 @@ class CommonUtils:
         if not self.git or not self.gitdir:\r
             return\r
 \r
+        if self.ShouldKeepFile(path):\r
+            return\r
+\r
         cmd = ('git', 'rm', path)\r
         self.RunAndCaptureOutput(cmd)\r
 \r
+    def ShouldKeepFile(self, path):\r
+        ext = os.path.splitext(path)[1].lower()\r
+        if ext.startswith('.'):\r
+            ext = ext[1:]\r
+        return ext in self.keep\r
+\r
     def FileConversionFinished(self, pkg, module, src, dst):\r
         if not self.git or not self.gitdir:\r
             return\r
@@ -755,7 +768,8 @@ class ConvertInfFile(CommonUtils):
                 src = self.mo.group(1)\r
                 srcExt = self.mo.group(2)\r
                 dst = os.path.splitext(src)[0] + '.nasm'\r
-                if src not in srcToDst:\r
+                fullDst = os.path.join(self.dir, dst)\r
+                if src not in srcToDst and not os.path.exists(fullDst):\r
                     srcToDst[src] = dst\r
                     srcToDst['order'].append(src)\r
         return srcToDst\r
@@ -843,36 +857,50 @@ class ConvertInfFile(CommonUtils):
             conv = ConvertAsmFile(fullSrc, fullDst, self)\r
             self.unsupportedSyntaxSeen = conv.unsupportedSyntaxSeen\r
 \r
-        lastLine = ''\r
         fileChanged = False\r
+        recentSources = list()\r
         i = 0\r
-        for line in self.lines:\r
-            line = line.rstrip()\r
+        while i < len(self.lines):\r
+            line = self.lines[i].rstrip()\r
             updatedLine = line\r
+            lineChanged = False\r
+            preserveOldSource = False\r
             for src in self.dstToSrc[dst]:\r
                 assert self.srcToDst[src] == dst\r
                 updatedLine = self.ReplacePreserveSpacing(\r
                     updatedLine, src, dst)\r
+                lineChanged = updatedLine != line\r
+                if lineChanged:\r
+                    preserveOldSource = self.ShouldKeepFile(src)\r
+                    break\r
 \r
-            lineChanged = updatedLine != line\r
             if lineChanged:\r
-                if lastLine.strip() == updatedLine.strip():\r
-                    self.lines[i] = None\r
-                else:\r
-                    self.lines[i] = updatedLine + '\n'\r
-\r
-            if self.diff:\r
-                if lineChanged:\r
-                    print('-%s' % line)\r
-                    if self.lines[i] is not None:\r
-                        print('+%s' % updatedLine)\r
+                if preserveOldSource:\r
+                    if updatedLine.strip() not in recentSources:\r
+                        self.lines.insert(i, updatedLine + '\n')\r
+                        recentSources.append(updatedLine.strip())\r
+                        i += 1\r
+                        if self.diff:\r
+                            print('+%s' % updatedLine)\r
+                    if self.diff:\r
+                        print('', line)\r
                 else:\r
+                    if self.diff:\r
+                        print('-%s' % line)\r
+                    if updatedLine.strip() in recentSources:\r
+                        self.lines[i] = None\r
+                    else:\r
+                        self.lines[i] = updatedLine + '\n'\r
+                        recentSources.append(updatedLine.strip())\r
+                        if self.diff:\r
+                            print('+%s' % updatedLine)\r
+            else:\r
+                if len(recentSources) > 0:\r
+                    recentSources = list()\r
+                if self.diff:\r
                     print('', line)\r
 \r
             fileChanged |= lineChanged\r
-            if self.lines[i] is not None:\r
-                lastLine = self.lines[i]\r
-\r
             i += 1\r
 \r
         if fileChanged:\r