Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write a compile_commands.json from build_ext #4358

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4358.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A ``compile_commands.json`` is now written to the ``build/`` directory when building extension modules.
10 changes: 9 additions & 1 deletion setuptools/_distutils/unixccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ class UnixCCompiler(CCompiler):
if sys.platform == "cygwin":
exe_extension = ".exe"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.compile_commands = []

def preprocess(
self,
source,
Expand Down Expand Up @@ -185,7 +189,11 @@ def preprocess(
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
compiler_so = compiler_fixup(self.compiler_so, cc_args + extra_postargs)
try:
self.spawn(compiler_so + cc_args + [src, '-o', obj] + extra_postargs)
cmd = compiler_so + cc_args + [src, '-o', obj] + extra_postargs
self.spawn(cmd)
self.compile_commands.append(
{"directory": os.getcwd(), "arguments": cmd, "file": src}
)
except DistutilsExecError as msg:
raise CompileError(msg)

Expand Down
24 changes: 23 additions & 1 deletion setuptools/command/build_ext.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import os
import sys
import itertools
from importlib.machinery import EXTENSION_SUFFIXES
from importlib.util import cache_from_source as _compiled_file_name
from typing import Dict, Iterator, List, Tuple
from typing import Any, Dict, Iterator, List, Tuple
from pathlib import Path

from distutils.command.build_ext import build_ext as _du_build_ext
Expand Down Expand Up @@ -89,10 +90,31 @@ def run(self):
"""Build extensions in build directory, then copy if --inplace"""
old_inplace, self.inplace = self.inplace, 0
_build_ext.run(self)
self._update_compilation_database(
getattr(self.compiler, "compile_commands", [])
)
self.inplace = old_inplace
if old_inplace:
self.copy_extensions_to_source()

def _update_compilation_database(self, commands: List[Dict[str, Any]]) -> None:
build_base = Path(self.get_finalized_command('build').build_base)
build_base.mkdir(exist_ok=True)

db_file = build_base / "compile_commands.json"
try:
database = json.loads(db_file.read_text(encoding="utf-8"))
except OSError:
database = []

# Drop existing entries for newly built files
built_files = {command["file"] for command in commands}
database = [obj for obj in database if obj.get("file") not in built_files]

database.extend(commands)
output = json.dumps(database, allow_nan=False, indent=4, sort_keys=True)
db_file.write_text(output, encoding="utf-8")

def _get_inplace_equivalent(self, build_py, ext: Extension) -> Tuple[str, str]:
fullname = self.get_ext_fullname(ext.name)
filename = self.get_ext_filename(fullname)
Expand Down
Loading