Skip to content

Commit ff602d4

Browse files
authored
Fix compile-database target (#59980)
Also fix race conditions caused by targets producing files being incorrectly marked as phony. Follow up to #59476 (guess there is a price to pay for using Claude) @lgoettgens that might help with the issues you are seeing in JuliaPackaging/Yggdrasil#12406
1 parent a933cf4 commit ff602d4

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ distcleanall: cleanall
712712

713713
# Generate compilation database (leverages existing clang tooling setup)
714714
compile-database:
715-
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/src compile-database-src
715+
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/src compile-database
716716

717717
test: check-whitespace $(JULIA_BUILD_MODE)
718718
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/test default JULIA_BUILD_MODE=$(JULIA_BUILD_MODE)

src/Makefile

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ endif
259259
default: $(JULIA_BUILD_MODE) # contains either "debug" or "release"
260260
all: debug release
261261

262-
release debug: %: libjulia-internal-% libjulia-codegen-% $(BUILDDIR)/compile_commands.json
262+
release debug: %: libjulia-internal-% libjulia-codegen-% regenerate-compile_commands
263263

264264
$(BUILDDIR):
265265
mkdir -p $(BUILDDIR)
@@ -558,6 +558,7 @@ clean:
558558
-rm -f $(BUILDDIR)/*.dbg.obj $(BUILDDIR)/*.o $(BUILDDIR)/*.dwo $(BUILDDIR)/*.$(SHLIB_EXT) $(BUILDDIR)/*.a $(BUILDDIR)/*.h.gen
559559
-rm -f $(BUILDDIR)/julia.expmap
560560
-rm -f $(BUILDDIR)/julia_version.h
561+
-rm -f $(BUILDDIR)/compile_commands.json
561562

562563
clean-flisp:
563564
-$(MAKE) -C $(SRCDIR)/flisp clean BUILDDIR='$(abspath $(BUILDDIR)/flisp)'
@@ -654,8 +655,10 @@ clean-analyzegc:
654655
rm -f $(build_shlibdir)/libImplicitAtomicsPlugin.$(SHLIB_EXT)
655656

656657
# Compilation database generation using existing clang infrastructure
657-
$(BUILDDIR)/compile_commands.json:
658-
@{ \
658+
.PHONY: regenerate-compile_commands
659+
regenerate-compile_commands:
660+
TMPFILE=$$(mktemp -p $(BUILDDIR) compile_commands.json.XXXXXX); \
661+
{ \
659662
CLANG_TOOLING_C_FLAGS="$$($(JULIAHOME)/contrib/escape_json.sh clang $(CLANG_TOOLING_C_FLAGS))"; \
660663
CLANG_TOOLING_CXX_FLAGS="$$($(JULIAHOME)/contrib/escape_json.sh clang $(CLANG_TOOLING_CXX_FLAGS))"; \
661664
echo "["; \
@@ -711,18 +714,17 @@ $(BUILDDIR)/compile_commands.json:
711714
printf '{\n "directory": "%s",\n "file": "%s",\n "arguments": [%s]\n}' "$(abspath $(SRCDIR))" "$$included_file" "$$cmd"; \
712715
done; \
713716
echo "]"; \
714-
} > $@.tmp
715-
@# This ensures we replace the file atomically, and avoid spurious rewrites
716-
@if ! cmp -s $@.tmp $@; then \
717-
mv $@.tmp $@; \
717+
} > $$TMPFILE; \
718+
if ! cmp -s $$TMPFILE $(BUILDDIR)/compile_commands.json; then \
719+
mv $$TMPFILE $(BUILDDIR)/compile_commands.json; \
718720
else \
719-
rm -f $@.tmp; \
721+
rm -f $$TMPFILE; \
720722
fi
721723

722-
compile-database: $(BUILDDIR)/compile_commands.json
724+
compile-database: regenerate-compile_commands
723725
$(MAKE) -C $(SRCDIR)/flisp compile-database BUILDDIR='$(abspath $(BUILDDIR)/flisp)'
724726
$(MAKE) -C $(SRCDIR)/support compile-database BUILDDIR='$(abspath $(BUILDDIR)/support)'
725-
@echo "Compilation database created: $<"
727+
@echo "Compilation database created for src"
726728

727729
.FORCE:
728-
.PHONY: default all debug release clean cleanall clean-* libccalltest libllvmcalltest julia_flisp.boot.inc.phony analyzegc analyzesrc compile-database $(BUILDDIR)/compile_commands.json .FORCE
730+
.PHONY: default all debug release clean cleanall clean-* libccalltest libllvmcalltest julia_flisp.boot.inc.phony analyzegc analyzesrc compile-database .FORCE

src/flisp/Makefile

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ DEBUGFLAGS_COMMON += $(FLAGS_COMMON)
6161

6262
default: release
6363

64-
release: $(BUILDDIR)/$(EXENAME)$(EXE) $(BUILDDIR)/compile_commands.json
64+
release: $(BUILDDIR)/$(EXENAME)$(EXE) regenerate-compile_commands
6565

66-
debug: $(BUILDDIR)/$(EXENAME)-debug$(EXE) $(BUILDDIR)/compile_commands.json
66+
debug: $(BUILDDIR)/$(EXENAME)-debug$(EXE) regenerate-compile_commands
6767

6868
$(BUILDDIR):
6969
mkdir -p $(BUILDDIR)
@@ -140,8 +140,10 @@ CLANG_TOOLING_C_FLAGS = $(CLANGSA_FLAGS) $(DEBUGFLAGS_CLANG) $(JCPPFLAGS_CLANG)
140140
INCLUDED_FLISP_FILES := flisp.c:cvalues.c flisp.c:types.c flisp.c:print.c flisp.c:read.c flisp.c:equal.c
141141

142142
# Compilation database generation
143-
$(BUILDDIR)/compile_commands.json:
144-
@{ \
143+
.PHONY: regenerate-compile_commands
144+
regenerate-compile_commands:
145+
TMPFILE=$$(mktemp -p $(BUILDDIR) compile_commands.json.XXXXXX); \
146+
{ \
145147
CLANG_TOOLING_C_FLAGS="$$($(JULIAHOME)/contrib/escape_json.sh clang $(CLANG_TOOLING_C_FLAGS))"; \
146148
echo "["; \
147149
first=true; \
@@ -158,16 +160,15 @@ $(BUILDDIR)/compile_commands.json:
158160
printf '{\n "directory": "%s",\n "file": "%s",\n "arguments": [%s]\n}' "$(abspath $(SRCDIR))" "$$included_file" "$$cmd"; \
159161
done; \
160162
echo "]"; \
161-
} > $@.tmp
162-
@# This ensures we replace the file atomically, and avoid spurious rewrites
163-
@if ! cmp -s $@.tmp $@; then \
164-
mv $@.tmp $@; \
163+
} > $$TMPFILE; \
164+
if ! cmp -s $$TMPFILE $(BUILDDIR)/compile_commands.json; then \
165+
mv $$TMPFILE $(BUILDDIR)/compile_commands.json; \
165166
else \
166-
rm -f $@.tmp; \
167+
rm -f $$TMPFILE; \
167168
fi
168169

169-
compile-database: $(BUILDDIR)/compile_commands.json
170-
@echo "Compilation database created for flisp: $<"
170+
compile-database: regenerate-compile_commands
171+
@echo "Compilation database created for src/flisp"
171172

172173
clean:
173174
rm -f $(BUILDDIR)/*.o
@@ -178,4 +179,4 @@ clean:
178179
rm -f $(BUILDDIR)/compile_commands.json
179180
rm -f $(BUILDDIR)/host/*
180181

181-
.PHONY: flisp-deps compile-database $(BUILDDIR)/compile_commands.json
182+
.PHONY: flisp-deps compile-database

src/support/Makefile

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ $(BUILDDIR)/host/Makefile:
5353
@printf "%s\n" 'BUILDING_HOST_TOOLS=1' >> $@
5454
@printf "%s\n" 'include $(SRCDIR)/Makefile' >> $@
5555

56-
release: $(BUILDDIR)/libsupport.a $(BUILDDIR)/compile_commands.json
57-
debug: $(BUILDDIR)/libsupport-debug.a $(BUILDDIR)/compile_commands.json
56+
release: $(BUILDDIR)/libsupport.a regenerate-compile_commands
57+
debug: $(BUILDDIR)/libsupport-debug.a regenerate-compile_commands
5858

5959
$(BUILDDIR)/libsupport.a: $(OBJS) | $(BUILDIR)
6060
rm -rf $@
@@ -78,8 +78,10 @@ CLANG_TOOLING_C_FLAGS = $(CLANGSA_FLAGS) $(DEBUGFLAGS_CLANG) $(JCPPFLAGS_CLANG)
7878
INCLUDED_SUPPORT_FILES := hashing.c:MurmurHash3.c
7979

8080
# Compilation database generation
81-
$(BUILDDIR)/compile_commands.json:
82-
@{ \
81+
.PHONY: regenerate-compile_commands
82+
regenerate-compile_commands:
83+
TMPFILE=$$(mktemp -p $(BUILDDIR) compile_commands.json.XXXXXX); \
84+
{ \
8385
CLANG_TOOLING_S_FLAGS="$$($(JULIAHOME)/contrib/escape_json.sh clang $(JCPPFLAGS) $(DEBUGFLAGS))"; \
8486
CLANG_TOOLING_C_FLAGS="$$($(JULIAHOME)/contrib/escape_json.sh clang $(JCPPFLAGS) $(JCFLAGS) $(DEBUGFLAGS))"; \
8587
echo "["; \
@@ -103,16 +105,15 @@ $(BUILDDIR)/compile_commands.json:
103105
printf '{\n "directory": "%s",\n "file": "%s",\n "arguments": [%s]\n}' "$(abspath $(SRCDIR))" "$$included_file" "$$cmd"; \
104106
done; \
105107
echo "]"; \
106-
} > $@.tmp
107-
@# This ensures we replace the file atomically, and avoid spurious rewrites
108-
@if ! cmp -s $@.tmp $@; then \
109-
mv $@.tmp $@; \
108+
} > $$TMPFILE; \
109+
if ! cmp -s $$TMPFILE $(BUILDDIR)/compile_commands.json; then \
110+
mv $$TMPFILE $(BUILDDIR)/compile_commands.json; \
110111
else \
111-
rm -f $@.tmp; \
112+
rm -f $$TMPFILE; \
112113
fi
113114

114-
compile-database: $(BUILDDIR)/compile_commands.json
115-
@echo "Compilation database created in support: $<"
115+
compile-database: regenerate-compile_commands
116+
@echo "Compilation database created for src/support"
116117

117118
clean:
118119
rm -f $(BUILDDIR)/*.o
@@ -125,4 +126,4 @@ clean:
125126
rm -f $(BUILDDIR)/compile_commands.json
126127
rm -f $(BUILDDIR)/host/*
127128

128-
.PHONY: compile-database $(BUILDDIR)/compile_commands.json
129+
.PHONY: compile-database

0 commit comments

Comments
 (0)