Skip to content

Commit

Permalink
Add custom build backend and update documentation to use build option…
Browse files Browse the repository at this point in the history
… with pip.
  • Loading branch information
fbriol committed Nov 7, 2024
1 parent c1f7a5f commit 8dab11d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
75 changes: 75 additions & 0 deletions _custom_build/backend.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------

Expand Down
9 changes: 9 additions & 0 deletions docs/source/setup/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[build-system]
requires = ["setuptools", "cmake"]
build-backend = "backend"
backend-path = ["_custom_build"]
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[build-system]
requires = ["setuptools", "cmake"]

[flake8]
exclude = docs,tests
max-line-length = 80
Expand Down

0 comments on commit 8dab11d

Please sign in to comment.