Builds a function object usable as distutils.ccompiler.CCompiler.compile.
(self)
| 421 | self._old: list[CCompilerMethod] = [] |
| 422 | |
| 423 | def function(self) -> CCompilerMethod: |
| 424 | """ |
| 425 | Builds a function object usable as distutils.ccompiler.CCompiler.compile. |
| 426 | """ |
| 427 | |
| 428 | def compile_function( |
| 429 | compiler: distutils.ccompiler.CCompiler, |
| 430 | sources: list[str], |
| 431 | output_dir: str | None = None, |
| 432 | macros: list[tuple[str] | tuple[str, str | None]] | None = None, |
| 433 | include_dirs: list[str] | None = None, |
| 434 | debug: bool = False, |
| 435 | extra_preargs: list[str] | None = None, |
| 436 | extra_postargs: list[str] | None = None, |
| 437 | depends: list[str] | None = None, |
| 438 | ) -> Any: |
| 439 | # These lines are directly from distutils.ccompiler.CCompiler |
| 440 | macros, objects, extra_postargs, pp_opts, build = compiler._setup_compile( # type: ignore[attr-defined] |
| 441 | output_dir, macros, include_dirs, sources, depends, extra_postargs |
| 442 | ) |
| 443 | cc_args = compiler._get_cc_args(pp_opts, debug, extra_preargs) # type: ignore[attr-defined] |
| 444 | |
| 445 | # The number of threads; start with default. |
| 446 | threads = self.default |
| 447 | |
| 448 | # Determine the number of compilation threads, unless set by an environment variable. |
| 449 | if self.envvar is not None: |
| 450 | threads = int(os.environ.get(self.envvar, self.default)) |
| 451 | |
| 452 | def _single_compile(obj: Any) -> None: |
| 453 | try: |
| 454 | src, ext = build[obj] |
| 455 | except KeyError: |
| 456 | return |
| 457 | |
| 458 | if not os.path.exists(obj) or self.needs_recompile(obj, src): |
| 459 | compiler._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) # type: ignore[attr-defined] |
| 460 | |
| 461 | try: |
| 462 | # Importing .synchronize checks for platforms that have some multiprocessing |
| 463 | # capabilities but lack semaphores, such as AWS Lambda and Android Termux. |
| 464 | import multiprocessing.synchronize |
| 465 | from multiprocessing.pool import ThreadPool |
| 466 | except ImportError: |
| 467 | threads = 1 |
| 468 | |
| 469 | if threads == 0: |
| 470 | try: |
| 471 | threads = multiprocessing.cpu_count() |
| 472 | threads = self.max if self.max and self.max < threads else threads |
| 473 | except NotImplementedError: |
| 474 | threads = 1 |
| 475 | |
| 476 | if threads > 1: |
| 477 | with ThreadPool(threads) as pool: |
| 478 | for _ in pool.imap_unordered(_single_compile, objects): |
| 479 | pass |
| 480 | else: |