Autoversion is a command-line tool written in Python that looks for preprocessor definitions in a C/C++ header file and modifies the value to increment version values.
Requirements:
- Python 3.12 needs to be installed on the system that uses this utility.
- The only file you need from this repository is the command-line tool, "autoversion.py"
As an example, if you give it the definition name MYAPP_VERSION, it will search (line by line) in the header file for that #define and increment any version values it detects. Multiple definition names can be given. If you notice any strangeness (\0) to my example header, it's because it's formatted to be used in version resources for Windows projects.
The format of the version should be, X.X.X.X, but each part is optional (so X.X is valid or even just X). The value separator can be a period or a comma. Version values can also be quoted. Below is an example of how Autoversion interprets your version values:
MAJOR.MINOR.REVISION.BUILD or 1.16.52.1
The version is incremented in this way ...
- MAJOR: Not incremented unless it is the only value in the version. If this is the only value in the version, it is incremented by + 1.
- MINOR: This is set to the two digit representation of the current year (eg. 16).
- REVISION: This is the month and day combined. If the current local date is May 2, the revision will be set to 52.
- BUILD: This is incremented by + 1.
The version separator (periods and commas), leading and trailing quotations, and trailing "\0" will be maintained when the version is modified. Here's an example of C/C++ source code Autoversion works with (or look at example.h):
#define MYAPP_PRODUCTVERSION 1,16,54,7
#define MYAPP_STRPRODUCTVERSION "1.16.54.7\0"
You can also keep the build date (timestamp) updated using the bdate_macro option. The timestamp is a unixtime value that you can handle with time_t in C++. Here's an example of C/C++ source code Autoversion works with for build dates (or look in example.h):
#define MYAPP_BUILDDATE 1502827651
If you want to convert this build date value into something human readable, then it can be done with this sample code:
// Note: I'm using Microsoft versions of some calls to avoid compiler
// warnings, hence the _s. This is C++, but it should be easy to convert
// into standard C.
std::string get_builddate()
{
// Get the build date in Unix Time:
time_t unix_time = MYAPP_BUILDDATE;
// Convert the Unix Time to Local Time:
tm local_time = {};
localtime_s(&local_time, &unix_time);
// Convert the Local Time to a string:
char tmp[48] = {};
asctime_s(tmp, &local_time);
// Remove newline (argh asctime!):
std::string result = tmp;
result.pop_back();
return result;
}
Another feature, the upcopyright option, lets you keep the year or years updated in all of your copyright macros. The algorithm searches for the case insensitive string "copyright" in either the macro name or the macro's value. If you add -l or --lastyear to your options then only the year found at the end of your macro value will be changed. This is handy for those copyrights that have ranges (eg. 2020 - 2024). Here's what the macro might look like:
#define MYAPP_COPYRIGHT "Copyright © 2024 Xeek. All Rights Reserved.\0"
usage: autoversion.py [--bdate_macro MACRO] [-c] [-l] [-p] [-v] [-?]
FILE [MACRO [MACRO ...]]
Autoversion is a Python command-line tool that looks for preprocessor
definitions in a C/C++ header file and modifies the value to increment version
values, etc.
positional arguments:
FILE Source file to modify
MACRO Version macros to modify
optional arguments:
--bdate_macro MACRO Build date macro
-c, --upcopyright Update the year in copyrights
-l, --lastyear Update only the last year found in copyrights
-p, --pyversion Display Python version
-v, --version Display Autoversion version
-?, --help
You can configure your Visual Studio project to run Autoversion every time the project is built by following these instructions. Note that these instructions assumes your project is C/C++, other language projects will have different configurations.
- Right click on your project and select "Properties" or press ALT + ENTER with the project selected.
- Make sure the Configuration dropdown is set to "All Configurations" and that Platform is set to "All Platforms".
- On the left side select Configuration Properties > Build Events > Pre-Build Event.
- For the Command Line "autoversion.py "myversion.h MYDEF_PRODUCTVERSION"
- Make sure you put in the path for autoversion.py if it's not in the project's directory. Also make sure that you put in the path/filename of your header and the name of your #define in place of mine. Multiple definition arguments are possible.