]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
[linux-kernel] Replace kernel-style comments 2593/head
authorNick Terrell <terrelln@fb.com>
Thu, 29 Apr 2021 22:50:23 +0000 (15:50 -0700)
committerNick Terrell <terrelln@fb.com>
Thu, 29 Apr 2021 22:50:23 +0000 (15:50 -0700)
Replace kernel-style comments with regular comments.

E.g.

```
/** Before */

/* After */

/**
 * Before
 */

/*
 * After
 */

/***********************************
 * Before
 ***********************************/

/* *********************************
 * After
 ***********************************/
```

contrib/freestanding_lib/freestanding.py
contrib/linux-kernel/Makefile

index a5bcf100f721970dc48c0b0a2b5ca75db28ce875..a3a015db1f339de279dd0fef72d8008b395b3ca0 100755 (executable)
@@ -432,7 +432,7 @@ class Freestanding(object):
             external_xxhash: bool, xxh64_state: Optional[str],
             xxh64_prefix: Optional[str], rewritten_includes: [(str, str)],
             defs: [(str, Optional[str])], replaces: [(str, str)],
-            undefs: [str], excludes: [str]
+            undefs: [str], excludes: [str], seds: [str],
     ):
         self._zstd_deps = zstd_deps
         self._mem = mem
@@ -446,6 +446,7 @@ class Freestanding(object):
         self._replaces = replaces
         self._undefs = undefs
         self._excludes = excludes
+        self._seds = seds
 
     def _dst_lib_file_paths(self):
         """
@@ -598,6 +599,48 @@ class Freestanding(object):
                 file.lines[i] = line
             file.write()
 
+    def _parse_sed(self, sed):
+        assert sed[0] == 's'
+        delim = sed[1]
+        match = re.fullmatch(f's{delim}(.+){delim}(.*){delim}(.*)', sed)
+        assert match is not None
+        regex = re.compile(match.group(1))
+        format_str = match.group(2)
+        is_global = match.group(3) == 'g'
+        return regex, format_str, is_global
+
+    def _process_sed(self, sed):
+        self._log(f"Processing sed: {sed}")
+        regex, format_str, is_global = self._parse_sed(sed)
+
+        for filepath in self._dst_lib_file_paths():
+            file = FileLines(filepath)
+            for i, line in enumerate(file.lines):
+                modified = False
+                while True:
+                    match = regex.search(line)
+                    if match is None:
+                        break
+                    replacement = format_str.format(match.groups(''), match.groupdict(''))
+                    b = match.start()
+                    e = match.end()
+                    line = line[:b] + replacement + line[e:]
+                    modified = True
+                    if not is_global:
+                        break
+                if modified:
+                    self._log(f"\t- {file.lines[i][:-1]}")
+                    self._log(f"\t+ {line[:-1]}")
+                file.lines[i] = line
+            file.write()
+
+    def _process_seds(self):
+        self._log("Processing seds")
+        for sed in self._seds:
+            self._process_sed(sed)
+
+
+
     def go(self):
         self._copy_source_lib()
         self._copy_zstd_deps()
@@ -606,6 +649,7 @@ class Freestanding(object):
         self._remove_excludes()
         self._rewrite_includes()
         self._replace_xxh64_prefix()
+        self._process_seds()
 
 
 def parse_optional_pair(defines: [str]) -> [(str, Optional[str])]:
@@ -643,6 +687,7 @@ def main(name, args):
     parser.add_argument("--xxh64-state", default=None, help="Alternate XXH64 state type (excluding _) e.g. --xxh64-state='struct xxh64_state'")
     parser.add_argument("--xxh64-prefix", default=None, help="Alternate XXH64 function prefix (excluding _) e.g. --xxh64-prefix=xxh64")
     parser.add_argument("--rewrite-include", default=[], dest="rewritten_includes", action="append", help="Rewrite an include REGEX=NEW (e.g. '<stddef\\.h>=<linux/types.h>')")
+    parser.add_argument("--sed", default=[], dest="seds", action="append", help="Apply a sed replacement. Format: `s/REGEX/FORMAT/[g]`. REGEX is a Python regex. FORMAT is a Python format string formatted by the regex dict.")
     parser.add_argument("-D", "--define", default=[], dest="defs", action="append", help="Pre-define this macro (can be passed multiple times)")
     parser.add_argument("-U", "--undefine", default=[], dest="undefs", action="append", help="Pre-undefine this macro (can be passed mutliple times)")
     parser.add_argument("-R", "--replace", default=[], dest="replaces", action="append", help="Pre-define this macro and replace the first ifndef block with its definition")
@@ -695,7 +740,8 @@ def main(name, args):
         args.defs,
         args.replaces,
         args.undefs,
-        args.excludes
+        args.excludes,
+        args.seds,
     ).go()
 
 if __name__ == "__main__":
index 2408a4b361e71256174df3055851420f48d67995..2ea370d372f291da2728448db2f169a8e602bd73 100644 (file)
@@ -24,6 +24,8 @@ libzstd:
                --rewrite-include '<stddef\.h>=<linux/types.h>' \
                --rewrite-include '"\.\./zstd.h"=<linux/zstd.h>' \
                --rewrite-include '"(\.\./common/)?zstd_errors.h"=<linux/zstd_errors.h>' \
+               --sed 's,/\*\*\*,/* *,g' \
+               --sed 's,/\*\*,/*,g' \
                -DZSTD_NO_INTRINSICS \
                -DZSTD_NO_UNUSED_FUNCTIONS \
                -DZSTD_LEGACY_SUPPORT=0 \