Skip to content

Commit

Permalink
Merge pull request #170 from 4ms/git-version
Browse files Browse the repository at this point in the history
Generate firmware update manifest json file and git metadata
  • Loading branch information
danngreen authored Dec 22, 2023
2 parents 6f22c10 + 2633a35 commit b498913
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build_test_firmware.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ jobs:
with:
cmake-version: '3.26.x'

- name: Git setup
run: git config --global --add safe.directory '*'

- name: Checkout
uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -71,4 +74,5 @@ jobs:
name: "Firmware Version: ${{ env.CI_REF_NAME }}"
files: |
firmware/build/main.uimg
firmware/build/metamodule-*.json
11 changes: 9 additions & 2 deletions firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ if(DEFINED LIMITED_MODULES_FILE)
validate_limited_modules_file(${LIMITED_MODULES_FILE})
endif()

#
# Git commit hash
#
include(cmake/CheckGit.cmake)
CheckGitSetup()

#
# Main Application
#
Expand All @@ -42,8 +48,9 @@ add_subdirectory(src/core_m4 ${M4_BIN_PATH})
add_subdirectory(src ${A7_BIN_PATH})

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/main.uimg
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/main.uimg ${CMAKE_CURRENT_BINARY_DIR}/metamodule-firmware.json
COMMAND flashing/elf_to_uimg.py --m4 $<TARGET_FILE:main_m4.elf> --a7 $<TARGET_FILE:main.elf> ${CMAKE_CURRENT_BINARY_DIR}/main.uimg
COMMAND flashing/manifest_generator.py --app "${CMAKE_CURRENT_BINARY_DIR}/main.uimg" --version "${GIT_FIRMWARE_VERSION_TAG}" "${CMAKE_CURRENT_BINARY_DIR}/metamodule-${GIT_FIRMWARE_VERSION_TAG}.json"
COMMAND ls -l ${CMAKE_CURRENT_BINARY_DIR}/main.uimg
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/main.uimg ${CMAKE_CURRENT_BINARY_DIR}/main-uimg.bin #Jlink and Ozone require .bin extension
DEPENDS main.elf main_m4.elf
Expand All @@ -54,7 +61,7 @@ add_custom_command(

add_custom_target(
combined-uimg ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/main.uimg
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/main.uimg ${CMAKE_CURRENT_BINARY_DIR}/metamodule-firmware.json
)


Expand Down
98 changes: 98 additions & 0 deletions firmware/cmake/CheckGit.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
set(CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR})
if (NOT DEFINED pre_configure_dir)
set(pre_configure_dir ${CMAKE_CURRENT_LIST_DIR})
endif ()

if (NOT DEFINED post_configure_dir)
set(post_configure_dir ${CMAKE_CURRENT_BINARY_DIR}/checkgit)
endif ()

set(pre_configure_file ${pre_configure_dir}/git_version.cpp.in)
set(post_configure_file ${post_configure_dir}/git_version.cpp)

function(CheckGitWrite git_hash)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/git-state.txt ${git_hash})
endfunction()

function(CheckGitRead git_hash)
if (EXISTS ${CMAKE_CURRENT_BINARY_DIR}/git-state.txt)
file(STRINGS ${CMAKE_CURRENT_BINARY_DIR}/git-state.txt CONTENT)
LIST(GET CONTENT 0 var)

set(${git_hash} ${var} PARENT_SCOPE)
endif ()
endfunction()

function(CheckGitVersion)
# Get the latest abbreviated commit hash of the working branch
execute_process(
COMMAND bash -c "git log -1 --format=%h"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND bash -c "git log -1 --format=%ci"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_COMMIT_TIME
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND bash -c "git describe --match firmware\\* --tags"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_FIRMWARE_VERSION_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND echo "Git hash: ${GIT_HASH}. Commit time: ${GIT_COMMIT_TIME}. Tag: ${GIT_FIRMWARE_VERSION_TAG}"
)

set(GIT_FIRMWARE_VERSION_TAG ${GIT_FIRMWARE_VERSION_TAG} PARENT_SCOPE)

CheckGitRead(GIT_HASH_CACHE)
if (NOT EXISTS ${post_configure_dir})
file(MAKE_DIRECTORY ${post_configure_dir})
endif ()

if (NOT EXISTS ${post_configure_dir}/git_version.h)
file(COPY ${pre_configure_dir}/git_version.h DESTINATION ${post_configure_dir})
endif()

if (NOT DEFINED GIT_HASH_CACHE)
set(GIT_HASH_CACHE "INVALID")
endif ()

# Only update the git_version.cpp if the hash has changed. This will
# prevent us from rebuilding the project more than we need to.
if (NOT ${GIT_HASH} STREQUAL "${GIT_HASH_CACHE}" OR NOT EXISTS ${post_configure_file})
# Set the GIT_HASH_CACHE variable so the next build won't have
# to regenerate the source file.
CheckGitWrite(${GIT_HASH})

configure_file(${pre_configure_file} ${post_configure_file} @ONLY)
endif ()

endfunction()

function(CheckGitSetup)

add_custom_target(AlwaysCheckGit COMMAND ${CMAKE_COMMAND}
-DRUN_CHECK_GIT_VERSION=1
-Dpre_configure_dir=${pre_configure_dir}
-Dpost_configure_file=${post_configure_dir}
-DGIT_HASH_CACHE=${GIT_HASH_CACHE}
-P ${CURRENT_LIST_DIR}/CheckGit.cmake
BYPRODUCTS ${post_configure_file}
)

CheckGitVersion()
set(GIT_FIRMWARE_VERSION_TAG ${GIT_FIRMWARE_VERSION_TAG} PARENT_SCOPE)
endfunction()

