-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to create Python wheels (#313)
* Rename Python bindings from pywraps2 to s2geometry and set SWIG CMake policies to remove warnings about using deprecated default binding name * Fix broken unit tests * Pass -DCMAKE_POSITION_INDEPENDENT_CODE=ON to CMake via setup.py for building Python wheel * Use a PEP 440-compliant pre-release version since the code in master does not correspond to already released version 0.10.0
- Loading branch information
1 parent
d184302
commit 7773d51
Showing
6 changed files
with
131 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[build-system] | ||
requires = [ | ||
"wheel", | ||
"setuptools", | ||
"setuptools_scm[toml]", | ||
"cmake_build_extension", | ||
] | ||
build-backend = "setuptools.build_meta" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[metadata] | ||
name = s2geometry | ||
version = 0.11.0.dev1 | ||
description = Python packaging of s2geometry | ||
author = Brian Miles | ||
author_email = selimnairb@gmail.com | ||
license= Apache 2 | ||
project_urls = | ||
Source = https://github.com/google/s2geometry | ||
classifiers = | ||
Programming Language :: Python :: 3 | ||
Operating System :: POSIX | ||
License :: OSI Approved :: Apache Software License | ||
|
||
[options] | ||
zip_safe = False | ||
packages = find: | ||
package_dir = =src | ||
python_requres = >=3.7 | ||
|
||
[options.packages.find] | ||
where = src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import cmake_build_extension | ||
import setuptools | ||
|
||
|
||
setuptools.setup( | ||
ext_modules=[ | ||
cmake_build_extension.CMakeExtension( | ||
# This could be anything you like, it is used to create build folders | ||
name="SwigBindings", | ||
# Name of the resulting package name (import s2geometry) | ||
install_prefix="s2geometry", | ||
# Selects the folder where the main CMakeLists.txt is stored | ||
# (it could be a subfolder) | ||
source_dir=str(Path(__file__).parent.absolute()), | ||
cmake_configure_options=[ | ||
# This option points CMake to the right Python interpreter, and helps | ||
# the logic of FindPython3.cmake to find the active version | ||
f"-DPython3_ROOT_DIR={Path(sys.prefix)}", | ||
'-DCALL_FROM_SETUP_PY:BOOL=ON', | ||
'-DBUILD_SHARED_LIBS:BOOL=OFF', | ||
'-DCMAKE_POSITION_INDEPENDENT_CODE=ON', | ||
'-DWITH_PYTHON=ON' | ||
] | ||
) | ||
], | ||
cmdclass=dict( | ||
# Enable the CMakeExtension entries defined above | ||
build_ext=cmake_build_extension.BuildExtension, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,43 @@ | ||
# Generate standard target names. | ||
cmake_policy(SET CMP0078 NEW) | ||
# Honor SWIG_MODULE_NAME via -module flag. | ||
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() | ||
|
||
include(${SWIG_USE_FILE}) | ||
include_directories(${Python3_INCLUDE_DIRS}) | ||
|
||
set(CMAKE_SWIG_FLAGS "") | ||
set_property(SOURCE s2.i PROPERTY SWIG_FLAGS "-module" "pywraps2") | ||
set_property(SOURCE s2.i PROPERTY SWIG_FLAGS "-module" "s2geometry") | ||
set_property(SOURCE s2.i PROPERTY CPLUSPLUS ON) | ||
|
||
swig_add_library(pywraps2 LANGUAGE python SOURCES s2.i) | ||
swig_add_library(s2geometry LANGUAGE python SOURCES s2.i) | ||
|
||
swig_link_libraries(pywraps2 ${Python3_LIBRARIES} s2) | ||
swig_link_libraries(s2geometry ${Python3_LIBRARIES} s2) | ||
enable_testing() | ||
add_test(NAME pywraps2_test COMMAND | ||
add_test(NAME s2geometry_test COMMAND | ||
${Python3_EXECUTABLE} | ||
"${PROJECT_SOURCE_DIR}/src/python/pywraps2_test.py") | ||
set_property(TEST pywraps2_test PROPERTY ENVIRONMENT | ||
"${PROJECT_SOURCE_DIR}/src/python/s2geometry_test.py") | ||
set_property(TEST s2geometry_test PROPERTY ENVIRONMENT | ||
"PYTHONPATH=$ENV{PYTHONPATH}:${PROJECT_BINARY_DIR}/python") | ||
|
||
# Install the wrapper. | ||
install(TARGETS _pywraps2 DESTINATION ${Python3_SITELIB}) | ||
install(FILES "${PROJECT_BINARY_DIR}/python/pywraps2.py" | ||
DESTINATION ${Python3_SITELIB}) | ||
install(TARGETS s2geometry DESTINATION ${S2GEOMETRY_INSTALL_PREFIX}) | ||
|
||
# Install swig-generated Python file (we rename it to __init__.py as it will | ||
# ultimately end up in a directory called s2geometry in site-packages, which will | ||
# serve as the module directory. | ||
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/s2geometry.py" | ||
DESTINATION ${S2GEOMETRY_INSTALL_PREFIX} | ||
RENAME __init__.py | ||
COMPONENT s2geometry) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters