Skip to content

Commit 46a5a3d

Browse files
authored
Prune and verify architectures for rebuilt macOS wheels (#21692)
This is to prune unused architectures from the wheels we rebuild and "repair" on macOS. The primary driver is to enable `delocate_wheel`'s architecture verification (`require_archs`) to ensure all bundled binaries and their dependencies have matching architectures. This brings additional benefits: - the name of rebuilt/repaired wheels now accurately reflect their content, (no foreign architecture in them) - there's no longer a need for renaming repaired wheels, as their names are now correctly set in the first place by the `pip` toolchain when rebuilding, - the size of each rebuilt wheel decreases by 10% to 50% - a modest but welcome improvement that helps reduce the Agent's software distribution costs. Single-architecture builds are obtained by setting environment variables in the `build_macos` function: - `ARCHFLAGS='-arch <target_arch>'`: set effective build output architecture (bundled binaries), - `_PYTHON_HOST_PLATFORM=macosx-X.Y-<target_arch>`: set wheel platform tag (used by the `pip` toolchain to set the wheel's name). Together, they ensure both binaries and wheel metadata match the single target architecture, with the former allowing `delocate_wheel` to verify architecture consistency. Why environment variables: there is no `--platform` flag for `pip wheel` to control the build output architecture, because the proposed flag (pypa/pip#5453) was never merged. Environment variables are the _de facto_ standard approach used by `cibuildwheel`, `conda-forge` as well as other wheel building tools. References: - pypa/pip#5453: proposal for `--platform` flag (not implemented), - pypa/cibuildwheel#997: cross-compilation implementation, - pypa/packaging#882: architecture tag control on macOS, - https://cibuildwheel.pypa.io/en/stable/platforms/
1 parent a32934f commit 46a5a3d

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

.builders/build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ def build_macos():
124124
'LDFLAGS': f'-L{prefix_path}/lib',
125125
'CFLAGS': f'-I{prefix_path}/include -O2',
126126
'CXXFLAGS': f'-I{prefix_path}/include -O2',
127+
# Use single-arch builds to avoid bundling extraneous binary payload and enable `require_archs` verification
128+
'ARCHFLAGS': f'-arch {os.uname().machine}',
129+
'_PYTHON_HOST_PLATFORM': f'macosx-{os.environ["MACOSX_DEPLOYMENT_TARGET"]}-{os.uname().machine}',
127130
# Build command for extra platform-specific build steps
128131
'DD_BUILD_COMMAND': f'bash {build_context_dir}/extra_build.sh'
129132
}

.builders/images/macos/extra_build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if [[ "${DD_BUILD_PYTHON_VERSION}" == "3" ]]; then
1919

2020
# lmdb doesnt't get the actual full path in its install name which means delocate won't find it
2121
# Luckily we can patch it here so that it does.
22-
install_name_tool -change liblmdb.so "${DD_PREFIX_PATH}/lib/liblmdb.so" "${DD_PREFIX_PATH}/lib//librdkafka.1.dylib"
22+
install_name_tool -change liblmdb.so "${DD_PREFIX_PATH}/lib/liblmdb.so" "${DD_PREFIX_PATH}/lib/librdkafka.1.dylib"
2323
always_build+=("confluent-kafka")
2424
fi
2525

.builders/scripts/repair_wheels.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,15 @@ def copy_filt_func(libname):
269269
shutil.move(wheel, Path(built_dir) / dest)
270270
continue
271271

272-
# Platform dependent wheels: rename with single arch and verify target macOS version
273-
single_arch = os.uname().machine
274-
dest = str(wheel_name._replace(platform_tag=wheel_name.platform_tag.replace('universal2', single_arch)))
272+
# Platform dependent wheels: prune excluded files, verify target architecture & macOS version
275273
copied_libs = delocate_wheel(
276274
str(wheel),
277-
os.path.join(built_dir, dest),
275+
os.path.join(built_dir, wheel.name),
278276
copy_filt_func=copy_filt_func,
279-
# require_archs=[single_arch], TODO(regis): address multi-arch confluent_kafka/cimpl.cpython-312-darwin.so
277+
require_archs=[os.uname().machine],
280278
require_target_macos_version=min_macos_version,
281279
)
282-
print(f'Repaired wheel to {dest}')
280+
print('Repaired wheel')
283281
if copied_libs:
284282
print('Libraries copied into the wheel:')
285283
print('\n'.join(copied_libs))

0 commit comments

Comments
 (0)