@@ -462,6 +462,12 @@ all::
462
462
# the global variable _wpgmptr containing the absolute path of the current
463
463
# executable (this is the case on Windows).
464
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
+ #
465
471
# Define DEVELOPER to enable more compiler warnings. Compiler version
466
472
# and family are auto detected, but could be overridden by defining
467
473
# 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 \
1256
1262
endif
1257
1263
endif
1258
1264
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
+
1259
1286
ifdef SANE_TOOL_PATH
1260
1287
SANE_TOOL_PATH_SQ = $(subst ','\'',$(SANE_TOOL_PATH ) )
1261
1288
BROKEN_PATH_FIX = 's|^\# @@BROKEN_PATH_FIX@@$$|git_broken_path_fix "$(SANE_TOOL_PATH_SQ)"|'
@@ -2369,16 +2396,30 @@ missing_dep_dirs =
2369
2396
dep_args =
2370
2397
endif
2371
2398
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
+
2372
2413
ASM_SRC := $(wildcard $(OBJECTS:o=S ) )
2373
2414
ASM_OBJ := $(ASM_SRC:S=o )
2374
2415
C_OBJ := $(filter-out $(ASM_OBJ ) ,$(OBJECTS ) )
2375
2416
2376
2417
.SUFFIXES :
2377
2418
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 ) $<
2382
2423
2383
2424
% .s : % .c GIT-CFLAGS FORCE
2384
2425
$(QUIET_CC )$(CC ) -o $@ -S $(ALL_CFLAGS ) $(EXTRA_CPPFLAGS ) $<
@@ -2401,6 +2442,14 @@ else
2401
2442
$(OBJECTS ) : $(LIB_H ) $(GENERATED_H )
2402
2443
endif
2403
2444
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
+
2404
2453
exec-cmd.sp exec-cmd.s exec-cmd.o : GIT-PREFIX
2405
2454
exec-cmd.sp exec-cmd.s exec-cmd.o : EXTRA_CPPFLAGS = \
2406
2455
' -DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \
@@ -3085,7 +3134,7 @@ clean: profile-clean coverage-clean cocciclean
3085
3134
$(RM ) $(TEST_PROGRAMS )
3086
3135
$(RM ) $(FUZZ_PROGRAMS )
3087
3136
$(RM ) $(HCC )
3088
- $(RM ) -r bin-wrappers $(dep_dirs )
3137
+ $(RM ) -r bin-wrappers $(dep_dirs ) $( compdb_dir ) compile_commands.json
3089
3138
$(RM ) -r po/build/
3090
3139
$(RM ) * .pyc * .pyo * /* .pyc * /* .pyo $(GENERATED_H ) $(ETAGS_TARGET ) tags cscope*
3091
3140
$(RM ) -r $(GIT_TARNAME ) .doc-tmp-dir
0 commit comments