Generating VersionInfo resource for Windows DLLs and applications with CMake

comments

I'm just answered stackoverflow question and I think that it is worth to put it here too with more explanation on the subject.

There is possibilities to add additional information to DLL or EXE application in Windows with resource files. Resource version info is very useful to distinguish what version is installed on production servers. You can include additional info like branch in VCS the binary was compiled from etc. This string is up for you! It's your helper.

For a long time I used prepared resource file with all necessary information. When necessary I changed the version information manually. My resources were as follows (part of resource):

VS_VERSION_INFO VERSIONINFO
    FILEVERSION 4,1,15,4096
    PRODUCTVERSION 4.1.0.1
    FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
    FILEFLAGS 0x1L
#else
    FILEFLAGS 0x0L
#endif
    FILEOS 0x4L
    FILETYPE 0x1L
    FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "041904b0"
        BEGIN
            VALUE "Comments", "TeamCity CI build, release, branch=master"
            VALUE "CompanyName", "codeofclimber"
            VALUE "FileDescription", ""
            VALUE "FileVersion", 4.1.15.4096
            VALUE "InternalName", ""
            VALUE "LegalCopyright", "Copyright (C) 2015 codeofclimber"
            VALUE "OriginalFilename", ""
            VALUE "ProductName", ""
            VALUE "ProductVersion", 4.1.0.1
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x419, 1200
    END
END

Note that file and product versions may be different. Its also important to note that top level FILEVERSION and PRODUCTVERSION are comma-delimited digits, and FileVersion and ProductVersion in StringFileInfo are dot-delimited digits.

Automated way to generate version information

Now I automated the way to generate such resource version information on configuring cmake project.

You need three files from github to generate resource version information:

Put it in cmake subdirectory of your project and make sure to include it to CMAKE_MODULE_PATH like:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)

Then before add_executable() or add_library(SHARED) your target, use:

include(generate_product_version)
generate_product_version(
    VersionFilesOutputVariable
    NAME "My Great Project"
    ICON ${PATH_TO_APPLICATION_ICON}
    VERSION_MAJOR 1
    VERSION_MINOR 3
    VERSION_PATH ${BUILD_COUNTER}
    VERSION_REVISION ${BUILD_REVISION}
)

VersionInfo.h and VersionResource.rc will be generated to cmake binaries folder. Variable VersionFilesOutputVariable will hold paths to these files. Just add this list to your target:

add_executable(MyGreatProject ${your-target-sources} ${VersionFilesOutputVariable})

Full list of supported resource strings:

  • NAME - name of executable, required parameter
  • BUNDLE - general product name. For example, if your executable is Microsoft Word then your bundle may be Microsoft Office.
  • ICON - path to application icon (${CMAKE_SOURCE_DIR}/product.ico by default)
  • VERSION_MAJOR - default value is 1
  • VERSION_MINOR - default value is 0
  • VERSION_PATCH - default value is 0
  • VERSION_REVISION - default value is 0
  • COMPANY_NAME - your company name, required parameter
  • COMPANY_COPYRIGHT - default value is ${COMPANY_NAME} (C) Copyright ${CURRENT_YEAR}
  • COMMENTS - default value is ${NAME} v${VERSION_MAJOR}.${VERSION_MINOR}
  • ORIGINAL_FILENAME - default value is ${NAME}
  • INTERNAL_NAME - default value is ${NAME}
  • FILE_DESCRIPTION - default value is ${NAME}

As general tip to version numbers I use following conventions:

  • major version should be changed manually when breaking changes in API occured
  • minor version should be changed manually when new features added
  • VERSION_PATH is build counter for current build from CI
  • VERSION_REVISION is 0
  • COMMENTS contains VCS branch information and build configuration

Conclusion

With introduced cmake module you can easily generate version info resource files for our projects.

Comments