Skip to content

Commit

Permalink
update pybind11, cmakelists with official cmake python3
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej Dundovic committed Jan 12, 2024
1 parent 0b0743f commit a00c6a8
Show file tree
Hide file tree
Showing 48 changed files with 1,975 additions and 675 deletions.
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ target_link_libraries(hermes ${HERMES_EXTRA_LIBRARIES})
# Python
# ----------------------------------------------------------------------------
option(ENABLE_PYTHON "Create python library via Pybind11" OFF)
include(FindPython)
find_package(Python COMPONENTS Interpreter Development)
include(FindPython3)
find_package(Python3 COMPONENTS Interpreter Development)

if(ENABLE_PYTHON AND PYTHONLIBS_FOUND)
include_directories(${PYTHON_INCLUDE_DIRS})
set(PYBIND11_PYTHON_VERSION ${PYTHON_DOT_VERSION})
if(ENABLE_PYTHON AND Python3_Development_FOUND)
include_directories(${Python3_INCLUDE_DIRS})
set(PYBIND11_PYTHON_VERSION ${Python3_VERSION})
add_subdirectory(lib/pybind11)
# for PYBIND11_EXPAND_SIDE_EFFECTS in pybind11.h - temporary,
# should be removed after the fix lands to the upstream
Expand Down Expand Up @@ -381,8 +381,8 @@ if(ENABLE_PYTHON AND PYTHONLIBS_FOUND)
LINKER_LANGUAGE CXX)
target_link_libraries(pyhermes PRIVATE hermes)

install(TARGETS pyhermes LIBRARY DESTINATION "${PYTHON_SITE_PACKAGES}")
endif(ENABLE_PYTHON AND PYTHONLIBS_FOUND)
install(TARGETS pyhermes LIBRARY DESTINATION "${Python3_SITELIB}")
endif(ENABLE_PYTHON AND Python3_Development_FOUND)

# ------------------------------------------------------------------
# Documentation
Expand Down
11 changes: 7 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ USER ${NB_UID}

# Install Python 3 packages
RUN mamba install --yes \
'conda-forge::sphinx' \
'healpy' && \
mamba clean --all -f -y && \
fix-permissions "${CONDA_DIR}" && \
Expand All @@ -32,13 +33,15 @@ RUN mamba install --yes \
WORKDIR /tmp
RUN git clone https://github.com/cosmicrays/hermes.git
WORKDIR /tmp/hermes
RUN conda init
ENV PATH /opt/conda/bin:$PATH
RUN /bin/bash -c "source activate base"
RUN mkdir build && \
cd build && \
CMAKE_PREFIX_PATH=${CONDA_DIR} cmake \
-DPython_EXECUTABLE=${CONDA_DIR}/bin/${PYTHON_VERSION} \
-DPython_INCLUDE_DIRS=${CONDA_DIR}/include/${PYTHON_VERSION} \
-DPython_LIBRARIES=${CONDA_DIR}/lib/${PYTHON_VERSION} \
-DPython3_EXECUTABLE=${CONDA_DIR}/bin/python${PYTHON_VERSION} \
-DPython3_INCLUDE_DIRS=${CONDA_DIR}/include/python${PYTHON_VERSION} \
-DPython3_LIBRARIES=${CONDA_DIR}/lib/python${PYTHON_VERSION} \
-DENABLE_PYTHON=On \
-DCMAKE_INSTALL_PREFIX=${CONDA_DIR} \
-DENABLE_TESTING=On .. && \
make -j && \
Expand Down
143 changes: 55 additions & 88 deletions lib/pybind11/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,47 @@ ci:
exclude: ^tools/JoinPaths.cmake$

repos:

# Clang format the codebase automatically
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: "v17.0.6"
hooks:
- id: clang-format
types_or: [c++, c, cuda]

# Ruff, the Python auto-correcting linter/formatter written in Rust
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.9
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format

# Check static types with mypy
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.8.0"
hooks:
- id: mypy
args: []
exclude: ^(tests|docs)/
additional_dependencies:
- markdown-it-py<3 # Drop this together with dropping Python 3.7 support.
- nox
- rich
- types-setuptools

# CMake formatting
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: "v0.6.13"
hooks:
- id: cmake-format
additional_dependencies: [pyyaml]
types: [file]
files: (\.cmake|CMakeLists.txt)(.in)?$

# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v4.3.0"
rev: "v4.5.0"
hooks:
- id: check-added-large-files
- id: check-case-conflict
Expand All @@ -39,109 +77,38 @@ repos:
- id: requirements-txt-fixer
- id: trailing-whitespace

# Upgrade old Python syntax
- repo: https://github.com/asottile/pyupgrade
rev: "v3.2.0"
hooks:
- id: pyupgrade
args: [--py36-plus]

# Nicely sort includes
- repo: https://github.com/PyCQA/isort
rev: "5.10.1"
hooks:
- id: isort

# Black, the code formatter, natively supports pre-commit
- repo: https://github.com/psf/black
rev: "22.10.0" # Keep in sync with blacken-docs
hooks:
- id: black

# Also code format the docs
- repo: https://github.com/asottile/blacken-docs
rev: "v1.12.1"
rev: "1.16.0"
hooks:
- id: blacken-docs
additional_dependencies:
- black==22.10.0 # keep in sync with black hook
- black==23.*

# Changes tabs to spaces
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: "v1.3.1"
rev: "v1.5.4"
hooks:
- id: remove-tabs

# Avoid directional quotes
- repo: https://github.com/sirosen/texthooks
rev: "0.4.0"
rev: "0.6.3"
hooks:
- id: fix-ligatures
- id: fix-smartquotes

# Autoremoves unused imports
- repo: https://github.com/hadialqattan/pycln
rev: "v2.1.1"
hooks:
- id: pycln
stages: [manual]

# Checking for common mistakes
- repo: https://github.com/pre-commit/pygrep-hooks
rev: "v1.9.0"
rev: "v1.10.0"
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-no-log-warn
- id: python-use-type-annotations
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal

# Automatically remove noqa that are not used
- repo: https://github.com/asottile/yesqa
rev: "v1.4.0"
hooks:
- id: yesqa
additional_dependencies: &flake8_dependencies
- flake8-bugbear
- pep8-naming

# Flake8 also supports pre-commit natively (same author)
- repo: https://github.com/PyCQA/flake8
rev: "5.0.4"
hooks:
- id: flake8
exclude: ^(docs/.*|tools/.*)$
additional_dependencies: *flake8_dependencies

# PyLint has native support - not always usable, but works for us
- repo: https://github.com/PyCQA/pylint
rev: "v2.15.5"
hooks:
- id: pylint
files: ^pybind11

# CMake formatting
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: "v0.6.13"
hooks:
- id: cmake-format
additional_dependencies: [pyyaml]
types: [file]
files: (\.cmake|CMakeLists.txt)(.in)?$

# Check static types with mypy
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.982"
hooks:
- id: mypy
args: []
exclude: ^(tests|docs)/
additional_dependencies: [nox, rich]

# Checks the manifest for missing files (native support)
- repo: https://github.com/mgedmin/check-manifest
rev: "0.48"
rev: "0.49"
hooks:
- id: check-manifest
# This is a slow hook, so only run this if --hook-stage manual is passed
Expand All @@ -152,15 +119,15 @@ repos:
# Use tools/codespell_ignore_lines_from_errors.py
# to rebuild .codespell-ignore-lines
- repo: https://github.com/codespell-project/codespell
rev: "v2.2.2"
rev: "v2.2.6"
hooks:
- id: codespell
exclude: ".supp$"
args: ["-x", ".codespell-ignore-lines"]
args: ["-x.codespell-ignore-lines", "-Lccompiler"]

# Check for common shell mistakes
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: "v0.8.0.4"
rev: "v0.9.0.6"
hooks:
- id: shellcheck

Expand All @@ -170,12 +137,12 @@ repos:
- id: disallow-caps
name: Disallow improper capitalization
language: pygrep
entry: PyBind|Numpy|Cmake|CCache|PyTest
entry: PyBind|\bNumpy\b|Cmake|CCache|PyTest
exclude: ^\.pre-commit-config.yaml$

# Clang format the codebase automatically
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: "v14.0.6"
# PyLint has native support - not always usable, but works for us
- repo: https://github.com/PyCQA/pylint
rev: "v3.0.3"
hooks:
- id: clang-format
types_or: [c++, c, cuda]
- id: pylint
files: ^pybind11
21 changes: 19 additions & 2 deletions lib/pybind11/.readthedocs.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# https://blog.readthedocs.com/migrate-configuration-v2/

