File: install_debug_symbols.py

package info (click to toggle)
cmake-format 0.6.13-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: python: 16,990; makefile: 14
file content (49 lines) | stat: -rw-r--r-- 1,434 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Helper script called by cmake at install time to install debug binary
"""
import argparse
import logging
import os
import shutil
import subprocess
import sys


def main():
  parser = argparse.ArgumentParser(description=__doc__)
  parser.add_argument("--bin-path", help="path to the debug binary")
  parser.add_argument("--lib-dir", help="path to the install libdir")
  args = parser.parse_args()

  readelf_output = subprocess.check_output([
      "readelf", "-n", args.bin_path
  ]).decode("utf-8")

  lastline = readelf_output.strip().rsplit("\n", 1)[1]

  if not lastline.lstrip().startswith("Build ID:"):
    logging.error("readelf output doesn't end with BUILD ID")
    return 1
  buildid = lastline.split(":", 1)[1].strip()
  destdir = os.path.join(args.lib_dir, "debug/.build-id", buildid[:2])

  if "DESTDIR" in os.environ:
    destdir = os.path.join(os.environ["DESTDIR"], destdir.lstrip("/"))

  if not os.path.exists(destdir):
    os.makedirs(destdir)

  destpath = os.path.join(destdir, buildid[2:] + ".debug")
  if (os.path.exists(destpath) and
      os.path.getmtime(destpath) >= os.path.getmtime(args.bin_path)):
    print("-- Up-to-date: {}".format(destpath))
  else:
    print("-- Installing: {}".format(destpath))
    subprocess.check_call([
        "objcopy", "--only-keep-debug", args.bin_path, destpath])
  return 0


if __name__ == "__main__":
  logging.basicConfig(level=logging.WARNING)
  sys.exit(main())