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
self._replaces = replaces
self._undefs = undefs
self._excludes = excludes
+ self._seds = seds
def _dst_lib_file_paths(self):
"""
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()
self._remove_excludes()
self._rewrite_includes()
self._replace_xxh64_prefix()
+ self._process_seds()
def parse_optional_pair(defines: [str]) -> [(str, Optional[str])]:
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")
args.defs,
args.replaces,
args.undefs,
- args.excludes
+ args.excludes,
+ args.seds,
).go()
if __name__ == "__main__":