Getting plugin version number from cmake project

I’m working on a plugin using cmake for managing the project.

I’d like to display a version string in the GUI and in order to avoid hard-coding I’d like to get it directly from the cmake project; before reinventing the wheel, is there a built-in way to do it?

In cmake I’m setting the version like this:

project(AUDIO_PLUGIN_EXAMPLE VERSION 0.0.1)

Thanks!

I have done something similar and use the “setTooltip” method over our Plugins’ logo to give the details about the build - you might find this handy:

    tbLogoButton.setTooltip(String(BUILD_TAG) + String("-") + String(BUILD_COMMIT_HASH));

These environment variables are set up with CMake, thus:

# Get the current Git commit hash
execute_process(
    COMMAND git rev-parse --short HEAD
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_COMMIT_HASH
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Get the latest Git tag
execute_process(
    COMMAND git describe --tags --abbrev=0
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_TAG
    OUTPUT_STRIP_TRAILING_WHITESPACE
)


# Reads in our VERSION file and sticks in it CURRENT_VERSION variable
# Be sure the file has no newlines!
# This exposes CURRENT_VERSION to the build system
# And it's later fed to JUCE so it shows up as VERSION in your IDE
file(STRINGS VERSION CURRENT_VERSION)

# Figure out the major version to append to our PROJECT_NAME
string(REGEX MATCH "([0-9]+)" MAJOR_VERSION ${CURRENT_VERSION})
message(STATUS "Major version: ${MAJOR_VERSION}")

# lets the app known if we're Debug or Release
CMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
VERSION="${CURRENT_VERSION}"

# Add compile definitions to pass the Git hash and tag as preprocessor macros
BUILD_COMMIT_HASH="${GIT_COMMIT_HASH}"
BUILD_TAG="${GIT_TAG}"

… which depends on a file called VERSION in the root directory, being updated with the version as you see fit.

Note that, because we have different beta’s and different builds, we use VERSION+BUILD_TAG+BUILD_COMMMIT to differentiate different distributables …

Thanks!

Not sure why but the syntax

BUILD_TAG="..."

didn’t work here - it complains about “Expected a command name, got unquoted argument with text” - I’m not a cmake expert so I might be missing something here.

However I found a simple way to just grab the version written in

project(AUDIO_PLUGIN_EXAMPLE VERSION 0.0.1)

and expose it to the C++ code:

target_compile_definitions(AudioPluginExample
    PUBLIC
        VERSION_STRING="${CMAKE_PROJECT_VERSION}")
        ...

which is exactly what I needed :slight_smile: (version string will be available in the code as VERSION_STRING).

Probably a copy/paste error - these snippets are from different parts of my CMakeList.txt … For me, its been very useful to be able to go back directly to the very commit in the repo that was used for different builds, especially when there are different builds floating around for different testing environments… anyway, glad you got a different solution working for you.

Otherwise you can use the JucePlugin_VersionString which i think does directly what you want.

1 Like

This library is really useful for CMake users.

oh yeah, this is the one I was looking for!!

Pretty sure the other techniques are good too, but for my super-simple scenario, this is the one! :slight_smile:

Thanks!