Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cmake): use cmake's FindPython module #151

Merged
merged 2 commits into from
Aug 1, 2024
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

-
- Use `cmake`'s `FindPython` module by [@XuehaiPan](https://github.com/XuehaiPan) in [#151](https://github.com/metaopt/optree/pull/151).

### Fixed

Expand Down
38 changes: 20 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
# ==============================================================================

cmake_minimum_required(VERSION 3.11) # for FetchContent
cmake_minimum_required(VERSION 3.18)
project(optree LANGUAGES CXX)

include(FetchContent)
Expand Down Expand Up @@ -123,52 +123,54 @@ function(system)
endif()
endfunction()

if(NOT DEFINED PYTHON_EXECUTABLE)
if(NOT DEFINED Python_EXECUTABLE)
if(WIN32)
set(PYTHON_EXECUTABLE "python.exe")
set(Python_EXECUTABLE "python.exe")
else()
set(PYTHON_EXECUTABLE "python")
set(Python_EXECUTABLE "python")
endif()
endif()

if(UNIX)
system(
STRIP OUTPUT_VARIABLE PYTHON_EXECUTABLE
COMMAND bash -c "type -P '${PYTHON_EXECUTABLE}'"
STRIP OUTPUT_VARIABLE Python_EXECUTABLE
COMMAND bash -c "type -P '${Python_EXECUTABLE}'"
)
endif()

system(
STRIP OUTPUT_VARIABLE PYTHON_VERSION
COMMAND "${PYTHON_EXECUTABLE}" -c "print('.'.join(map(str, __import__('sys').version_info[:3])))"
STRIP OUTPUT_VARIABLE Python_VERSION
COMMAND "${Python_EXECUTABLE}" -c "print('.'.join(map(str, __import__('sys').version_info[:3])))"
)

message(STATUS "Use Python version: ${PYTHON_VERSION}")
message(STATUS "Use Python executable: \"${PYTHON_EXECUTABLE}\"")
message(STATUS "Use Python version: ${Python_VERSION}")
message(STATUS "Use Python executable: \"${Python_EXECUTABLE}\"")

if(NOT DEFINED PYTHON_INCLUDE_DIR)
unset(Python_INCLUDE_DIR)
if(NOT DEFINED Python_INCLUDE_DIR)
message(STATUS "Auto detecting Python include directory...")
system(
STRIP OUTPUT_VARIABLE PYTHON_INCLUDE_DIR
COMMAND "${PYTHON_EXECUTABLE}" -c "print(__import__('sysconfig').get_path('platinclude'))"
STRIP OUTPUT_VARIABLE Python_INCLUDE_DIR
COMMAND "${Python_EXECUTABLE}" -c "print(__import__('sysconfig').get_path('platinclude'))"
)
endif()

if("${PYTHON_INCLUDE_DIR}" STREQUAL "")
if("${Python_INCLUDE_DIR}" STREQUAL "")
message(FATAL_ERROR "Python include directory not found")
else()
message(STATUS "Detected Python include directory: \"${PYTHON_INCLUDE_DIR}\"")
include_directories("${PYTHON_INCLUDE_DIR}")
message(STATUS "Detected Python include directory: \"${Python_INCLUDE_DIR}\"")
include_directories("${Python_INCLUDE_DIR}")
endif()

# Include pybind11
set(PYBIND11_PYTHON_VERSION "${PYTHON_VERSION}")
set(PYBIND11_PYTHON_VERSION "${Python_VERSION}")
set(PYBIND11_FINDPYTHON ON)

if(NOT DEFINED PYBIND11_CMAKE_DIR)
message(STATUS "Auto detecting pybind11 CMake directory...")
system(
STRIP OUTPUT_VARIABLE PYBIND11_CMAKE_DIR
COMMAND "${PYTHON_EXECUTABLE}" -m pybind11 --cmakedir
COMMAND "${Python_EXECUTABLE}" -m pybind11 --cmakedir
)
endif()

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ cmake-configure: cmake-install
cmake -S . -B cmake-build-debug \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DPYTHON_EXECUTABLE="$(PYTHON)" \
-DPython_EXECUTABLE="$(PYTHON)" \
-DOPTREE_CXX_WERROR="$(OPTREE_CXX_WERROR)"

