2020-09-16 21:13:41 +00:00
|
|
|
"""
|
|
|
|
This module provides helpers for C++11+ projects using pybind11.
|
|
|
|
|
|
|
|
LICENSE:
|
|
|
|
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>, All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
may be used to endorse or promote products derived from this software
|
|
|
|
without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
"""
|
|
|
|
|
2020-10-14 18:08:41 +00:00
|
|
|
# IMPORTANT: If you change this file in the pybind11 repo, also review
|
|
|
|
# setup_helpers.pyi for matching changes.
|
|
|
|
#
|
|
|
|
# If you copy this file in, you don't
|
|
|
|
# need the .pyi file; it's just an interface file for static type checkers.
|
|
|
|
|
2020-09-16 21:13:41 +00:00
|
|
|
import contextlib
|
|
|
|
import os
|
2021-08-13 16:37:05 +00:00
|
|
|
import platform
|
2021-11-08 21:27:32 +00:00
|
|
|
import shlex
|
2020-09-16 21:13:41 +00:00
|
|
|
import shutil
|
|
|
|
import sys
|
2021-08-13 16:37:05 +00:00
|
|
|
import sysconfig
|
2020-09-16 21:13:41 +00:00
|
|
|
import tempfile
|
|
|
|
import threading
|
|
|
|
import warnings
|
2022-02-11 02:28:08 +00:00
|
|
|
from functools import lru_cache
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Iterable,
|
|
|
|
Iterator,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Tuple,
|
|
|
|
TypeVar,
|
|
|
|
Union,
|
|
|
|
)
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from setuptools import Extension as _Extension
|
2021-08-13 16:37:05 +00:00
|
|
|
from setuptools.command.build_ext import build_ext as _build_ext
|
2020-09-16 21:13:41 +00:00
|
|
|
except ImportError:
|
2023-11-08 07:42:54 +00:00
|
|
|
from distutils.command.build_ext import ( # type: ignore[assignment]
|
|
|
|
build_ext as _build_ext,
|
|
|
|
)
|
2023-06-21 17:25:10 +00:00
|
|
|
from distutils.extension import Extension as _Extension # type: ignore[assignment]
|
2020-09-16 21:13:41 +00:00
|
|
|
|
2020-10-02 14:03:35 +00:00
|
|
|
import distutils.ccompiler
|
2021-08-13 16:37:05 +00:00
|
|
|
import distutils.errors
|
2020-09-16 21:13:41 +00:00
|
|
|
|
2021-07-30 17:48:41 +00:00
|
|
|
WIN = sys.platform.startswith("win32") and "mingw" not in sysconfig.get_platform()
|
2020-09-16 21:13:41 +00:00
|
|
|
MACOS = sys.platform.startswith("darwin")
|
|
|
|
STD_TMPL = "/std:c++{}" if WIN else "-std=c++{}"
|
|
|
|
|
|
|
|
|
|
|
|
# It is recommended to use PEP 518 builds if using this module. However, this
|
|
|
|
# file explicitly supports being copied into a user's project directory
|
|
|
|
# standalone, and pulling pybind11 with the deprecated setup_requires feature.
|
|
|
|
# If you copy the file, remember to add it to your MANIFEST.in, and add the current
|
|
|
|
# directory into your path if it sits beside your setup.py.
|
|
|
|
|
|
|
|
|
2023-06-21 17:25:10 +00:00
|
|
|
class Pybind11Extension(_Extension):
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
|
|
|
Build a C++11+ Extension module with pybind11. This automatically adds the
|
|
|
|
recommended flags when you init the extension and assumes C++ sources - you
|
|
|
|
can further modify the options yourself.
|
|
|
|
|
|
|
|
The customizations are:
|
|
|
|
|
|
|
|
* ``/EHsc`` and ``/bigobj`` on Windows
|
|
|
|
* ``stdlib=libc++`` on macOS
|
|
|
|
* ``visibility=hidden`` and ``-g0`` on Unix
|
|
|
|
|
2021-07-04 23:58:35 +00:00
|
|
|
Finally, you can set ``cxx_std`` via constructor or afterwards to enable
|
2020-09-16 21:13:41 +00:00
|
|
|
flags for C++ std, and a few extra helper flags related to the C++ standard
|
|
|
|
level. It is _highly_ recommended you either set this, or use the provided
|
|
|
|
``build_ext``, which will search for the highest supported extension for
|
|
|
|
you if the ``cxx_std`` property is not set. Do not set the ``cxx_std``
|
|
|
|
property more than once, as flags are added when you set it. Set the
|
|
|
|
property to None to disable the addition of C++ standard flags.
|
|
|
|
|
|
|
|
If you want to add pybind11 headers manually, for example for an exact
|
|
|
|
git checkout, then set ``include_pybind11=False``.
|
|
|
|
"""
|
|
|
|
|
2021-01-19 23:20:30 +00:00
|
|
|
# flags are prepended, so that they can be further overridden, e.g. by
|
|
|
|
# ``extra_compile_args=["-g"]``.
|
2020-09-16 21:13:41 +00:00
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def _add_cflags(self, flags: List[str]) -> None:
|
2021-01-19 23:20:30 +00:00
|
|
|
self.extra_compile_args[:0] = flags
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def _add_ldflags(self, flags: List[str]) -> None:
|
2021-01-19 23:20:30 +00:00
|
|
|
self.extra_link_args[:0] = flags
|
2020-09-16 21:13:41 +00:00
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
2020-09-16 21:13:41 +00:00
|
|
|
self._cxx_level = 0
|
|
|
|
cxx_std = kwargs.pop("cxx_std", 0)
|
|
|
|
|
|
|
|
if "language" not in kwargs:
|
|
|
|
kwargs["language"] = "c++"
|
|
|
|
|
|
|
|
include_pybind11 = kwargs.pop("include_pybind11", True)
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
# Include the installed package pybind11 headers
|
|
|
|
if include_pybind11:
|
|
|
|
# If using setup_requires, this fails the first time - that's okay
|
|
|
|
try:
|
|
|
|
import pybind11
|
|
|
|
|
|
|
|
pyinc = pybind11.get_include()
|
|
|
|
|
|
|
|
if pyinc not in self.include_dirs:
|
|
|
|
self.include_dirs.append(pyinc)
|
2022-02-11 02:28:08 +00:00
|
|
|
except ModuleNotFoundError:
|
2020-09-16 21:13:41 +00:00
|
|
|
pass
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
self.cxx_std = cxx_std
|
2020-09-16 21:13:41 +00:00
|
|
|
|
2021-01-19 23:20:30 +00:00
|
|
|
cflags = []
|
2020-09-16 21:13:41 +00:00
|
|
|
if WIN:
|
2021-01-19 23:20:30 +00:00
|
|
|
cflags += ["/EHsc", "/bigobj"]
|
2020-09-16 21:13:41 +00:00
|
|
|
else:
|
2021-11-08 21:27:32 +00:00
|
|
|
cflags += ["-fvisibility=hidden"]
|
|
|
|
env_cflags = os.environ.get("CFLAGS", "")
|
|
|
|
env_cppflags = os.environ.get("CPPFLAGS", "")
|
|
|
|
c_cpp_flags = shlex.split(env_cflags) + shlex.split(env_cppflags)
|
|
|
|
if not any(opt.startswith("-g") for opt in c_cpp_flags):
|
|
|
|
cflags += ["-g0"]
|
2021-01-19 23:20:30 +00:00
|
|
|
self._add_cflags(cflags)
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
@property
|
2022-02-11 02:28:08 +00:00
|
|
|
def cxx_std(self) -> int:
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
2022-02-11 02:28:08 +00:00
|
|
|
The CXX standard level. If set, will add the required flags. If left at
|
|
|
|
0, it will trigger an automatic search when pybind11's build_ext is
|
|
|
|
used. If None, will have no effect. Besides just the flags, this may
|
|
|
|
add a macos-min 10.9 or 10.14 flag if MACOSX_DEPLOYMENT_TARGET is
|
|
|
|
unset.
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
|
|
|
return self._cxx_level
|
|
|
|
|
|
|
|
@cxx_std.setter
|
2022-02-11 02:28:08 +00:00
|
|
|
def cxx_std(self, level: int) -> None:
|
2020-09-16 21:13:41 +00:00
|
|
|
if self._cxx_level:
|
2023-02-17 20:31:08 +00:00
|
|
|
warnings.warn(
|
2023-02-17 22:13:29 +00:00
|
|
|
"You cannot safely change the cxx_level after setting it!", stacklevel=2
|
2023-02-17 20:31:08 +00:00
|
|
|
)
|
2020-09-16 21:13:41 +00:00
|
|
|
|
2020-09-18 18:57:47 +00:00
|
|
|
# MSVC 2015 Update 3 and later only have 14 (and later 17) modes, so
|
|
|
|
# force a valid flag here.
|
2020-09-16 21:13:41 +00:00
|
|
|
if WIN and level == 11:
|
|
|
|
level = 14
|
|
|
|
|
|
|
|
self._cxx_level = level
|
|
|
|
|
|
|
|
if not level:
|
|
|
|
return
|
|
|
|
|
2021-01-19 23:20:30 +00:00
|
|
|
cflags = [STD_TMPL.format(level)]
|
|
|
|
ldflags = []
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
if MACOS and "MACOSX_DEPLOYMENT_TARGET" not in os.environ:
|
2020-09-18 18:57:47 +00:00
|
|
|
# C++17 requires a higher min version of macOS. An earlier version
|
2020-10-29 15:37:42 +00:00
|
|
|
# (10.12 or 10.13) can be set manually via environment variable if
|
|
|
|
# you are careful in your feature usage, but 10.14 is the safest
|
|
|
|
# setting for general use. However, never set higher than the
|
|
|
|
# current macOS version!
|
|
|
|
current_macos = tuple(int(x) for x in platform.mac_ver()[0].split(".")[:2])
|
|
|
|
desired_macos = (10, 9) if level < 17 else (10, 14)
|
|
|
|
macos_string = ".".join(str(x) for x in min(current_macos, desired_macos))
|
2022-02-12 00:06:16 +00:00
|
|
|
macosx_min = f"-mmacosx-version-min={macos_string}"
|
2021-01-19 23:20:30 +00:00
|
|
|
cflags += [macosx_min]
|
|
|
|
ldflags += [macosx_min]
|
2020-09-16 21:13:41 +00:00
|
|
|
|
2021-01-19 23:20:30 +00:00
|
|
|
self._add_cflags(cflags)
|
|
|
|
self._add_ldflags(ldflags)
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Just in case someone clever tries to multithread
|
|
|
|
tmp_chdir_lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2022-02-11 02:28:08 +00:00
|
|
|
def tmp_chdir() -> Iterator[str]:
|
2020-09-16 21:13:41 +00:00
|
|
|
"Prepare and enter a temporary directory, cleanup when done"
|
|
|
|
|
|
|
|
# Threadsafe
|
|
|
|
with tmp_chdir_lock:
|
|
|
|
olddir = os.getcwd()
|
|
|
|
try:
|
|
|
|
tmpdir = tempfile.mkdtemp()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
yield tmpdir
|
|
|
|
finally:
|
|
|
|
os.chdir(olddir)
|
|
|
|
shutil.rmtree(tmpdir)
|
|
|
|
|
|
|
|
|
|
|
|
# cf http://bugs.python.org/issue26689
|
2022-02-11 02:28:08 +00:00
|
|
|
def has_flag(compiler: Any, flag: str) -> bool:
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
|
|
|
Return the flag if a flag name is supported on the
|
|
|
|
specified compiler, otherwise None (can be used as a boolean).
|
|
|
|
If multiple flags are passed, return the first that matches.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with tmp_chdir():
|
2022-02-11 02:28:08 +00:00
|
|
|
fname = Path("flagcheck.cpp")
|
|
|
|
# Don't trigger -Wunused-parameter.
|
2022-02-15 22:48:33 +00:00
|
|
|
fname.write_text("int main (int, char **) { return 0; }", encoding="utf-8")
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
try:
|
2022-02-11 02:28:08 +00:00
|
|
|
compiler.compile([str(fname)], extra_postargs=[flag])
|
2020-09-16 21:13:41 +00:00
|
|
|
except distutils.errors.CompileError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# Every call will cache the result
|
|
|
|
cpp_flag_cache = None
|
|
|
|
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
@lru_cache()
|
|
|
|
def auto_cpp_level(compiler: Any) -> Union[str, int]:
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
2020-09-18 18:57:47 +00:00
|
|
|
Return the max supported C++ std level (17, 14, or 11). Returns latest on Windows.
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
|
|
|
|
2020-09-18 18:57:47 +00:00
|
|
|
if WIN:
|
|
|
|
return "latest"
|
|
|
|
|
|
|
|
levels = [17, 14, 11]
|
2020-09-16 21:13:41 +00:00
|
|
|
|
|
|
|
for level in levels:
|
|
|
|
if has_flag(compiler, STD_TMPL.format(level)):
|
|
|
|
return level
|
|
|
|
|
|
|
|
msg = "Unsupported compiler -- at least C++11 support is needed!"
|
|
|
|
raise RuntimeError(msg)
|
|
|
|
|
|
|
|
|
2023-06-21 17:25:10 +00:00
|
|
|
class build_ext(_build_ext): # noqa: N801
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
|
|
|
Customized build_ext that allows an auto-search for the highest supported
|
2020-11-11 16:45:28 +00:00
|
|
|
C++ level for Pybind11Extension. This is only needed for the auto-search
|
|
|
|
for now, and is completely optional otherwise.
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def build_extensions(self) -> None:
|
2020-09-16 21:13:41 +00:00
|
|
|
"""
|
|
|
|
Build extensions, injecting C++ std for Pybind11Extension if needed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
for ext in self.extensions:
|
|
|
|
if hasattr(ext, "_cxx_level") and ext._cxx_level == 0:
|
2022-02-11 02:28:08 +00:00
|
|
|
ext.cxx_std = auto_cpp_level(self.compiler)
|
2020-09-16 21:13:41 +00:00
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
super().build_extensions()
|
2020-10-02 14:03:35 +00:00
|
|
|
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def intree_extensions(
|
|
|
|
paths: Iterable[str], package_dir: Optional[Dict[str, str]] = None
|
|
|
|
) -> List[Pybind11Extension]:
|
2021-07-13 21:21:55 +00:00
|
|
|
"""
|
|
|
|
Generate Pybind11Extensions from source files directly located in a Python
|
|
|
|
source tree.
|
|
|
|
|
|
|
|
``package_dir`` behaves as in ``setuptools.setup``. If unset, the Python
|
|
|
|
package root parent is determined as the first parent directory that does
|
|
|
|
not contain an ``__init__.py`` file.
|
|
|
|
"""
|
|
|
|
exts = []
|
2022-02-11 02:28:08 +00:00
|
|
|
|
2022-02-15 22:48:33 +00:00
|
|
|
if package_dir is None:
|
|
|
|
for path in paths:
|
2021-07-13 21:21:55 +00:00
|
|
|
parent, _ = os.path.split(path)
|
|
|
|
while os.path.exists(os.path.join(parent, "__init__.py")):
|
|
|
|
parent, _ = os.path.split(parent)
|
|
|
|
relname, _ = os.path.splitext(os.path.relpath(path, parent))
|
|
|
|
qualified_name = relname.replace(os.path.sep, ".")
|
|
|
|
exts.append(Pybind11Extension(qualified_name, [path]))
|
2022-02-15 22:48:33 +00:00
|
|
|
return exts
|
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
for prefix, parent in package_dir.items():
|
|
|
|
if path.startswith(parent):
|
|
|
|
relname, _ = os.path.splitext(os.path.relpath(path, parent))
|
|
|
|
qualified_name = relname.replace(os.path.sep, ".")
|
|
|
|
if prefix:
|
|
|
|
qualified_name = prefix + "." + qualified_name
|
|
|
|
exts.append(Pybind11Extension(qualified_name, [path]))
|
|
|
|
break
|
2021-07-13 21:21:55 +00:00
|
|
|
else:
|
2022-02-15 22:48:33 +00:00
|
|
|
msg = (
|
|
|
|
f"path {path} is not a child of any of the directories listed "
|
|
|
|
f"in 'package_dir' ({package_dir})"
|
|
|
|
)
|
|
|
|
raise ValueError(msg)
|
2022-02-11 02:28:08 +00:00
|
|
|
|
2021-07-13 21:21:55 +00:00
|
|
|
return exts
|
|
|
|
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def naive_recompile(obj: str, src: str) -> bool:
|
2020-11-11 16:45:28 +00:00
|
|
|
"""
|
|
|
|
This will recompile only if the source file changes. It does not check
|
|
|
|
header files, so a more advanced function or Ccache is better if you have
|
|
|
|
editable header files in your package.
|
|
|
|
"""
|
|
|
|
return os.stat(obj).st_mtime < os.stat(src).st_mtime
|
|
|
|
|
|
|
|
|
2023-02-22 14:18:55 +00:00
|
|
|
def no_recompile(obg: str, src: str) -> bool: # noqa: ARG001
|
2020-11-11 16:45:28 +00:00
|
|
|
"""
|
|
|
|
This is the safest but slowest choice (and is the default) - will always
|
|
|
|
recompile sources.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
S = TypeVar("S", bound="ParallelCompile")
|
|
|
|
|
|
|
|
CCompilerMethod = Callable[
|
|
|
|
[
|
|
|
|
distutils.ccompiler.CCompiler,
|
|
|
|
List[str],
|
|
|
|
Optional[str],
|
2024-04-02 15:14:08 +00:00
|
|
|
Optional[List[Union[Tuple[str], Tuple[str, Optional[str]]]]],
|
2022-02-11 02:28:08 +00:00
|
|
|
Optional[List[str]],
|
|
|
|
bool,
|
|
|
|
Optional[List[str]],
|
|
|
|
Optional[List[str]],
|
|
|
|
Optional[List[str]],
|
|
|
|
],
|
|
|
|
List[str],
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-10-02 14:03:35 +00:00
|
|
|
# Optional parallel compile utility
|
|
|
|
# inspired by: http://stackoverflow.com/questions/11013851/speeding-up-build-process-with-distutils
|
|
|
|
# and: https://github.com/tbenthompson/cppimport/blob/stable/cppimport/build_module.py
|
|
|
|
# and NumPy's parallel distutils module:
|
|
|
|
# https://github.com/numpy/numpy/blob/master/numpy/distutils/ccompiler.py
|
2022-02-11 02:28:08 +00:00
|
|
|
class ParallelCompile:
|
2020-10-02 14:03:35 +00:00
|
|
|
"""
|
|
|
|
Make a parallel compile function. Inspired by
|
2022-02-11 02:28:08 +00:00
|
|
|
numpy.distutils.ccompiler.CCompiler.compile and cppimport.
|
2020-10-02 14:03:35 +00:00
|
|
|
|
|
|
|
This takes several arguments that allow you to customize the compile
|
|
|
|
function created:
|
|
|
|
|
2020-11-11 16:45:28 +00:00
|
|
|
envvar:
|
|
|
|
Set an environment variable to control the compilation threads, like
|
|
|
|
NPY_NUM_BUILD_JOBS
|
|
|
|
default:
|
|
|
|
0 will automatically multithread, or 1 will only multithread if the
|
|
|
|
envvar is set.
|
|
|
|
max:
|
|
|
|
The limit for automatic multithreading if non-zero
|
|
|
|
needs_recompile:
|
|
|
|
A function of (obj, src) that returns True when recompile is needed. No
|
|
|
|
effect in isolated mode; use ccache instead, see
|
|
|
|
https://github.com/matplotlib/matplotlib/issues/1507/
|
|
|
|
|
|
|
|
To use::
|
2020-10-02 14:03:35 +00:00
|
|
|
|
|
|
|
ParallelCompile("NPY_NUM_BUILD_JOBS").install()
|
2020-11-11 16:45:28 +00:00
|
|
|
|
|
|
|
or::
|
|
|
|
|
2020-10-02 14:03:35 +00:00
|
|
|
with ParallelCompile("NPY_NUM_BUILD_JOBS"):
|
|
|
|
setup(...)
|
2020-11-11 16:45:28 +00:00
|
|
|
|
|
|
|
By default, this assumes all files need to be recompiled. A smarter
|
|
|
|
function can be provided via needs_recompile. If the output has not yet
|
|
|
|
been generated, the compile will always run, and this function is not
|
|
|
|
called.
|
2020-10-02 14:03:35 +00:00
|
|
|
"""
|
|
|
|
|
2020-11-11 16:45:28 +00:00
|
|
|
__slots__ = ("envvar", "default", "max", "_old", "needs_recompile")
|
2020-10-02 14:03:35 +00:00
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
envvar: Optional[str] = None,
|
|
|
|
default: int = 0,
|
2022-02-15 22:48:33 +00:00
|
|
|
max: int = 0, # pylint: disable=redefined-builtin
|
2022-02-11 02:28:08 +00:00
|
|
|
needs_recompile: Callable[[str, str], bool] = no_recompile,
|
|
|
|
) -> None:
|
2020-10-02 14:03:35 +00:00
|
|
|
self.envvar = envvar
|
|
|
|
self.default = default
|
|
|
|
self.max = max
|
2020-11-11 16:45:28 +00:00
|
|
|
self.needs_recompile = needs_recompile
|
2022-02-12 00:06:16 +00:00
|
|
|
self._old: List[CCompilerMethod] = []
|
2020-10-02 14:03:35 +00:00
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def function(self) -> CCompilerMethod:
|
2020-10-02 14:03:35 +00:00
|
|
|
"""
|
|
|
|
Builds a function object usable as distutils.ccompiler.CCompiler.compile.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def compile_function(
|
2022-02-11 02:28:08 +00:00
|
|
|
compiler: distutils.ccompiler.CCompiler,
|
|
|
|
sources: List[str],
|
|
|
|
output_dir: Optional[str] = None,
|
2024-04-02 15:14:08 +00:00
|
|
|
macros: Optional[List[Union[Tuple[str], Tuple[str, Optional[str]]]]] = None,
|
2022-02-11 02:28:08 +00:00
|
|
|
include_dirs: Optional[List[str]] = None,
|
|
|
|
debug: bool = False,
|
|
|
|
extra_preargs: Optional[List[str]] = None,
|
|
|
|
extra_postargs: Optional[List[str]] = None,
|
|
|
|
depends: Optional[List[str]] = None,
|
|
|
|
) -> Any:
|
2020-10-02 14:03:35 +00:00
|
|
|
# These lines are directly from distutils.ccompiler.CCompiler
|
2022-02-11 02:28:08 +00:00
|
|
|
macros, objects, extra_postargs, pp_opts, build = compiler._setup_compile( # type: ignore[attr-defined]
|
2020-10-02 14:03:35 +00:00
|
|
|
output_dir, macros, include_dirs, sources, depends, extra_postargs
|
|
|
|
)
|
2022-02-11 02:28:08 +00:00
|
|
|
cc_args = compiler._get_cc_args(pp_opts, debug, extra_preargs) # type: ignore[attr-defined]
|
2020-10-02 14:03:35 +00:00
|
|
|
|
|
|
|
# The number of threads; start with default.
|
|
|
|
threads = self.default
|
|
|
|
|
|
|
|
# Determine the number of compilation threads, unless set by an environment variable.
|
|
|
|
if self.envvar is not None:
|
|
|
|
threads = int(os.environ.get(self.envvar, self.default))
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def _single_compile(obj: Any) -> None:
|
2020-10-02 14:03:35 +00:00
|
|
|
try:
|
|
|
|
src, ext = build[obj]
|
|
|
|
except KeyError:
|
|
|
|
return
|
2020-11-11 16:45:28 +00:00
|
|
|
|
|
|
|
if not os.path.exists(obj) or self.needs_recompile(obj, src):
|
2022-02-11 02:28:08 +00:00
|
|
|
compiler._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) # type: ignore[attr-defined]
|
2020-10-02 14:03:35 +00:00
|
|
|
|
|
|
|
try:
|
2021-06-18 14:17:34 +00:00
|
|
|
# Importing .synchronize checks for platforms that have some multiprocessing
|
|
|
|
# capabilities but lack semaphores, such as AWS Lambda and Android Termux.
|
|
|
|
import multiprocessing.synchronize
|
2020-10-02 14:03:35 +00:00
|
|
|
from multiprocessing.pool import ThreadPool
|
|
|
|
except ImportError:
|
|
|
|
threads = 1
|
|
|
|
|
|
|
|
if threads == 0:
|
|
|
|
try:
|
|
|
|
threads = multiprocessing.cpu_count()
|
|
|
|
threads = self.max if self.max and self.max < threads else threads
|
|
|
|
except NotImplementedError:
|
|
|
|
threads = 1
|
|
|
|
|
|
|
|
if threads > 1:
|
2022-02-11 02:28:08 +00:00
|
|
|
with ThreadPool(threads) as pool:
|
2021-12-13 17:45:33 +00:00
|
|
|
for _ in pool.imap_unordered(_single_compile, objects):
|
|
|
|
pass
|
2020-10-02 14:03:35 +00:00
|
|
|
else:
|
|
|
|
for ob in objects:
|
|
|
|
_single_compile(ob)
|
|
|
|
|
|
|
|
return objects
|
|
|
|
|
|
|
|
return compile_function
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def install(self: S) -> S:
|
2022-02-15 22:48:33 +00:00
|
|
|
"""
|
|
|
|
Installs the compile function into distutils.ccompiler.CCompiler.compile.
|
|
|
|
"""
|
2022-02-11 02:28:08 +00:00
|
|
|
distutils.ccompiler.CCompiler.compile = self.function() # type: ignore[assignment]
|
2020-10-02 14:03:35 +00:00
|
|
|
return self
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def __enter__(self: S) -> S:
|
2020-11-11 16:45:28 +00:00
|
|
|
self._old.append(distutils.ccompiler.CCompiler.compile)
|
2020-10-02 14:03:35 +00:00
|
|
|
return self.install()
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
def __exit__(self, *args: Any) -> None:
|
|
|
|
distutils.ccompiler.CCompiler.compile = self._old.pop() # type: ignore[assignment]
|