Skip to content

Commit

Permalink
Merge pull request #18 from Embedded-System-Lovers/add-cmake-support
Browse files Browse the repository at this point in the history
Fixes #3 via add cmake support
  • Loading branch information
ckormanyos authored Jun 24, 2024
2 parents 9467652 + 55804a7 commit c1d22f1
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 3 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/WCH_V307_RISC-V.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ jobs:
bash ./Rebuild.sh
ls -la ../Output/BareMetal_WCH_V307_RISC-V.hex
working-directory: ./Build
target-gcc-riscv32-unknown-elf-cmake:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
with:
fetch-depth: '0'
- name: update-tools
run: |
wget --no-check-certificate https://buildbot.embecosm.com/job/riscv32-gcc-ubuntu2204-release/10/artifact/riscv32-embecosm-ubuntu2204-gcc13.2.0.tar.gz
tar -xzf riscv32-embecosm-ubuntu2204-gcc13.2.0.tar.gz -C ${{ runner.workspace }}
- name: target-gcc-riscv32-unknown-elf-cmake
run: |
mkdir -p Output && cd Output
PATH="${{ runner.workspace }}/riscv32-embecosm-ubuntu2204-gcc13.2.0/bin:$PATH"
echo 'query compiler version'
riscv32-unknown-elf-g++ -v
cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain-unix.cmake .. && make
ls -la ./BareMetal_WCH_V307_RISC-V.hex
target-gcc-riscv32-unknown-elf-macos:
runs-on: macos-latest
defaults:
Expand Down
2 changes: 1 addition & 1 deletion Build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ OPT_MODIFIED_O2 = -O2 \

NO_OPT = -O0

OPT = $(NO_OPT)
OPT = $(OPT_MODIFIED_O2)

############################################################################################
# GCC Compiler verbose flags
Expand Down
157 changes: 157 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#
# Root CMakeLists.txt
#
cmake_minimum_required (VERSION 3.15)
project(BareMetal_WCH_V307_RISC-V ASM C CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)

