Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ add_library(mylib ...)
# Python package version.
# AUTHOR
# Package author name.
# EMAIL
# Package author email address.
# URL
# Package website.
# PYTHON_REQUIRES
Expand All @@ -40,6 +42,8 @@ add_library(mylib ...)
# CMake targets which belong into the same wheel.
# MODULE_DEPENDENCIES
# Python module dependencies (requirements.txt content)
# SCRIPTS
# Additional scripts that should be part of the wheel.
# SUBMODULES
# Any pybind11 submodules must be listed here to support imports like
# "from mod.sub import x". A nested submodule must be listed like
Expand All @@ -48,14 +52,19 @@ add_wheel(mylib-python-bindings
NAME mylib
VERSION "0.0.1"
AUTHOR "Bob Ross"
EMAIL "email@address.com"
URL "http://python.org"
PYTHON_REQUIRES ">=3.8"
DESCRIPTION "Binary Python wheel."
DEPLOY_FILES "MY_LICENSE.txt"
TARGET_DEPENDENCIES
dependency-lib
MODULE_DEPENDENCIES
pypi-dependency1 pypi-dependency2)
pypi-dependency1 pypi-dependency2
SCRIPTS
/path/to/python/script1
/path/to/python/script2
)
```

The `add_wheel` command will create a temporary `setup.py` for your project in the build folder, which bundles the necessary files. The execution of this `setup.py` is attached to the custom target `wheelname-setup-py`. It will be executed when you run `cmake --build .` in your build directory.
Expand Down
18 changes: 16 additions & 2 deletions python-wheel.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ function (add_wheel WHEEL_TARGET)
# Parse arguments
cmake_parse_arguments(WHEEL
""
"NAME;AUTHOR;URL;PYTHON_REQUIRES;VERSION;DESCRIPTION;README_PATH;LICENSE_PATH"
"TARGET_DEPENDENCIES;MODULE_DEPENDENCIES;DEPLOY_FILES;SUBMODULES" ${ARGN})
"NAME;AUTHOR;URL;EMAIL;PYTHON_REQUIRES;VERSION;DESCRIPTION;README_PATH;LICENSE_PATH"
"TARGET_DEPENDENCIES;MODULE_DEPENDENCIES;DEPLOY_FILES;SUBMODULES;SCRIPTS" ${ARGN})

to_python_list_string(WHEEL_MODULE_DEPENDENCIES WHEEL_MODULE_DEPENDENCIES_PYLIST)
to_python_list_string(WHEEL_SCRIPTS WHEEL_SCRIPTS_PYLIST)

if (NOT WHEEL_VERSION)
message(FATAL_ERROR "Missing wheel version.")
Expand All @@ -57,6 +58,10 @@ function (add_wheel WHEEL_TARGET)
set(WHEEL_URL "")
endif()

if (NOT WHEEL_EMAIL)
set(WHEEL_EMAIL "")
endif()

if (NOT WHEEL_PYTHON_REQUIRES)
set(WHEEL_PYTHON_REQUIRES ">=3.8")
endif()
Expand Down Expand Up @@ -131,6 +136,15 @@ function (add_wheel WHEEL_TARGET)
endforeach()
endif()

if (WHEEL_SCRIPTS)
foreach (file IN LISTS WHEEL_SCRIPTS)
add_custom_command(TARGET ${WHEEL_TARGET}-copy-files
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Copying script from ${file} to ${WHEEL_PACKAGE_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy "${file}" "${WHEEL_PACKAGE_DIR}/")
endforeach()
endif()

set(SETUP_FILE "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
configure_file("${PY_WHEEL_SETUP_FILE}" "${SETUP_FILE}")

Expand Down
14 changes: 12 additions & 2 deletions setup.py.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

import os, sys, platform
import os, sys, platform, stat
import zipfile
import subprocess
import re
Expand Down Expand Up @@ -56,11 +56,12 @@ class install_lib(_install_lib):
self.build_dir = '@WHEEL_LIB_DIR@'


setup(
kwargs = dict(
name = "@WHEEL_NAME@",
version = "@WHEEL_VERSION@",
description = "@WHEEL_DESCRIPTION@",
author = "@WHEEL_AUTHOR@",
author_email='@WHEEL_EMAIL@',
url = "@WHEEL_URL@",

ext_modules = [CMakeExtension("@WHEEL_TARGET@")],
Expand All @@ -74,6 +75,9 @@ setup(
package_data = {
"@WHEEL_NAME@": ["*"]
},
scripts = [
@WHEEL_SCRIPTS_PYLIST@
],

cmdclass = {
'bdist': bdist,
Expand All @@ -82,3 +86,9 @@ setup(
'build_ext': cmake_build_ext,
},
)

for script in kwargs['scripts']:
st = os.stat(script)
os.chmod(script, st.st_mode | stat.S_IEXEC)

setup(**kwargs)