From 8dab11d5362255e3711ba0bce85b5da359c53f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20BRIOL?= Date: Thu, 7 Nov 2024 22:45:58 +0100 Subject: [PATCH] Add custom build backend and update documentation to use build option with pip. --- .pre-commit-config.yaml | 2 + _custom_build/backend.py | 75 +++++++++++++++++++++++++++++++++++++ docs/source/conf.py | 4 +- docs/source/setup/build.rst | 9 +++++ pyproject.toml | 4 ++ setup.cfg | 3 -- 6 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 _custom_build/backend.py create mode 100644 pyproject.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c0d3dae7..edc60915 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,6 +35,8 @@ repos: rev: v0.32.0 hooks: - id: yapf + additional_dependencies: + - toml files: \.py(?:i)? # - repo: https://github.com/myint/docformatter # rev: "v1.7.5" diff --git a/_custom_build/backend.py b/_custom_build/backend.py new file mode 100644 index 00000000..ac99b95b --- /dev/null +++ b/_custom_build/backend.py @@ -0,0 +1,75 @@ +# Copyright (c) 2024 CNES +# +# All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. +import argparse +import sys + +import setuptools.build_meta + + +def usage(args: dict[str, str | list[str] | None]) -> argparse.Namespace: + """Parse the command line arguments.""" + parser = argparse.ArgumentParser('Custom build backend') + parser.add_argument('--c-compiler', help='Preferred C compiler') + parser.add_argument('--cxx-compiler', help='Preferred C++ compiler') + parser.add_argument('--generator', help='Selected CMake generator') + parser.add_argument('--cmake-args', help='Additional arguments for CMake') + parser.add_argument('--mkl', help='Using MKL as BLAS library') + return parser.parse_args(args=[f"--{k}={v}" for k, v in args.items()]) + + +def decode_bool(value: str | None) -> bool: + """Decode a boolean value.""" + if value is None: + return False + value = value.lower() + return value in {'1', 'true', 'yes'} + + +class _CustomBuildMetaBackend(setuptools.build_meta._BuildMetaBackend): + """Custom build backend. + + This class is used to pass the option from pip to the setup.py script. + + Reference: https://setuptools.pypa.io/en/latest/build_meta.html + """ + + def run_setup(self, setup_script='setup.py'): + """Run the setup script.""" + args = usage(self.config_settings or {}) # type: ignore[arg-type] + setuptools_args = [] + if args.c_compiler: + setuptools_args.append(f"--c-compiler={args.c_compiler}") + if args.cxx_compiler: + setuptools_args.append(f"--cxx-compiler={args.cxx_compiler}") + if args.generator: + setuptools_args.append(f"--generator={args.generator}") + if args.cmake_args: + setuptools_args.append(f"--cmake-args={args.cmake_args}") + if args.parallel: + setuptools_args.append(f"--parallel={args.parallel}") + if decode_bool(args.mkl): + setuptools_args.append('--mkl=yes') + + if setuptools_args: + sys.argv = (sys.argv[:1] + ['build_ext'] + setuptools_args + + sys.argv[1:]) + return super().run_setup(setup_script) + + def build_wheel( + self, + wheel_directory, + config_settings=None, + metadata_directory=None, + ): + """Build the wheel.""" + self.config_settings = config_settings + return super().build_wheel( + wheel_directory, + config_settings, + metadata_directory, + ) + + +build_wheel = _CustomBuildMetaBackend().build_wheel diff --git a/docs/source/conf.py b/docs/source/conf.py index c8cee777..2c977410 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -39,9 +39,9 @@ def get_build_dirname(): author = 'CNES/CLS' # The short X.Y version -version = '2024.11.0' +version = '2024.11.0.dev1' # The full version, including alpha/beta/rc tags -release = '2024.11.0' +release = '2024.11.0.dev1' # -- General configuration --------------------------------------------------- diff --git a/docs/source/setup/build.rst b/docs/source/setup/build.rst index 8e6ed167..2ab7e796 100644 --- a/docs/source/setup/build.rst +++ b/docs/source/setup/build.rst @@ -172,3 +172,12 @@ Install ======= To install this library, type the command ``python3 -m pip install .``. + +If you want to pass some options to the ``build_ext`` command, you can use the +``--config-settings``or ``-C`` option. For example, to compile the library using +MKL as BLAS library and the Visual Studio 17 2022 generator, type the following +command: + +.. code-block:: bash + + python3 -m pip install . -Cmkl=yes -Cgenerator="Visual Studio 17 2022" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..5f5ee837 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,4 @@ +[build-system] +requires = ["setuptools", "cmake"] +build-backend = "backend" +backend-path = ["_custom_build"] diff --git a/setup.cfg b/setup.cfg index f3fa1935..3f2d0a65 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,3 @@ -[build-system] -requires = ["setuptools", "cmake"] - [flake8] exclude = docs,tests max-line-length = 80