wrap-valgrind.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) The Bitcoin Core developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or https://opensource.org/license/mit/.
   5  
   6  import os
   7  import shlex
   8  from pathlib import Path
   9  
  10  
  11  def main():
  12      base_root = Path(os.environ["BASE_ROOT_DIR"])
  13      base_out = Path(os.environ["BASE_OUTDIR"])
  14      suppressions_file = base_root / "test" / "sanitizer_suppressions" / "valgrind.supp"
  15      target_names = {b.name for b in (base_out / "bin").iterdir()}
  16  
  17      for exe in base_root.rglob("*"):
  18          if exe.name in target_names and exe.is_file() and os.access(exe, os.X_OK):
  19              print(f"Wrap {exe} ...")
  20              original_path = exe.with_name(f"{exe.name}_orig")
  21              exe.rename(original_path)
  22              exe.write_text(
  23                  "#!/usr/bin/env bash\n"
  24                  "exec valgrind --gen-suppressions=all --quiet --error-exitcode=1 "
  25                  f"--suppressions={shlex.quote(str(suppressions_file))} "
  26                  f'{shlex.quote(str(original_path))} "$@"\n'
  27              )
  28              exe.chmod(exe.stat().st_mode | 0o111)
  29  
  30  
  31  if __name__ == "__main__":
  32      main()
  33