Compiles the source code in a provided directory and returns the compiled artifact.
(self, output_path: Path)
| 365 | return include_flags + host_flags |
| 366 | |
| 367 | def compile(self, output_path: Path) -> None: |
| 368 | """Compiles the source code in a provided directory and returns the compiled artifact.""" |
| 369 | with tempfile.TemporaryDirectory() as temp_dir_str: |
| 370 | temp_dir = Path(temp_dir_str) |
| 371 | source_filename = f"main{self.source_suffix}" |
| 372 | object_filename = f"main{self.output_suffix}" |
| 373 | source_path = temp_dir / source_filename |
| 374 | object_path = temp_dir / object_filename |
| 375 | with source_path.open("w", encoding="utf-8") as file: |
| 376 | file.write(self.source_code) |
| 377 | _cc.create_shared( |
| 378 | output=object_filename, |
| 379 | objects=[source_filename], |
| 380 | options=self.compile_options, |
| 381 | cc=self.compiler, |
| 382 | cwd=temp_dir, |
| 383 | ccache_env=( |
| 384 | { |
| 385 | "CCACHE_COMPILERCHECK": "content", |
| 386 | "CCACHE_NOHASHDIR": "1", |
| 387 | } |
| 388 | if shutil.which("ccache") |
| 389 | else None |
| 390 | ), |
| 391 | ) |
| 392 | shutil.move(str(object_path), str(output_path)) |
| 393 | |
| 394 | def load(self) -> Module: |
| 395 | with tempfile.TemporaryDirectory() as temp_dir_str: |