Skip to content

Commit

Permalink
Add build for k230_cli (#3)
Browse files Browse the repository at this point in the history
* Add k230_flash_cli build
* Bump to v0.0.3
  • Loading branch information
kendryte747 authored Oct 11, 2024
1 parent ae39ba3 commit 25147dd
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 18 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ name: Python-Build
on:
push:
tags: [ "v*" ]
branches: [ "dev", "test/*"]
pull_request:
branches: [ "main" ]

concurrency:
group: Python-Build-${{ github.ref }}
Expand Down Expand Up @@ -47,9 +44,9 @@ jobs:
if-no-files-found: error

pypi-publish:
name: Upload release to PyPI
name: Upload Artifacts to PyPI
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
# if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
needs: ["build-wheel"]
environment:
name: pypi
Expand Down
125 changes: 125 additions & 0 deletions .github/workflows/build_cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
name: Cli-Build

on:
push:
tags: [ "v*" ]
branches: [ "dev", "test/*"]
pull_request:
branches: [ "main" ]

concurrency:
group: Cli-Build-${{ github.ref }}
cancel-in-progress: true

jobs:
build-cli-windows:
name: build-cli-windows
runs-on: ubuntu-latest
concurrency:
group: ${{ github.repository }}-${{ github.workflow }}-Windows
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Get Git revision and set as environment variable
id: get_revision
run: |
revision=$(git describe --long --tag --dirty --always)
echo "REVISION=$revision" >> $GITHUB_ENV
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Pull Docker image
run: |
docker pull mstorsjo/llvm-mingw
- name: Build K230 Cli for Windows
run: |
rm -rf ${{ github.workspace }}/build && mkdir -p ${{ github.workspace }}/build
docker run --name llvm_mingw -v "${PWD}:/project:ro" mstorsjo/llvm-mingw /bin/bash -c 'cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/build/dist -DBUILD_WITH_MINGW=ON -DCMAKE_TOOLCHAIN_FILE=/project/src/kburn/cli/cmake/mingw-toolchain.cmake -S /project/src/kburn -B /build; cmake --build /build --config Release; cmake --install /build --prefix /build/dist'
docker cp llvm_mingw:/build/dist ${{ github.workspace }}/dist
docker rm llvm_mingw
ls -alh ${{ github.workspace }}/dist
- name: Compress artifacts
run: |
cd ${{ github.workspace }}/dist/bin
zip -r K230FlahsCli-Windows-${{ env.REVISION }}.zip *
- name: Upload k230_flash-cli Build Artifact
uses: actions/upload-artifact@v4
with:
name: "K230FlahsCli-Windows-${{ env.REVISION }}"
path: ${{ github.workspace }}/dist/bin/K230FlahsCli-Windows-${{ env.REVISION }}.zip
if-no-files-found: error

build-cli-liunx:
name: build-cli-liunx
runs-on: ubuntu-20.04
concurrency:
group: ${{ github.repository }}-${{ github.workflow }}-Liunx
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Get Git revision and set as environment variable
id: get_revision
run: |
revision=$(git describe --long --tag --dirty --always)
echo "REVISION=$revision" >> $GITHUB_ENV
- name: Install Depencies
run: |
sudo apt update && sudo apt install -y libudev-dev
- name: Build for Linux
run: |
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/build/dist -S ${{ github.workspace }}/src/kburn -B ${{ github.workspace }}/build
cmake --build ${{ github.workspace }}/build --config Release
cmake --install ${{ github.workspace }}/build --prefix ${{ github.workspace }}/build/dist
- name: Compress artifacts
run: |
cd ${{ github.workspace }}/build/dist
zip -r K230FlahsCli-Linux-${{ env.REVISION }}.zip *
- name: Upload k230_flash-cli Build Artifact
uses: actions/upload-artifact@v4
with:
name: "K230FlahsCli-Linux-${{ env.REVISION }}"
path: ${{ github.workspace }}/build/dist/K230FlahsCli-Linux-${{ env.REVISION }}.zip
if-no-files-found: error

release:
if: startsWith(github.ref, 'refs/tags/')
needs: [build-cli-windows, build-cli-liunx]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- uses: actions/download-artifact@v4
with:
path: ./release

- name: List Release Directory
run: ls -R ./release

- name: Upload Release Assets
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
generate_release_notes: true
tag_name: ${{ github.ref_name }}
files: |
./release/**/*.zip
7 changes: 0 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
cmake_minimum_required(VERSION 3.18)

set(K230_FLASH_VERSION_MAJOR 0)
set(K230_FLASH_VERSION_MINOR 0)
set(K230_FLASH_VERSION_PATCH 2)

set(K230_FLASH_VERSION_STRING ${K230_FLASH_VERSION_MAJOR}.${K230_FLASH_VERSION_MINOR}.${K230_FLASH_VERSION_PATCH})
message(STATUS "K230_FLASH_VERSION_STRING = ${K230_FLASH_VERSION_STRING}")

project(kburn_top)

# libkburn
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def run(self):
self.outfiles = self.distribution.data_files

def find_version():
with io.open("CMakeLists.txt", encoding="utf8") as f:
with io.open("src/kburn/CMakeLists.txt", encoding="utf8") as f:
version_file = f.read()

version_major = re.findall(r"K230_FLASH_VERSION_MAJOR (.+?)", version_file)
Expand Down
9 changes: 9 additions & 0 deletions src/kburn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ function(load_binary target variable src)
target_sources("${target}" PRIVATE "${tgt}")
endfunction()

set(K230_FLASH_VERSION_MAJOR 0)
set(K230_FLASH_VERSION_MINOR 0)
set(K230_FLASH_VERSION_PATCH 3)

set(K230_FLASH_VERSION_STRING ${K230_FLASH_VERSION_MAJOR}.${K230_FLASH_VERSION_MINOR}.${K230_FLASH_VERSION_PATCH})
message(STATUS "K230_FLASH_VERSION_STRING = ${K230_FLASH_VERSION_STRING}")

option(IS_CIBUILDWHEEL "Build in cibuildwheel docker" OFF)

if(DEFINED ENV{CI})
set(IS_CI TRUE)
else()
Expand Down
6 changes: 5 additions & 1 deletion src/kburn/burner_k230/burner_brom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ bool K230BROMBurner::get_loader(const char **loader, size_t *size) {
case KBURN_MEDIUM_SPI_NOR: {
loader_buffer = k230_loader_spi_nor;
loader_size = k230_loader_spi_nor_size;
}break;
} break;
case KBURN_MEDIUM_INVAILD: {
loader_buffer = NULL;
loader_size = 0;
} break;
}

