Hmm. Well, I don't get anywhere with this.
The situation is that this is to be part of the ESP32 build, so the cmake setup is largely out of my hands. OK, that's not strictly true since I could override a lot and even take it over completely, but the aim is to have minimal change to the standard idf.py command.
So, I don't know what codeTarget is. Thinking about it, and having had this work once just by including version.h in the appropriate .c, would this actually fix the problem? It seems to be that I would just be telling the system that something depends on version.h, and that I will build version.h every time. However, after the first run, version.h will exist and will be unchanged, so whatever depends on it won't be compiled, and then my custom command gets run.
That's what I've see happen : first run, my pre-build.bat gets run, the version.h change triggers a build of the dependant source, modules linked, everything is fine. Second run, dependency check figures nothing to do, pre-build.bat gets run, everything is linked.