Configure CONFIG_VERSION_CODE at build time

I am building a custom image of openwrt and I would like to be sure that the last commit ID is written in CONFIG_VERSION_CODE at build time.
I know I can solve this using a script, but I would like to know if there is a solution that works when people just lunch make.
I know that some people (me, specifically) will forget to run build using the script and running make with the wrong commit ID would make things more complicated to figure out during testing etc.
Is there a way to execute some kind of script before the actual parsing of .config happens?
I see that packages have a "prereq" target, but I don't know if that fits my requirements and how I can define it in my Makefile.

You could always have the makefile get the git branch hash and put this into CONFIG_VERSION_CODE.
The git command you'd be looking for is something like git rev-parse --short=8 HEAD.

You could probably even do some magic and look at the git status to see if it's "dirty" (i.e. has modifications from the last git commit) and indicate this also.

You'd probably need to maintain the makefile mod in your own separate repository however, unless you can come up with a way to only bring it in optionally for others (and hence make it something that could go into the OpenWRT master).

Thank you for your reply.
Yes, I was looking into a mix of git tag (to get the actual version label) and git rev-parse to get latest commit.
But I hope there's a way to do this without changing the main Makefile, limiting this to an optional package.
My idea is to have a package I can include that will trigger generation of a version label, then writing this in .config.
I see that download and compile steps are a bit too far into the build process to safely change .config, that's why I looked into prereq step, but coulnd't find any doc/sample about it.