Ok since I posted the first two attempts I'll add the latest fix here also (while recognizing that this is a good example of why posting solutions to email lists is not a good idea in general). The problem was MAKECMDGOALS which means solution didn't work when switching between targets. So:
OSMVF = .old_serialized_make_vars NSMVF = .new_serialized_make_vars SVDOF = .serialized_vars_diff_output # Variables To Not Serialize. MAKECMDGOALS is in here because we don't # consider a change of the target specified on the command line a good # reason to rebuild everything. VTNS = MAKECMDGOALS # Vars To Serialize VTS = $(filter-out $(VTNS), $(.VARIABLES)) # Put serialization of all VTS flavor/name/value into $(NSMVF) $(file \ >$(NSMVF), \ $(foreach v,$(VTS),$(flavor $(v)) var $(v) is $(value $(v))$(nl))) # Create or update $(OSMVF) if necessary $(shell \ ( \ [ -a $(OSMVF) ] \ && \ (diff $(OSMVF) $(NSMVF) >$(SVDOF)) \ ) \ || \ cp $(NSMVF) $(OSMVF) \ ; \ rm $(NSMVF) ) # Build Rule Inputs. This is supposed to list everything that influences # the build system itself. FIXXME: this won't work right with generated # dependency files with .mk extensions (so use a different extension :) BRI = Makefile \ $(wildcard Makefile.*) \ $(shell find . -name "*.mk" -print) \ $(OSMVF) # Means all targets should depend on target in $(BRI) .EXTRA_PREREQS = $(BRI)