Skip to content

Commit

Permalink
Initial support for building Python wheels
Browse files Browse the repository at this point in the history
  • Loading branch information
selimnairb committed Aug 29, 2023
1 parent 5d1b1d9 commit 345d5f1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 45 deletions.
71 changes: 29 additions & 42 deletions api/swig/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ cmake_policy(SET CMP0078 NEW)
cmake_policy(SET CMP0086 NEW)

# Handle where to install the resulting Python package
#if (CALL_FROM_SETUP_PY)
# # The CMakeExtension will set CMAKE_INSTALL_PREFIX to the root
# # of the resulting wheel archive
# set(S2GEOMETRY_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
#else()
# # The Python package is installed directly in the folder of the
# # detected interpreter (system, user, or virtualenv)
# set(S2GEOMETRY_INSTALL_PREFIX ${Python3_SITELIB})
#endif()
if (CALL_FROM_SETUP_PY)
# The CMakeExtension will set CMAKE_INSTALL_PREFIX to the root
# of the resulting wheel archive
set(BAGPY_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
else()
# The Python package is installed directly in the folder of the
# detected interpreter (system, user, or virtualenv)
set(BAGPY_INSTALL_PREFIX ${Python3_SITELIB})
endif()

if(Python_LOOKUP_VERSION)
set(Python_FIND_STRATEGY VERSION)
Expand Down Expand Up @@ -56,38 +56,25 @@ target_link_libraries(bagPy
baglib
)

#if(${CMAKE_SYSTEM_NAME} MATCHES Darwin)
# if(${CMAKE_BUILD_TYPE} STREQUAL Release OR ${CMAKE_BUILD_TYPE} STREQUAL MinSizeRel)
# set(BAGPY_LIB _bagPy.so)
# else()
# set(BAGPY_LIB _bagPyd.so)
# endif()
# # Somehow CPython on macOS looks for native code to end in .so instead of .dylib so copy the
# # compiled extension to a file ending in .so...
# add_custom_command(TARGET bagPy POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:bagPy> "${BAGPY_LIB}")
#elseif(${CMAKE_SYSTEM_NAME} MATCHES Linux)
# if(${CMAKE_BUILD_TYPE} STREQUAL Release OR ${CMAKE_BUILD_TYPE} STREQUAL MinSizeRel)
# set(BAGPY_LIB _bagPy.so)
# else()
# set(BAGPY_LIB _bagPyd.so)
# endif()
#elseif(${CMAKE_SYSTEM_NAME} MATCHES Windows)
# # TODO: Adjust this for debug builds on Windows as above
# set(BAGPY_LIB _bagPy.pyd)
#endif()

# Files to install with Python
#set(PYTHON_INSTALL_FILES
# ${CMAKE_CURRENT_BINARY_DIR}/${BAGPY_MODULE_NAME}
# ${CMAKE_CURRENT_BINARY_DIR}/${BAGPY_LIB})
if(${CMAKE_SYSTEM_NAME} MATCHES Darwin)
set(BAGPY_LIB _bagPy.so)
# Somehow CPython on macOS looks for native code to end in .so instead of .dylib so copy the
# compiled extension to a file ending in .so...
add_custom_command(TARGET bagPy POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:bagPy> "${BAGPY_LIB}")
elseif(${CMAKE_SYSTEM_NAME} MATCHES Linux)
set(BAGPY_LIB _bagPy.so)
elseif(${CMAKE_SYSTEM_NAME} MATCHES Windows)
set(BAGPY_LIB _bagPy.pyd)
endif()

# Configure setup.py and copy to output directory
#set(SETUP_PY_IN ${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in)
#set(SETUP_PY_OUT ${CMAKE_CURRENT_BINARY_DIR}/setup.py)
#configure_file(${SETUP_PY_IN} ${SETUP_PY_OUT})
# Install the wrapper.
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${BAGPY_LIB} DESTINATION ${BAGPY_INSTALL_PREFIX})

# Install target to call setup.py
#add_custom_target(install-python
# DEPENDS _bagPy
# COMMAND python ${SETUP_PY_OUT} install)
# Install swig-generated Python file (we rename it to __init__.py as it will
# ultimately end up in a directory called bagPy in site-packages, which will
# serve as the module directory.
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/bagPy.py"
DESTINATION ${BAGPY_INSTALL_PREFIX}
RENAME __init__.py
COMPONENT bagPy)
2 changes: 1 addition & 1 deletion api/swig/python/setup.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ python_requres = >=3.8

[options.packages.find]
where =
${CMAKE_SOURCE_DIR}/api
${CMAKE_SOURCE_DIR}/api/python
4 changes: 2 additions & 2 deletions docs/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#### Build C++ library, tests, and example binaries:
```shell
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -B build -S . -DCMAKE_INSTALL_PREFIX=/usr/local \
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -B build -S . \
-DBAG_BUILD_TESTS:BOOL=ON -DBAG_CODE_COVERAGE:BOOL=ON \
-DBAG_BUILD_PYTHON:BOOL=OFF -DBAG_BUILD_EXAMPLES:BOOL=ON
cmake --build build -j 8
Expand Down Expand Up @@ -39,7 +39,7 @@ $ python -m pip wheel -w dist/ .

Then you can install the wheel with:
```shell
$ python -m pip
$ python -m pip install dist/bagPy-*.whl
```

## Windows: Visual Studio 2022/2019
Expand Down

0 comments on commit 345d5f1

Please sign in to comment.