diff --git a/.github/workflows/python-pip.yml b/.github/workflows/python-pip.yml new file mode 100644 index 000000000..eea0745da --- /dev/null +++ b/.github/workflows/python-pip.yml @@ -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 }} diff --git a/CMakeLists.txt b/CMakeLists.txt index fbb4fa1fa..0cf191b16 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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) @@ -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) @@ -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") diff --git a/bindings/python/mgis/CMakeLists.txt b/bindings/python/mgis/CMakeLists.txt index 5bfcf1e1c..1a83cc56e 100644 --- a/bindings/python/mgis/CMakeLists.txt +++ b/bindings/python/mgis/CMakeLists.txt @@ -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) diff --git a/bindings/python/mgis/py.typed b/bindings/python/mgis/py.typed new file mode 100644 index 000000000..e69de29bb diff --git a/bindings/python/pip/pyproject.toml b/bindings/python/pip/pyproject.toml new file mode 100644 index 000000000..24677e2e0 --- /dev/null +++ b/bindings/python/pip/pyproject.toml @@ -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 = "" diff --git a/bindings/python/pip/setup.py b/bindings/python/pip/setup.py new file mode 100644 index 000000000..630e37f19 --- /dev/null +++ b/bindings/python/pip/setup.py @@ -0,0 +1,333 @@ +"""Build of the mgis pip package. + +MGIS is configured, built and installed with cmake, then the installation +is laid out as follows: + +- the python modules (the mgis package) are placed at the root of the + package, +- the libraries, headers and data are placed in the bin, lib, include and + share directories of the mgis module, which is thus the MGISHOME + directory, as a regular installation of MGIS. + +MGIS is built against the tfel-mfront wheel declared in pyproject.toml, +which pip installs in the isolated build environment. This script locates +that installation, mandates it to cmake (require-tfel and +MGIS_REQUIRED_TFEL_VERSION), makes the binaries of the wheel look for the +TFEL libraries in the sibling tfel wheel at run time, and records the +version of tfel-mfront in the package, where importing it checks it. + +The TFEL libraries are never copied into this wheel: a behaviour loaded in +python would then load a second copy of TFEL (cf. pyproject.toml). +""" + +import importlib.metadata +import importlib.util +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +from setuptools import setup +from setuptools.command.build_ext import build_ext +from setuptools.dist import Distribution + +pip_directory = Path(__file__).resolve().parent +mgis_sources = pip_directory.parents[2] + +# The tfel-mfront wheel this package is built against and depends on. +# pyproject.toml cannot compute the pin, so it is written twice there, and +# check_tfel_dependency verifies that the two pins and this constant agree. +# Bump the three values together. +TFEL_DEPENDENCY = "tfel-mfront==5.2.0.dev0" + +# python code prepended to the __init__.py script of the mgis package: +# MGISHOME, the DLL directories on windows, and the check of the version of +# tfel-mfront. The version of TFEL of the installation the wheel was built +# against is recorded at build time (@TFEL_MFRONT_VERSION@). +pip_setup = '''\ +# added by the mgis pip package +import os as _os + +_os.environ["MGISHOME"] = _os.path.dirname(_os.path.abspath(__file__)) +if _os.name == "nt": + # since Python 3.8, the directories listed in PATH are not searched for + # the DLLs needed by the extension modules: the bin directory of this + # package, where MFrontGenericInterface.dll is installed, and the bin + # directory of the tfel-mfront wheel, where the TFEL DLLs are, are + # registered. importing mgis does not necessarily run tfel/__init__.py. + import importlib.util as _importlib_util + + for _name in ("mgis", "tfel"): + _spec = _importlib_util.find_spec(_name) + for _directory in (_spec.submodule_search_locations if _spec else ()): + _directory = _os.path.join(_directory, "bin") + if _os.path.isdir(_directory): + _os.add_dll_directory(_directory) + +_TFEL_MFRONT_VERSION = "@TFEL_MFRONT_VERSION@" + +# The libraries of the tfel-mfront wheel are not versioned, so the dynamic +# loader would silently use another version of TFEL than the one this wheel +# was built against, whose ABI may differ. The version of the installed +# tfel-mfront package is thus checked, which also covers the cases where the +# libraries are not the ones of a wheel (TFELHOME set externally), and which +# is what cmake verified at configuration time (require-tfel and +# MGIS_REQUIRED_TFEL_VERSION). +def _check_tfel_mfront(): + from importlib import metadata + + try: + _version = metadata.version("tfel-mfront") + except metadata.PackageNotFoundError: + _version = None + if _version != _TFEL_MFRONT_VERSION: + raise RuntimeError( + "this mgis wheel was built against tfel-mfront " + + _TFEL_MFRONT_VERSION + ", but version " + str(_version) + + " is installed") + + +_check_tfel_mfront() +''' + + +def get_mgis_version(): + """return the MGIS version, as defined in the main CMakeLists.txt""" + cmakelists = (mgis_sources / "CMakeLists.txt").read_text() + version = re.search( + r'project\("mfront-generic-interface"\s+VERSION\s+(\d+)\.(\d+)\.(\d+)', + cmakelists) + if version is None: + raise RuntimeError("MGIS version not found in CMakeLists.txt") + version = ".".join(version.groups()) + if re.search( + r"set\(MFrontGenericInterfaceSupportDevelopmentVersion\s+ON\)", + cmakelists): + return version + "-dev" + return version + + +def check_tfel_dependency(): + """check that the pins of pyproject.toml agree with TFEL_DEPENDENCY""" + pins = set(re.findall(r'"(tfel-mfront==[^"]+)"', + (pip_directory / "pyproject.toml").read_text())) + if pins != {TFEL_DEPENDENCY}: + raise RuntimeError( + "inconsistent TFEL dependency: pyproject.toml pins " + + str(sorted(pins)) + " while setup.py uses '" + + TFEL_DEPENDENCY + "'") + + +def get_tfelhome(): + """path of the tfel module installed in the build environment. The + package is not imported: find_spec does not load any library""" + spec = importlib.util.find_spec("tfel") + if spec is None or not spec.submodule_search_locations: + raise RuntimeError( + "the tfel-mfront package, declared in [build-system].requires " + "of pyproject.toml, is not installed in the build environment") + return Path(next(iter(spec.submodule_search_locations))) + + +def get_tfel_version(tfelhome): + """return the version of TFEL as reported by the tfel-config program of + the tfel-mfront installation, in the same way as cmake's FindTFEL""" + tfel_config = tfelhome / "bin" / ( + "tfel-config.exe" if os.name == "nt" else "tfel-config") + output = subprocess.run([str(tfel_config), "--version"], check=True, + stdout=subprocess.PIPE, text=True).stdout + return output.split()[1] + + +class BinaryDistribution(Distribution): + # compiled modules inside: platform-specific wheel, not py3-none-any + def has_ext_modules(self): + return True + + +def build_and_install_mgis(build_directory, install_directory, tfelhome, + tfel_version): + import pybind11 + + # copy of pybind11 at a fixed path: the isolated builds install it in a + # directory whose name changes at each build, which prevents ccache from + # reusing the compilations of the bindings + pybind11_directory = Path(build_directory).parent / "pybind11" + shutil.rmtree(pybind11_directory, ignore_errors=True) + shutil.copytree(Path(pybind11.__file__).parent, pybind11_directory) + + # every option is set explicitly: the content of the wheel shall not + # depend on what the build environment happens to provide (HDF5...) + cmake_options = [ + "-DCMAKE_BUILD_TYPE=Release", + "-DCMAKE_INSTALL_PREFIX=" + str(install_directory), + "-DMGIS_PYTHON_MODULES_INSTALL_DIRECTORY=site-packages/mgis", + "-DPython_EXECUTABLE=" + sys.executable, + "-Dpybind11_DIR=" + str(pybind11_directory / "share" / "cmake" / + "pybind11"), + "-Denable-python-bindings=ON", + "-Denable-pybind11-stubgen=ON", + "-Denable-portable-build=ON", + "-Denable-testing=OFF", + "-Denable-website=OFF", + "-Denable-doxygen-doc=OFF", + "-Denable-c-bindings=OFF", + "-Denable-fortran-bindings=OFF", + "-Denable-fenics-bindings=OFF", + "-Denable-julia-bindings=OFF", + "-Denable-cuda-algorithms=OFF", + "-Denable-openmp=OFF", + "-Denable-exceptions=OFF", + "-Denable-hdf5-support=OFF", + "-Denable-hdf5-automatic-support=OFF", + "-Ddisable-tfel=OFF", + # the wheel can never be built against another TFEL than the one it + # depends on: cmake checks TFEL_FOUND, MGIS_HAVE_MFRONT_SUPPORT, + # TFEL_PYTHON_BINDINGS and MGIS_REQUIRED_TFEL_VERSION + "-Drequire-tfel=ON", + "-DMGIS_REQUIRED_TFEL_VERSION=" + tfel_version, + "-DTFEL_DIR=" + str(tfelhome / "share" / "tfel" / "cmake"), + ] + if os.name == "nt": + cmake_options += ["-A", "x64"] + jobs = os.environ.get("CMAKE_BUILD_PARALLEL_LEVEL", str(os.cpu_count() or 1)) + environment = dict(os.environ, TFELHOME=str(tfelhome)) + subprocess.run(["cmake", "-S", str(mgis_sources), "-B", str(build_directory)] + + cmake_options, check=True, env=environment) + subprocess.run(["cmake", "--build", str(build_directory), "--config", + "Release", "--parallel", jobs], check=True, env=environment) + subprocess.run(["cmake", "--install", str(build_directory), "--config", + "Release"], check=True, env=environment) + + +def is_binary(path, magic_numbers): + with open(path, "rb") as f: + return f.read(4) in magic_numbers + + +def materialize_symlinks(package_directory): + """replace the symbolic links of the wheel by copies of the files they + point to: wheels do not store symbolic links (MGIS installs its library + unversioned, so there is none in practice)""" + for path in package_directory.rglob("*"): + if path.is_symlink(): + target = path.resolve() + path.unlink() + shutil.copy2(target, path) + + +def set_relative_runtime_paths(package_directory): + """make all the binaries of the wheel look for the libraries in + mgis/lib and in the lib directory of the tfel-mfront wheel. + + The tfel directory is not part of this wheel: it is the python package + of the tfel-mfront wheel, installed in site-packages next to this one. + The relative path of a binary to that directory is hence computed as if + the root of the wheel was a site-packages directory, which it is once + installed, whereas the build directory of the package is not. + + auditwheel and delocate keep the libraries already in the wheel, and + the TFEL ones are excluded from the repair, so that the runtime paths + below are used to find them: see pyproject.toml. + + On linux, DT_RPATH is used rather than DT_RUNPATH, since it is + searched before LD_LIBRARY_PATH: the binaries of the package use the + libraries of the package, even if LD_LIBRARY_PATH points to another + installation, whose libraries may have the same names.""" + fake_site_packages = package_directory.parent / "site-packages" + if sys.platform.startswith("linux"): + magic_numbers = (b"\x7fELF",) + elif sys.platform == "darwin": + magic_numbers = (b"\xcf\xfa\xed\xfe", b"\xca\xfe\xba\xbe") + else: + return + for path in sorted(package_directory.rglob("*")): + if path.is_symlink() or not path.is_file(): + continue + if not is_binary(path, magic_numbers): + continue + # where the directory of the binary will be once the wheel is + # installed, simulated in a site-packages directory + directory = fake_site_packages / path.parent.relative_to( + package_directory) + rpaths = [] + for library_directory in (fake_site_packages / "mgis" / "lib", + fake_site_packages / "tfel" / "lib"): + relative_path = os.path.relpath(library_directory, directory) + if sys.platform.startswith("linux"): + rpaths.append("$ORIGIN" if relative_path == "." + else "$ORIGIN/" + relative_path) + elif relative_path != ".": + rpaths.append("@loader_path/" + relative_path) + if sys.platform.startswith("linux"): + subprocess.run(["patchelf", "--force-rpath", "--set-rpath", + ":".join(dict.fromkeys(rpaths)), str(path)], + check=True) + else: + load_commands = subprocess.run(["otool", "-l", str(path)], + check=True, capture_output=True, + text=True).stdout.splitlines() + current_rpaths = [load_commands[i + 2].split()[1] + for i, line in enumerate(load_commands) + if line.strip() == "cmd LC_RPATH"] + options = [] + for rpath in current_rpaths: + options += ["-delete_rpath", rpath] + for rpath in rpaths: + options += ["-add_rpath", rpath] + subprocess.run(["install_name_tool"] + options + [str(path)], + check=True) + # install_name_tool invalidates the signature of the binary + subprocess.run(["codesign", "--force", "--sign", "-", str(path)], + check=True) + + +def layout_package(install_directory, package_directory, + tfel_mfront_version): + """lay out the MGIS installation as a python package""" + shutil.copytree(install_directory / "site-packages", package_directory, + dirs_exist_ok=True) + mgis_module = package_directory / "mgis" + for directory in ("bin", "lib", "include", "share"): + if (install_directory / directory).is_dir(): + shutil.copytree(install_directory / directory, + mgis_module / directory, dirs_exist_ok=True) + init_file = mgis_module / "__init__.py" + init_file.write_text( + pip_setup.replace("@TFEL_MFRONT_VERSION@", tfel_mfront_version) + + init_file.read_text()) + materialize_symlinks(package_directory) + set_relative_runtime_paths(package_directory) + + +class build_mgis(build_ext): + """build MGIS with cmake and fill the build directory of the package. + + build_ext is used since it is the only build command run for a + distribution without python packages declared to setuptools""" + + def run(self): + check_tfel_dependency() + tfelhome = get_tfelhome() + tfel_version = get_tfel_version(tfelhome) + tfel_mfront_version = importlib.metadata.version("tfel-mfront") + # with python 3.9 on Windows, resolve keeps relative the paths which + # do not exist yet + build_temp = Path(self.build_temp).absolute().resolve() + install_directory = build_temp / "install" + build_and_install_mgis(build_temp / "build", install_directory, + tfelhome, tfel_version) + layout_package(install_directory, Path(self.build_lib), + tfel_mfront_version) + + +mgis_version = get_mgis_version() + +setup( + # 3.1.0-dev is not a valid PEP 440 version, 3.1.0.dev0 is + version=mgis_version.replace("-dev", ".dev0"), + distclass=BinaryDistribution, + cmdclass={"build_ext": build_mgis}, +) diff --git a/bindings/python/pip/test_wheel.py b/bindings/python/pip/test_wheel.py new file mode 100644 index 000000000..aff2fadc8 --- /dev/null +++ b/bindings/python/pip/test_wheel.py @@ -0,0 +1,138 @@ +"""Test of an installed mgis wheel, run by cibuildwheel. + +On Linux and macOS, a behaviour is compiled with the mfront program of the +tfel-mfront wheel, integrated from python through mgis.behaviour and loaded +through a project using find_package(MFrontGenericInterface) from the +wheel, which checks that the executables, the headers, the cmake package +and the libraries of the two wheels are found and are consistent. On +Windows, building behaviours requires Visual Studio and is not tested. +""" + +import ast +import os +import re +import shutil +import subprocess +import sys +import tempfile +from importlib import metadata + +import mgis +import mgis.behaviour as mgis_bv +import mgis.function as mgis_fct +import mgis.material_property as mgis_mp +import mgis.model as mgis_model +import tfel # sets TFELHOME_, used by MFrontGenericInterfaceConfig + +# the import of mgis checked that the tfel-mfront wheel this wheel was +# built against is the one installed; check it again explicitly +assert mgis._TFEL_MFRONT_VERSION == metadata.version("tfel-mfront") + +mgishome = os.path.dirname(os.path.abspath(mgis.__file__)) + +# the TFEL libraries must not have been vendored into the mgis wheel: +# they are provided by the tfel-mfront wheel, and a copy under a mangled +# name would make a behaviour load two copies of TFEL +vendored = re.compile( + r"(lib)?(TFEL|MTest|MFM|MFront(?!GenericInterface))" + r".*\.(so|dylib|dll)(\.|$)") +for directory, _, files in os.walk(mgishome): + for name in files: + if vendored.search(name): + sys.exit("the mgis wheel must not contain the library " + name) + +# the stubs of the python modules are installed next to them, as well as +# the py.typed marker. Type checkers stop on a stub which is not valid python +for stub in ("_mgis.pyi", "behaviour.pyi", "material_property.pyi", + "model.pyi", "function.pyi", "py.typed"): + path = os.path.join(mgishome, stub) + if not os.path.isfile(path): + sys.exit("missing " + stub) + if stub.endswith(".pyi"): + with open(path, encoding="utf-8") as f: + ast.parse(f.read(), path) + +if os.name == "nt": + sys.exit(0) + +behaviour = """\ +@DSL Implicit; +@Behaviour Elasticity; +@Brick StandardElasticity; +@ElasticMaterialProperties {150e9, 0.3}; +""" + +library = "libBehaviour.dylib" if sys.platform == "darwin" else "libBehaviour.so" + +tfel_version = subprocess.run([shutil.which("tfel-config"), "--version"], + check=True, stdout=subprocess.PIPE, + text=True).stdout.split()[1] + +with tempfile.TemporaryDirectory() as directory: + os.chdir(directory) + with open("Elasticity.mfront", "w") as f: + f.write(behaviour) + subprocess.run([shutil.which("mfront"), "--obuild", "--interface=generic", + "Elasticity.mfront"], check=True) + + # integrate the behaviour from python: with the full strain tensor + # imposed ({EXX: 1e-3, the other components zero}), the stress is + # (lambda + 2 mu) EXX for E = 150 GPa and nu = 0.3 + b = mgis_bv.load(os.path.join("src", library), "Elasticity", + mgis_bv.Hypothesis.Tridimensional) + assert b.tfel_version == tfel_version, \ + "the behaviour was built with TFEL " + b.tfel_version + \ + ", not " + tfel_version + d = mgis_bv.BehaviourData(b) + d.dt = 1 + mgis_bv.setExternalStateVariable(d.s1, "Temperature", 293.15) + o = mgis_bv.getVariableOffset(b.gradients, "Strain", b.hypothesis) + mgis_bv.update(d) + d.s1.gradients[o] = 1e-3 + mgis_bv.integrate(d, b) + sxx = float(d.s1.thermodynamic_forces[o]) + e, nu = 150e9, 0.3 + expected = e * (1 - nu) / ((1 + nu) * (1 - 2 * nu)) * 1e-3 + print("SXX:", sxx, "expected:", expected) + if abs(sxx - expected) > 1e-3 * abs(expected): + sys.exit("unexpected stress value") + + # the cmake package exported by the wheel must be usable: MFrontGeneric + # InterfaceConfig.cmake finds TFEL through the TFELHOME_ + # variable set by the import of tfel above + with open("check.cxx", "w") as f: + f.write("""\ +#include +#include + +int main(int argc, char *argv[]) { + if (argc < 3) { + return 1; + } + const auto b = mgis::behaviour::load(argv[1], argv[2], + mgis::behaviour::Hypothesis::\ +TRIDIMENSIONAL); + std::cout << b.behaviour << std::endl; + return 0; +} +""") + with open("CMakeLists.txt", "w") as f: + f.write("""\ +cmake_minimum_required(VERSION 3.20) +project(mgis_wheel_check CXX) +find_package(MFrontGenericInterface REQUIRED) +add_executable(check check.cxx) +target_link_libraries(check PRIVATE mgis::MFrontGenericInterface) +""") + subprocess.run([shutil.which("cmake"), "-S", ".", "-B", "build", + "-DCMAKE_BUILD_TYPE=Release", + "-DCMAKE_PREFIX_PATH=" + mgishome], check=True) + subprocess.run([shutil.which("cmake"), "--build", "build"], check=True) + check = os.path.join("build", "check" + (".exe" if os.name == "nt" else "")) + out = subprocess.run([check, os.path.join(directory, "src", library), + "Elasticity"], check=True, stdout=subprocess.PIPE, + text=True).stdout.strip() + if out != "Elasticity": + sys.exit("unexpected output of the cmake check: " + out) + +print("the mgis wheel is consistent") diff --git a/bindings/python/src/Behaviour.cxx b/bindings/python/src/Behaviour.cxx index 76cc9a221..c8c316fa7 100644 --- a/bindings/python/src/Behaviour.cxx +++ b/bindings/python/src/Behaviour.cxx @@ -245,40 +245,63 @@ void declareBehaviour(pybind11::module_ &m) { pybind11::class_(m, "Behaviour") // pybind11::class_("Behaviour") .def("getInitializeFunctionsNames", Behaviour_getInitializeFunctionsNames) - .def("getInitializeFunctionInputs", Behaviour_getInitializeFunctionInputs) + .def("getInitializeFunctionInputs", Behaviour_getInitializeFunctionInputs, + "name"_a) .def("getPostProcessingsNames", Behaviour_getPostProcessingsNames) - .def("getPostProcessingOutputs", Behaviour_getPostProcessingOutputs) - .def("setParameter", setParameter1) - .def("setIntegerParameter", setParameter2) - .def("setUnsignedShortParameter", setParameter3) - .def("rotateGradients", rotate_gradients_in_place_member) - .def("rotateGradients", rotate_gradients_out_of_place_member) + .def("getPostProcessingOutputs", Behaviour_getPostProcessingOutputs, + "name"_a) + .def("setParameter", setParameter1, "name"_a, "value"_a) + .def("setIntegerParameter", setParameter2, "name"_a, "value"_a) + .def("setUnsignedShortParameter", setParameter3, "name"_a, "value"_a) + .def("rotateGradients", rotate_gradients_in_place_member, "gradients"_a, + "rotation_matrix"_a) + .def("rotateGradients", rotate_gradients_out_of_place_member, + "material_frame_gradients"_a, "global_frame_gradients"_a, + "rotation_matrix"_a) .def("rotateThermodynamicForces", - rotate_thermodynamic_forces_in_place_member) + rotate_thermodynamic_forces_in_place_member, + "thermodynamic_forces"_a, "rotation_matrix"_a) .def("rotateThermodynamicForces", - rotate_thermodynamic_forces_out_of_place_member) + rotate_thermodynamic_forces_out_of_place_member, + "global_frame_thermodynamic_forces"_a, + "material_frame_thermodynamic_forces"_a, "rotation_matrix"_a) .def("rotateTangentOperatorBlocks", - rotate_tangent_operator_blocks_in_place_member) + rotate_tangent_operator_blocks_in_place_member, + "tangent_operator_blocks"_a, "rotation_matrix"_a) .def("rotateTangentOperatorBlocks", - rotate_tangent_operator_blocks_out_of_place_member); + rotate_tangent_operator_blocks_out_of_place_member, + "global_frame_tangent_operator_blocks"_a, + "material_frame_tangent_operator_blocks"_a, "rotation_matrix"_a); // wrapping free functions - m.def("rotateGradients", rotate_gradients_in_place); - m.def("rotateGradients", rotate_gradients_out_of_place); - m.def("rotateThermodynamicForces", rotate_thermodynamic_forces_in_place); - m.def("rotateThermodynamicForces", rotate_thermodynamic_forces_out_of_place); - m.def("rotateTangentOperatorBlocks", rotate_tangent_operator_blocks_in_place); + m.def("rotateGradients", rotate_gradients_in_place, "gradients"_a, + "behaviour"_a, "rotation_matrix"_a); + m.def("rotateGradients", rotate_gradients_out_of_place, + "material_frame_gradients"_a, "behaviour"_a, "global_frame_gradients"_a, + "rotation_matrix"_a); + m.def("rotateThermodynamicForces", rotate_thermodynamic_forces_in_place, + "thermodynamic_forces"_a, "behaviour"_a, "rotation_matrix"_a); + m.def("rotateThermodynamicForces", rotate_thermodynamic_forces_out_of_place, + "global_frame_thermodynamic_forces"_a, "behaviour"_a, + "material_frame_thermodynamic_forces"_a, "rotation_matrix"_a); + m.def("rotateTangentOperatorBlocks", rotate_tangent_operator_blocks_in_place, + "tangent_operator_blocks"_a, "behaviour"_a, "rotation_matrix"_a); m.def("rotateTangentOperatorBlocks", - rotate_tangent_operator_blocks_out_of_place); + rotate_tangent_operator_blocks_out_of_place, + "global_frame_tangent_operator_blocks"_a, "behaviour"_a, + "material_frame_tangent_operator_blocks"_a, "rotation_matrix"_a); - m.def("load", load_ptr); - m.def("load", load_ptr2); + m.def("load", load_ptr, "library"_a, "behaviour"_a, "hypothesis"_a); + m.def("load", load_ptr2, "options"_a, "library"_a, "behaviour"_a, + "hypothesis"_a); m.def("loadFromDatabase", Behaviour_loadFromDatabase, "name"_a, "hypothesis"_a, "material"_a = std::optional{}); m.def("loadFromDatabase", Behaviour_loadFromDatabase2, "options"_a, "name"_a, "hypothesis"_a, "material"_a = std::optional{}); - m.def("setParameter", setParameter1); - m.def("setIntegerParameter", setParameter2); - m.def("setUnsignedShortParameter", setParameter3); + m.def("setParameter", setParameter1, "behaviour"_a, "name"_a, "value"_a); + m.def("setIntegerParameter", setParameter2, "behaviour"_a, "name"_a, + "value"_a); + m.def("setUnsignedShortParameter", setParameter3, "behaviour"_a, "name"_a, + "value"_a); } // end of declareBehaviour diff --git a/bindings/python/src/BehaviourData.cxx b/bindings/python/src/BehaviourData.cxx index 327af0056..03bf9735e 100644 --- a/bindings/python/src/BehaviourData.cxx +++ b/bindings/python/src/BehaviourData.cxx @@ -39,6 +39,7 @@ static std::string BehaviourData_getErrorMessage( } // end of BehaviourData_getErrorMessage void declareBehaviourData(pybind11::module_& m) { + using namespace pybind11::literals; using mgis::behaviour::Behaviour; using mgis::behaviour::BehaviourData; using mgis::behaviour::BehaviourDataView; @@ -63,7 +64,7 @@ void declareBehaviourData(pybind11::module_& m) { // free functions BehaviourDataView (*ptr_make_view)(BehaviourData&) = &mgis::behaviour::make_view; - m.def("update", ptr_update); - m.def("revert", ptr_revert); - m.def("make_view", ptr_make_view, pybind11::keep_alive<0, 1>()); + m.def("update", ptr_update, "data"_a); + m.def("revert", ptr_revert, "data"_a); + m.def("make_view", ptr_make_view, "data"_a, pybind11::keep_alive<0, 1>()); } // end of declareBehaviourData diff --git a/bindings/python/src/BehaviourDescription.cxx b/bindings/python/src/BehaviourDescription.cxx index 214a33980..bf3ad3778 100644 --- a/bindings/python/src/BehaviourDescription.cxx +++ b/bindings/python/src/BehaviourDescription.cxx @@ -109,6 +109,7 @@ BehaviourDescription_getTangentOperatorBlocks( } // end of BehaviourDescription_getTangentOperatorBlocks void declareBehaviourDescription(pybind11::module_ &m) { + using namespace pybind11::literals; using mgis::behaviour::BehaviourDescription; // double (*getParameterDefaultValue1)(const BehaviourDescription &, @@ -275,38 +276,48 @@ void declareBehaviourDescription(pybind11::module_ &m) { "list of unsigned short parameters (same as the `usparams` property)") .def_property_readonly("tangent_operator_blocks", BehaviourDescription_getTangentOperatorBlocks) - .def("getParameterDefaultValue", getParameterDefaultValue1) - .def("getIntegerParameterDefaultValue", getParameterDefaultValue2) - .def("getUnsignedShortParameterDefaultValue", getParameterDefaultValue3) - .def("hasBounds", hasBounds) - .def("hasLowerBound", hasLowerBound) - .def("hasUpperBound", hasUpperBound) - .def("getLowerBound", getLowerBound) - .def("getUpperBound", getUpperBound) - .def("hasPhysicalBounds", hasPhysicalBounds) - .def("hasLowerPhysicalBound", hasLowerPhysicalBound) - .def("hasUpperPhysicalBound", hasUpperPhysicalBound) - .def("getLowerPhysicalBound", getLowerPhysicalBound) - .def("getUpperPhysicalBound", getUpperPhysicalBound); + .def("getParameterDefaultValue", getParameterDefaultValue1, "name"_a) + .def("getIntegerParameterDefaultValue", getParameterDefaultValue2, + "name"_a) + .def("getUnsignedShortParameterDefaultValue", getParameterDefaultValue3, + "name"_a) + .def("hasBounds", hasBounds, "name"_a) + .def("hasLowerBound", hasLowerBound, "name"_a) + .def("hasUpperBound", hasUpperBound, "name"_a) + .def("getLowerBound", getLowerBound, "name"_a) + .def("getUpperBound", getUpperBound, "name"_a) + .def("hasPhysicalBounds", hasPhysicalBounds, "name"_a) + .def("hasLowerPhysicalBound", hasLowerPhysicalBound, "name"_a) + .def("hasUpperPhysicalBound", hasUpperPhysicalBound, "name"_a) + .def("getLowerPhysicalBound", getLowerPhysicalBound, "name"_a) + .def("getUpperPhysicalBound", getUpperPhysicalBound, "name"_a); m.def("isStandardFiniteStrainBehaviour", - mgis::behaviour::isStandardFiniteStrainBehaviour, + mgis::behaviour::isStandardFiniteStrainBehaviour, "library"_a, + "behaviour"_a, "return if the given behaviour is a standard finite strain behaviour, " "i.e. is a finite strain behaviour using the standard finite strain " "kinematic (called F-Cauchy although the stress measure can be chosen " "when loading the behaviour)"); - m.def("getParameterDefaultValue", getParameterDefaultValue1); - m.def("getIntegerParameterDefaultValue", getParameterDefaultValue2); - m.def("getUnsignedShortParameterDefaultValue", getParameterDefaultValue3); - m.def("hasBounds", hasBounds); - m.def("hasLowerBound", hasLowerBound); - m.def("hasUpperBound", hasUpperBound); - m.def("getLowerBound", getLowerBound); - m.def("getUpperBound", getUpperBound); - m.def("hasPhysicalBounds", hasPhysicalBounds); - m.def("hasLowerPhysicalBound", hasLowerPhysicalBound); - m.def("hasUpperPhysicalBound", hasUpperPhysicalBound); - m.def("getLowerPhysicalBound", getLowerPhysicalBound); - m.def("getUpperPhysicalBound", getUpperPhysicalBound); + m.def("getParameterDefaultValue", getParameterDefaultValue1, "behaviour"_a, + "name"_a); + m.def("getIntegerParameterDefaultValue", getParameterDefaultValue2, + "behaviour"_a, "name"_a); + m.def("getUnsignedShortParameterDefaultValue", getParameterDefaultValue3, + "behaviour"_a, "name"_a); + m.def("hasBounds", hasBounds, "behaviour"_a, "name"_a); + m.def("hasLowerBound", hasLowerBound, "behaviour"_a, "name"_a); + m.def("hasUpperBound", hasUpperBound, "behaviour"_a, "name"_a); + m.def("getLowerBound", getLowerBound, "behaviour"_a, "name"_a); + m.def("getUpperBound", getUpperBound, "behaviour"_a, "name"_a); + m.def("hasPhysicalBounds", hasPhysicalBounds, "behaviour"_a, "name"_a); + m.def("hasLowerPhysicalBound", hasLowerPhysicalBound, "behaviour"_a, + "name"_a); + m.def("hasUpperPhysicalBound", hasUpperPhysicalBound, "behaviour"_a, + "name"_a); + m.def("getLowerPhysicalBound", getLowerPhysicalBound, "behaviour"_a, + "name"_a); + m.def("getUpperPhysicalBound", getUpperPhysicalBound, "behaviour"_a, + "name"_a); } // end of declareBehaviour diff --git a/bindings/python/src/CMakeLists.txt b/bindings/python/src/CMakeLists.txt index 51ff2fef4..2d8287d39 100644 --- a/bindings/python/src/CMakeLists.txt +++ b/bindings/python/src/CMakeLists.txt @@ -1,34 +1,5 @@ -function(mgis_python_module fullname name) - if(${ARGC} LESS 1) - message(FATAL_ERROR "mgis_python_module: no source specified") - endif(${ARGC} LESS 1) - add_library(py_${fullname} MODULE ${ARGN}) - target_include_directories(py_${fullname} PRIVATE - $) - if(TARGET pybind11::lto) - target_link_libraries(py_${fullname} PRIVATE - MFrontGenericInterface - pybind11::module pybind11::lto pybind11::windows_extras) - else(TARGET pybind11::lto) - target_link_libraries(py_${fullname} PRIVATE - MFrontGenericInterface - pybind11::module pybind11::windows_extras) - endif(TARGET pybind11::lto) - pybind11_extension(py_${fullname}) - if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) - # Strip unnecessary sections of the binary on Linux/macOS - pybind11_strip(py_${fullname}) - endif() - set_target_properties(py_${fullname} PROPERTIES PREFIX "") - set_target_properties(py_${fullname} PROPERTIES OUTPUT_NAME ${name}) - set_target_properties(py_${fullname} PROPERTIES - CXX_VISIBILITY_PRESET "hidden" - CUDA_VISIBILITY_PRESET "hidden") - # installation - install(TARGETS py_${fullname} - DESTINATION ${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY} - COMPONENT python_bindings) -endfunction(mgis_python_module) +# the mgis_python_module function is defined in +# cmake/modules/mgis-python-modules.cmake set(MGIS_MODULE_SOURCES) list(APPEND MGIS_MODULE_SOURCES diff --git a/bindings/python/src/FiniteStrainSupport.cxx b/bindings/python/src/FiniteStrainSupport.cxx index 84c2762d4..a5f5b6308 100644 --- a/bindings/python/src/FiniteStrainSupport.cxx +++ b/bindings/python/src/FiniteStrainSupport.cxx @@ -38,6 +38,7 @@ static void py_convertFiniteStrainTangentOperator( } // end of py_py_convertFiniteStrainTangentOperator void declareFiniteStrainSupport(pybind11::module_& m) { + using namespace pybind11::literals; using namespace mgis::behaviour; pybind11::enum_(m, "FiniteStrainStress") .value("PK1", FiniteStrainStress::PK1) @@ -45,7 +46,9 @@ void declareFiniteStrainSupport(pybind11::module_& m) { pybind11::enum_(m, "FiniteStrainTangentOperator") .value("DPK1_DF", FiniteStrainTangentOperator::DPK1_DF); - m.def("convertFiniteStrainStress", py_convertFiniteStrainStress); + m.def("convertFiniteStrainStress", py_convertFiniteStrainStress, "stress"_a, + "manager"_a, "type"_a); m.def("convertFiniteStrainTangentOperator", - py_convertFiniteStrainTangentOperator); + py_convertFiniteStrainTangentOperator, "tangent_operator"_a, + "manager"_a, "type"_a); } // end of declareFiniteStrainSupport diff --git a/bindings/python/src/Integrate.cxx b/bindings/python/src/Integrate.cxx index 9908ef859..067356486 100644 --- a/bindings/python/src/Integrate.cxx +++ b/bindings/python/src/Integrate.cxx @@ -153,6 +153,7 @@ MaterialDataManager_executePostProcessing3( } void declareIntegrate(pybind11::module_& m) { + using namespace pybind11::literals; using namespace mgis::behaviour; pybind11::enum_(m, "IntegrationType") @@ -226,21 +227,28 @@ void declareIntegrate(pybind11::module_& m) { &MultiThreadedBehaviourIntegrationResult::results); m.def("executeInitializeFunction", - BehaviourDataView_executeInitializeFunction); + BehaviourDataView_executeInitializeFunction, "view"_a, "behaviour"_a, + "name"_a); m.def("executeInitializeFunction", - BehaviourDataView_executeInitializeFunction2); + BehaviourDataView_executeInitializeFunction2, "view"_a, "behaviour"_a, + "name"_a, "inputs"_a); m.def("executeInitializeFunction", - MaterialDataManager_executeInitializeFunction); + MaterialDataManager_executeInitializeFunction, "manager"_a, "name"_a); m.def("executeInitializeFunction", - MaterialDataManager_executeInitializeFunction1); + MaterialDataManager_executeInitializeFunction1, "manager"_a, "name"_a, + "begin"_a, "end"_a); m.def("executeInitializeFunction", - MaterialDataManager_executeInitializeFunction2); + MaterialDataManager_executeInitializeFunction2, "thread_pool"_a, + "manager"_a, "name"_a); m.def("executeInitializeFunction", - MaterialDataManager_executeInitializeFunction3); + MaterialDataManager_executeInitializeFunction3, "manager"_a, "name"_a, + "inputs"_a); m.def("executeInitializeFunction", - MaterialDataManager_executeInitializeFunction4); + MaterialDataManager_executeInitializeFunction4, "manager"_a, "name"_a, + "inputs"_a, "begin"_a, "end"_a); m.def("executeInitializeFunction", - MaterialDataManager_executeInitializeFunction5); + MaterialDataManager_executeInitializeFunction5, "thread_pool"_a, + "manager"_a, "name"_a, "inputs"_a); int (*integrate_ptr1)(BehaviourDataView&, const Behaviour&) = integrate; int (*integrate_ptr2)(MaterialDataManager&, const IntegrationType, @@ -271,21 +279,34 @@ void declareIntegrate(pybind11::module_& m) { mgis::ThreadPool&, MaterialDataManager&, const BehaviourIntegrationOptions&, const mgis::real) = integrate_debug; - m.def("integrate", &integrateBehaviourData1); - m.def("integrate", integrate_ptr1); - m.def("integrate", integrate_ptr2); - m.def("integrate", integrate_ptr3); - m.def("integrate", integrate_ptr4); - m.def("integrate", integrate_ptr5); - m.def("integrate_debug", &integrateDebugBehaviourData1); - m.def("integrate_debug", integrate_debug_ptr1); - m.def("integrate_debug", integrate_debug_ptr2); - m.def("integrate_debug", integrate_debug_ptr3); - m.def("integrate_debug", integrate_debug_ptr4); - m.def("integrate_debug", integrate_debug_ptr5); - m.def("executePostProcessing", BehaviourDataView_executePostProcessing); - m.def("executePostProcessing", MaterialDataManager_executePostProcessing); - m.def("executePostProcessing", MaterialDataManager_executePostProcessing2); - m.def("executePostProcessing", MaterialDataManager_executePostProcessing3); + m.def("integrate", &integrateBehaviourData1, "data"_a, "behaviour"_a); + m.def("integrate", integrate_ptr1, "view"_a, "behaviour"_a); + m.def("integrate", integrate_ptr2, "manager"_a, "integration_type"_a, "dt"_a, + "begin"_a, "end"_a); + m.def("integrate", integrate_ptr3, "thread_pool"_a, "manager"_a, + "integration_type"_a, "dt"_a); + m.def("integrate", integrate_ptr4, "manager"_a, "options"_a, "dt"_a, + "begin"_a, "end"_a); + m.def("integrate", integrate_ptr5, "thread_pool"_a, "manager"_a, "options"_a, + "dt"_a); + m.def("integrate_debug", &integrateDebugBehaviourData1, "data"_a, + "behaviour"_a); + m.def("integrate_debug", integrate_debug_ptr1, "view"_a, "behaviour"_a); + m.def("integrate_debug", integrate_debug_ptr2, "manager"_a, + "integration_type"_a, "dt"_a, "begin"_a, "end"_a); + m.def("integrate_debug", integrate_debug_ptr3, "thread_pool"_a, "manager"_a, + "integration_type"_a, "dt"_a); + m.def("integrate_debug", integrate_debug_ptr4, "manager"_a, "options"_a, + "dt"_a, "begin"_a, "end"_a); + m.def("integrate_debug", integrate_debug_ptr5, "thread_pool"_a, "manager"_a, + "options"_a, "dt"_a); + m.def("executePostProcessing", BehaviourDataView_executePostProcessing, + "outputs"_a, "view"_a, "behaviour"_a, "name"_a); + m.def("executePostProcessing", MaterialDataManager_executePostProcessing, + "outputs"_a, "manager"_a, "name"_a); + m.def("executePostProcessing", MaterialDataManager_executePostProcessing2, + "outputs"_a, "manager"_a, "name"_a, "begin"_a, "end"_a); + m.def("executePostProcessing", MaterialDataManager_executePostProcessing3, + "outputs"_a, "thread_pool"_a, "manager"_a, "name"_a); } // end of declareIntegrate diff --git a/bindings/python/src/MaterialDataManager.cxx b/bindings/python/src/MaterialDataManager.cxx index cd45b2885..088283ec8 100644 --- a/bindings/python/src/MaterialDataManager.cxx +++ b/bindings/python/src/MaterialDataManager.cxx @@ -62,6 +62,7 @@ static pybind11::object MaterialDataManager_getSpeedOfSound( } // end of MaterialDataManager_getSpeedOfSound void declareMaterialDataManager(pybind11::module_& m) { + using namespace pybind11::literals; using mgis::size_type; using mgis::behaviour::Behaviour; using mgis::behaviour::MaterialDataManager; @@ -82,10 +83,11 @@ void declareMaterialDataManager(pybind11::module_& m) { "(the arrays bound through it must be kept alive by the " "caller)") .def("bindTangentOperator", - &MaterialDataManagerInitializer_bindTangentOperator, + &MaterialDataManagerInitializer_bindTangentOperator, "K"_a, "use the given array to store the tangent operator blocks", pybind11::keep_alive<1, 2>()) .def("bindSpeedOfSound", &MaterialDataManagerInitializer_bindSpeedOfSound, + "speed_of_sound"_a, "use the given array to store the speed of sounds", pybind11::keep_alive<1, 2>()); // exporting the MaterialDataManager class @@ -96,13 +98,14 @@ void declareMaterialDataManager(pybind11::module_& m) { const MaterialDataManagerInitializer&>(), pybind11::keep_alive<1, 2>(), pybind11::keep_alive<1, 4>()) .def("setThreadSafe", &MaterialDataManager::setThreadSafe, + "thread_safe"_a, "specify if various operations performed by the MaterialDataManager " "(memory allocations) shall be protected by a mutex") .def("allocateArrayOfTangentOperatorBlocks", &MaterialDataManager::allocateArrayOfTangentOperatorBlocks, "allocate the tangent operator blocks") .def("useExternalArrayOfTangentOperatorBlocks", - &MaterialDataManager_useExternalArrayOfTangentOperatorBlocks, + &MaterialDataManager_useExternalArrayOfTangentOperatorBlocks, "K"_a, "use and externally defined array to store the tangent operator " "blocks", pybind11::keep_alive<1, 2>()) @@ -114,6 +117,7 @@ void declareMaterialDataManager(pybind11::module_& m) { "allocate the array of speed of sounds") .def("useExternalArrayOfSpeedOfSounds", &MaterialDataManager_useExternalArrayOfSpeedOfSounds, + "speed_of_sound"_a, "use and externally defined array to store the spped of sounds", pybind11::keep_alive<1, 2>()) .def("releaseArrayOfSpeedOfSounds", @@ -132,7 +136,7 @@ void declareMaterialDataManager(pybind11::module_& m) { .def("update", ptr_update) .def("revert", ptr_revert); // free functions - m.def("update", ptr_update); - m.def("revert", ptr_revert); + m.def("update", ptr_update, "manager"_a); + m.def("revert", ptr_revert, "manager"_a); } // end of declareMaterialDataManager diff --git a/bindings/python/src/MaterialProperty.cxx b/bindings/python/src/MaterialProperty.cxx index 0bec92275..eafc3e1c3 100644 --- a/bindings/python/src/MaterialProperty.cxx +++ b/bindings/python/src/MaterialProperty.cxx @@ -19,6 +19,7 @@ void declareMaterialProperty(pybind11::module_&); void declareMaterialProperty(pybind11::module_& m) { + using namespace pybind11::literals; using mgis::material_property::MaterialProperty; // wrapping the MaterialProperty class pybind11::class_(m, "MaterialProperty") @@ -38,6 +39,6 @@ void declareMaterialProperty(pybind11::module_& m) { .def_readonly("inputs", &MaterialProperty::inputs, "inputs of the material property"); // wrapping free functions - m.def("load", mgis::material_property::load); + m.def("load", mgis::material_property::load, "library"_a, "name"_a); } // end of declareMaterialProperty diff --git a/bindings/python/src/MaterialStateManager.cxx b/bindings/python/src/MaterialStateManager.cxx index b0d309102..09b832464 100644 --- a/bindings/python/src/MaterialStateManager.cxx +++ b/bindings/python/src/MaterialStateManager.cxx @@ -276,6 +276,7 @@ static void MaterialStateManager_setExternalStateVariable4( void declareMaterialStateManager(pybind11::module_&); void declareMaterialStateManager(pybind11::module_& m) { + using namespace pybind11::literals; using mgis::size_type; using mgis::behaviour::Behaviour; using mgis::behaviour::MaterialStateManager; @@ -310,22 +311,27 @@ void declareMaterialStateManager(pybind11::module_& m) { m, "MaterialStateManagerInitializer") .def(pybind11::init<>()) .def("bindGradients", &MaterialStateManagerInitializer_bindGradients, + "gradients"_a, "use the given array to store the gradients", pybind11::keep_alive<1, 2>()) .def("bindThermodynamicForces", &MaterialStateManagerInitializer_bindThermodynamicForces, + "thermodynamic_forces"_a, "use the given array to store the thermodynamic forces", pybind11::keep_alive<1, 2>()) .def("bindInternalStateVariables", &MaterialStateManagerInitializer_bindInternalStateVariables, + "internal_state_variables"_a, "use the given array to store the internal state variables", pybind11::keep_alive<1, 2>()) .def("bindStoredEnergies", &MaterialStateManagerInitializer_bindStoredEnergies, + "stored_energies"_a, "use the given array to store the stored energies", pybind11::keep_alive<1, 2>()) .def("bindDissipatedEnergies", &MaterialStateManagerInitializer_bindDissipatedEnergies, + "dissipated_energies"_a, "use the given array to store the dissipated energies", pybind11::keep_alive<1, 2>()); // wrapping the MaterialStateManager class @@ -351,46 +357,68 @@ void declareMaterialStateManager(pybind11::module_& m) { &MaterialStateManager_getDissipatedEnergies) .def_property_readonly("internal_state_variables", &MaterialStateManager_getInternalStateVariables) - .def("setMaterialProperty", &MaterialStateManager_setMaterialProperty) - .def("setMaterialProperty", &MaterialStateManager_setMaterialProperty2) + .def("setMaterialProperty", &MaterialStateManager_setMaterialProperty, + "name"_a, "value"_a) + .def("setMaterialProperty", &MaterialStateManager_setMaterialProperty2, + "name"_a, "value"_a, "update_policy"_a) .def("setMaterialProperty", &MaterialStateManager_setMaterialProperty3, + "name"_a, "values"_a, "storage_mode"_a, storage_mode_doc) .def("setMaterialProperty", &MaterialStateManager_setMaterialProperty4, + "name"_a, "values"_a, "storage_mode"_a, "update_policy"_a, storage_mode_doc) - .def("setMassDensity", &MaterialStateManager_setMassDensity) - .def("setMassDensity", &MaterialStateManager_setMassDensity2) - .def("setMassDensity", &MaterialStateManager_setMassDensity3, + .def("setMassDensity", &MaterialStateManager_setMassDensity, "value"_a) + .def("setMassDensity", &MaterialStateManager_setMassDensity2, "value"_a, + "update_policy"_a) + .def("setMassDensity", &MaterialStateManager_setMassDensity3, "values"_a, + "storage_mode"_a, storage_mode_doc) - .def("setMassDensity", &MaterialStateManager_setMassDensity4, + .def("setMassDensity", &MaterialStateManager_setMassDensity4, "values"_a, + "storage_mode"_a, "update_policy"_a, storage_mode_doc) .def("setExternalStateVariable", - &MaterialStateManager_setExternalStateVariable) + &MaterialStateManager_setExternalStateVariable, "name"_a, "value"_a) .def("setExternalStateVariable", - &MaterialStateManager_setExternalStateVariable2) + &MaterialStateManager_setExternalStateVariable2, "name"_a, "value"_a, + "update_policy"_a) .def("setExternalStateVariable", - &MaterialStateManager_setExternalStateVariable3, storage_mode_doc) + &MaterialStateManager_setExternalStateVariable3, "name"_a, + "values"_a, "storage_mode"_a, storage_mode_doc) .def("setExternalStateVariable", - &MaterialStateManager_setExternalStateVariable4, storage_mode_doc); + &MaterialStateManager_setExternalStateVariable4, "name"_a, + "values"_a, "storage_mode"_a, "update_policy"_a, storage_mode_doc); // free functions - m.def("setMaterialProperty", &MaterialStateManager_setMaterialProperty); - m.def("setMaterialProperty", &MaterialStateManager_setMaterialProperty2); + m.def("setMaterialProperty", &MaterialStateManager_setMaterialProperty, + "state"_a, "name"_a, "value"_a); + m.def("setMaterialProperty", &MaterialStateManager_setMaterialProperty2, + "state"_a, "name"_a, "value"_a, "update_policy"_a); m.def("setMaterialProperty", &MaterialStateManager_setMaterialProperty3, + "state"_a, "name"_a, "values"_a, "storage_mode"_a, storage_mode_doc); m.def("setMaterialProperty", &MaterialStateManager_setMaterialProperty4, + "state"_a, "name"_a, "values"_a, "storage_mode"_a, "update_policy"_a, storage_mode_doc); - m.def("setMassDensity", &MaterialStateManager_setMassDensity); - m.def("setMassDensity", &MaterialStateManager_setMassDensity2); - m.def("setMassDensity", &MaterialStateManager_setMassDensity3, + m.def("setMassDensity", &MaterialStateManager_setMassDensity, "state"_a, + "value"_a); + m.def("setMassDensity", &MaterialStateManager_setMassDensity2, "state"_a, + "value"_a, "update_policy"_a); + m.def("setMassDensity", &MaterialStateManager_setMassDensity3, "state"_a, + "values"_a, "storage_mode"_a, storage_mode_doc); - m.def("setMassDensity", &MaterialStateManager_setMassDensity4, + m.def("setMassDensity", &MaterialStateManager_setMassDensity4, "state"_a, + "values"_a, "storage_mode"_a, "update_policy"_a, storage_mode_doc); m.def("setExternalStateVariable", - &MaterialStateManager_setExternalStateVariable); + &MaterialStateManager_setExternalStateVariable, "state"_a, "name"_a, + "value"_a); m.def("setExternalStateVariable", - &MaterialStateManager_setExternalStateVariable2); + &MaterialStateManager_setExternalStateVariable2, "state"_a, "name"_a, + "value"_a, "update_policy"_a); m.def("setExternalStateVariable", - &MaterialStateManager_setExternalStateVariable3, storage_mode_doc); + &MaterialStateManager_setExternalStateVariable3, "state"_a, "name"_a, + "values"_a, "storage_mode"_a, storage_mode_doc); m.def("setExternalStateVariable", - &MaterialStateManager_setExternalStateVariable4, storage_mode_doc); + &MaterialStateManager_setExternalStateVariable4, "state"_a, "name"_a, + "values"_a, "storage_mode"_a, "update_policy"_a, storage_mode_doc); } // end of declareMaterialStateManager diff --git a/bindings/python/src/Model.cxx b/bindings/python/src/Model.cxx index d7d6707fb..1f8d2d46a 100644 --- a/bindings/python/src/Model.cxx +++ b/bindings/python/src/Model.cxx @@ -32,7 +32,8 @@ void declareModel(pybind11::module_& m) { // wrapping free functions m.def("load", pybind11::overload_cast( - mgis::model::load)); + mgis::model::load), + "library"_a, "model"_a, "hypothesis"_a); m.def("loadFromDatabase", Model_loadFromDatabase, "name"_a, "hypothesis"_a, "material"_a = std::optional{}); diff --git a/bindings/python/src/State.cxx b/bindings/python/src/State.cxx index 2b5e29e6d..4cb9e9c6b 100644 --- a/bindings/python/src/State.cxx +++ b/bindings/python/src/State.cxx @@ -102,6 +102,7 @@ static void State_setExternalStateVariable2(mgis::behaviour::State& s, } // end of State_setExternalStateVariable2 void declareState(pybind11::module_& m) { + using namespace pybind11::literals; using mgis::behaviour::Behaviour; using mgis::behaviour::State; pybind11::class_(m, "State") @@ -118,13 +119,17 @@ void declareState(pybind11::module_& m) { .def_property_readonly("external_state_variables", &State_getExternalStateVariables); // - m.def("setMaterialProperty", State_setMaterialProperty, + m.def("setMaterialProperty", State_setMaterialProperty, "state"_a, "name"_a, + "value"_a, "set the value of a material property by name"); - m.def("setMaterialProperty", State_setMaterialProperty2, + m.def("setMaterialProperty", State_setMaterialProperty2, "state"_a, + "offset"_a, "value"_a, "set the value of a material property by offset"); - m.def("setExternalStateVariable", State_setExternalStateVariable, + m.def("setExternalStateVariable", State_setExternalStateVariable, "state"_a, + "name"_a, "value"_a, "set the value of an external state variable by name"); - m.def("setExternalStateVariable", State_setExternalStateVariable2, + m.def("setExternalStateVariable", State_setExternalStateVariable2, "state"_a, + "offset"_a, "value"_a, "set the value of an external state variable by offset"); } // end of declareState diff --git a/bindings/python/src/Variable.cxx b/bindings/python/src/Variable.cxx index 73276cb7a..afefedf81 100644 --- a/bindings/python/src/Variable.cxx +++ b/bindings/python/src/Variable.cxx @@ -83,6 +83,7 @@ static mgis::size_type getVariableSizeByString( void declareVariable(pybind11::module_&); void declareVariable(pybind11::module_& m) { + using namespace pybind11::literals; using mgis::behaviour::Variable; // wrapping the Variable::Type enum pybind11::enum_(m, "VariableType") @@ -129,19 +130,23 @@ void declareVariable(pybind11::module_& m) { "`Tensor`, `Tensor1D`, `Tensor2D`, `Tensor3D`, `HigherOrderTensor` " "and `Array`"); // free functions - m.def("getVariable", getVariableByString, + m.def("getVariable", getVariableByString, "variables"_a, "name"_a, pybind11::return_value_policy::copy); m.def("getVariableSize", pybind11::overload_cast( - &mgis::behaviour::getVariableSize)); - m.def("getVariableSize", getVariableSizeByString); + &mgis::behaviour::getVariableSize), + "variable"_a, "hypothesis"_a); + m.def("getVariableSize", getVariableSizeByString, "variables"_a, "name"_a, + "hypothesis"_a); m.def("getArraySize", pybind11::overload_cast&, const mgis::behaviour::Hypothesis>( - &mgis::behaviour::getArraySize)); - m.def("getVariableOffset", getVariableOffsetByString); + &mgis::behaviour::getArraySize), + "variables"_a, "hypothesis"_a); + m.def("getVariableOffset", getVariableOffsetByString, "variables"_a, "name"_a, + "hypothesis"_a); m.def("getVariableTypeSymbolicRepresentation", - &mgis::behaviour::getVariableTypeSymbolicRepresentation); + &mgis::behaviour::getVariableTypeSymbolicRepresentation, "type"_a); } // end of declareVariable diff --git a/bindings/python/src/behaviour-module.cxx b/bindings/python/src/behaviour-module.cxx index 49f9ac355..706a0e15f 100644 --- a/bindings/python/src/behaviour-module.cxx +++ b/bindings/python/src/behaviour-module.cxx @@ -33,10 +33,12 @@ PYBIND11_MODULE(behaviour, m) { declareBehaviourDescription(m); declareBehaviour(m); declareState(m); - declareBehaviourData(m); + // a class shall be declared before the functions and the classes using it, + // so that it appears by its python name in their signatures declareBehaviourDataView(m); - declareMaterialDataManager(m); + declareBehaviourData(m); declareMaterialStateManager(m); + declareMaterialDataManager(m); declareIntegrate(m); declareFiniteStrainSupport(m); } // end of module behaviour diff --git a/bindings/python/src/model-module.cxx b/bindings/python/src/model-module.cxx index 361fd83fb..702243924 100644 --- a/bindings/python/src/model-module.cxx +++ b/bindings/python/src/model-module.cxx @@ -12,9 +12,18 @@ * CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt). */ +#include #include // forward declaration void declareModel(pybind11::module_&); -PYBIND11_MODULE(model, m) { declareModel(m); } // end of module model +PYBIND11_MODULE(model, m) { + // the types of the signatures of this module are declared by the behaviour + // module, which shall thus be loaded first, so that they appear by their + // python names. The name of the package depends on the suffix of MGIS. + const auto name = m.attr("__name__").cast(); + pybind11::module_::import( + (name.substr(0, name.rfind('.')) + ".behaviour").c_str()); + declareModel(m); +} // end of module model diff --git a/cmake/modules/mgis-python-modules.cmake b/cmake/modules/mgis-python-modules.cmake new file mode 100644 index 000000000..501e0e9a0 --- /dev/null +++ b/cmake/modules/mgis-python-modules.cmake @@ -0,0 +1,186 @@ +# Python modules of MGIS: definition of the modules and generation of +# their stubs using pybind11-stubgen, following the same design as TFEL +# (cf. cmake/modules/tfel-python-modules.cmake in thelfer/tfel). + +# Root directory of the python modules in the build tree +set(MGIS_PYTHON_MODULES_BUILD_DIR "${PROJECT_BINARY_DIR}/bindings/python") + +# Name of the python package in the build tree and in the installation: +# mgis or mgis_ when MGIS_APPEND_SUFFIX is ON. Only the name of the +# package carries the suffix, not the names of the modules. +if(MGIS_APPEND_SUFFIX) + set(MGIS_PYTHON_PACKAGE_NAME "mgis_${MGIS_SUFFIX_FOR_PYTHON_MODULES}") +else(MGIS_APPEND_SUFFIX) + set(MGIS_PYTHON_PACKAGE_NAME "mgis") +endif(MGIS_APPEND_SUFFIX) + +# Define a pybind11 module of the mgis package. +# +# \param fullname name of the cmake target, py_ +# \param name name of the module, as imported by python +# \param ARGN sources of the module +function(mgis_python_module fullname name) + if(${ARGC} LESS 3) + message(FATAL_ERROR "mgis_python_module: no source specified") + endif(${ARGC} LESS 3) + add_library(py_${fullname} MODULE ${ARGN}) + target_include_directories(py_${fullname} PRIVATE + $) + if(TARGET pybind11::lto) + target_link_libraries(py_${fullname} PRIVATE + MFrontGenericInterface + pybind11::module pybind11::lto pybind11::windows_extras) + else(TARGET pybind11::lto) + target_link_libraries(py_${fullname} PRIVATE + MFrontGenericInterface + pybind11::module pybind11::windows_extras) + endif(TARGET pybind11::lto) + pybind11_extension(py_${fullname}) + if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) + # Strip unnecessary sections of the binary on Linux/macOS + pybind11_strip(py_${fullname}) + endif() + set_target_properties(py_${fullname} PROPERTIES PREFIX "") + set_target_properties(py_${fullname} PROPERTIES OUTPUT_NAME ${name}) + set_target_properties(py_${fullname} PROPERTIES + CXX_VISIBILITY_PRESET "hidden" + CUDA_VISIBILITY_PRESET "hidden") + # The modules shall be next to the __init__.py script of their package, + # which imports them. Multi-configuration generators append the name of + # the configuration to the output directory, unless it holds a generator + # expression. + set_target_properties(py_${fullname} PROPERTIES + LIBRARY_OUTPUT_DIRECTORY + "$<1:${MGIS_PYTHON_MODULES_BUILD_DIR}/${MGIS_PYTHON_PACKAGE_NAME}>") + # installation + install(TARGETS py_${fullname} + DESTINATION ${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY} + COMPONENT python_bindings) + python_module_stubgen(py_${fullname} ${name}) +endfunction(mgis_python_module) + +# Register a python module so that a stub file is generated for it using +# pybind11-stubgen by the generate_python_stubs function. +# +# Nothing is done if the enable-pybind11-stubgen option is OFF or if +# pybind11-stubgen has not been found. +# +# \param target target associated with the python module +# \param name name of the module, as it is imported by python +function(python_module_stubgen target name) + if(NOT (enable-pybind11-stubgen AND pybind11_stubgen)) + return() + endif(NOT (enable-pybind11-stubgen AND pybind11_stubgen)) + set_property(GLOBAL APPEND PROPERTY MGIS_PYTHON_STUBGEN_MODULES + "${target}:${MGIS_PYTHON_PACKAGE_NAME}.${name}") +endfunction(python_module_stubgen) + +# Generate the stub files of all the python modules registered by the +# python_module_stubgen function using pybind11-stubgen and install them +# next to the modules they describe. +# +# The stubs are generated by the python-stubs target, which is built when +# the project is built. +# +# This function shall be called after all the calls to add_subdirectory as +# it requires that all the python modules and all the MGIS libraries have +# been declared. +# +# Nothing is done if the enable-pybind11-stubgen option is OFF or if +# pybind11-stubgen has not been found. +function(generate_python_stubs) + if(NOT (enable-pybind11-stubgen AND pybind11_stubgen)) + return() + endif(NOT (enable-pybind11-stubgen AND pybind11_stubgen)) + get_property(MGIS_PYTHON_STUBGEN_MODULES + GLOBAL PROPERTY MGIS_PYTHON_STUBGEN_MODULES) + if(NOT MGIS_PYTHON_STUBGEN_MODULES) + return() + endif(NOT MGIS_PYTHON_STUBGEN_MODULES) + # Importing a python module triggers the execution of the __init__.py + # script of the package, which imports the other modules of the package. + # Hence all the python modules must be built before a stub can be + # generated. + set(pybind11_stubgen_dependencies) + foreach(entry ${MGIS_PYTHON_STUBGEN_MODULES}) + string(REGEX REPLACE ":.*$" "" target "${entry}") + list(APPEND pybind11_stubgen_dependencies "${target}") + endforeach(entry ${MGIS_PYTHON_STUBGEN_MODULES}) + # the environment variables passed to pybind11-stubgen + set(mgis_stubgen_pythonpath "${MGIS_PYTHON_MODULES_BUILD_DIR}") + if(WIN32) + # On windows, the directories containing the dynamic link libraries of + # the build tree, which the python modules depend on, are neither those + # of the python interpreter nor those of the python modules. The python + # interpreter ignores the PATH variable when it imports an extension + # module, but the directories can be added to the search path of the + # dynamic loader with the os.add_dll_directory function. This is done by + # a sitecustomize script, which is placed in a directory added to the + # PYTHONPATH variable for the execution of pybind11-stubgen. + # + # The MGIS python modules depend on the MGIS libraries, given by the + # MGIS_LIBRARIES global property, and on the TFEL libraries, installed + # in the bin directory of TFELHOME. + get_property(mgis_stubgen_libraries GLOBAL PROPERTY MGIS_LIBRARIES) + set(mgis_stubgen_library_directories) + foreach(mgis_stubgen_library ${mgis_stubgen_libraries}) + list(APPEND mgis_stubgen_library_directories + "$") + endforeach(mgis_stubgen_library ${mgis_stubgen_libraries}) + if(TFEL_FOUND) + list(APPEND mgis_stubgen_library_directories "${TFELHOME}/bin") + endif(TFEL_FOUND) + string(JOIN "\", r\"" mgis_stubgen_library_directories + ${mgis_stubgen_library_directories}) + set(mgis_stubgen_sitecustomize_directory + "${PROJECT_BINARY_DIR}/bindings/stubgen/$") + file(GENERATE + OUTPUT "${mgis_stubgen_sitecustomize_directory}/sitecustomize.py" + CONTENT "import os + +for _directory in dict.fromkeys((r\"${mgis_stubgen_library_directories}\", )): + if os.path.isdir(_directory): + os.add_dll_directory(_directory) +") + set(mgis_stubgen_pythonpath + "${mgis_stubgen_sitecustomize_directory};${MGIS_PYTHON_MODULES_BUILD_DIR}") + endif(WIN32) + # The PYTHONPATH variable is extended, not replaced: pybind11-stubgen and + # the python bindings of TFEL may only be found through it, as in the + # isolated build environments of pip. + if(NOT "$ENV{PYTHONPATH}" STREQUAL "") + if(WIN32) + string(APPEND mgis_stubgen_pythonpath ";$ENV{PYTHONPATH}") + else(WIN32) + string(APPEND mgis_stubgen_pythonpath ":$ENV{PYTHONPATH}") + endif(WIN32) + endif() + # the paths of the PYTHONPATH variable are separated by a semicolon on windows + string(REPLACE ";" "$" mgis_stubgen_pythonpath + "${mgis_stubgen_pythonpath}") + set(pybind11_stubgen_files) + foreach(entry ${MGIS_PYTHON_STUBGEN_MODULES}) + string(REGEX REPLACE "^[^:]*:" "" module "${entry}") + string(REPLACE "." "/" module_path "${module}") + set(stub_file "${MGIS_PYTHON_MODULES_BUILD_DIR}/${module_path}.pyi") + add_custom_command(OUTPUT "${stub_file}" + COMMAND "${CMAKE_COMMAND}" -E env + "PYTHONPATH=${mgis_stubgen_pythonpath}" + "${Python_EXECUTABLE}" -m pybind11_stubgen "${module}" + "--output-dir" "${MGIS_PYTHON_MODULES_BUILD_DIR}" + DEPENDS ${pybind11_stubgen_dependencies} + COMMENT "Generating stub for python module ${module}" + VERBATIM) + list(APPEND pybind11_stubgen_files "${stub_file}") + install(FILES "${stub_file}" + DESTINATION "${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY}" + COMPONENT python_bindings) + endforeach(entry ${MGIS_PYTHON_STUBGEN_MODULES}) + # PEP 561 marker: type checkers only use the stubs of an installed package + # if it holds this marker. Without stubs, it would make them fail. + install(FILES "${PROJECT_SOURCE_DIR}/bindings/python/mgis/py.typed" + DESTINATION "${MGIS_PYTHON_MODULES_INSTALL_DIRECTORY}" + COMPONENT python_bindings) + add_custom_target(python-stubs ALL + DEPENDS ${pybind11_stubgen_files}) +endfunction(generate_python_stubs) diff --git a/cmake/modules/mgis.cmake b/cmake/modules/mgis.cmake index cdbc438c3..77d05a2cd 100644 --- a/cmake/modules/mgis.cmake +++ b/cmake/modules/mgis.cmake @@ -23,6 +23,9 @@ function(mgis_library name) endif(${ARGC} LESS 2) add_library(${name} SHARED ${ARGN}) add_library(mgis::${name} ALIAS ${name}) + # reference to the library, used by generate_python_stubs to know the + # directories containing the dynamic link libraries of the build tree + set_property(GLOBAL APPEND PROPERTY MGIS_LIBRARIES "${name}") # the installed headers require C++20 target_compile_features(${name} PUBLIC cxx_std_20) if(MGIS_APPEND_SUFFIX) diff --git a/docs/web/install.md b/docs/web/install.md index 870ab269b..0b2d519d5 100644 --- a/docs/web/install.md +++ b/docs/web/install.md @@ -111,6 +111,26 @@ generated by the `TFEL` project. This allows several versions of `MGIS` to be installed in the same prefix. +# Installing using `pip` + +Binary wheels of the `python` bindings are available on the Python +Package Index: + +~~~~{.bash} +$ pip install mgis +~~~~ + +The wheel contains the `MGIS` library, headers and `cmake` package, in +addition to the `python` modules, so that behaviours can be compiled and +solvers built using it. It requires a `C++` compiler to compile `MFront` +implementations, and depends on the `tfel-mfront` wheel, pinned to the +version of `TFEL` it has been built against: the `TFEL` libraries are not +duplicated in the `MGIS` wheel and a single consistent pair of wheels is +installed. + +Wheels are provided for `Linux` (`x86_64`), `macOS` (`arm64`) and +`Windows` (`AMD64`), for `python` versions 3.9 and above. + # Installing using `spack` `spack` is flexible package manager (https://spack.io/). diff --git a/docs/web/release-notes-3.2.md b/docs/web/release-notes-3.2.md index b43bff118..9b92f9ef6 100644 --- a/docs/web/release-notes-3.2.md +++ b/docs/web/release-notes-3.2.md @@ -280,6 +280,22 @@ if (!isInvariantSatisfied) { } ~~~~ +## `pip` package + +The python bindings of `MGIS` can be installed using `pip`, see the +[installation page](install.html). The wheel depends on the `tfel-mfront` +wheel, pinned to the version of `TFEL` it is built against. + +## Stubs of the python modules + +When the python bindings are enabled and the `pybind11-stubgen` tool is +found, the stub files (`.pyi` files) of the python modules are generated +by the `python-stubs` target and installed next to the modules, with a +`py.typed` marker. The `enable-pybind11-stubgen` option turns this off. + +The arguments of the python functions and methods now have names, and can +be passed by keyword. + # New features of the `MGIS/Behaviour` library ## Save/restore operations in `MaterialStateManager` and `MaterialDataManager` @@ -683,6 +699,10 @@ For more details, see +## Issue 319: Add support for `pybind11_stubgen` + +For more details, see + ## Issue 308: Wrong statements and typos in the documentation and error messages For more details, see @@ -839,6 +859,10 @@ For more details, see +## Issue 254: Add a `pip` package + +For more details, see + ## Issue 250: Add `ErrorBacktrace::assertOrTerminate` For more details, see