Skip to content

Commit 4f4cb66

Browse files
committed
Merge branch 'pb/clang-json-compilation-database'
Developer support. * pb/clang-json-compilation-database: Makefile: add support for generating JSON compilation database
2 parents e465444 + 3821c38 commit 4f4cb66

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
/git.spec
197197
*.exe
198198
*.[aos]
199+
*.o.json
199200
*.py[co]
200201
.depend/
201202
*.gcda
@@ -217,6 +218,7 @@
217218
/tags
218219
/TAGS
219220
/cscope*
221+
/compile_commands.json
220222
*.hcc
221223
*.obj
222224
*.lib

Makefile

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,12 @@ all::
462462
# the global variable _wpgmptr containing the absolute path of the current
463463
# executable (this is the case on Windows).
464464
#
465+
# Define GENERATE_COMPILATION_DATABASE to "yes" to generate JSON compilation
466+
# database entries during compilation if your compiler supports it, using the
467+
# `-MJ` flag. The JSON entries will be placed in the `compile_commands/`
468+
# directory, and the JSON compilation database 'compile_commands.json' will be
469+
# created at the root of the repository.
470+
#
465471
# Define DEVELOPER to enable more compiler warnings. Compiler version
466472
# and family are auto detected, but could be overridden by defining
467473
# COMPILER_FEATURES (see config.mak.dev). You can still set
@@ -1256,6 +1262,27 @@ $(error please set COMPUTE_HEADER_DEPENDENCIES to yes, no, or auto \
12561262
endif
12571263
endif
12581264

1265+
ifndef GENERATE_COMPILATION_DATABASE
1266+
GENERATE_COMPILATION_DATABASE = no
1267+
endif
1268+
1269+
ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
1270+
compdb_check = $(shell $(CC) $(ALL_CFLAGS) \
1271+
-c -MJ /dev/null \
1272+
-x c /dev/null -o /dev/null 2>&1; \
1273+
echo $$?)
1274+
ifneq ($(compdb_check),0)
1275+
override GENERATE_COMPILATION_DATABASE = no
1276+
$(warning GENERATE_COMPILATION_DATABASE is set to "yes", but your compiler does not \
1277+
support generating compilation database entries)
1278+
endif
1279+
else
1280+
ifneq ($(GENERATE_COMPILATION_DATABASE),no)
1281+
$(error please set GENERATE_COMPILATION_DATABASE to "yes" or "no" \
1282+
(not "$(GENERATE_COMPILATION_DATABASE)"))
1283+
endif
1284+
endif
1285+
12591286
ifdef SANE_TOOL_PATH
12601287
SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH))
12611288
BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix "$(SANE_TOOL_PATH_SQ)"|'
@@ -2369,16 +2396,30 @@ missing_dep_dirs =
23692396
dep_args =
23702397
endif
23712398

2399+
compdb_dir = compile_commands
2400+
2401+
ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
2402+
missing_compdb_dir = $(compdb_dir)
2403+
$(missing_compdb_dir):
2404+
@mkdir -p $@
2405+
2406+
compdb_file = $(compdb_dir)/$(subst /,-,$@.json)
2407+
compdb_args = -MJ $(compdb_file)
2408+
else
2409+
missing_compdb_dir =
2410+
compdb_args =
2411+
endif
2412+
23722413
ASM_SRC := $(wildcard $(OBJECTS:o=S))
23732414
ASM_OBJ := $(ASM_SRC:S=o)
23742415
C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
23752416

23762417
.SUFFIXES:
23772418

2378-
$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs)
2379-
$(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
2380-
$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs)
2381-
$(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
2419+
$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir)
2420+
$(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
2421+
$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir)
2422+
$(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
23822423

23832424
%.s: %.c GIT-CFLAGS FORCE
23842425
$(QUIET_CC)$(CC) -o $@ -S $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
@@ -2401,6 +2442,14 @@ else
24012442
$(OBJECTS): $(LIB_H) $(GENERATED_H)
24022443
endif
24032444

2445+
ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
2446+
all:: compile_commands.json
2447+
compile_commands.json:
2448+
@$(RM) $@
2449+
$(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)/*.o.json > $@+
2450+
@if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi
2451+
endif
2452+
24042453
exec-cmd.sp exec-cmd.s exec-cmd.o: GIT-PREFIX
24052454
exec-cmd.sp exec-cmd.s exec-cmd.o: EXTRA_CPPFLAGS = \
24062455
'-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \
@@ -3085,7 +3134,7 @@ clean: profile-clean coverage-clean cocciclean
30853134
$(RM) $(TEST_PROGRAMS)
30863135
$(RM) $(FUZZ_PROGRAMS)
30873136
$(RM) $(HCC)
3088-
$(RM) -r bin-wrappers $(dep_dirs)
3137+
$(RM) -r bin-wrappers $(dep_dirs) $(compdb_dir) compile_commands.json
30893138
$(RM) -r po/build/
30903139
$(RM) *.pyc *.pyo */*.pyc */*.pyo $(GENERATED_H) $(ETAGS_TARGET) tags cscope*
30913140
$(RM) -r $(GIT_TARNAME) .doc-tmp-dir

0 commit comments

Comments
 (0)