Skip to content
Open
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
127 changes: 127 additions & 0 deletions .github/workflows/python-pip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
name: Build and Publish Python Pip Package

on:
push:
tags:
- 'MFrontGenericInterfaceSupport-[0-9]+.[0-9]+*'
branches:
- master
pull_request:
branches:
- master

jobs:
build-wheels:
name: Build wheel (${{ matrix.os }}, ${{ matrix.python }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
# all the python versions on master and for the tags, only one
# for the pull requests
python: ${{ github.event_name == 'pull_request' && fromJSON('["cp312"]') || fromJSON('["cp39", "cp310", "cp311", "cp312", "cp313", "cp314", "cp315"]') }}

steps:
- uses: actions/checkout@v4

# one cache per system, shared by the versions of python and saved by the
# job of the version also built for the pull requests. The ccache actions
# only support Linux and macOS, which are the systems of the builds of
# check.yml: the build of the windows wheel is not accelerated
- name: Set up ccache
id: ccache
if: matrix.os != 'windows-latest'
uses: ./.github/actions/setup-ccache
with:
key: wheel-${{ matrix.os }}

# the environment variables CMAKE_C_COMPILER_LAUNCHER and
# CMAKE_CXX_COMPILER_LAUNCHER are used by cmake, including when it is
# run by setup.py inside the build container of cibuildwheel
- name: Use ccache in the builds
if: matrix.os != 'windows-latest'
shell: bash
run: |
echo "CMAKE_C_COMPILER_LAUNCHER=ccache" >> "$GITHUB_ENV"
echo "CMAKE_CXX_COMPILER_LAUNCHER=ccache" >> "$GITHUB_ENV"

# on Linux, the wheels are built in a container which mounts the file
# system of the runner on /host, where ccache is not installed: the
# static musl build of ccache is copied into the container
#
# see bindings/python/pip/pyproject.toml for the configuration
# of cibuildwheel and bindings/python/pip/setup.py for the build
- name: Build and test the wheel
uses: pypa/cibuildwheel@v4.3.2
with:
package-dir: bindings/python/pip
output-dir: wheelhouse
env:
CIBW_BUILD: ${{ matrix.python }}-*
CIBW_BEFORE_ALL_LINUX: >-
curl --proto '=https' -sSfL https://github.com/ccache/ccache/releases/download/v4.14.2/ccache-4.14.2-linux-x86_64-musl-static.tar.gz
| tar xz --strip-components=1 -C /usr/local/bin ccache-4.14.2-linux-x86_64-musl-static/ccache
# files writable by the runner, which removes the unused ones (save-ccache)
CIBW_ENVIRONMENT_LINUX: >-
CCACHE_DIR=/host${{ runner.temp }}/ccache CCACHE_MAXSIZE=1G CCACHE_UMASK=000
CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache

- name: Save ccache
if: ${{ matrix.os != 'windows-latest' && !cancelled() && steps.ccache.outcome == 'success' && matrix.python == 'cp312' }}
uses: ./.github/actions/save-ccache

- name: Upload Pip Wheel as Artifact
uses: actions/upload-artifact@v4
with:
name: mgis-wheel-${{ matrix.os }}-${{ matrix.python }}
path: wheelhouse/*.whl

publish-pypi:
name: Publish to PyPI
needs: build-wheels
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/MFrontGenericInterfaceSupport-')

steps:
- name: Download All Artifacts
uses: actions/download-artifact@v4
with:
path: ${{github.workspace}}/artifacts
pattern: mgis-wheel-*
merge-multiple: true

- name: List Downloaded Artifacts
run: |
ls -R ${{github.workspace}}/artifacts

- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: ${{github.workspace}}/artifacts/
password: ${{ secrets.PYPI_API_TOKEN }}
verbose: true

publish-github-release:
name: Publish to GitHub Release
needs: build-wheels
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/MFrontGenericInterfaceSupport-')
permissions:
contents: write

steps:
- name: Download All Artifacts
uses: actions/download-artifact@v4
with:
path: ${{github.workspace}}/release-artifacts
pattern: mgis-wheel-*
merge-multiple: true

- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
files: ${{github.workspace}}/release-artifacts/**/*.whl
generate_release_notes: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
86 changes: 69 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,41 @@ if(enable-python-bindings)
set(CPACK_COMPONENT_PYTHON_BINDINGS_DESCRIPTION
"Contains the python bindings for MGIS")
set(CPACK_COMPONENT_PYTHON_BINDINGS_DEPENDS mgis)
option(enable-pybind11-stubgen "generates stubs for python extensions" ON)
if(enable-pybind11-stubgen)
# The modules are imported to generate their stubs: pybind11-stubgen
# shall be available for the python interpreter they are built for.
execute_process(COMMAND "${Python_EXECUTABLE}" -c "import pybind11_stubgen"
RESULT_VARIABLE pybind11_stubgen_import_status
OUTPUT_QUIET ERROR_QUIET)
if(pybind11_stubgen_import_status EQUAL 0)
set(pybind11_stubgen ON)
message(STATUS "Found pybind11-stubgen for ${Python_EXECUTABLE}")
endif()
endif()
include(cmake/modules/mgis-python-modules.cmake)
# store the location of the python modules in the MGIS_PYTHON_MODULES_INSTALL_DIRECTORY variable
if(MGIS_APPEND_SUFFIX)
if(WIN32)
set(MGIS_PYTHON_MODULES_INSTALL_DIRECTORY
bin/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/mgis_${MGIS_SUFFIX_FOR_PYTHON_MODULES})
else(WIN32)
set(MGIS_PYTHON_MODULES_INSTALL_DIRECTORY
"lib${LIB_SUFFIX}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/mgis_${MGIS_SUFFIX_FOR_PYTHON_MODULES}")
endif(WIN32)
else(MGIS_APPEND_SUFFIX)
if(WIN32)
set(MGIS_PYTHON_MODULES_INSTALL_DIRECTORY
"bin/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/mgis")
else(WIN32)
set(MGIS_PYTHON_MODULES_INSTALL_DIRECTORY
"lib${LIB_SUFFIX}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/mgis")
endif(WIN32)
endif(MGIS_APPEND_SUFFIX)
# the variable can be set by the user, for instance to "site-packages" by
# the pip package, see bindings/python/pip
if(NOT MGIS_PYTHON_MODULES_INSTALL_DIRECTORY)
if(MGIS_APPEND_SUFFIX)
if(WIN32)
set(MGIS_PYTHON_MODULES_INSTALL_DIRECTORY
bin/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/mgis_${MGIS_SUFFIX_FOR_PYTHON_MODULES})
else(WIN32)
set(MGIS_PYTHON_MODULES_INSTALL_DIRECTORY
"lib${LIB_SUFFIX}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/mgis_${MGIS_SUFFIX_FOR_PYTHON_MODULES}")
endif(WIN32)
else(MGIS_APPEND_SUFFIX)
if(WIN32)
set(MGIS_PYTHON_MODULES_INSTALL_DIRECTORY
"bin/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/mgis")
else(WIN32)
set(MGIS_PYTHON_MODULES_INSTALL_DIRECTORY
"lib${LIB_SUFFIX}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages/mgis")
endif(WIN32)
endif(MGIS_APPEND_SUFFIX)
endif(NOT MGIS_PYTHON_MODULES_INSTALL_DIRECTORY)
endif(enable-python-bindings)

# fenics-bindings (placed before compiler detection)
Expand Down Expand Up @@ -223,6 +240,9 @@ endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")

#detecting TFEL and MFront
option(disable-tfel "explicitly disable TFEL support. By default, MGIS tries to use TFEL for tests and MGIS/Function" OFF)
option(require-tfel "the configuration fails if TFEL is not found. It is used by the pip package, which requires TFEL with all the features MGIS can use: see bindings/python/pip" OFF)
set(MGIS_REQUIRED_TFEL_VERSION "" CACHE STRING
"if non-empty, the configuration fails if the version of the TFEL installation is not this one")
if(NOT disable-tfel)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
find_package(TFEL)
Expand All @@ -232,6 +252,31 @@ if(NOT disable-tfel)
include(behaviours)
endif(TFEL_FOUND)
endif(NOT disable-tfel)
if(require-tfel)
if(disable-tfel)
message(FATAL_ERROR
"require-tfel and disable-tfel are mutually exclusive")
endif(disable-tfel)
if(NOT TFEL_FOUND)
message(FATAL_ERROR
"TFEL support is required (require-tfel is ON) but TFEL was not found. "
"Set TFELHOME or TFEL_DIR to help cmake find it")
endif(NOT TFEL_FOUND)
if(NOT MGIS_HAVE_MFRONT_SUPPORT)
message(FATAL_ERROR
"TFEL support is required (require-tfel is ON) but the TFEL installation provides no MFront support "
"(the TFEL libraries TFELMFrontDatabase or MTestFileGenerator were not found)")
endif(NOT MGIS_HAVE_MFRONT_SUPPORT)
if(enable-python-bindings AND NOT TFEL_PYTHON_BINDINGS)
message(FATAL_ERROR
"the python bindings of MGIS require the python bindings of TFEL "
"(the mfront.database module), which are not available in this TFEL installation")
endif(enable-python-bindings AND NOT TFEL_PYTHON_BINDINGS)
if(MGIS_REQUIRED_TFEL_VERSION AND NOT (TFEL_VERSION STREQUAL MGIS_REQUIRED_TFEL_VERSION))
message(FATAL_ERROR
"MGIS_REQUIRED_TFEL_VERSION is '${MGIS_REQUIRED_TFEL_VERSION}' but TFEL version '${TFEL_VERSION}' was found")
endif(MGIS_REQUIRED_TFEL_VERSION AND NOT (TFEL_VERSION STREQUAL MGIS_REQUIRED_TFEL_VERSION))
endif(require-tfel)

#documentations
option(enable-doxygen-doc "enable generation of the Doxygen documentation" OFF)
Expand Down Expand Up @@ -318,6 +363,13 @@ add_subdirectory(bindings)
# shall be included after the other directories
add_subdirectory(env)

# Generation of the stubs of the python modules using pybind11-stubgen.
# This call is placed at the end of the file as it requires that all the
# python modules and all the MGIS libraries have been declared.
if(enable-python-bindings)
generate_python_stubs()
endif(enable-python-bindings)

# Packaging
if(MFrontGenericInterfaceSupportDevelopmentVersion)
set(CPACK_PACKAGE_VERSION_PATCH "${CMAKE_PROJECT_VERSION_PATCH}-dev")
Expand Down
20 changes: 13 additions & 7 deletions bindings/python/mgis/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
add_subdirectory(fenics)

if(MGIS_HAVE_TFEL AND TFEL_PYTHON_BINDINGS)
install(FILES __init__-have-tfel.py
DESTINATION ${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY}
COMPONENT python_bindings
RENAME __init__.py)
set(mgis_python_init_script __init__-have-tfel.py)
else()
install(FILES __init__.py
DESTINATION ${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY}
COMPONENT python_bindings)
set(mgis_python_init_script __init__.py)
endif()

install(FILES ${mgis_python_init_script}
DESTINATION ${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY}
COMPONENT python_bindings
RENAME __init__.py)
# In the build tree, the package directory where the modules are placed
# needs the __init__.py script so that the package can be imported, which
# pybind11-stubgen requires to generate the stubs.
configure_file(${mgis_python_init_script}
${MGIS_PYTHON_MODULES_BUILD_DIR}/${MGIS_PYTHON_PACKAGE_NAME}/__init__.py
COPYONLY)
Empty file added bindings/python/mgis/py.typed
Empty file.
87 changes: 87 additions & 0 deletions bindings/python/pip/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Build of the mgis pip package.
#
# setup.py configures, builds and installs MGIS with cmake, then lays out
# the installation as a python package. The wheels are built for each
# python version by cibuildwheel, see .github/workflows/python-pip.yml
#
# The TFEL dependency (tfel-mfront) is pinned below, both for the build
# and for the run. setup.py checks that the two pins and the constant
# TFEL_DEPENDENCY of setup.py agree: bump the three values together.

[build-system]
requires = [
"setuptools>=64",
"cmake>=3.20",
"pybind11>=2.12",
"pybind11-stubgen>=3.0",
"numpy>=2.0",
"tfel-mfront==5.2.0.dev0",
"patchelf; sys_platform == 'linux'",
]
build-backend = "setuptools.build_meta"

[project]
name = "mgis"
dynamic = ["version"]
description = "MFrontGenericInterfaceSupport with Python bindings"
readme = { text = "MGIS provides a generic interface to integrate MFront generated material libraries in solvers. This package provides the MGIS library, headers and Python bindings. It depends on the tfel-mfront package, pinned to the version it was built against. A C++ compiler is required to build MFront implementations.", content-type = "text/plain" }
authors = [{ name = "Thomas Helfer", email = "thomas.helfer@cea.fr" }]
requires-python = ">=3.9"
dependencies = ["numpy", "tfel-mfront==5.2.0.dev0"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)",
"License :: OSI Approved :: CEA CNRS Inria Logiciel Libre License - CECILL-C",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Programming Language :: Python :: 3.15",
"Topic :: Scientific/Engineering",
]

[project.urls]
Homepage = "https://thelfer.github.io/mgis/web/index.html"
Source = "https://github.com/thelfer/MFrontGenericInterfaceSupport"

# the content of the package is produced by setup.py
[tool.setuptools]
packages = []
py-modules = []

[tool.cibuildwheel]
build-verbosity = 1
skip = "*-musllinux_*"
test-command = "python {package}/test_wheel.py"

[tool.cibuildwheel.linux]
archs = ["x86_64"]
# auditwheel shall not vendor the TFEL libraries: they are provided by the
# tfel-mfront wheel, installed next to this one. Vendoring them under
# mangled names would make a behaviour loaded in python load a second copy
# of TFEL. The rpath of the libraries of this wheel points to the sibling
# tfel/lib directory, see set_relative_runtime_paths in setup.py.
# The patterns are globs (auditwheel >= 6), which also behave correctly as
# regular expressions for older versions. They do not match
# libMFrontGenericInterface*, the library of MGIS itself.
repair-wheel-command = "auditwheel repair -w {dest_dir} {wheel} --exclude libTFEL* --exclude libMTest* --exclude libMFM* --exclude libMFrontQuery* --exclude libMFrontLogStream* --exclude libMFrontProfiling* --exclude libMFrontGenericParallelRuntime*"

[tool.cibuildwheel.macos]
archs = ["arm64"]
environment = { MACOSX_DEPLOYMENT_TARGET = "11.0" }
# same reason as on linux: delocate shall not move the TFEL libraries,
# which are provided by the tfel-mfront wheel, into this wheel
repair-wheel-command = "delocate-wheel --require-archs {delocate_archs} -w {dest_dir} -v -x libTFEL.* -x libMTest.* -x libMFM.* -x libMFrontQuery.* -x libMFrontLogStream.* -x libMFrontProfiling.* -x libMFrontGenericParallelRuntime.* {wheel}"

[tool.cibuildwheel.windows]
archs = ["AMD64"]
# The DLLs of MGIS are in mgis/bin, and those of TFEL in tfel/bin, where
# the python modules find them (os.add_dll_directory). delvewheel would
# copy these DLLs under other names, so a behaviour loaded in python would
# load a second copy of them.
repair-wheel-command = ""
Loading
Loading