set(DEPLOY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(EXECUTABLE_NAME "${CMAKE_PROJECT_NAME}")
set(EXECUTABLE_FORMAT elf)
set(EXECUTABLE "${EXECUTABLE_NAME}.${EXECUTABLE_FORMAT}")
set(SRC_DIR "${CMAKE_SOURCE_DIR}/Code")
set(LD_SCRIPT "${SRC_DIR}/Memory_Map.ld")
set(MAP_FILE "${DEPLOY_DIR}/${EXECUTABLE_NAME}.map")

# Set default build type if not defined
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

# Listed flags
# Architecture
set(ARCH_FLAGS_LIST
-march=rv32imafc
-mabi=ilp32f
-msmall-data-limit=0
-falign-functions=4
)

# Optimization
set(OPT_RELEASE_FLAGS_LIST
-O2
-fno-reorder-blocks-and-partition
-fno-reorder-functions
)

set(OPT_DEBUG_FLAGS_LIST
-O0
)

if(CMAKE_BUILD_TYPE MATCHES Release)
set(OPT_FLAGS_LIST ${OPT_RELEASE_FLAGS_LIST})
else()
set(OPT_FLAGS_LIST ${OPT_DEBUG_FLAGS_LIST})
endif()

# C flags
set(C_FLAGS_LIST
${OPT_FLAG_LIST}
${ARCH_FLAGS_LIST}
-ffast-math
-g3
-Wconversion
-Wsign-conversion
-Wunused-parameter
-Wuninitialized
-Wmissing-declarations
-Wshadow
-Wunreachable-code
-Wmissing-include-dirs
-Wall
-Wextra
-fomit-frame-pointer
-gdwarf-2
-fomit-frame-pointer
-fsingle-precision-constant
)

# C++ flags
set(CXX_FLAGS_LIST
${C_FLAGS_LIST}
-fno-exceptions
-fno-rtti
-fno-use-cxa-atexit
-fno-nonansi-builtins
-fno-threadsafe-statics
-fno-enforce-eh-specs
-ftemplate-depth=128
-Wzero-as-null-pointer-constant
)

# Assembler flags
set(ASM_FLAGS_LIST
-march=rv32imafc
-alh
)

# LD flags
set(LD_FLAGS_LIST
-nostartfiles
-nostdlib
-Wl,-T ${LD_SCRIPT}
-Wl,--print-memory-usage
-Wl,--print-map
-Wl,-Map="${MAP_FILE}"
)

# Concatenate listed flags so that they can be parsed when assigned to CMAKE_<X>_FLAGS symbols
string(REPLACE ";" " " C_FLAGS "${C_FLAGS_LIST}")
string(REPLACE ";" " " CXX_FLAGS "${CXX_FLAGS_LIST}")
string(REPLACE ";" " " ASM_FLAGS "${ASM_FLAGS_LIST}")
string(REPLACE ";" " " LD_FLAGS "${LD_FLAGS_LIST}")

# Bind toolchain defintions
set(CMAKE_ASM_COMPILER ${CPP})
set(CMAKE_C_COMPILER ${CC})
set(CMAKE_CXX_COMPILER ${CPP})
set(CMAKE_ASM_FLAGS "${CXX_FLAGS}")
set(CMAKE_C_FLAGS "${C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${LD_FLAGS}")
set(CMAKE_VERBOSE_MAKEFILE OFF) # Set it to ON to have verbose build output

# Construct executable
add_executable(${EXECUTABLE}
${SRC_DIR}/Mcal/Mcu.c
${SRC_DIR}/Mcal/Port.c
${SRC_DIR}/Mcal/SysTick.c
${SRC_DIR}/Startup/boot.s
${SRC_DIR}/Startup/intvect.c
${SRC_DIR}/Startup/Startup.c
${SRC_DIR}/Appli/main.c
)

target_include_directories(${EXECUTABLE}
PRIVATE
${SRC_DIR}
${SRC_DIR}/Mcal
)

target_link_libraries(${EXECUTABLE}
PRIVATE
)

target_link_directories(${EXECUTABLE}
PRIVATE
)

target_link_options(${EXECUTABLE}
PRIVATE
)

# Post build commands
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${OBJCOPY} -Oihex ${DEPLOY_DIR}/${EXECUTABLE} ${DEPLOY_DIR}/${EXECUTABLE_NAME}.hex)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${OBJCOPY} -Obinary ${DEPLOY_DIR}/${EXECUTABLE} ${DEPLOY_DIR}/${EXECUTABLE_NAME}.bin)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${OBJDUMP} -d ${DEPLOY_DIR}/${EXECUTABLE} > ${DEPLOY_DIR}/${EXECUTABLE_NAME}.disa)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${SIZE} --format=berkeley ${DEPLOY_DIR}/${EXECUTABLE})

# Print build info
message("=====================================")
message("Project: ${CMAKE_PROJECT_NAME}")
message("Build type: ${CMAKE_BUILD_TYPE}")
message("=====================================")
3 changes: 2 additions & 1 deletion Code/Mcal/Mcu.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
//=========================================================================================
// Includes
//=========================================================================================
#include "CH32V30xxx.h"
#include <CH32V30xxx.h>
#include <Mcu.h>


//-----------------------------------------------------------------------------------------
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ in order to observe the blinky toggle (see the orange wire).

Build on `*nix*` is easy using `gcc-riscv32-unknown-elf`

Both Make and Cmake can be used to build the Application:
Both classic GNU-Make as well as CMake can be used to build the Application:

```sh
cd WCH_V307_RISC-V/Build
Expand All @@ -60,6 +60,12 @@ cd WCH_V307_RISC-V/Build
./Rebuild.sh
```

### CMake
```sh
mkdir Output && cd Output
cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain-unix.cmake .. && make
```

The build results including ELF-file, HEX-mask, MAP-file
and assembly list file are created in the `Output` directory.

Expand Down
14 changes: 14 additions & 0 deletions cmake/toolchain-unix.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(TOOLCHAIN riscv32-unknown-elf)
set(AR ${TOOLCHAIN}-ar)
set(AS ${TOOLCHAIN}-as)
set(CC ${TOOLCHAIN}-gcc)
set(CPP ${TOOLCHAIN}-g++)
set(LD ${TOOLCHAIN}-ld)
set(GDB ${TOOLCHAIN}-gdb)
set(OBJCOPY ${TOOLCHAIN}-objcopy)
set(OBJDUMP ${TOOLCHAIN}-objdump)
set(SIZE ${TOOLCHAIN}-size)
set(READELF ${TOOLCHAIN}-readelf)

0 comments on commit c1d22f1

Please sign in to comment.