Debian Codegraph @

tests/check_public_api.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import subprocess
4 import tempfile
5 import os
6 import re
7 import sys
8
9
10 def get_public_symbols_from_shared_object(prefix):
11 """
12 Find public (exported) symbols in a shared library (*.so) by calling
13 the 'nm' command.
14 :param prefix: CMAKE_INSTALL_PREFIX
15 :return: a set of public symbols
16 """
17 so_path = os.path.join(prefix, "lib64", "libopenscap.so")
18 nm_command = ["nm", "--demangle", "--dynamic", "--defined-only", so_path]
19 nm_output_bytes = subprocess.check_output(nm_command)
20 nm_output = nm_output_bytes.decode(encoding="utf-8")
21 symbols = {line.split()[2] for line in nm_output.splitlines()}
22 symbols = symbols.difference({"__bss_start", "_edata", "_end", "_fini",
23 "_init"})
24 return symbols
25
26
27 def get_public_headers(prefix):
28 """
29 It is useful to check if all the public headers are actually installed
30 under $PREFIX/include/openscap.
31 :param prefix: CMAKE_INSTALL_PREFIX
32 :return: a set of public headers filepaths
33 """
34 public_headers = set()
35 include_path = os.path.join(prefix, "include", "openscap")
36 for root, dirs, files in os.walk(include_path):
37 for f in files:
38 full_path = os.path.join(root, f)
39 public_headers.add(full_path)
40 return public_headers
41
42
43 def get_public_symbols_from_header(header_path):
44 """
45 Parses a header file to find symbols marked by OSCAP_API macro.
46 :param header_path: path to the header file
47 :return: a list of public symbols in the header file
48 """
49 symbols = set()
50 prototype_regex = re.compile(r"^[ \t]*OSCAP_API[^;]*;", re.MULTILINE)
51 with open(header_path, "r") as header_file:
52 content = header_file.read()
53 prototypes = prototype_regex.findall(content)
54 function_name_regex = re.compile(r"([a-zA-Z0-9_]+)\s*\(.*\)")
55 data_name_regex = re.compile(r"\s+([a-zA-Z0-9_]+)\s*;")
56 for p in prototypes:
57 p = p.replace("\n", " ")
58 match = function_name_regex.search(p)
59 if not match:
60 match = data_name_regex.search(p)
61 if not match:
62 print("Invalid prototype '%s'" % p, file=sys.stderr)
63 continue
64 symbol_name = match.group(1)
65 symbols.add(symbol_name)
66 return symbols
67
68
69 def get_public_symbols_from_headers(headers):
70 """
71 Find public (exported) symbols in header files by finding symbols
72 marked as OSCAP_API.
73 :param headers: a list of public headers file paths
74 :return: a set of public symbols in public headers
75 """
76 symbols = set()
77 for h in headers:
78 symbols.update(get_public_symbols_from_header(h))
79 return symbols
80
81
82 def _run_command(command):
83 try:
84 subprocess.run(command, check=True,
85 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
86 encoding="utf-8")
87 except subprocess.CalledProcessError as e:
88 print(e.output)
89 print(e.stderr, file=sys.stderr)
90 raise RuntimeError
91
92
93 def analyze_project_artifacts(src_dir):
94 """
95 Analyze the project and find all the symbols declared as public in
96 public header files and all the symbols actually exported in built
97 library. This is done on a real build and a real installation.
98 The artifacts are installed in a temporary directory which is
99 automatically removed when this function returns.
100 :param src_dir: Path to project sources
101 :return: tuple (header_symbols, so_symbols)
102 """
103 cwd = os.getcwd()
104 build_dir = os.path.normpath(os.path.join(src_dir, "build"))
105 build_dir_contents = os.listdir(build_dir)
106 if ".gitkeep" in build_dir_contents:
107 build_dir_contents.remove(".gitkeep")
108 if build_dir_contents:
109 print("Directory '%s' is not empty" % build_dir, file=sys.stderr)
110 raise RuntimeError
111 os.chdir(build_dir)
112 with tempfile.TemporaryDirectory() as prefix:
113 cmake_command = ["cmake",
114 "-DENABLE_PYTHON3=FALSE",
115 "-DENABLE_PERL=FALSE",
116 "-DCMAKE_INSTALL_PREFIX=" + prefix,
117 "-DENABLE_TESTS=FALSE",
118 ".."]
119
120 _run_command(cmake_command)
121 make_command = ["make"]
122 _run_command(make_command)
123 make_docs_command = ["make", "docs"]
124 _run_command(make_docs_command)
125 make_install_command = ["make", "install"]
126 _run_command(make_install_command)
127 public_headers = get_public_headers(prefix)
128 so_symbols = get_public_symbols_from_shared_object(prefix)
129 header_symbols = get_public_symbols_from_headers(public_headers)
130 os.chdir(cwd)
131 return header_symbols, so_symbols
132
133
134 def main():
135 src_dir = os.path.join(os.getcwd(), "..")
136
137 try:
138 header_symbols, so_symbols = analyze_project_artifacts(src_dir)
139 except RuntimeError as e:
140 print("Could not analyze OpenSCAP public API", file=sys.stderr)
141 sys.exit(1)
142
143 print("Shared object symbols: %d" % len(so_symbols))
144 print("Public header symbols: %d\n" % len(header_symbols))
145 print()
146
147 so_only = so_symbols.difference(header_symbols)
148 if so_only:
149 print("The following %d symbols are exported in binary, "
150 "but are not present in public header files:" % len(so_only))
151 for s in sorted(so_only):
152 print(s)
153 print()
154
155 header_only = header_symbols.difference(so_symbols)
156 if header_only:
157 print("The following %d symbols are present in public header files, "
158 "but are not exported in binary:" % len(header_only))
159 for s in sorted(header_only):
160 print(s)
161
162
163 if __name__ == "__main__":
164 main()