|
7 | 7 | import glob |
8 | 8 | import sys |
9 | 9 | import argparse |
| 10 | +import shutil |
10 | 11 |
|
11 | 12 | SCRIPT_DIR = os.path.dirname(__file__) |
12 | 13 | ROOT_DIR = os.path.abspath(os.getcwd()) |
|
19 | 20 | from wheel_builder_utils import push_dir, push_env |
20 | 21 | from windows_build_common import DEFAULT_PY_ENVS, venv_paths |
21 | 22 |
|
| 23 | +def install_and_import(package): |
| 24 | + """ |
| 25 | + Install package with pip and import in current script. |
| 26 | + """ |
| 27 | + import importlib |
| 28 | + try: |
| 29 | + importlib.import_module(package) |
| 30 | + except ImportError: |
| 31 | + import pip |
| 32 | + pip.main(['install', package]) |
| 33 | + finally: |
| 34 | + globals()[package] = importlib.import_module(package) |
| 35 | + |
22 | 36 | def build_wheels(py_envs=DEFAULT_PY_ENVS, cleanup=True, cmake_options=[]): |
23 | 37 | for py_env in py_envs: |
24 | 38 | python_executable, \ |
@@ -64,33 +78,65 @@ def build_wheels(py_envs=DEFAULT_PY_ENVS, cleanup=True, cmake_options=[]): |
64 | 78 | if cleanup: |
65 | 79 | check_call([python_executable, "setup.py", "clean"]) |
66 | 80 |
|
67 | | - |
68 | | -def fixup_wheel(py_envs, filepath, lib_paths:str=''): |
69 | | - lib_paths = lib_paths.strip() if lib_paths.isspace() else lib_paths.strip() + ";" |
70 | | - lib_paths += "C:/P/IPP/oneTBB-prefix/bin" |
| 81 | +def rename_wheel_init(py_env, filepath): |
| 82 | + """ |
| 83 | + Rename module __init__ file in wheel. |
| 84 | + This is required to prevent modules to override ITK's __init__ file on install. |
| 85 | + If the module ships its own __init__ file, it is automatically renamed to |
| 86 | + __init_{module_name}__ by this function. The renamed __init__ file will be executed |
| 87 | + by ITK's __init__ file when loading ITK. |
| 88 | + """ |
| 89 | + python_executable, python_include_dir, python_library, pip, ninja_executable, path = venv_paths(py_env) |
| 90 | + |
| 91 | + # Get module info |
| 92 | + install_and_import("pkginfo") |
| 93 | + w = pkginfo.Wheel(filepath) |
| 94 | + module_name = w.name.split("itk-")[-1] |
| 95 | + module_version = w.version |
| 96 | + |
| 97 | + dist_dir = os.path.dirname(filepath) |
| 98 | + wheel_dir = os.path.join(dist_dir, "itk_" + module_name + "-" + module_version) |
| 99 | + init_dir = os.path.join(wheel_dir, "itk") |
| 100 | + init_file = os.path.join(init_dir, "__init__.py") |
| 101 | + |
| 102 | + # Unpack wheel and rename __init__ file if it exists. |
| 103 | + check_call([python_executable, "-m", "wheel", "unpack", filepath, "-d", dist_dir]) |
| 104 | + if os.path.isfile(init_file): |
| 105 | + shutil.move(init_file, os.path.join(init_dir, "__init_" + module_name + "__.py")) |
| 106 | + # Pack wheel and clean wheel folder |
| 107 | + check_call([python_executable, "-m", "wheel", "pack", wheel_dir, "-d", dist_dir]) |
| 108 | + shutil.rmtree(wheel_dir) |
| 109 | + |
| 110 | +def fixup_wheel(py_envs, filepath, lib_paths:str='', exclude_libs:str=''): |
| 111 | + lib_paths = ';'.join(["C:/P/IPP/oneTBB-prefix/bin",lib_paths.strip()]).strip(';') |
71 | 112 | print(f'Library paths for fixup: {lib_paths}') |
72 | 113 |
|
73 | 114 | py_env = py_envs[0] |
74 | 115 |
|
75 | 116 | delve_wheel = os.path.join(ROOT_DIR, "venv-" + py_env, "Scripts", "delvewheel.exe") |
76 | 117 | check_call([delve_wheel, "repair", "--no-mangle-all", "--add-path", |
77 | | - lib_paths, "--ignore-in-wheel", "-w", |
| 118 | + lib_paths, "--no-dll", exclude_libs, "--ignore-in-wheel", "-w", |
78 | 119 | os.path.join(ROOT_DIR, "dist"), filepath]) |
79 | 120 |
|
| 121 | + # The delve_wheel patch loading shared libraries is added to the module |
| 122 | + # __init__ file. Rename this file here to prevent conflicts on installation. |
| 123 | + # The renamed __init__ file will be executed when loading ITK. |
| 124 | + rename_wheel_init(py_env, filepath) |
80 | 125 |
|
81 | | -def fixup_wheels(py_envs, lib_paths:str=''): |
| 126 | +def fixup_wheels(py_envs, lib_paths:str='', exclude_libs:str=''): |
82 | 127 | # shared library fix-up |
83 | 128 | for wheel in glob.glob(os.path.join(ROOT_DIR, "dist", "*.whl")): |
84 | | - fixup_wheel(py_envs, wheel, ';'.join(lib_paths)) |
| 129 | + fixup_wheel(py_envs, wheel, lib_paths, exclude_libs) |
85 | 130 |
|
86 | 131 | if __name__ == '__main__': |
87 | 132 | parser = argparse.ArgumentParser(description='Driver script to build ITK Python module wheels.') |
88 | 133 | parser.add_argument('--py-envs', nargs='+', default=DEFAULT_PY_ENVS, |
89 | 134 | help='Target Python environment versions, e.g. "37-x64".') |
90 | 135 | parser.add_argument('--no-cleanup', dest='cleanup', action='store_false', help='Do not clean up temporary build files.') |
91 | 136 | parser.add_argument('--lib-paths', nargs=1, default='', help='Add semicolon-delimited library directories for delvewheel to include in the module wheel') |
| 137 | + parser.add_argument('--exclude-libs', nargs=1, default='', help='Add semicolon-delimited library names that must not be included in the module wheel, e.g. "nvcuda.dll"') |
92 | 138 | parser.add_argument('cmake_options', nargs='*', help='Extra options to pass to CMake, e.g. -DBUILD_SHARED_LIBS:BOOL=OFF') |
93 | 139 | args = parser.parse_args() |
94 | 140 |
|
95 | 141 | build_wheels(cleanup=args.cleanup, py_envs=args.py_envs, cmake_options=args.cmake_options) |
96 | | - fixup_wheels(args.py_envs, ';'.join(args.lib_paths)) |
| 142 | + fixup_wheels(args.py_envs, ';'.join(args.lib_paths), ';'.join(args.exclude_libs)) |
0 commit comments