Skip to content

Commit cf8c3f3

Browse files
committed
ENH: Add argument parsing for Linux module wheels scripts
Add argument parsing allowing users to: - Forward CMake options to the module - Add option to exclude libs from repaired wheel on Linux Some shared libraries like the CUDA driver library must be provided at runtime. Hence, it must be excluded from the repaired wheel (while it must be provide in LD_LIBRARY_PATH for the project to compile). The optional python version is still sent as positional argument for backward compatibility.
1 parent eb1cd68 commit cf8c3f3

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

scripts/dockcross-manylinux-download-cache-and-build-module-wheels.sh

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@
33
# This module should be pulled and run from an ITKModule root directory to generate the Linux python wheels of this module,
44
# it is used by the azure-pipeline.yml file contained in ITKModuleTemplate: https://github.com/InsightSoftwareConsortium/ITKModuleTemplate
55

6+
# -----------------------------------------------------------------------
7+
# Script argument parsing
8+
#
9+
usage()
10+
{
11+
echo "Usage:
12+
dockcross-manylinux-download-cache-and-build-module-wheels
13+
[ -h | --help ] show usage
14+
[ -c | --cmake_options ] space-delimited string containing CMake options to forward to the module (e.g. \"-DBUILD_TESTING=OFF\")
15+
[ -x | --exclude_libs ] semicolon-delimited library names to exclude when repairing wheel (e.g. \"libcuda.so\")
16+
[ python_version ] build wheel for a specific python version. (e.g. cp39)"
17+
exit 2
18+
}
19+
20+
FORWARD_ARGS=("$@") # Store arguments to forward them later
21+
PARSED_ARGS=$(getopt -a -n dockcross-manylinux-download-cache-and-build-module-wheels \
22+
-o hc:x: --long help,cmake_options:,exclude_libs: -- "$@")
23+
eval set -- "$PARSED_ARGS"
24+
25+
while :
26+
do
27+
case "$1" in
28+
-h | --help) usage; break ;;
29+
-c | --cmake_options) CMAKE_OPTIONS="$2" ; shift 2 ;;
30+
-x | --exclude_libs) EXCLUDE_LIBS="$2" ; shift 2 ;;
31+
--) shift; break ;;
32+
*) echo "Unexpected option: $1.";
33+
usage; break ;;
34+
esac
35+
done
36+
# -----------------------------------------------------------------------
37+
638
# Packages distributed by github are in zstd format, so we need to download that binary to uncompress
739
if [[ ! -f zstd-1.2.0-linux.tar.gz ]]; then
840
curl https://data.kitware.com/api/v1/file/592dd8068d777f16d01e1a92/download -o zstd-1.2.0-linux.tar.gz
@@ -22,7 +54,7 @@ if [[ ! -f ./ITKPythonBuilds-linux.tar.zst ]]; then
2254
exit 255
2355
fi
2456
./zstd-1.2.0-linux/bin/unzstd ./ITKPythonBuilds-linux.tar.zst -o ITKPythonBuilds-linux.tar
25-
if [ "$#" -le 1 ]; then
57+
if [ "$#" -lt 1 ]; then
2658
echo "Extracting all files";
2759
tar xf ITKPythonBuilds-linux.tar
2860
else
@@ -39,4 +71,5 @@ if [[ ! -f ./ITKPythonPackage/scripts/dockcross-manylinux-build-module-wheels.sh
3971
fi
4072
cp -a ITKPythonPackage/oneTBB-prefix ./
4173

74+
set -- "${FORWARD_ARGS[@]}"; # Restore initial argument list
4275
./ITKPythonPackage/scripts/dockcross-manylinux-build-module-wheels.sh "$@"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Patch auditwheel to skip actions on libraries specified in the whitelist.
2+
Other arguments are forwarded to auditwheel."""
3+
4+
import argparse
5+
import sys
6+
7+
from auditwheel.main import main
8+
from auditwheel.policy import _POLICIES as POLICIES
9+
10+
11+
def exclude_libs(whitelist):
12+
# Do not include the following libraries when repairing wheels.
13+
for lib in whitelist.split(';'):
14+
for p in POLICIES:
15+
p['lib_whitelist'].append(lib)
16+
17+
if __name__ == "__main__":
18+
parser = argparse.ArgumentParser(description='Driver script to build ITK Python module wheels.')
19+
parser.add_argument('command', nargs=1, default='', help='auditwheel command (e.g. repair).')
20+
parser.add_argument('wheel_file', nargs=1, default='', help='auditwheel wheel file.')
21+
parser.add_argument('--wheel-dir', '-w', nargs=1, default='', type=str, help='Directory to store delocated wheels.')
22+
parser.add_argument('--whitelist', nargs=1, default=None, type=str, help='Semicolon-delimited libraries to exclude from repaired wheel (e.g. libcuda.so)')
23+
args = parser.parse_args()
24+
25+
if args.whitelist is not None:
26+
whitelist = ';'.join(args.whitelist)
27+
exclude_libs(whitelist)
28+
# Do not forward whitelist args to auditwheel
29+
sys.argv.remove('--whitelist')
30+
sys.argv.remove(whitelist)
31+
32+
sys.exit(main())

scripts/internal/manylinux-build-module-wheels.sh

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@
1616
# /tmp/dockcross-manylinux-x64 -a "$DOCKER_ARGS" manylinux-build-module-wheels.sh
1717
#
1818

19+
# -----------------------------------------------------------------------
20+
# Script argument parsing
21+
#
22+
usage()
23+
{
24+
echo "Usage:
25+
manylinux-build-module-wheels
26+
[ -h | --help ] show usage
27+
[ -c | --cmake_options ] space-separated string of CMake options to forward to the module (e.g. \"-DBUILD_TESTING=OFF\")
28+
[ -x | --exclude_libs ] semicolon-separated library names to exclude when repairing wheel (e.g. \"libcuda.so\")
29+
[ python_version ] build wheel for a specific python version. (e.g. cp39)"
30+
exit 2
31+
}
32+
33+
PARSED_ARGS=$(getopt -a -n dockcross-manylinux-download-cache-and-build-module-wheels \
34+
-o hc:x: --long help,cmake_options:,exclude_libs: -- "$@")
35+
eval set -- "$PARSED_ARGS"
36+
37+
while :
38+
do
39+
case "$1" in
40+
-h | --help) usage; break ;;
41+
-c | --cmake_options) CMAKE_OPTIONS="$2" ; shift 2 ;;
42+
-x | --exclude_libs) EXCLUDE_LIBS="$2" ; shift 2 ;;
43+
--) shift; break ;;
44+
*) echo "Unexpected option: $1.";
45+
usage; break ;;
46+
esac
47+
done
48+
49+
PYTHON_VERSION="$@"
50+
# -----------------------------------------------------------------------
51+
1952
# -----------------------------------------------------------------------
2053
# These variables are set in common script:
2154
#
@@ -66,13 +99,19 @@ for PYBIN in "${PYBINARIES[@]}"; do
6699
-DBUILD_TESTING:BOOL=OFF \
67100
-DPython3_EXECUTABLE:FILEPATH=${Python3_EXECUTABLE} \
68101
-DPython3_INCLUDE_DIR:PATH=${Python3_INCLUDE_DIR} \
102+
${CMAKE_OPTIONS} \
69103
|| exit 1
70104
${PYBIN}/python setup.py clean
71105
done
72106

73107
if test "${ARCH}" == "x64"; then
108+
# Make sure auditwheel is installed for this python exe before importing
109+
# it in auditwheel_whitelist_monkeypatch.py
110+
sudo ${Python3_EXECUTABLE} -m pip install auditwheel
74111
for whl in dist/*linux_$(uname -p).whl; do
75-
auditwheel repair ${whl} -w /work/dist/
112+
# Repair wheel using monkey patch to exclude shared libraries provided in whitelist
113+
${Python3_EXECUTABLE} "${script_dir}/auditwheel_whitelist_monkeypatch.py" \
114+
repair ${whl} -w /work/dist/ --whitelist "${EXCLUDE_LIBS}"
76115
rm ${whl}
77116
done
78117
fi

0 commit comments

Comments
 (0)