Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
convert version generating scripts to python, generate automatically …
Browse files Browse the repository at this point in the history
…during build
  • Loading branch information
benedekkupper committed Dec 5, 2024
1 parent 2243fca commit 3e46ac2
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 6 deletions.
17 changes: 17 additions & 0 deletions scripts/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import subprocess
import os
import json

def exec(cmd):
return subprocess.check_output(cmd, shell=True).decode('utf-8').strip()

def get_git_info():
return {
'repo': exec('git remote get-url origin').replace('https://github.com/', '').replace('[email protected]:', '').replace('.git', ''),
'tag': exec('git tag --points-at HEAD') or exec('git rev-parse --short HEAD'),
'root': exec('git rev-parse --show-toplevel')
}

def read_package_json():
with open(os.path.join(os.path.dirname(__file__), 'package.json'), 'r') as f:
return json.load(f)
11 changes: 11 additions & 0 deletions scripts/generate_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3
import sys
from common import get_git_info, read_package_json
from generate_versions_utils import generate_versions

if __name__ == "__main__":
use_real_data = '--withMd5Sums' in sys.argv
use_zero_versions = '--withZeroVersions' in sys.argv
package_json = read_package_json()
git_info = get_git_info()
generate_versions(package_json, git_info, use_real_data, use_zero_versions)
64 changes: 64 additions & 0 deletions scripts/generate_versions_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import hashlib
import os
import json
import shutil

ZERO_MD5 = '00000000000000000000000000000000'

version_property_prefixes = ['firmware', 'deviceProtocol', 'moduleProtocol', 'userConfig', 'hardwareConfig', 'smartMacros']
patch_versions = ['Major', 'Minor', 'Patch']

def generate_versions(package_json, git_info, use_real_shas, use_zero_versions):
package_json = json.loads(json.dumps(package_json)) # Deep copy

if use_zero_versions:
git_info = {
'repo': '',
'tag': ''
}

version_variables = '\n'.join([
f"const version_t {prefix}Version = {{ {', '.join(['0' if use_zero_versions else package_json[f'{prefix}Version'].split('.')[i] for i in range(len(patch_versions))])} }};"
for prefix in version_property_prefixes
])

device_md5_sums = '\n'.join([
f' [{device["deviceId"]}] = "{ZERO_MD5 if not use_real_shas else calculate_md5_checksum_of_file(os.path.join(os.path.dirname(__file__), "..", device["source"]))}",'
for device in package_json['devices']
])

module_md5_sums = '\n'.join([
f' [{module["moduleId"]}] = "{ZERO_MD5 if not use_real_shas else calculate_md5_checksum_of_file(os.path.join(os.path.dirname(__file__), "..", module["source"]))}",'
for module in package_json['modules']
])

with open(os.path.join(os.path.dirname(__file__), '..', 'shared', 'versions.c'), 'w') as f:
f.write(f"""// Please do not edit this file by hand!
// It is to be regenerated by /scripts/generate_versions.py
#include "versioning.h"
{version_variables}
const char gitRepo[] = "{git_info['repo']}";
const char gitTag[] = "{git_info['tag']}";
#ifdef DEVICE_COUNT
const char *const DeviceMD5Checksums[DEVICE_COUNT + 1] = {{
{device_md5_sums}
}};
#endif
const char *const ModuleMD5Checksums[ModuleId_AllCount] = {{
{module_md5_sums}
}};
""")

return {
'devices': package_json['devices'],
'modules': package_json['modules']
}

def calculate_md5_checksum_of_file(file_path):
with open(file_path, 'rb') as f:
file_data = f.read()
return hashlib.md5(file_data).hexdigest()
21 changes: 15 additions & 6 deletions shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ target_include_directories(${PROJECT_NAME}-shared PUBLIC
${PROJECT_SOURCE_DIR}
)

if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/versions.c")
message(WARNING "Generating missing versions.c for UHK")
find_package(Python 3 REQUIRED)
if(Python_VERSION_MAJOR LESS 3)
message(FATAL_ERROR "Python 3 is required versions generation.")
endif()
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/versions.c"
COMMAND ${Python3_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/generate_versions.py"
DEPENDS "${PROJECT_SOURCE_DIR}/scripts/generate_versions.py"
)
endif()
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/versions.c PROPERTIES GENERATED TRUE)

target_sources(${PROJECT_NAME}-shared PRIVATE
bool_array_converter.c
buffer.c
crc16.c
fallback_versions.c
key_matrix.c
slave_protocol.c
versions.c
)

# add the versions.c source file conditionally
if(NOT NOVERSIONS)
target_sources(${PROJECT_NAME}-shared PRIVATE versions.c)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/versions.c PROPERTIES GENERATED TRUE)
endif()

0 comments on commit 3e46ac2

Please sign in to comment.