1
0
mirror of synced 2025-01-18 09:04:52 +01:00

build: Make sure changing commit hash doesn't trigger a full rebuild

Closes #1137
This commit is contained in:
WerWolv 2023-06-26 14:01:45 +02:00
parent 15cd2b693c
commit d527675bda
16 changed files with 167 additions and 106 deletions

View File

@ -570,6 +570,8 @@ jobs:
# Fedora cmake build (in imhex.spec)
- name: 📦 Build RPM
env:
QA_RPATHS: 18
run: |
fedpkg --path $GITHUB_WORKSPACE --release ${{ matrix.mock_release }} mockbuild --enable-network -N --root $GITHUB_WORKSPACE/mock.cfg extra_args -- -v

View File

@ -14,47 +14,6 @@ macro(addDefines)
message(FATAL_ERROR "IMHEX_VERSION is not defined")
endif ()
if (DEFINED IMHEX_COMMIT_HASH_LONG AND DEFINED IMHEX_COMMIT_HASH_SHORT AND DEFINED IMHEX_COMMIT_BRANCH)
add_compile_definitions(
GIT_COMMIT_HASH_LONG="${IMHEX_COMMIT_HASH_LONG}"
GIT_COMMIT_HASH_SHORT="${IMHEX_COMMIT_HASH_SHORT}"
GIT_BRANCH="${IMHEX_COMMIT_BRANCH}"
)
else()
# Get the current working branch
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT_BRANCH
)
# Get the latest abbreviated commit hash of the working branch
execute_process(
COMMAND git log -1 --format=%h --abbrev=7
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH_SHORT
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT_HASH_SHORT
)
execute_process(
COMMAND git log -1 --format=%H
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH_LONG
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT_HASH_LONG
)
if (RESULT_BRANCH EQUAL 0 AND RESULT_HASH_LONG EQUAL 0 AND RESULT_HASH_SHORT EQUAL 0)
add_compile_definitions(
GIT_COMMIT_HASH_SHORT="${GIT_COMMIT_HASH_SHORT}"
GIT_COMMIT_HASH_LONG="${GIT_COMMIT_HASH_LONG}"
GIT_BRANCH="${GIT_BRANCH}")
endif ()
endif ()
set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} -DPROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR} -DPROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR} -DPROJECT_VERSION_PATCH=${PROJECT_VERSION_PATCH} ")
set(IMHEX_VERSION_STRING ${IMHEX_VERSION})
@ -71,10 +30,16 @@ macro(addDefines)
set(IMHEX_VERSION_STRING ${IMHEX_VERSION_STRING}-MinSizeRel)
add_compile_definitions(NDEBUG)
endif ()
add_compile_definitions(IMHEX_VERSION="${IMHEX_VERSION_STRING}")
endmacro()
function(addDefineToSource SOURCE DEFINE)
set_property(
SOURCE ${SOURCE}
APPEND
PROPERTY COMPILE_DEFINITIONS "${DEFINE}"
)
endfunction()
# Detect current OS / System
macro(detectOS)
if (WIN32)
@ -164,7 +129,7 @@ macro(configurePackingResources)
set(MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/resources/dist/macos/Info.plist.in")
set(MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
set(MACOSX_BUNDLE_GUI_IDENTIFIER "net.WerWolv.ImHex")
set(MACOSX_BUNDLE_LONG_VERSION_STRING "${PROJECT_VERSION}-${GIT_COMMIT_HASH_SHORT}")
set(MACOSX_BUNDLE_LONG_VERSION_STRING "${PROJECT_VERSION}-${IMHEX_COMMIT_HASH_SHORT}")
set(MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
string(TIMESTAMP CURR_YEAR "%Y")

View File

@ -71,3 +71,44 @@ endif ()
target_link_libraries(libimhex PRIVATE ${FMT_LIBRARIES})
target_link_libraries(libimhex PUBLIC dl imgui ${NFD_LIBRARIES} magic ${CAPSTONE_LIBRARIES} LLVMDemangle microtar ${NLOHMANN_JSON_LIBRARIES} ${YARA_LIBRARIES} ${LIBCURL_LIBRARIES} ${MBEDTLS_LIBRARIES} ${LIBBACKTRACE_LIBRARIES} libpl libpl-gen ${MINIAUDIO_LIBRARIES} libwolv-utils libwolv-io libwolv-hash libwolv-net libwolv-containers)
if (DEFINED IMHEX_COMMIT_HASH_LONG AND DEFINED IMHEX_COMMIT_HASH_SHORT AND DEFINED IMHEX_COMMIT_BRANCH)
set(GIT_COMMIT_HASH_LONG "${IMHEX_COMMIT_HASH_LONG}")
set(GIT_COMMIT_HASH_SHORT "${IMHEX_COMMIT_HASH_SHORT}")
set(GIT_BRANCH "${IMHEX_COMMIT_BRANCH}")
else()
# Get the current working branch
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT_BRANCH
)
# Get the latest abbreviated commit hash of the working branch
execute_process(
COMMAND git log -1 --format=%h --abbrev=7
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH_SHORT
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT_HASH_SHORT
)
execute_process(
COMMAND git log -1 --format=%H
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH_LONG
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE RESULT_HASH_LONG
)
endif ()
if ((NOT GIT_COMMIT_HASH_SHORT STREQUAL "") AND (NOT GIT_COMMIT_HASH_LONG STREQUAL "") AND (NOT GIT_BRANCH STREQUAL ""))
addDefineToSource(source/api/imhex_api.cpp "GIT_COMMIT_HASH_SHORT=\"${GIT_COMMIT_HASH_SHORT}\"")
addDefineToSource(source/api/imhex_api.cpp "GIT_COMMIT_HASH_LONG=\"${GIT_COMMIT_HASH_LONG}\"")
addDefineToSource(source/api/imhex_api.cpp "GIT_BRANCH=\"${GIT_BRANCH}\"")
endif ()
addDefineToSource(source/api/imhex_api.cpp "IMHEX_VERSION=\"${IMHEX_VERSION_STRING}\"")

View File

@ -507,11 +507,42 @@ namespace hex {
*/
bool isPortableVersion();
/**
* @brief Gets the current Operating System name
* @return Operating System name
*/
std::string getOSName();
/**
* @brief Gets the current Operating System version
* @return Operating System version
*/
std::string getOSVersion();
/**
* @brief Gets the current CPU architecture
* @return CPU architecture
*/
std::string getArchitecture();
/**
* @brief Gets the current ImHex version
* @return ImHex version
*/
std::string getImHexVersion();
/**
* @brief Gets the current git commit hash
* @param longHash Whether to return the full hash or the shortened version
* @return Git commit hash
*/
std::string getCommitHash(bool longHash = false);
/**
* @brief Gets the current git commit branch
* @return Git commit branch
*/
std::string getCommitBranch();
}
}

View File

@ -301,7 +301,7 @@ namespace hex {
}
runtime.addDefine("__IMHEX__");
runtime.addDefine("__IMHEX_VERSION__", IMHEX_VERSION);
runtime.addDefine("__IMHEX_VERSION__", ImHexApi::System::getImHexVersion());
}
void addPragma(const std::string &name, const pl::api::PragmaHandler &handler) {

View File

@ -586,6 +586,39 @@ namespace hex {
#endif
}
std::string getImHexVersion() {
#if defined IMHEX_VERSION
return IMHEX_VERSION;
#else
return "Unknown";
#endif
}
std::string getCommitHash(bool longHash) {
if (longHash) {
#if defined GIT_COMMIT_HASH_LONG
return GIT_COMMIT_HASH_LONG;
#else
return "Unknown";
#endif
}
else {
#if defined GIT_COMMIT_HASH_SHORT
return GIT_COMMIT_HASH_SHORT;
#else
return "Unknown";
#endif
}
}
std::string getCommitBranch() {
#if defined GIT_BRANCH
return GIT_BRANCH;
#else
return "Unknown";
#endif
}
}
}