*loader = loader_buffer;
Expand Down
51 changes: 51 additions & 0 deletions src/kburn/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.4...3.18)

option(BUILD_WITH_MINGW "Build in Dokcer MingW" OFF)

project(k230_flash)

add_executable(${PROJECT_NAME} k230_flash.cpp)
Expand All @@ -15,4 +17,53 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
add_subdirectory(CLI11)
target_link_libraries(${PROJECT_NAME} PRIVATE CLI11::CLI11)

if(APPLE)
# macOS-specific rpath settings
target_link_options(${PROJECT_NAME} PRIVATE
"-Wl,-rpath,@loader_path"
"-Wl,-rpath,@loader_path/../lib"
"-Wl,-rpath,@executable_path"
)
elseif(UNIX AND NOT APPLE)
# Linux-specific rpath settings
target_link_options(${PROJECT_NAME} PRIVATE
"-Wl,-rpath,$ORIGIN"
"-Wl,-rpath,$ORIGIN/../lib"
)
elseif(WIN32)

endif()

# Set RPATH for installed binaries (common for macOS and Linux)
if(NOT WIN32)
set_target_properties(${PROJECT_NAME} PROPERTIES
INSTALL_RPATH "$ORIGIN"
BUILD_WITH_INSTALL_RPATH TRUE
)
endif()

install(TARGETS ${PROJECT_NAME})

get_target_property(OUTPUT_EXE_NAME ${PROJECT_NAME} OUTPUT_NAME)

message(STATUS "CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR}")
message(STATUS "CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR}")

# this is just for ci build, so hard code the script path

