Commit dd805f4f authored by David Yip's avatar David Yip

Merge branch '191-record-application-version-in-the-application' into 'master'

Resolve "Record application version in the application"

Closes #191

See merge request !173
parents 34e2ade7 4c942d40
Pipeline #3039 passed with stages
in 7 minutes and 56 seconds
......@@ -12,6 +12,7 @@ set(JJ_ASAN OFF CACHE BOOL "Enable AddressSanitizer")
set(JJ_UBSAN OFF CACHE BOOL "Enable UndefinedBehaviorSanitizer")
set(JJ_TSAN OFF CACHE BOOL "Enable ThreadSanitizer")
set(JJ_GUI_USE_QT_QUICK_COMPILER OFF CACHE BOOL "Use the Qt Quick Compiler to precompile QML")
set(JJ_USE_GIT_VER ON CACHE BOOL "Update version from repository state")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
......
......@@ -10,6 +10,10 @@ Copyright 2014, 2015 Ableton AG, Berlin; used under the terms of the MIT License
Copyright 2018 Dave Gandy and Fork Awesome contributors; used under the terms of the SIL Open Font License, Version 1.1.
## [git_watcher](https://github.com/andrew-hardin/cmake-git-version-tracking)
Copyright 2019 Andrew Hardin; used under the terms of the MIT License.
## [Google Benchmark](https://github.com/google/benchmark)
Copyright 2015 Google Inc; used under the terms of the Apache License, Version 2.0.
......
......@@ -31,6 +31,7 @@
#include <QThread>
#include <QTranslator>
#include <app/setappname.h>
#include <app/version.h>
#include <consolelogwriter.h>
#include <databaseerrorshim.h>
#include <db/bookmarkmodel.h>
......@@ -84,6 +85,7 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
player::init(&argc, &argv);
app.setApplicationVersion(jj::Version);
QCommandLineParser parser;
parser.addOption(
......
......@@ -259,6 +259,7 @@ set(jj_SOURCES
db/urlmodel/playlistdataprovider.h
db/index.h db/extradatacache.h network/webfinger/hostmetaparser.h network/webfinger/error.cpp network/webfinger/error.h
db/playqueueurlsmodel.cpp db/playqueueurlsmodel.h
app/version.h.in
)
set(jj_RESOURCES
......@@ -304,6 +305,25 @@ if(CMAKE_BUILD_TYPE STREQUAL Debug)
target_compile_definitions(jj PRIVATE JJ_LINEARSEARCH_STATS)
endif()
set(_version_output ${CMAKE_CURRENT_BINARY_DIR}/generated-version/app/version.h)
target_include_directories(jj PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/generated-version)
if(JJ_USE_GIT_VER)
message(STATUS "Using git data for versioning")
set(PRE_CONFIGURE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/app/version.h.in)
set(POST_CONFIGURE_FILE ${_version_output})
include(git_watcher)
else()
set(_version_file "${CMAKE_CURRENT_SOURCE_DIR}/version")
message(STATUS "Using ${_version_file} for versioning")
if(EXISTS "${_version_file}")
file(STRINGS "${_version_file}" GIT_HEAD_SHA1)
configure_file(app/version.h.in ${_version_output} @ONLY)
else()
message(FATAL_ERROR "${_version_file} does not exist; it should contain a version")
endif()
endif()
if(WIN32)
install(TARGETS jj RUNTIME DESTINATION usr/bin COMPONENT Runtime)
......
#pragma once
#include <QLatin1String>
namespace jj
{
inline const auto Version = QLatin1Literal("@GIT_HEAD_SHA1@");
}
# git_watcher.cmake
#
# License: MIT
# Source: https://raw.githubusercontent.com/andrew-hardin/cmake-git-version-tracking/master/git_watcher.cmake
# This file defines the functions and targets needed to monitor
# the state of a git repo. If the state changes (e.g. a commit is made),
# then a file gets reconfigured.
#
# The behavior of this script can be modified by defining any of these variables:
#
# PRE_CONFIGURE_FILE (REQUIRED)
# -- The path to the file that'll be configured.
#
# POST_CONFIGURE_FILE (REQUIRED)
# -- The path to the configured PRE_CONFIGURE_FILE.
#
# GIT_STATE_FILE (OPTIONAL)
# -- The path to the file used to store the previous build's git state.
# Defaults to the current binary directory.
#
# GIT_WORKING_DIR (OPTIONAL)
# -- The directory from which git commands will be run.
# Defaults to the directory with the top level CMakeLists.txt.
#
# GIT_EXECUTABLE (OPTIONAL)
# -- The path to the git executable. It'll automatically be set if the
# user doesn't supply a path.
#
# Script design:
# - This script was designed similar to a Python application
# with a Main() function. I wanted to keep it compact to
# simplify "copy + paste" usage.
#
# - This script is made to operate in two CMake contexts:
# 1. Configure time context (when build files are created).
# 2. Build time context (called via CMake -P)
# If you see something odd (e.g. the NOT DEFINED clauses),
# consider that it can run in one of two contexts.
# Short hand for converting paths to absolute.
macro(PATH_TO_ABSOLUTE var_name)
get_filename_component(${var_name} "${${var_name}}" ABSOLUTE)
endmacro()
# Check that a required variable is set.
macro(CHECK_REQUIRED_VARIABLE var_name)
if(NOT DEFINED ${var_name})
message(FATAL_ERROR "The \"${var_name}\" variable must be defined.")
endif()
PATH_TO_ABSOLUTE(${var_name})
endmacro()
# Check that an optional variable is set, or, set it to a default value.
macro(CHECK_OPTIONAL_VARIABLE var_name default_value)
if(NOT DEFINED ${var_name})
set(${var_name} ${default_value})
endif()
PATH_TO_ABSOLUTE(${var_name})
endmacro()
CHECK_REQUIRED_VARIABLE(PRE_CONFIGURE_FILE)
CHECK_REQUIRED_VARIABLE(POST_CONFIGURE_FILE)
CHECK_OPTIONAL_VARIABLE(GIT_STATE_FILE "${CMAKE_BINARY_DIR}/git-state")
CHECK_OPTIONAL_VARIABLE(GIT_WORKING_DIR "${CMAKE_SOURCE_DIR}")
# Check the optional git variable.
# If it's not set, we'll try to find it using the CMake packaging system.
if(NOT DEFINED GIT_EXECUTABLE)
find_package(Git QUIET REQUIRED)
endif()
CHECK_REQUIRED_VARIABLE(GIT_EXECUTABLE)
# Function: GitStateChangedAction
# Description: this function is executed when the state of the git
# repo changes (e.g. a commit is made).
function(GitStateChangedAction _state_as_list)
# Set variables by index, then configure the file w/ these variables defined.
LIST(GET _state_as_list 0 GIT_RETRIEVED_STATE)
LIST(GET _state_as_list 1 GIT_HEAD_SHA1)
LIST(GET _state_as_list 2 GIT_IS_DIRTY)
configure_file("${PRE_CONFIGURE_FILE}" "${POST_CONFIGURE_FILE}" @ONLY)
endfunction()
# Function: GetGitState
# Description: gets the current state of the git repo.
# Args:
# _working_dir (in) string; the directory from which git commands will be executed.
# _state (out) list; a collection of variables representing the state of the
# repository (e.g. commit SHA).
function(GetGitState _working_dir _state)
# Get the hash for HEAD.
set(_success "true")
execute_process(COMMAND
"${GIT_EXECUTABLE}" describe --always HEAD
WORKING_DIRECTORY "${_working_dir}"
RESULT_VARIABLE res
OUTPUT_VARIABLE _hashvar
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(_success "false")
set(_hashvar "GIT-NOTFOUND")
endif()
# Get whether or not the working tree is dirty.
execute_process(COMMAND
"${GIT_EXECUTABLE}" status --porcelain
WORKING_DIRECTORY "${_working_dir}"
RESULT_VARIABLE res
OUTPUT_VARIABLE out
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(_success "false")
set(_dirty "false")
else()
if(NOT "${out}" STREQUAL "")
set(_dirty "true")
else()
set(_dirty "false")
endif()
endif()
# Return a list of our variables to the parent scope.
set(${_state} ${_success} ${_hashvar} ${_dirty} PARENT_SCOPE)
endfunction()
# Function: CheckGit
# Description: check if the git repo has changed. If so, update the state file.
# Args:
# _working_dir (in) string; the directory from which git commands will be ran.
# _state_changed (out) bool; whether or no the state of the repo has changed.
# _state (out) list; the repository state as a list (e.g. commit SHA).
function(CheckGit _working_dir _state_changed _state)
# Get the current state of the repo.
GetGitState("${_working_dir}" state)
# Set the output _state variable.
# (Passing by reference in CMake is awkward...)
set(${_state} ${state} PARENT_SCOPE)
# Check if the state has changed compared to the backup on disk.
if(EXISTS "${GIT_STATE_FILE}")
file(READ "${GIT_STATE_FILE}" OLD_HEAD_CONTENTS)
if(OLD_HEAD_CONTENTS STREQUAL "${state}")
# State didn't change.
set(${_state_changed} "false" PARENT_SCOPE)
return()
endif()
endif()
# The state has changed.
# We need to update the state file on disk.
# Future builds will compare their state to this file.
file(WRITE "${GIT_STATE_FILE}" "${state}")
set(${_state_changed} "true" PARENT_SCOPE)
endfunction()
# Function: SetupGitMonitoring
# Description: this function sets up custom commands that make the build system
# check the state of git before every build. If the state has
# changed, then a file is configured.
function(SetupGitMonitoring)
add_custom_target(check_git_repository
ALL
DEPENDS ${PRE_CONFIGURE_FILE}
BYPRODUCTS ${POST_CONFIGURE_FILE}
COMMENT "Checking the git repository for changes..."
COMMAND
${CMAKE_COMMAND}
-D_BUILD_TIME_CHECK_GIT=TRUE
-DGIT_WORKING_DIR=${GIT_WORKING_DIR}
-DGIT_EXECUTABLE=${GIT_EXECUTABLE}
-DGIT_STATE_FILE=${GIT_STATE_FILE}
-DPRE_CONFIGURE_FILE=${PRE_CONFIGURE_FILE}
-DPOST_CONFIGURE_FILE=${POST_CONFIGURE_FILE}
-P "${CMAKE_CURRENT_LIST_FILE}")
endfunction()
# Function: Main
# Description: primary entry-point to the script. Functions are selected based
# on whether it's configure or build time.
function(Main)
if(_BUILD_TIME_CHECK_GIT)
# Check if the repo has changed.
# If so, run the change action.
CheckGit("${GIT_WORKING_DIR}" did_change state)
if(did_change)
GitStateChangedAction("${state}")
endif()
else()
# >> Executes at configure time.
SetupGitMonitoring()
endif()
endfunction()
# And off we go...
Main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment