CMake has been implemented to manage ARM projects.

Add the mkversion bat and ps1 implementation to the Windows platform compilation.
Make armlib a static library so that bootrom and armsrc can be reused.
This commit is contained in:
DXL authored and Philippe Teuwen committed 2026-08-19 15:51:50 +02:00
1 parent 355d58b1cb
commit 0191fb78ec
15 files changed
+1936

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
# ----------------------------------------------------------
# Fix error when 'compile a simple test program.'
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
# ----------------------------------------------------------
+46
View File
@@ -0,0 +1,46 @@
if (NOT DEFINED PLATFORM)
message(FATAL_ERROR "Include this module after PLATFORM check.")
endif ()
#[[
Make All variables private(Exclude EXE)!!!
]]
# For get_exe_suffix function to detect the suffix of executable for current compiler, like '.exe' for Windows.
include(${CMAKE_CURRENT_LIST_DIR}/GetExeSuffixByCompiler.cmake)
set(_fc_MYINCLUDES -I${CMAKE_CURRENT_LIST_DIR}/../common_fpga)
set(_fc_MYCFLAGS -std=c99 -D_ISOC99_SOURCE)
if (PLATFORM STREQUAL PM3ICOPYX)
set(_fc_MYDEFS -DXC3)
else ()
set(_fc_MYDEFS)
endif ()
set(_fc_MYINCLUDES ${_fc_MYINCLUDES} -I${CMAKE_CURRENT_LIST_DIR}/../common/lz4)
set(_fc_MYCFLAGS ${_fc_MYCFLAGS} -DLZ4_MEMORY_USAGE=20 -Wno-redundant-decls -Wno-old-style-definition -Wno-missing-prototypes -Wno-missing-declarations)
set(_fc_MYSRCS ${_fc_MYSRCS}
${CMAKE_CURRENT_LIST_DIR}/fpga_compress/fpga_compress.c
${CMAKE_CURRENT_LIST_DIR}/../common/lz4/lz4hc.c
${CMAKE_CURRENT_LIST_DIR}/../common/lz4/lz4.c)
# The target name for fpga_compress
set(FPGA_COMPRESS fpga_compress)
get_exe_suffix(FPGA_COMPRESS_EXE_SUFFIX ${C_COMPILER_HOST})
get_filename_component(FPGA_COMPRESS_EXE "${CMAKE_CURRENT_LIST_DIR}/fpga_compress/${FPGA_COMPRESS}${FPGA_COMPRESS_EXE_SUFFIX}" ABSOLUTE) # executable filename + suffix
add_custom_command(OUTPUT ${FPGA_COMPRESS_EXE}
# COMMAND ${CMAKE_COMMAND} -E echo "[=] CC ${FPGA_COMPRESS} executable"
COMMAND ${C_COMPILER_HOST}
${_fc_MYCFLAGS}
${_fc_MYDEFS}
${_fc_MYINCLUDES}
${_fc_MYSRCS}
-o ${FPGA_COMPRESS_EXE}
DEPENDS ${_fc_MYSRCS} # if src update, this custom command will run. tips: only .c files watch now, you can add .h files watch if need.
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/fpga_compress
COMMENT "Build 'fpga_compress' executable for next step"
VERBATIM)
# add_custom_target always run if project build, but add_custom_command can cached output if no files update.
add_custom_target(${FPGA_COMPRESS} DEPENDS ${FPGA_COMPRESS_EXE})
+24
View File
@@ -0,0 +1,24 @@
function(get_exe_suffix OUTPUT_VAR GCC_COMPILER)
set(TEST_SRC ${CMAKE_CURRENT_BINARY_DIR}/detect_suffix.c)
set(TEST_EXE ${CMAKE_CURRENT_BINARY_DIR}/detect_suffix)
file(WRITE ${TEST_SRC} "int main(){return 0;}\n")
execute_process(
COMMAND ${GCC_COMPILER} ${TEST_SRC} -o ${TEST_EXE}
RESULT_VARIABLE RES
)
if (EXISTS "${TEST_EXE}.exe")
set(${OUTPUT_VAR} ".exe" PARENT_SCOPE)
else ()
set(${OUTPUT_VAR} "" PARENT_SCOPE)
endif ()
# Clear
file(REMOVE ${TEST_SRC} ${TEST_EXE} ${TEST_EXE}.exe ${TEST_EXE}.o)
endfunction()
# Usage:
# get_exe_suffix(${C_COMPILER_HOST} DETECTED_SUFFIX)
# message(STATUS "Detected suffix: '${DETECTED_SUFFIX}'")
+109
View File
@@ -0,0 +1,109 @@
# ------------------------------------------------------------------------
# Power by DXL
#
# Detect platform for run mkversion script
#
# Injection variable:
# MKVERSION_SCRIPT_FOUND - Is script found?
# MKVERSION_SCRIPT - The script full path
# MKVERSION_SCRIPT_TYPE - Script file type: "bat" or "sh"
# VERSION_INFO - Version info from mkversion.(bat|sh) --short
# MKVERSION_AVAILABLE - A boolean variable, tell me is script working and VERSION_INFO set.
# MKVERSION_CMD - The final command used to generate the source file version_pm3.c
# Only once run on setup(MKVERSION_AVAILABLE is a cached variable. Next reload will direct set and skip.)
if (DEFINED MKVERSION_AVAILABLE)
return()
endif ()
# Setup variable and export
set(MKVERSION_SCRIPT_FOUND FALSE)
set(MKVERSION_SCRIPT "")
set(MKVERSION_SCRIPT_TYPE "")
set(VERSION_INFO "unknown")
set(MKVERSION_AVAILABLE FALSE)
# The script for platform
set(MKVERSION_CANDIDATES_BAT "${CMAKE_CURRENT_LIST_DIR}/mkversion.bat")
set(MKVERSION_CANDIDATES_SH "${CMAKE_CURRENT_LIST_DIR}/mkversion.sh")
# We are running on the Windows?
set(MKVERSION_RUN_NATIVE_WINDOWS FALSE)
execute_process(COMMAND uname OUTPUT_VARIABLE uname)
if (uname MATCHES "^MSYS" OR uname MATCHES "^MINGW" OR uname MATCHES "^Lin" OR uname MATCHES "^Darwin")
message(STATUS "Not running on native Windows (uname is ${uname}).")
else ()
set(MKVERSION_RUN_NATIVE_WINDOWS TRUE)
endif ()
# First setup, we can run once '--short' for mkversion script to get like 'Iceman/master/b36b61feb-dirty'
if (MKVERSION_RUN_NATIVE_WINDOWS)
execute_process(
COMMAND cmd /c call "${MKVERSION_CANDIDATES_BAT}" --short
OUTPUT_VARIABLE _output
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
RESULT_VARIABLE _result
)
if (${_result} EQUAL 0)
set(MKVERSION_SCRIPT "${MKVERSION_CANDIDATES_BAT}")
set(MKVERSION_SCRIPT_TYPE "bat")
set(VERSION_INFO "${_output}")
set(MKVERSION_SCRIPT_FOUND TRUE)
endif ()
else ()
# Is 'sh' available?
find_program(SH_COMMAND sh)
if (SH_COMMAND)
execute_process(
COMMAND "${SH_COMMAND}" "${MKVERSION_CANDIDATES_SH}" --short
OUTPUT_VARIABLE _output
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
RESULT_VARIABLE _result
)
if (${_result} EQUAL 0)
set(MKVERSION_SCRIPT "${MKVERSION_CANDIDATES_SH}")
set(MKVERSION_SCRIPT_TYPE "sh")
set(VERSION_INFO "${_output}")
set(MKVERSION_SCRIPT_FOUND TRUE)
endif ()
endif ()
endif ()
# Finally, the script found and version info from '--short' available?
if (MKVERSION_SCRIPT_FOUND AND NOT "${VERSION_INFO}" STREQUAL "")
set(MKVERSION_AVAILABLE TRUE)
else ()
set(MKVERSION_AVAILABLE FALSE)
endif ()
# Export finally cmd for generate version_pm3.c
if (MKVERSION_SCRIPT_TYPE STREQUAL "sh") # What's shell type we are?
set(MKVERSION_CMD ${CMAKE_COMMAND} -E env bash ${MKVERSION_SCRIPT}) # Unix/Linux/macOS: call sh
elseif (MKVERSION_SCRIPT_TYPE STREQUAL "bat")
set(MKVERSION_CMD cmd /c ${MKVERSION_SCRIPT}) # Windows: call bat
else ()
message(FATAL_ERROR "MKVERSION_SCRIPT_TYPE must be 'sh' or 'bat', but got '${MKVERSION_SCRIPT_TYPE}'")
endif ()
# Set variables to cached level, next reload CmakeLists.txt will skip run this subscript(MKVersionScript.cmake).
set(MKVERSION_SCRIPT_FOUND ${MKVERSION_SCRIPT_FOUND} CACHE INTERNAL "Whether a mkversion script was found")
set(MKVERSION_SCRIPT ${MKVERSION_SCRIPT} CACHE INTERNAL "Full path to the mkversion script")
set(MKVERSION_SCRIPT_TYPE ${MKVERSION_SCRIPT_TYPE} CACHE INTERNAL "Type of script: 'bat' or 'sh'")
set(VERSION_INFO ${VERSION_INFO} CACHE INTERNAL "Version info string from --short")
set(MKVERSION_AVAILABLE ${MKVERSION_AVAILABLE} CACHE INTERNAL "Whether version info is available")
set(MKVERSION_CMD ${MKVERSION_CMD} CACHE INTERNAL "Command to generate version info")
# Debug info(Optional, can enable when debuging...)
if (FALSE)
message(STATUS "===================================================================")
message(STATUS "MKVERSION_SCRIPT_FOUND: ${MKVERSION_SCRIPT_FOUND}")
message(STATUS "MKVERSION_SCRIPT: ${MKVERSION_SCRIPT}")
message(STATUS "MKVERSION_SCRIPT_TYPE: ${MKVERSION_SCRIPT_TYPE}")
message(STATUS "VERSION_INFO: ${VERSION_INFO}")
message(STATUS "MKVERSION_CMD: ${MKVERSION_CMD}")
message(STATUS "===================================================================")
endif ()
+23
View File
@@ -0,0 +1,23 @@
# If it has already been found, there is no need to search again.
if(NOT DEFINED CMAKE_C_COMPILER)
set(TOOLCHAIN_PREFIX arm-none-eabi-)
# 1. Find the compiler
find_program(ARM_GCC_COMPILER ${TOOLCHAIN_PREFIX}gcc)
find_program(ARM_GXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
find_program(ARM_ASM_COMPILER ${TOOLCHAIN_PREFIX}gcc)
# 2. Check if the core C compiler is found
if(ARM_GCC_COMPILER)
message(STATUS "Found ARM GCC: ${ARM_GCC_COMPILER}")
# Set the found absolute paths (with or without .exe) directly to CMake
set(CMAKE_C_COMPILER ${ARM_GCC_COMPILER} CACHE PATH "C compiler")
set(CMAKE_CXX_COMPILER ${ARM_GXX_COMPILER} CACHE PATH "CXX compiler")
set(CMAKE_ASM_COMPILER ${ARM_ASM_COMPILER} CACHE PATH "ASM compiler")
else()
message(WARNING "ARM GCC (${TOOLCHAIN_PREFIX}gcc) not found in PATH!")
endif()
endif ()
+20
View File
@@ -0,0 +1,20 @@
@echo off
setlocal
:: Power by DXL
:: The mkversion.ps1 wrapper. why not mkversion.bat implement all function?
:: > Not good idea to call sha256 for arm src, so you known..
:: Did not output localization
set LC_ALL=C
set LANG=C
:: Where are this script?
set "SCRIPT_DIR=%~dp0"
:: Call PowerShell script, and send params
powershell -ExecutionPolicy Bypass -File "%SCRIPT_DIR%mkversion.ps1" %*
if %ERRORLEVEL% NEQ 0 (
exit /b %ERRORLEVEL%
)
+191
View File
@@ -0,0 +1,191 @@
<#
Power By DXL
The mkversion.sh same functions.
#>
param(
[string]$Destination,
[switch]$Short,
[switch]$Force,
[switch]$Undecided
)
# 默认 fork 名称(可自定义)
$fullgitinfo = "Iceman"
$clean = 2 # 2 = undecided, 1 = clean, 0 = dirty
# 获取脚本所在目录
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$pm3Root = Join-Path $ScriptDir ".."
# 检查是否在 Git 工作目录中
$inGit = $false
$gitDir = Join-Path $pm3Root ".git"
if (Test-Path $gitDir -PathType Container) {
# 尝试执行 git 命令
$result = $null
try {
$result = git -C $pm3Root rev-parse --is-inside-work-tree 2>$null
} catch { }
if ($result -eq "true") {
$inGit = $true
}
}
if ($inGit) {
try {
$gitVersion = (git -C $pm3Root describe --dirty --always).Trim()
$gitBranch = (git -C $pm3Root rev-parse --abbrev-ref HEAD).Trim()
if ($Undecided) {
if ($gitVersion -like "*-dirty") {
$clean = 0
} else {
$clean = 1
}
}
$fullgitinfo = "$fullgitinfo/$gitBranch/$gitVersion"
# 使用 FORCED_DATE 或当前时间
if ($env:FORCED_DATE -match '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$') {
$ctime = $env:FORCED_DATE
} else {
$ctime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
} catch {
$fullgitinfo = "$fullgitinfo/master/release (git_error)"
$ctime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
} else {
$fullgitinfo = "$fullgitinfo/master/release (no_git)"
$readmePath = Join-Path $pm3Root "README.md"
if (Test-Path $readmePath) {
$dl_time = (Get-Item $readmePath).LastWriteTime
$ctime = $dl_time.ToString("yyyy-MM-dd HH:mm:ss")
} else {
$ctime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}
# --short 模式:只输出版本字符串
if ($Short) {
if ($fullgitinfo.Length -gt 49) {
$fullgitinfo = $fullgitinfo.Substring(0, 46) + "..."
}
Write-Output $fullgitinfo
exit 0
}
# 检查是否提供了输出文件路径
if (-not $Destination) {
Write-Error "Error: missing destination filename"
exit 1
}
# 计算 ARM 源码的 SHA256 哈希(前 9 位)
$armSrc = Join-Path $pm3Root "armsrc"
$commonArm = Join-Path $pm3Root "common_arm"
$files = @()
if (Test-Path $armSrc) {
$files += Get-ChildItem $armSrc | Where-Object {
($_.Extension -eq ".c" -or $_.Extension -eq ".h") -and
$_.Name -notmatch "^(disabled|version_pm3|fpga_version_info)"
}
}
if (Test-Path $commonArm) {
$files += Get-ChildItem $commonArm | Where-Object {
($_.Extension -eq ".c" -or $_.Extension -eq ".h") -and
$_.Name -notmatch "^(disabled|version_pm3|fpga_version_info)"
}
}
$sha = "no sha256"
if ($files.Count -gt 0) {
# 按文件名排序(确保一致性)
$sortedFiles = $files | Sort-Object Name
# 创建 SHA256 实例
$sha256 = New-Object System.Security.Cryptography.SHA256Managed
foreach ($file in $sortedFiles) {
try {
$content = [System.IO.File]::ReadAllBytes($file.FullName)
$sha256.TransformBlock($content, 0, $content.Length, $null, 0) | Out-Null
} catch {
Write-Warning "Failed to read file: $($file.FullName)"
}
}
# 完成哈希计算
$sha256.TransformFinalBlock([byte[]]@(), 0, 0)
$hashBytes = $sha256.Hash
$shaHex = -join ($hashBytes | ForEach-Object { $_.ToString("x2") })
$sha = $shaHex.Substring(0, 9)
}
# 截断 fullgitinfo 到最多 49 字符
if ($fullgitinfo.Length -gt 49) {
$fullgitinfo = $fullgitinfo.Substring(0, 46) + "..."
}
# 检查是否需要重新生成文件(避免无意义更新)
$regenerate = $true
if (-not $Force -and (Test-Path $Destination)) {
try {
$content = Get-Content $Destination -Raw
# 提取 clean 状态(第13行附近)
if ($content -match '^\s*(\d+)\s*$' -and $matches[1] -eq $clean) {
# 提取 fullgitinfo(第14行)
if ($content -match '^\s*"([^"]+)"\s*$' -and $matches[1] -eq $fullgitinfo) {
# 提取 sha(第16行)
if ($content -match '^\s*"([^"]+)"\s*$' -and $matches[1] -eq $sha) {
$regenerate = $false
}
}
}
} catch {
$regenerate = $true
}
}
# 仅当需要时才写入文件
if ($regenerate) {
$tmpFile = "$Destination.tmp"
$output = @"
#include "common.h"
/* Generated file, do not edit */
#ifndef ON_DEVICE
#define SECTVERSINFO
#else
#define SECTVERSINFO __attribute__((section(".version_information")))
#endif
const struct version_information_t SECTVERSINFO g_version_information = {
VERSION_INFORMATION_MAGIC,
1,
1,
$clean,
"$fullgitinfo",
"$ctime",
"$sha"
};
"@
try {
$output | Out-File -FilePath $tmpFile -Encoding ASCII -Force
Move-Item -Path $tmpFile -Destination $Destination -Force
} catch {
Write-Error "Failed to write version file: $_"
if (Test-Path $tmpFile) {
Remove-Item $tmpFile -Force
}
exit 1
}
}
exit 0