if(WIN32 AND BUILD_WITH_MINGW)
install(CODE "
execute_process(
COMMAND ${CMAKE_COMMAND} -E echo \"Running copy_dlls.sh\"
COMMAND bash \"/project/src/kburn/cli/cmake/copy_dlls.sh\"
\"${OUTPUT_EXE_NAME}\" \"${TOOLCHAIN_ROOT}\" \"${CMAKE_INSTALL_PREFIX}\" \"${OBJDUMP_COMMAND}\"
RESULT_VARIABLE RESULT
COMMAND_ECHO STDOUT
COMMAND_ERROR_IS_FATAL ANY
)
if(NOT RESULT EQUAL 0)
message(FATAL_ERROR \"Failed to execute copy_dlls.sh.\")
endif()
")
endif()
44 changes: 44 additions & 0 deletions src/kburn/cli/cmake/copy_dlls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

set -e # Exit immediately if a command exits with a non-zero status.

OUTPUT_EXE_NAME="$1" # The first argument is the output executable name.
TOOLCHAIN_ROOT="$2" # The second argument is the toolchain root directory.
INSTALL_PREFIX="$3" # The third argument is the install prefix.
OBJDUMP_COMMAND="$4" # The fourth argument is the objdump command.

# Check if the required arguments are provided
if [ -z "$OUTPUT_EXE_NAME" ] || [ -z "$TOOLCHAIN_ROOT" ] || [ -z "$INSTALL_PREFIX" ] || [ -z "$OBJDUMP_COMMAND" ]; then
echo "Usage: $0 <output_exe_name> <toolchain_root> <install_prefix> <objdump_command>"
exit 1
fi

# Extract DLL names using objdump
DLL_NAMES=$($OBJDUMP_COMMAND -p "$INSTALL_PREFIX/bin/$OUTPUT_EXE_NAME.exe" | grep "DLL Name" | awk '{print $3}')

# Check if DLL_NAMES is empty
if [ -z "$DLL_NAMES" ]; then
echo "No DLL names found for $OUTPUT_EXE_NAME."
exit 1
fi

# Skip specific DLLs and copy the rest
for DLL in $DLL_NAMES; do
case "$DLL" in
"libunwind.dll" | "libc++.dll")
FOUND_DLL=$(find "$TOOLCHAIN_ROOT" -name "$DLL" 2>/dev/null)
if [ -n "$FOUND_DLL" ]; then
echo "Copying DLL: $FOUND_DLL to $INSTALL_PREFIX/bin"
cp "$FOUND_DLL" "$INSTALL_PREFIX/bin"
else
echo "Error: Required DLL not found: $DLL"
exit 1
fi
;;
*)
echo "Skipping DLL: $DLL"
;;
esac
done

echo "All required DLLs have been copied."
14 changes: 14 additions & 0 deletions src/kburn/cli/cmake/mingw-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# mingw-toolchain.cmake
set(CMAKE_SYSTEM_NAME Windows)

set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
set(OBJDUMP_COMMAND x86_64-w64-mingw32-objdump)

set(CMAKE_FIND_ROOT_PATH /opt/llvm-mingw)
set(TOOLCHAIN_ROOT /opt/llvm-mingw/x86_64-w64-mingw32/bin)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
6 changes: 2 additions & 4 deletions src/kburn/cli/k230_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ struct kburn_usb_dev_info poll_and_open_device(const std::string& path = "", boo
steady_clock::time_point progress_start_time;

KBurner::progress_fn_t progress = [](void* ctx, size_t iteration, size_t total) {
(void *)ctx;

if (iteration == 0) {
// Set the start time when iteration is 0
progress_start_time = steady_clock::now();
Expand Down Expand Up @@ -411,10 +409,10 @@ int main(int argc, char **argv) {
size_t file_size;
char *file_data = readFile(filename, file_size);

printf("Write %s to 0x%08X, Size: %zd.\n", filename.c_str(), address, file_size);
printf("Write %s to 0x%08lX, Size: %zd.\n", filename.c_str(), address, file_size);

if(false == uboot_burner->write(file_data, file_size, address)) {
printf("Write %s to 0x%08X failed.\n", filename.c_str(), address);
printf("Write %s to 0x%08lX failed.\n", filename.c_str(), address);

delete uboot_burner;
goto _exit;
Expand Down

0 comments on commit 25147dd

Please sign in to comment.