462
# the global variable _wpgmptr containing the absolute path of the current
463
# executable (this is the case on Windows).
464
#
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
+#
471
# Define DEVELOPER to enable more compiler warnings. Compiler version
472
# and family are auto detected, but could be overridden by defining
473
# COMPILER_FEATURES (see config.mak.dev). You can still set
1264
endif
1265
endif
1266
1267
+ifndef GENERATE_COMPILATION_DATABASE
1268
+GENERATE_COMPILATION_DATABASE = no
1269
+endif
1270
+
1271
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
1272
+compdb_check = $(shell $(CC) $(ALL_CFLAGS) \
1273
+ -c -MJ /dev/null \
1274
+ -x c /dev/null -o /dev/null 2>&1; \
1275
+ echo $$?)
1276
+ifneq ($(compdb_check),0)
1277
+override GENERATE_COMPILATION_DATABASE = no
1278
+$(warning GENERATE_COMPILATION_DATABASE is set to "yes", but your compiler does not \
1279
+support generating compilation database entries)
1280
+endif
1281
+else
1282
+ifneq ($(GENERATE_COMPILATION_DATABASE),no)
1283
+$(error please set GENERATE_COMPILATION_DATABASE to "yes" or "no" \
1284
+(not "$(GENERATE_COMPILATION_DATABASE)"))
1285
+endif
1286
+endif
1287
+
1288
ifdef SANE_TOOL_PATH
1289
SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH))
1290
BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix "$(SANE_TOOL_PATH_SQ)"|'
2408
dep_args =
2409
endif
2410
2411
+compdb_dir = compile_commands
2412
+
2413
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
2414
+missing_compdb_dir = $(compdb_dir)
2415
+$(missing_compdb_dir):
2416
+ @mkdir -p $@
2417
+
2418
+compdb_file = $(compdb_dir)/$(subst /,-,$@.json)
2419
+compdb_args = -MJ $(compdb_file)
2420
+else
2421
+missing_compdb_dir =
2422
+compdb_args =
2423
+endif
2424
+
2425
ASM_SRC := $(wildcard $(OBJECTS:o=S))
2426
ASM_OBJ := $(ASM_SRC:S=o)
2427
C_OBJ := $(filter-out $(ASM_OBJ),$(OBJECTS))
2428
2429
.SUFFIXES:
2430
2390
-$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs)
2391
- $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
2392
-$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs)
2393
- $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
2431
+$(C_OBJ): %.o: %.c GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir)
2432
+ $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
2433
+$(ASM_OBJ): %.o: %.S GIT-CFLAGS $(missing_dep_dirs) $(missing_compdb_dir)
2434
+ $(QUIET_CC)$(CC) -o $*.o -c $(dep_args) $(compdb_args) $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
2435
2436
%.s: %.c GIT-CFLAGS FORCE
2437
$(QUIET_CC)$(CC) -o $@ -S $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) $<
2454
$(OBJECTS): $(LIB_H) $(GENERATED_H)
2455
endif
2456
2457
+ifeq ($(GENERATE_COMPILATION_DATABASE),yes)
2458
+all:: compile_commands.json
2459
+compile_commands.json:
2460
+ @$(RM) $@
2461
+ $(QUIET_GEN)sed -e '1s/^/[/' -e '$$s/,$$/]/' $(compdb_dir)/*.o.json > $@+
2462
+ @if test -s $@+; then mv $@+ $@; else $(RM) $@+; fi
2463
+endif
2464
+
2465
exec-cmd.sp exec-cmd.s exec-cmd.o: GIT-PREFIX
2466
exec-cmd.sp exec-cmd.s exec-cmd.o: EXTRA_CPPFLAGS = \
2467
'-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \
3166
$(RM) $(TEST_PROGRAMS)
3167
$(RM) $(FUZZ_PROGRAMS)
3168
$(RM) $(HCC)
3120
- $(RM) -r bin-wrappers $(dep_dirs)
3169
+ $(RM) -r bin-wrappers $(dep_dirs) $(compdb_dir) compile_commands.json
3170
$(RM) -r po/build/
3171
$(RM) *.pyc *.pyo */*.pyc */*.pyo $(GENERATED_H) $(ETAGS_TARGET) tags cscope*
3172
$(RM) -r $(GIT_TARNAME) .doc-tmp-dir