[SOLVED]How to generate build number?
-
Hello,
Usually I use "VERSION variable":http://qt-project.org/doc/qt-5/qmake-variable-reference.html#version for version my builds but unfortunately it needs manual modification whenever I need to create a new release so I need some number generated automatically whenever I build my applications (build number)
Is there any way to generate a build number automatically instead of generating it manually (ex. using TIMEDATE functions of the operating system with "system function":http://qt-project.org/doc/qt-5/qmake-test-function-reference.html#system-command)?
-
BTW, I "found this thread":http://qt-project.org/forums/viewthread/6908 but unfortunately it didn't help me out because the suggested solution didn't work!
-
You don't use any build system (for example: Jenkins), right?
What you can do is to add a text file containing the last build number, and make your build process increment the number inside that file, and then compile it into your binary.
How can you compile the build number into the binary?
Pass something like this to qmake, and then use that variable in your source code:
@
DEFINES+="BUILDNUMBER=12345"
@You have suggested using timestamps. This is a nice idea and is actually much easier. Datetime functions are part of C++ standard, so they are available on all platforms and compilers. Something like this should work:
@
QString compilationTime = QString("%1T%2").arg(DATE).arg(TIME);
@You can also throw that data into a QDateTime to get a nice and consistent date format of your choice.
-
[quote]You have suggested using timestamps. This is a nice idea and is actually much easier. Datetime functions are part of C++ standard, so they are available on all platforms and compilers. Something like this should work:
QString compilationTime = QString("%1T%2").arg(__DATE__).arg(__TIME__);
You can also throw that data into a QDateTime to get a nice and consistent date format of your choice.[/quote]
But the snippet you've suggested changes on run-time while I want a solution works only on build-time (prebuild)
-
__DATE__ and __TIME__ are predefined compile-time macros, not functions evaluated at run-time. They will expand to a constant string literal with the compile-time values.
One thing to remember when using them though, is that you need to make sure the file containing them is actually compiled when you build. If you don't they will have old values next time you link. Something like "touch" as a pre-build event will do the job. You can also do a full rebuild when you need them to update. -
Thank you a lot guys (specially sierdzio); The solution is much easier than I expected.
[code]QString build = QString("%1%2")
.arg(QLocale(QLocale::C).toDate(QString(DATE).simplified(), QLatin1String("MMM d yyyy")).toString("yyyyMMdd"))
.arg(QString("%1%2%3%4%5%6").arg(TIME[0])
.arg(TIME[1])
.arg(TIME[3])
.arg(TIME[4])
.arg(TIME[6])
.arg(TIME[7]));[/code]The output: 20140505150357
-
Side note: If you just want an automatically increasing build number rather than a time stamp (what you have now), you could be using my "AutoInc":https://github.com/lordmulder/LameXP/blob/master/etc/Utilities/AutoInc.exe utility. It will scan the specified header file for the specified macro and, if found, increase its value by one. There's also an optional timeout, so it won't be increased again, if the file was already updated recently.
Another thing you should consider: If you are working with a revision control system (like Git or SVN), you could simply be counting the number of revisions (commits) and make that the build number. It's what x264 and friends do. The advantage is that your build numbers will always be "in sync" with the repository and each build number directly maps to a specific revision/commit. This is especially useful, if there are many developers on the project...
-
[quote]Side note: If you just want an automatically increasing build number rather than a time stamp (what you have now), you could be using my AutoInc [github.com] utility[/quote]
Thank you; I found your tool before but for some reason it didn't work :( Any way; Time Stamp fits my need exactly.[quote]Another thing you should consider: If you are working with a revision control system (like Git or SVN)[/quote]
Nice idea but I can't use it always because I don't versioning all my projects and most projects I work on use Bzr which somehow is going the die.