View File

@ -1,4 +1,5 @@
#include <hex/api/plugin_manager.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/utils.hpp>
@ -73,9 +74,13 @@ namespace hex {
return false;
const auto requestedVersion = getCompatibleVersion();
if (requestedVersion != IMHEX_VERSION) {
log::error("Refused to load plugin '{}' which was built for a different version of ImHex: '{}'", wolv::util::toUTF8String(this->m_path.filename()), requestedVersion);
return false;
if (requestedVersion != ImHexApi::System::getImHexVersion()) {
if (requestedVersion.empty()) {
log::warn("Plugin '{}' did not specify a compatible version, assuming it is compatible with the current version of ImHex.", wolv::util::toUTF8String(this->m_path.filename()));
} else {
log::error("Refused to load plugin '{}' which was built for a different version of ImHex: '{}'", wolv::util::toUTF8String(this->m_path.filename()), requestedVersion);
return false;
}
}
if (this->m_initializePluginFunction != nullptr) {

View File

@ -135,10 +135,10 @@ namespace hex::init {
drawList->AddText(ImVec2(15, 120) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("WerWolv 2020 - {0}", &__DATE__[7]).c_str());
#if defined(DEBUG) && defined(GIT_BRANCH) && defined(GIT_COMMIT_HASH_SHORT)
drawList->AddText(ImVec2(15, 140) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0} : {1} {2}@{3}", IMHEX_VERSION, ICON_FA_CODE_BRANCH, GIT_BRANCH, GIT_COMMIT_HASH_SHORT).c_str());
#if defined(DEBUG)
drawList->AddText(ImVec2(15, 140) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0} : {1} {2}@{3}", ImHexApi::System::getImHexVersion(), ICON_FA_CODE_BRANCH, ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash()).c_str());
#else
drawList->AddText(ImVec2(15, 140) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0}", IMHEX_VERSION).c_str());
drawList->AddText(ImVec2(15, 140) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0}", ImHexApi::System::getImHexVersion()).c_str());
#endif
}

View File

@ -39,7 +39,7 @@ namespace hex::init {
// Check if we should check for updates
if (checkForUpdates == 1){
HttpRequest request("GET", GitHubApiURL + "/releases/latest"s);
request.setTimeout(2000);
request.setTimeout(500);
// Query the GitHub API for the latest release version
auto response = request.execute().get();
@ -58,7 +58,7 @@ namespace hex::init {
return false;
// Convert the current version string to a format that can be compared to the latest release
auto versionString = std::string(IMHEX_VERSION);
auto versionString = ImHexApi::System::getImHexVersion();
size_t versionLength = std::min(versionString.find_first_of('-'), versionString.length());
auto currVersion = "v" + versionString.substr(0, versionLength);
@ -78,34 +78,29 @@ namespace hex::init {
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.general.uuid", uuid);
}
// Make telemetry request
nlohmann::json telemetry = {
{ "uuid", uuid },
{ "format_version", "1" },
{ "imhex_version", IMHEX_VERSION },
#if defined(GIT_COMMIT_HASH_LONG) && defined(GIT_BRANCH)
{ "imhex_commit", fmt::format("{}@{}", GIT_COMMIT_HASH_LONG, GIT_BRANCH) },
#else
{ "imhex_commit", "??@??" },
#endif
{ "install_type", ImHexApi::System::isPortableVersion() ? "Portable" : "Installed" },
{ "os", ImHexApi::System::getOSName() },
{ "os_version", ImHexApi::System::getOSVersion() },
{ "arch", ImHexApi::System::getArchitecture() },
{ "gpu_vendor", ImHexApi::System::getGPUVendor() }
};
TaskManager::createBackgroundTask("Sending statistics...", [uuid, versionString](auto&) {
// Make telemetry request
nlohmann::json telemetry = {
{ "uuid", uuid },
{ "format_version", "1" },
{ "imhex_version", versionString },
{ "imhex_commit", fmt::format("{}@{}", ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash()) },
{ "install_type", ImHexApi::System::isPortableVersion() ? "Portable" : "Installed" },
{ "os", ImHexApi::System::getOSName() },
{ "os_version", ImHexApi::System::getOSVersion() },
{ "arch", ImHexApi::System::getArchitecture() },
{ "gpu_vendor", ImHexApi::System::getGPUVendor() }
};
HttpRequest telemetryRequest("POST", ImHexApiURL + "/telemetry"s);
telemetryRequest.setTimeout(2000);
HttpRequest telemetryRequest("POST", ImHexApiURL + "/telemetry"s);
telemetryRequest.setTimeout(500);
telemetryRequest.setBody(telemetry.dump());
telemetryRequest.addHeader("Content-Type", "application/json");
// Execute request
response = telemetryRequest.execute().get();
if (response.getStatusCode() != 200)
return false;
telemetryRequest.setBody(telemetry.dump());
telemetryRequest.addHeader("Content-Type", "application/json");
// Execute request
telemetryRequest.execute();
});
}
return true;
}

View File

@ -39,10 +39,8 @@ int main(int argc, char **argv, char **envp) {
{
Window::initNative();
log::info("Welcome to ImHex {}!", IMHEX_VERSION);
#if defined(GIT_BRANCH) && defined(GIT_COMMIT_HASH_SHORT)
log::info("Compiled using commit {}@{}", GIT_BRANCH, GIT_COMMIT_HASH_SHORT);
#endif
log::info("Welcome to ImHex {}!", ImHexApi::System::getImHexVersion());
log::info("Compiled using commit {}@{}", ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash());
init::WindowSplash splashWindow;

View File

@ -83,7 +83,8 @@ if (WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--allow-multiple-definition -fvisibility=hidden")
endif()
add_compile_definitions(IMHEX_PROJECT_NAME="${PROJECT_NAME}")
target_compile_definitions(${PROJECT_NAME} PRIVATE IMHEX_PROJECT_NAME="${PROJECT_NAME}")
target_compile_definitions(${PROJECT_NAME} PRIVATE IMHEX_VERSION="${IMHEX_VERSION_STRING}")
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
setupCompilerFlags(${PROJECT_NAME})

View File

@ -49,12 +49,9 @@ namespace hex::plugin::builtin {
ImGui::TextUnformatted("hex.builtin.welcome.server_contact.data_collected.version"_lang);
ImGui::TableNextColumn();
ImGui::TextFormattedWrapped("{}\n{}@{}\n{}",
IMHEX_VERSION,
#if defined (GIT_COMMIT_HASH_LONG) && defined (GIT_BRANCH)
GIT_COMMIT_HASH_LONG, GIT_BRANCH,
#else
"??", "??",
#endif
ImHexApi::System::getImHexVersion(),
ImHexApi::System::getCommitHash(true),
ImHexApi::System::getCommitBranch(),
ImHexApi::System::isPortableVersion() ? "Portable" : "Installed"
);

View File

@ -17,15 +17,9 @@ namespace hex::plugin::builtin {
nlohmann::json result;
result["build"] = {
{ "version", IMHEX_VERSION },
#if defined(GIT_COMMIT_HASH_LONG)
{ "commit", GIT_COMMIT_HASH_LONG },
#endif
#if defined(GIT_BRANCH)
{ "branch", GIT_BRANCH },
#endif
{ "version", ImHexApi::System::getImHexVersion() },
{ "commit", ImHexApi::System::getCommitHash(true) },
{ "branch", ImHexApi::System::getCommitBranch() }
};
std::vector<std::string> commands;

View File

@ -150,7 +150,7 @@ namespace hex::plugin::builtin {
}
{
const auto metadataContent = hex::format("{}\n{}", MetadataHeaderMagic, IMHEX_VERSION);
const auto metadataContent = hex::format("{}\n{}", MetadataHeaderMagic, ImHexApi::System::getImHexVersion());
tar.writeString(MetadataPath, metadataContent);
}

View File

@ -60,13 +60,11 @@ namespace hex::plugin::builtin {
ImGui::Image(this->m_logoTexture, scaled({ 64, 64 }));
ImGui::TableNextColumn();
ImGui::TextFormatted("ImHex Hex Editor v{} by WerWolv - " ICON_FA_CODE_BRANCH, IMHEX_VERSION);
ImGui::TextFormatted("ImHex Hex Editor v{} by WerWolv - " ICON_FA_CODE_BRANCH, ImHexApi::System::getImHexVersion());
#if defined(GIT_BRANCH) && defined(GIT_COMMIT_HASH_SHORT) && defined(GIT_COMMIT_HASH_LONG)
ImGui::SameLine();
if (ImGui::Hyperlink(hex::format("{0}@{1}", GIT_BRANCH, GIT_COMMIT_HASH_SHORT).c_str()))
hex::openWebpage("https://github.com/WerWolv/ImHex/commit/" GIT_COMMIT_HASH_LONG);
#endif
ImGui::SameLine();
if (ImGui::Hyperlink(hex::format("{0}@{1}", ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash()).c_str()))
hex::openWebpage("https://github.com/WerWolv/ImHex/commit/" + ImHexApi::System::getCommitHash(true));
ImGui::TextUnformatted("hex.builtin.view.help.about.translator"_lang);

View File

@ -34,7 +34,8 @@ if (WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--allow-multiple-definition -fvisibility=hidden")
endif()
add_compile_definitions(IMHEX_PROJECT_NAME="${PROJECT_NAME}")
target_compile_definitions(${PROJECT_NAME} PRIVATE IMHEX_PROJECT_NAME="${PROJECT_NAME}")
target_compile_definitions(${PROJECT_NAME} PRIVATE IMHEX_VERSION="${IMHEX_VERSION_STRING}")
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
setupCompilerFlags(${PROJECT_NAME})