Skip to content

Commit

Permalink
Don't include versioned libs in wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreinking committed Aug 29, 2024
1 parent 4ce77b1 commit 2b5186d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 106 deletions.
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,12 @@ WITH_PYTHON_BINDINGS = true
WITH_TESTS = false
WITH_TUTORIALS = false

##
# Don't version libHalide.so/dylib -- wheels are zip files that do
# not understand symbolic links. Including version information here
# causes the final wheel to have three copies of our library. Not good.
Halide_VERSION_OVERRIDE = ""
Halide_SOVERSION_OVERRIDE = ""

[tool.setuptools_scm]
version_scheme = "release-branch-semver"
170 changes: 67 additions & 103 deletions python_bindings/src/halide/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,64 +1,63 @@
set(native_sources
PyArgument.cpp
PyBoundaryConditions.cpp
PyBuffer.cpp
PyCallable.cpp
PyConciseCasts.cpp
PyDerivative.cpp
PyEnums.cpp
PyError.cpp
PyExpr.cpp
PyExternFuncArgument.cpp
PyFunc.cpp
PyFuncRef.cpp
PyGenerator.cpp
PyHalide.cpp
PyImageParam.cpp
PyInlineReductions.cpp
PyIROperator.cpp
PyLambda.cpp
PyLoopLevel.cpp
PyModule.cpp
PyParam.cpp
PyParameter.cpp
PyPipeline.cpp
PyRDom.cpp
PyStage.cpp
PyTarget.cpp
PyTuple.cpp
PyType.cpp
PyVar.cpp
PyVarOrRVar.cpp
)
list(TRANSFORM native_sources PREPEND "halide_/")
pybind11_add_module(Halide_Python)
add_library(Halide::Python ALIAS Halide_Python)

set(python_sources
__init__.py
_generator_helpers.py
imageio.py
)
set_target_properties(
Halide_Python
PROPERTIES
OUTPUT_NAME halide_
EXPORT_NAME Python
)

target_sources(
Halide_Python
PRIVATE
halide_/PyArgument.cpp
halide_/PyBoundaryConditions.cpp
halide_/PyBuffer.cpp
halide_/PyCallable.cpp
halide_/PyConciseCasts.cpp
halide_/PyDerivative.cpp
halide_/PyEnums.cpp
halide_/PyError.cpp
halide_/PyExpr.cpp
halide_/PyExternFuncArgument.cpp
halide_/PyFunc.cpp
halide_/PyFuncRef.cpp
halide_/PyGenerator.cpp
halide_/PyHalide.cpp
halide_/PyImageParam.cpp
halide_/PyInlineReductions.cpp
halide_/PyIROperator.cpp
halide_/PyLambda.cpp
halide_/PyLoopLevel.cpp
halide_/PyModule.cpp
halide_/PyParam.cpp
halide_/PyParameter.cpp
halide_/PyPipeline.cpp
halide_/PyRDom.cpp
halide_/PyStage.cpp
halide_/PyTarget.cpp
halide_/PyTuple.cpp
halide_/PyType.cpp
halide_/PyVar.cpp
halide_/PyVarOrRVar.cpp
)

# It is technically still possible for a user to override the LIBRARY_OUTPUT_DIRECTORY by setting
# CMAKE_LIBRARY_OUTPUT_DIRECTORY_<CONFIG>, but they do so at their own peril. If a user needs to
# do this, they should use the CMAKE_PROJECT_Halide_Python_INCLUDE_BEFORE variable to override it
# just for this project, rather than globally, and they should ensure that the last path component
# is `halide`. Otherwise, the tests will break.
pybind11_add_module(Halide_Python MODULE ${native_sources})
add_library(Halide::Python ALIAS Halide_Python)
set_target_properties(
Halide_Python
PROPERTIES
LIBRARY_OUTPUT_NAME halide_
LIBRARY_OUTPUT_DIRECTORY "$<CONFIG>/halide"
EXPORT_NAME Python
Halide_Python PROPERTIES LIBRARY_OUTPUT_DIRECTORY "$<CONFIG>/halide"
)

if (Halide_ASAN_ENABLED)
set_target_properties(
Halide_Python
PROPERTIES
CMAKE_SHARED_LINKER_FLAGS -shared-libasan
Halide_Python PROPERTIES CMAKE_SHARED_LINKER_FLAGS -shared-libasan
)
endif ()

target_link_libraries(Halide_Python PRIVATE Halide::Halide)

# TODO: There's precious little information about why Python only sometimes prevents DLLs from loading from the PATH
Expand All @@ -74,22 +73,24 @@ add_custom_command(
)

# Copy our Python source files over so that we have a valid package in the binary directory.
# TODO: When upgrading to CMake 3.23 or beyond, investigate the FILE_SET feature.
set(build_tree_pys "")
foreach (pysrc IN LISTS python_sources)
# TODO: CMake 3.22 still doesn't allow target-dependent genex in OUTPUT, but we can hack around this using a stamp
# file. Fix this hack up if and when they ever improve this feature.
set(stamp_file "${CMAKE_CURRENT_BINARY_DIR}/.${pysrc}.stamp")
add_custom_command(
OUTPUT "${stamp_file}"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/${pysrc}" "$<TARGET_FILE_DIR:Halide_Python>/${pysrc}"
COMMAND ${CMAKE_COMMAND} -E touch "${stamp_file}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${pysrc}"
VERBATIM
)
list(APPEND build_tree_pys "${stamp_file}")
endforeach ()
add_custom_target(Halide_Python_sources ALL DEPENDS ${build_tree_pys})
set(python_sources
__init__.py
_generator_helpers.py
imageio.py)

list(TRANSFORM python_sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/"
OUTPUT_VARIABLE python_sources_source_dir)

set(stamp_file "$<CONFIG>/Halide_Python_sources.stamp")
add_custom_command(
OUTPUT "${stamp_file}"
COMMAND "${CMAKE_COMMAND}" -E copy -t $<TARGET_FILE_DIR:Halide_Python> ${python_sources_source_dir}
COMMAND "${CMAKE_COMMAND}" -E touch ${stamp_file}
DEPENDS ${python_sources_source_dir}
VERBATIM
)

add_custom_target(Halide_Python_sources DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${stamp_file}")
add_dependencies(Halide_Python Halide_Python_sources)

##
Expand All @@ -102,14 +103,9 @@ include(GNUInstallDirs)
set(Halide_INSTALL_PYTHONDIR "${CMAKE_INSTALL_LIBDIR}/python3/site-packages/halide"
CACHE STRING "Path to the Python site-packages folder")

install(DIRECTORY "$<TARGET_FILE_DIR:Halide_Python>/"
install(FILES ${python_sources}
DESTINATION "${Halide_INSTALL_PYTHONDIR}"
COMPONENT Halide_Python
FILES_MATCHING
PATTERN "*.py"
PATTERN "*/halide_" EXCLUDE
PATTERN "*/CMakeFiles" EXCLUDE
PATTERN "*/__pycache__" EXCLUDE)
COMPONENT Halide_Python)

install(TARGETS Halide_Python
EXPORT Halide_Targets
Expand All @@ -118,38 +114,6 @@ install(TARGETS Halide_Python

get_property(halide_is_imported TARGET Halide::Halide PROPERTY IMPORTED)
get_property(halide_type TARGET Halide::Halide PROPERTY TYPE)
cmake_dependent_option(
Halide_Python_INSTALL_IMPORTED_DEPS "" OFF
"halide_is_imported;halide_type STREQUAL \"SHARED_LIBRARY\"" OFF
)

if (Halide_Python_INSTALL_IMPORTED_DEPS)
# The following might be a bit confusing, but installing both libHalide
# and its SONAME symbolic link causes the following bad behavior:
# 1. CMake does the right thing and installs libHalide.so.X.Y.Z
# (TARGET_FILE) as a real file and libHalide.so.X
# (TARGET_SONAME_FILE_NAME) as a symbolic link to the former.
# 2. Setuptools dutifully packs both of these into a Python wheel, which
# is a structured zip file. Zip files do not support symbolic links.
# Thus, two independent copies of libHalide are inserted, bloating the
# package.
# The Python module (on Unix systems) links to the SONAME file, and
# installing the symbolic link directly results in a broken link. Hence,
# the renaming dance here.

if (NOT MSVC)
set(rename_arg RENAME "$<TARGET_SONAME_FILE_NAME:Halide::Halide>")
else ()
# DLL systems do not have sonames.
set(rename_arg "")
endif ()

# TODO: when we upgrade to CMake 3.22, replace with RUNTIME_DEPENDENCY_SET?
install(FILES "$<TARGET_FILE:Halide::Halide>"
DESTINATION "${Halide_INSTALL_PYTHONDIR}"
COMPONENT Halide_Python
${rename_arg})
endif ()

if (
NOT CMAKE_INSTALL_RPATH # Honor user overrides
Expand Down
16 changes: 13 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ if (NOT BUILD_SHARED_LIBS)
endif ()

# Set the (shared) library version
set(Halide_VERSION_OVERRIDE "${Halide_VERSION}"
CACHE STRING "VERSION to set for custom Halide packaging")
mark_as_advanced(Halide_VERSION_OVERRIDE)

if (Halide_VERSION_OVERRIDE)
# Empty is considered a value distinct from not-defined
set_target_properties(Halide PROPERTIES VERSION "${Halide_VERSION_OVERRIDE}")
endif ()

set(Halide_SOVERSION_OVERRIDE "${Halide_VERSION_MAJOR}"
CACHE STRING "SOVERSION to set for custom Halide packaging")
mark_as_advanced(Halide_SOVERSION_OVERRIDE)

set_target_properties(Halide PROPERTIES
VERSION "${Halide_VERSION}"
SOVERSION "${Halide_SOVERSION_OVERRIDE}")
if (Halide_SOVERSION_OVERRIDE)
# Empty is considered a value distinct from not-defined
set_target_properties(Halide PROPERTIES SOVERSION "${Halide_SOVERSION_OVERRIDE}")
endif ()

# Always build with PIC, even when static
set_target_properties(Halide PROPERTIES POSITION_INDEPENDENT_CODE ON)
Expand Down

0 comments on commit 2b5186d

Please sign in to comment.