Skip to content

Commit

Permalink
Add a mixxx-build-date library that has a reliable dirty result and b…
Browse files Browse the repository at this point in the history
…uild date in case of dirty
  • Loading branch information
daschuer committed May 2, 2021
1 parent 828e467 commit c0e4855
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 31 deletions.
47 changes: 19 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ endif()

# Get the current commit ref
execute_process(
COMMAND git describe --tags --always --dirty=-modified
COMMAND git describe --tags --always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_DESCRIBE
OUTPUT_STRIP_TRAILING_WHITESPACE
Expand All @@ -333,20 +333,6 @@ else()
message(STATUS "Git describe: ${GIT_DESCRIBE}")
endif()

# Get the current commit date
execute_process(
COMMAND git show --quiet --format=%cI --date=short
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_DATE
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(NOT GIT_COMMIT_DATE)
message(STATUS "Git commit date: unknown")
else()
message(STATUS "Git commit date: ${GIT_COMMIT_DATE}")
endif()

# Get the current working branch
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
Expand All @@ -372,18 +358,8 @@ execute_process(
if(NOT GIT_COMMIT_COUNT)
set(GIT_COMMIT_COUNT "unknown")
endif()

# Check if the worktree is dirty
execute_process(
COMMAND git diff --quiet
RESULT_VARIABLE GIT_WORKTREE_DIRTY
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
ERROR_QUIET
)
if(GIT_WORKTREE_DIRTY EQUAL "1")
set(GIT_COMMIT_COUNT "${GIT_COMMIT_COUNT}+")
endif()
message(STATUS "Git commit count: ${GIT_COMMIT_COUNT}")

if(MSVC)
# sccache support
find_program(SCCACHE_EXECUTABLE "sccache")
Expand Down Expand Up @@ -1190,7 +1166,22 @@ endif()
# The mixxx executable
add_executable(mixxx WIN32 src/main.cpp)
set_target_properties(mixxx-lib PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY}")
target_link_libraries(mixxx PUBLIC mixxx-lib)
target_link_libraries(mixxx PUBLIC mixxx-lib mixxx-build-date)

add_library(mixxx-build-date STATIC EXCLUDE_FROM_ALL
${CMAKE_BINARY_DIR}/src/builddate.cpp
)
target_include_directories(mixxx-build-date PUBLIC src)

add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/src/builddate.cpp
DEPENDS mixxx-lib src/builddate.cpp.in
COMMAND ${CMAKE_COMMAND}
-D CMAKE_BINARY_DIR_=${CMAKE_BINARY_DIR}
-D CMAKE_SOURCE_DIR_=${CMAKE_SOURCE_DIR}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/builddate.cmake
VERBATIM
)

#
# Installation and Packaging
Expand Down Expand Up @@ -1566,7 +1557,7 @@ add_executable(mixxx-test
src/test/wwidgetstack_test.cpp
)
set_target_properties(mixxx-test PROPERTIES AUTOMOC ON)
target_link_libraries(mixxx-test PUBLIC mixxx-lib gtest gmock)
target_link_libraries(mixxx-test PUBLIC mixxx-lib mixxx-build-date gtest gmock)

#
# Benchmark tests
Expand Down
27 changes: 27 additions & 0 deletions cmake/builddate.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Check if the worktree is dirty
execute_process(
COMMAND git diff --quiet
RESULT_VARIABLE DIRTY
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR_}
ERROR_QUIET
)

if(DIRTY EQUAL "1")
message(STATUS "Git worktree is dirty")
# This depends on mixxx-lib so it will pick a new date
# Whenever mixxx-lib has changed.
string(TIMESTAMP BUILD_DATE "%Y-%m-%dT%H:%M:%SZ" UTC)
else()
message(STATUS "Git worktree is clean")
# Get the current commit date in case the build tree is unchanged
# The leads to reproducible builds
execute_process(
COMMAND git show --quiet --format=%cI --date=short
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR_}
OUTPUT_VARIABLE BUILD_DATE
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()

configure_file(${CMAKE_SOURCE_DIR_}/src/builddate.cpp.in ${CMAKE_BINARY_DIR_}/src/builddate.cpp @ONLY)
11 changes: 11 additions & 0 deletions src/builddate.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "builddate.h"

//static
const char* BuildDate::date() {
return "@BUILD_DATE@";
};

//static
bool BuildDate::dirty() {
return static_cast<bool>(1);
};
7 changes: 7 additions & 0 deletions src/builddate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

class BuildDate {
public:
static const char* date();
static bool dirty();
};
5 changes: 4 additions & 1 deletion src/dialog/dlgaboutdlg.ui
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Git Commit Date:</string>
<string>Date:</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -136,6 +136,9 @@
<property name="text">
<string>Unknown</string>
</property>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
</layout>
Expand Down
8 changes: 6 additions & 2 deletions src/util/versionstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <vorbis/codec.h>

#define VERSION_STORE
#include "builddate.h"
#include "version.h"

namespace {
Expand All @@ -39,7 +40,6 @@ const QString kMixxxVersionSuffix = QString(MIXXX_VERSION_SUFFIX);
const QString kMixxx = QStringLiteral("Mixxx");
const QString kGitBranch = QString(GIT_BRANCH);
const QString kGitDescribe = QString(GIT_DESCRIBE);
const QDateTime kGitCommitDate = QDateTime::fromString(GIT_COMMIT_DATE, Qt::ISODate);
const QString kBuildFlags = QString(BUILD_FLAGS);

} // namespace
Expand All @@ -64,7 +64,7 @@ QString VersionStore::versionSuffix() {
}

QDateTime VersionStore::date() {
return kGitCommitDate;
return QDateTime::fromString(BuildDate::date(), Qt::ISODate);
}

// static
Expand Down Expand Up @@ -128,6 +128,10 @@ QString VersionStore::gitVersion() {
gitVersion = QStringLiteral("unknown");
}

if (BuildDate::dirty()) {
gitVersion.append(QStringLiteral("-modified"));
}

QString gitBranch = VersionStore::gitBranch();
if (!gitBranch.isEmpty()) {
gitVersion.append(QStringLiteral(" (") + gitBranch + QStringLiteral(" branch)"));
Expand Down

0 comments on commit c0e4855

Please sign in to comment.