tools: fix stampfile dependency handling when the list of build dirs changes (#15186)