Skip to content

Commit

Permalink
test cases/linuxlike/14 static dynamic linkage: Adding matrix by static
Browse files Browse the repository at this point in the history
Not only Solaris doesn't ship static libraries, it's a common practice.
See, for example 'test cases/frameworks/1 boost'.

Adding possibility to skip the static linkage with the help of
SKIP_STATIC_ZLIB env var (like SKIP_STATIC_BOOST).

Adding the possibility to test dynamic llinkage (it was only static one).

TODO: dynamic test on cygwin
  • Loading branch information
dememax committed Oct 30, 2024
1 parent 1840bb0 commit eff98b9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 42 deletions.
5 changes: 3 additions & 2 deletions test cases/linuxlike/14 static dynamic linkage/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "zlib.h"

int main(void) {
printf("%s\n", zlibVersion());
return 0;
const char * const v = zlibVersion();
printf("%s\n", v ? v : "<NULL>");
return !v;
}
53 changes: 21 additions & 32 deletions test cases/linuxlike/14 static dynamic linkage/meson.build
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
project('static dynamic', 'c')
project('zlib linkage', 'c')

# Solaris does not ship static libraries
if host_machine.system() == 'sunos'
has_static = false
else
has_static = true
endif
s = get_option('static')

cc = meson.get_compiler('c')

z_default = cc.find_library('z')
if has_static
z_static = cc.find_library('z', static: true)
endif
z_dynamic = cc.find_library('z', static: false)

exe_default = executable('main_default', 'main.c', dependencies: [z_default])
if has_static
exe_static = executable('main_static', 'main.c', dependencies: [z_static])
endif
exe_dynamic = executable('main_dynamic', 'main.c', dependencies: [z_dynamic])

test('test default', exe_default)
if has_static
test('test static', exe_static)
endif
test('test dynamic', exe_dynamic)

if has_static
test('verify static linking', find_program('verify_static.py'),
args: ['--platform=' + host_machine.system(), exe_static.full_path()])
endif
test('verify dynamic linking', find_program('verify_static.py'),
args: ['--platform=' + host_machine.system(), exe_dynamic.full_path()],
should_fail: true)
z = cc.find_library('z', static: s)

exe = executable('main_zlib', 'main.c', dependencies: [z])

# first step: the executable should compile and work
test('test zlib', exe)

# to check the zlib static/dynamic symbols in the resulting binary
find_program('nm')

# second step: static linkage
test('verify static zlib linking', find_program('verify_static.py'),
args: ['--platform=' + host_machine.system(), '--static', exe.full_path()],
should_fail: not s)

# third step: dynamic linkage
test('verify dynamic zlib linking', find_program('verify_static.py'),
args: ['--platform=' + host_machine.system(), exe.full_path()],
should_fail: s)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option('static', type: 'boolean', value: false)
10 changes: 10 additions & 0 deletions test cases/linuxlike/14 static dynamic linkage/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"matrix": {
"options": {
"static": [
{ "val": "true", "skip_on_env": [ "SKIP_STATIC_ZLIB" ] },
{ "val": "false" }
]
}
}
}
26 changes: 18 additions & 8 deletions test cases/linuxlike/14 static dynamic linkage/verify_static.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env python3
"""Test script that checks if zlib was statically linked to executable"""
"""Test script that checks if zlib was statically or dynamically linked to executable"""
import subprocess
import sys
import argparse

def handle_common(path):
def handle_common(path, is_static):
"""Handle the common case."""
try:
output = subprocess.check_output(['nm', '--defined-only', '-P', '-A', path]).decode('utf-8')
sym_opt = '--defined-only' if is_static else '--undefined-only'
output = subprocess.check_output(['nm', sym_opt, '-P', '-A', path]).decode('utf-8')
except subprocess.CalledProcessError:
# some NMs only support -U. Older binutils only supports --defined-only.
output = subprocess.check_output(['nm', '-UPA', path]).decode('utf-8')
opts = '-UPA' if is_static else '-uPA'
output = subprocess.check_output(['nm', opts, path]).decode('utf-8')
# POSIX format. Prints all *defined* symbols, looks like this:
# builddir/main_static: zlibVersion T 1190 39
# or
Expand All @@ -18,19 +21,26 @@ def handle_common(path):
return 0
return 1

def handle_cygwin(path):
def handle_cygwin(path, is_static):
"""Handle the Cygwin case."""
output = subprocess.check_output(['nm', path]).decode('utf-8')
# TODO: Dynamic test!
if (('I __imp_zlibVersion' in output) or ('D __imp_zlibVersion' in output)):
return 1
return 0

def main():
"""Main function"""
if len(sys.argv) > 2 and sys.argv[1] == '--platform=cygwin':
return handle_cygwin(sys.argv[2])
parser = argparse.ArgumentParser()
parser.add_argument('path', help='executable path')
parser.add_argument('-p', '--platform')
parser.add_argument('-s', '--static', action='store_true', default=False)
args = parser.parse_args()
print('args:', args)
if args.platform == 'cygwin':
return handle_cygwin(args.path, args.static)
else:
return handle_common(sys.argv[2])
return handle_common(args.path, args.static)


if __name__ == '__main__':
Expand Down

0 comments on commit eff98b9

Please sign in to comment.