cmake-build: cmake-configure
Expand Down
42 changes: 21 additions & 21 deletions conda-recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ channels:
- conda-forge

dependencies:
- python = 3.11
- python = 3.12
- pip

# Dependency
- typing-extensions >= 4.5.0

# Build toolchain
- cmake >= 3.11
- cmake >= 3.18
- make
- cxx-compiler
- pybind11 >= 2.11.1
- pybind11 >= 2.13.1

# Benchmark
- pytorch::pytorch >= 2.0, < 2.4.0a0
Expand All @@ -50,35 +50,35 @@ dependencies:
- termcolor

# Documentation
- sphinx >= 5.2.1
- sphinx
- sphinx-rtd-theme
- sphinx-autobuild
- sphinx-copybutton
- sphinxcontrib-spelling
- sphinxcontrib-bibtex
- sphinx-autodoc-typehints >= 1.19.2
- sphinx-autodoc-typehints
- pyenchant
- hunspell-en
- docutils

# Testing
- pytest
- pytest-cov
- pytest-xdist
- isort
- conda-forge::pytest
- conda-forge::pytest-cov
- conda-forge::pytest-xdist
- conda-forge::isort
- conda-forge::black
- pylint
- mypy
- flake8
- flake8-bugbear
- flake8-comprehensions
- flake8-docstrings
- flake8-pyi
- flake8-simplify
- ruff
- doc8
- pydocstyle
- xdoctest
- conda-forge::pylint
- conda-forge::mypy
- conda-forge::flake8
- conda-forge::flake8-bugbear
- conda-forge::flake8-comprehensions
- conda-forge::flake8-docstrings
- conda-forge::flake8-pyi
- conda-forge::flake8-simplify
- conda-forge::ruff
- conda-forge::doc8
- conda-forge::pydocstyle
- conda-forge::xdoctest
- conda-forge::clang-format >= 14
- conda-forge::clang-tools >= 14 # clang-tidy
- conda-forge::cpplint
Expand Down
10 changes: 5 additions & 5 deletions docs/conda-recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ channels:
- conda-forge

dependencies:
- python = 3.11
- python = 3.12
- pip

# Dependency
- typing-extensions >= 4.5.0

# Build toolchain
- cmake >= 3.11
- cmake >= 3.18
- make
- cxx-compiler
- pybind11 >= 2.11.1
- pybind11 >= 2.13.1

# Documentation
- sphinx >= 5.2.1
- sphinx
- sphinx-rtd-theme
- sphinx-autobuild
- sphinx-copybutton
- sphinxcontrib-spelling
- sphinxcontrib-bibtex
- sphinx-autodoc-typehints >= 1.19.2
- sphinx-autodoc-typehints
- pyenchant
- hunspell-en
- docutils
4 changes: 2 additions & 2 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
--requirement ../requirements.txt

sphinx >= 5.2.1
sphinx
sphinx-autoapi
sphinx-autobuild
sphinx-copybutton
sphinx-rtd-theme
sphinxcontrib-bibtex
sphinx-autodoc-typehints >= 1.19.2
sphinx-autodoc-typehints
docutils

jax[cpu] >= 0.4.6
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ lint = [
]
test = ["pytest", "pytest-cov", "pytest-xdist"]
docs = [
"sphinx >= 5.2.1",
"sphinx",
"sphinx-autoapi",
"sphinx-autobuild",
"sphinx-copybutton",
"sphinx-rtd-theme",
"sphinxcontrib-bibtex",
"sphinx-autodoc-typehints >= 1.19.2",
"sphinx-autodoc-typehints",
"docutils",
"jax[cpu]",
"numpy",
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def build_extension(self, ext):
f'-DCMAKE_BUILD_TYPE={config}',
f'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{config.upper()}={ext_path.parent}',
f'-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{config.upper()}={build_temp}',
f'-DPYTHON_EXECUTABLE={sys.executable}',
f'-DPYTHON_INCLUDE_DIR={sysconfig.get_path("platinclude")}',
f'-DPython_EXECUTABLE={sys.executable}',
f'-DPython_INCLUDE_DIR={sysconfig.get_path("platinclude")}',
]

if platform.system() == 'Darwin':
Expand Down
Loading