Skip to content

Commit 47d338c

Browse files
xhaakontuexen
authored andcommitted
Fix shared library build for Windows with MSVC and Meson (sctplab#328)
usrsctp-1.dll built with Meson didn't export any symbols. Meson needs a .def file listing the names of functions to export when creating a shared library on Windows. This patch generates usrsctp.def automatically at build time from the output of "dumpbin /linkermember" run on the static version of usrsctp lib. Kudos to Lennart Grahl for help with fixing broken static build.
1 parent 2a5aff3 commit 47d338c

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

gen-def.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
#
3+
# gen-def.py usrsctp.lib
4+
import re
5+
import sys
6+
import subprocess
7+
from shutil import which
8+
9+
try:
10+
lib_file = sys.argv[1]
11+
except:
12+
print('Usage: gen-def.py LIB-FILE')
13+
exit(-1)
14+
15+
print('EXPORTS')
16+
17+
if which('dumpbin'):
18+
dumpbin_cmd = subprocess.run(['dumpbin', '/linkermember:1', lib_file],
19+
stdout=subprocess.PIPE)
20+
21+
pattern = re.compile('\s*[0-9a-fA-F]+ _?(?P<functionname>usrsctp_[^\s]*)')
22+
23+
for line in dumpbin_cmd.stdout.decode('utf-8').splitlines():
24+
match = pattern.match(line)
25+
if match:
26+
print(match.group('functionname'))

meson.build

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,32 @@ endif
183183
subdir('usrsctplib')
184184

185185
# Build library
186-
usrsctp = library('usrsctp', sources,
187-
dependencies: dependencies,
188-
include_directories: include_dirs,
189-
install: true,
190-
version: meson.project_version())
186+
if compiler.get_id() == 'msvc' and get_option('default_library') == 'shared'
187+
# Needed by usrsctp_def
188+
find_program('dumpbin')
189+
190+
usrsctp_static = static_library('usrsctp-static', sources,
191+
dependencies: dependencies,
192+
include_directories: include_dirs)
193+
194+
usrsctp_def = custom_target('usrsctp.def',
195+
command: [find_program('gen-def.py'), '@INPUT@'],
196+
input: usrsctp_static,
197+
output: 'usrsctp.def',
198+
capture: true)
199+
200+
usrsctp = shared_library('usrsctp',
201+
link_whole: usrsctp_static,
202+
vs_module_defs: usrsctp_def,
203+
install: true,
204+
version: meson.project_version())
205+
else
206+
usrsctp = library('usrsctp', sources,
207+
dependencies: dependencies,
208+
include_directories: include_dirs,
209+
install: true,
210+
version: meson.project_version())
211+
endif
191212

192213
# Declare dependency
193214
usrsctp_dep = declare_dependency(

0 commit comments

Comments
 (0)