version: 2

build:
os: ubuntu-22.04
apt_packages:
- librsvg2-bin
tools:
python: "3.11"

sphinx:
configuration: docs/conf.py

python:
version: 3
requirements_file: docs/requirements.txt
install:
- requirements: docs/requirements.txt

formats:
- pdf
57 changes: 50 additions & 7 deletions lib/pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.

cmake_minimum_required(VERSION 3.4)
# Propagate this policy (FindPythonInterp removal) so it can be detected later
if(NOT CMAKE_VERSION VERSION_LESS "3.27")
cmake_policy(GET CMP0148 _pybind11_cmp0148)
endif()

cmake_minimum_required(VERSION 3.5)

# The `cmake_minimum_required(VERSION 3.4...3.22)` syntax does not work with
# The `cmake_minimum_required(VERSION 3.5...3.27)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.22)
if(${CMAKE_VERSION} VERSION_LESS 3.27)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.22)
cmake_policy(VERSION 3.27)
endif()

if(_pybind11_cmp0148)
cmake_policy(SET CMP0148 ${_pybind11_cmp0148})
unset(_pybind11_cmp0148)
endif()

# Avoid infinite recursion if tests include this as a subdirectory
Expand Down Expand Up @@ -82,9 +92,15 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(pybind11_system "")

set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if(CMAKE_VERSION VERSION_LESS "3.18")
set(_pybind11_findpython_default OFF)
else()
set(_pybind11_findpython_default ON)
endif()
else()
set(PYBIND11_MASTER_PROJECT OFF)
set(pybind11_system SYSTEM)
set(_pybind11_findpython_default OFF)
endif()

# Options
Expand All @@ -106,9 +122,18 @@ cmake_dependent_option(
"Install pybind11 headers in Python include directory instead of default installation prefix"
OFF "PYBIND11_INSTALL" OFF)

cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF
cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" ${_pybind11_findpython_default}
"NOT CMAKE_VERSION VERSION_LESS 3.12" OFF)

# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests
# (makes transition easier while we support both modes).
if(PYBIND11_MASTER_PROJECT
AND PYBIND11_FINDPYTHON
AND DEFINED PYTHON_EXECUTABLE
AND NOT DEFINED Python_EXECUTABLE)
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
endif()

# NB: when adding a header don't forget to also add it to setup.py
set(PYBIND11_HEADERS
include/pybind11/detail/class.h
Expand All @@ -126,11 +151,13 @@ set(PYBIND11_HEADERS
include/pybind11/complex.h
include/pybind11/options.h
include/pybind11/eigen.h
include/pybind11/eigen/common.h
include/pybind11/eigen/matrix.h
include/pybind11/eigen/tensor.h
include/pybind11/embed.h
include/pybind11/eval.h
include/pybind11/gil.h
include/pybind11/gil_safe_call_once.h
include/pybind11/iostream.h
include/pybind11/functional.h
include/pybind11/numpy.h
Expand All @@ -139,7 +166,9 @@ set(PYBIND11_HEADERS
include/pybind11/pytypes.h
include/pybind11/stl.h
include/pybind11/stl_bind.h
include/pybind11/stl/filesystem.h)
include/pybind11/stl/filesystem.h
include/pybind11/type_caster_pyobject_ptr.h
include/pybind11/typing.h)

# Compare with grep and warn if mismatched
if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12)
Expand Down Expand Up @@ -275,7 +304,21 @@ if(PYBIND11_INSTALL)

# pkg-config support
if(NOT prefix_for_pc_file)
set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}")
if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}")
set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}")
else()
set(pc_datarootdir "${CMAKE_INSTALL_DATAROOTDIR}")
if(CMAKE_VERSION VERSION_LESS 3.20)
set(prefix_for_pc_file "\${pcfiledir}/..")
while(pc_datarootdir)
get_filename_component(pc_datarootdir "${pc_datarootdir}" DIRECTORY)
string(APPEND prefix_for_pc_file "/..")
endwhile()
else()
cmake_path(RELATIVE_PATH CMAKE_INSTALL_PREFIX BASE_DIRECTORY CMAKE_INSTALL_DATAROOTDIR
OUTPUT_VARIABLE prefix_for_pc_file)
endif()
endif()
endif()
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in"
Expand Down
Loading

0 comments on commit a00c6a8

Please sign in to comment.