Debian Codegraph @

test cases/linuxlike/14 static dynamic linkage/verify_static.py

raw
1 #!/usr/bin/env python3
2 """Test script that checks if zlib was statically linked to executable"""
3 import subprocess
4 import sys
5
6 def handle_common(path):
7 """Handle the common case."""
8 try:
9 output = subprocess.check_output(['nm', '--defined-only', '-P', '-A', path]).decode('utf-8')
10 except subprocess.CalledProcessError:
11 # some NMs only support -U. Older binutils only supports --defined-only.
12 output = subprocess.check_output(['nm', '-UPA', path]).decode('utf-8')
13 # POSIX format. Prints all *defined* symbols, looks like this:
14 # builddir/main_static: zlibVersion T 1190 39
15 # or
16 # builddir/main_static: zlibVersion D 1fde0 30
17 if ': zlibVersion ' in output:
18 return 0
19 return 1
20
21 def handle_cygwin(path):
22 """Handle the Cygwin case."""
23 output = subprocess.check_output(['nm', path]).decode('utf-8')
24 if (('I __imp_zlibVersion' in output) or ('D __imp_zlibVersion' in output)):
25 return 1
26 return 0
27
28 def main():
29 """Main function"""
30 if len(sys.argv) > 2 and sys.argv[1] == '--platform=cygwin':
31 return handle_cygwin(sys.argv[2])
32 else:
33 return handle_common(sys.argv[2])
34
35
36 if __name__ == '__main__':
37 sys.exit(main())