# This is used to run this function from an external cmake process.
if (RUN_CHECK_GIT_VERSION)
CheckGitVersion()
endif ()
5 changes: 5 additions & 0 deletions firmware/cmake/git_version.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <string_view>

std::string_view GIT_HASH = "@GIT_HASH@";
std::string_view GIT_COMMIT_TIME = "@GIT_COMMIT_TIME@";
std::string_view GIT_FIRMWARE_VERSION_TAG = "@GIT_FIRMWARE_VERSION_TAG@";
6 changes: 6 additions & 0 deletions firmware/cmake/git_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
#include <string_view>

extern std::string_view GIT_HASH;
extern std::string_view GIT_COMMIT_TIME;
extern std::string_view GIT_FIRMWARE_VERSION_TAG;
67 changes: 67 additions & 0 deletions firmware/flashing/manifest_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

import argparse
import logging
import json
import os

ManifestFormatVersion = 1

def process_file(filename, imagetype, version):
entry = {}
entry["type"] = imagetype
entry["filename"] = os.path.basename(filename)
entry["filesize"] = os.stat(filename).st_size

#TODO md5
entry["md5"] = "123"

entry["version"] = {}
v = version.split(".")
if len(v) > 0:
entry["version"]["major"] = v[0]
if len(v) > 1:
entry["version"]["minor"] = v[1]
if len(v) > 2:
entry["version"]["revision"] = v[2]

return entry

def parse_file_version(version):
""" Convert a string like: firmware-v0.6.2-278-g52dfd038
Or: v1.2.3
Or: 2.1.5
Into: 0.6.2
"""
vsplit = version.split("-")
if len(vsplit) > 1:
v = vsplit[1]
else:
v = vsplit[0]

v = v.strip("v")
return v

if __name__ == "__main__":
parser = argparse.ArgumentParser("Generate firmware update manifest json file")
parser.add_argument("--app", dest="app_file", help="Input application uimg file")
parser.add_argument("--version", dest="version", help="Version string e.g. \"1.2.3\"")
parser.add_argument("out_file", help="Output json file")
parser.add_argument("-v", dest="verbose", help="Verbose logging", action="store_true")
args = parser.parse_args()

if args.verbose:
logging.basicConfig(level=logging.DEBUG)


j = {'version': ManifestFormatVersion}

version = parse_file_version(args.version)

j["files"] = []
j["files"].append(process_file(args.app_file, "app", version))

with open(args.out_file, "w+") as out_file:
data_json = json.dumps(j, indent=4)
out_file.write(data_json)

5 changes: 4 additions & 1 deletion firmware/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ add_executable(
${FWDIR}/src/fs/fatfs/fattime.cc
${FWDIR}/src/fs/time_convert.cc
${FWDIR}/src/flash_loader/flash_loader.cc

${FWDIR}/src/gui/elements/element_name.cc

#
Expand Down Expand Up @@ -218,6 +217,8 @@ add_executable(
#
${COMP_ARTWORK_SOURCES}
${FACEPLATE_ARTWORK_SOURCES}
#
${CMAKE_BINARY_DIR}/checkgit/git_version.cpp
)

if (ENABLE_WIFI_BRIDGE)
Expand Down Expand Up @@ -281,6 +282,8 @@ target_include_directories(
#
${FWDIR}/lib/jansson
${FWDIR}/lib/jansson/jansson/src
#
${CMAKE_BINARY_DIR}/checkgit
)

if (ENABLE_WIFI_BRIDGE)
Expand Down
4 changes: 4 additions & 0 deletions firmware/src/core_a7/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "debug.hh"
#include "drivers/cache.hh"
#include "fs/time_convert.hh"
#include "git_version.h"
#include "hsem_handler.hh"
#include "params.hh"
#include "patch_file/file_storage_proxy.hh"
Expand All @@ -14,6 +15,7 @@
#include "patch_play/patch_playloader.hh"
#include "system/time.hh"
#include "uart_log.hh"

// #include "core_intercom/semaphore_action.hh" //TODO use this

namespace MetaModule
Expand All @@ -33,6 +35,8 @@ void main() {

HAL_Delay(50);
print_time();
printf("Build: %s (%s)\n", GIT_HASH.data(), GIT_COMMIT_TIME.data());
printf("Version: %s\n", GIT_FIRMWARE_VERSION_TAG.data());

PatchPlayer patch_player;
FileStorageProxy file_storage_proxy{
Expand Down
23 changes: 23 additions & 0 deletions firmware/src/gui/pages/hardware_test_tab.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "gui/helpers/lv_helpers.hh"
#include "gui/pages/base.hh"
#include "gui/pages/page_list.hh"
#include "gui/slsexport/meta5/ui.h"
#include "gui/styles.hh"

namespace MetaModule
{

struct HardwareTestTab {

void prepare_focus(lv_group_t *group) {
this->group = group;
}

void update() {
}

private:
lv_group_t *group = nullptr;
};
} // namespace MetaModule
23 changes: 23 additions & 0 deletions firmware/src/gui/pages/prefs_tab.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "gui/helpers/lv_helpers.hh"
#include "gui/pages/base.hh"
#include "gui/pages/page_list.hh"
#include "gui/slsexport/meta5/ui.h"
#include "gui/styles.hh"

namespace MetaModule
{

struct PrefsTab {

void prepare_focus(lv_group_t *group) {
this->group = group;
}

void update() {
}

private:
lv_group_t *group = nullptr;
};
} // namespace MetaModule
Loading

0 comments on commit b498913

Please sign in to comment.