Skip to content

Commit

Permalink
scikit-build-core appears to work
Browse files Browse the repository at this point in the history
Next step is to add a yml using cibuildwheel, and then we can flesh out the tests
  • Loading branch information
nbelakovski committed Jan 12, 2024
1 parent bcdfa8d commit 2e76f3d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ install/
build/
include/
!c/include/
!python/pybind11/include/
lib/
mod/

Expand Down Expand Up @@ -227,3 +228,6 @@ cython_debug/

# Mac files
.DS_Store

# Version file
_version.txt
30 changes: 21 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,33 @@ endif ()

# Get the version number
find_package(Git)
set(IS_REPO FALSE)
if(GIT_EXECUTABLE)
# --always means git describe will output the commit hash if no tags are found
# This is usually the case for forked repos since they do not clone tags by default.
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --always --dirty
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE PRIMA_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
# If git is not available, that may indicate we are building on macports which
# downloads the bundle from github (which uses git archive) and so the version
# number should be in .git-archival.txt
file(STRINGS .git-archival.txt PRIMA_VERSION)
if(PRIMA_VERSION MATCHES "describe")
message(WARNING "No git detected and .git-archival.txt does not contain a version number")
set(PRIMA_VERSION "unknown")
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE GIT_RESULT ERROR_QUIET)
if (GIT_RESULT EQUAL 0)
set(IS_REPO TRUE)
endif()
endif()
if(NOT GIT_EXECUTABLE OR NOT IS_REPO)
# If git is not available, or this isn't a repo, that may indicate we are building
# on macports which downloads the bundle from github (which uses git archive) and
# so the version number should be in .git-archival.txt.
# Alternatively it might mean that we're building the Python bindings, in which case
# the version is output in _version.txt. I know, it's complicated. I don't make the rules.
if(EXISTS _version.txt)
file(STRINGS _version.txt PRIMA_VERSION)
else()
file(STRINGS .git-archival.txt PRIMA_VERSION)
if(PRIMA_VERSION MATCHES "describe")
message(WARNING "No git detected and .git-archival.txt does not contain a version number")
set(PRIMA_VERSION "unknown")
endif()
endif()

endif()
Expand Down
19 changes: 6 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["scikit-build-core", "pybind11", "numpy"]
requires = ["scikit-build-core", "numpy"]
build-backend = "scikit_build_core.build"

[project]
Expand All @@ -8,17 +8,10 @@ dependencies = ["numpy"]
dynamic = ["version"]

[tool.scikit-build]
cmake.targets = ["prima_pybind"]


# VERSION OPTION 1
# [tool.scikit-build.metadata.version]
# provider = "scikit_build_core.metadata.regex"
# input = "VERSION.txt"
# regex = '(?P<value>[0-9.]+)'
# VERSION OPTION 2
cmake.targets = ["_prima"]
metadata.version.provider = "scikit_build_core.metadata.setuptools_scm"
sdist.include = ["pyVERSION.txt"]
sdist.include = [".git-archival.txt"]
install.components = ["Prima_Python_C_Extension"]

[tool.setuptools_scm] # Section required
version_file = "pyVERSION.txt"
# END VERSION OPTIONS
version_file = "_version.txt"
41 changes: 19 additions & 22 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,32 @@ if (NOT _IMPORT_NUMPY EQUAL 0)
return()
endif ()

# This section is cribbed from scipy/meson.build
if(WIN32 AND (CMAKE_C_COMPILER_ID MATCHES "GNU"))
message("MinGW detected, adding compiler and linker flags")
add_link_options(-lucrt -static)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mlong-double-64 -D__USE_MINGW_ANSI_STDIO=1")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__USE_MINGW_ANSI_STDIO=1")
endif()

if (WIN32)
set (PYTHON_SITE_PACKAGES Lib/site-packages)
else ()
set (PYTHON_SITE_PACKAGES ${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages)
endif ()

set(PYBIND11_NEWPYTHON ON)
add_subdirectory(pybind11)
pybind11_add_module(_prima _prima.cpp)
target_include_directories(_prima PRIVATE ${CMAKE_SOURCE_DIR}/c/include)
target_link_libraries(_prima PRIVATE primac primaf)
target_compile_definitions(_prima PRIVATE VERSION_INFO=${PRIMA_VERSION})

install (TARGETS _prima DESTINATION ${PYTHON_SITE_PACKAGES}/prima/)
install (FILES __init__.py DESTINATION ${PYTHON_SITE_PACKAGES}/prima/)
install (FILES _nlconstraints.py DESTINATION ${PYTHON_SITE_PACKAGES}/prima/)
install (FILES _linearconstraints.py DESTINATION ${PYTHON_SITE_PACKAGES}/prima/)
install (FILES _bounds.py DESTINATION ${PYTHON_SITE_PACKAGES}/prima/)
# This section is cribbed from scipy/meson.build
if(WIN32 AND (CMAKE_C_COMPILER_ID MATCHES "GNU"))
message(STATUS "MinGW detected, adding compiler and linker flags")
target_link_options(_prima PUBLIC -lucrt -static)
target_compile_options(_prima PUBLIC $<$<COMPILE_LANGUAGE:C>:-mlong-double-64 -D__USE_MINGW_ANSI_STDIO=1>)
target_compile_options(_prima PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-D__USE_MINGW_ANSI_STDIO=1>)
endif()

install (TARGETS _prima DESTINATION prima/ COMPONENT Prima_Python_C_Extension)
# The following are not added to the component because scikit-build-core automatically
# detects files in python/<package_name> and adds them to the wheel. These commands
# are here in case one is building via cmake directory and not via scikit-build-core
file(GLOB SUPPORTING_PY_FILES "${CMAKE_CURRENT_SOURCE_DIR}/prima/*.py")
install (FILES ${SUPPORTING_PY_FILES} DESTINATION prima)

macro (prima_add_py_test name)
add_test (NAME example_${name}_python COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/examples/example_${name}.py)
set_tests_properties (example_${name}_python PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}/${PYTHON_SITE_PACKAGES}")
add_test (NAME example_${name}_python COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/examples/${name}_example.py)
# set_tests_properties (example_${name}_python PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}/${PYTHON_SITE_PACKAGES}")
if (WIN32)
file(TO_NATIVE_PATH "${PROJECT_BINARY_DIR}/bin" _BIN_PATH)
set_property(TEST example_${name}_python APPEND PROPERTY ENVIRONMENT "PATH=${_BIN_PATH}\\;$ENV{PATH}")
Expand All @@ -66,6 +63,6 @@ add_test (NAME python-tests
COMMAND ${Python_EXECUTABLE} -m pytest --capture=no
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests
)
set_tests_properties(python-tests
PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}/${PYTHON_SITE_PACKAGES}")
#set_tests_properties(python-tests
#PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_INSTALL_PREFIX}/${PYTHON_SITE_PACKAGES}")

2 changes: 1 addition & 1 deletion python/__init__.py → python/prima/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._prima import minimize as _minimize
from ._prima import minimize as _minimize, __version__
from ._nlconstraints import NonlinearConstraint, process_single_nl_constraint, process_multiple_nl_constraints
from ._linearconstraints import LinearConstraint, process_single_linear_constraint, process_multiple_linear_constraints
from ._bounds import process_bounds
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2e76f3d

Please sign in to comment.