Skip to content

Commit

Permalink
Update the CMake build
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Jun 24, 2024
1 parent 4f59a50 commit bd5db7a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 60 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/WCH_V307_RISC-V.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ jobs:
fetch-depth: '0'
- name: update-tools
run: |
wget --no-check-certificate https://github.com/xpack-dev-tools/riscv-none-embed-gcc-xpack/releases/download/v10.2.0-1.2/xpack-riscv-none-embed-gcc-10.2.0-1.2-linux-x64.tar.gz
tar -xvzf xpack-riscv-none-embed-gcc-10.2.0-1.2-linux-x64.tar.gz
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-riscv-none-embed
run: |
PATH="${{ runner.workspace }}/riscv32-embecosm-ubuntu2204-gcc13.2.0/bin:$PATH"
echo 'query compiler version'
riscv32-unknown-elf-g++ -v
mkdir -p Output && cd Output
PATH=../xpack-riscv-none-embed-gcc-10.2.0-1.2/bin:$PATH
cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain-unix.cmake .. && make
ls -la ./BareMetal_WCH_V307_RISC-V.hex
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
97 changes: 44 additions & 53 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
cmake_minimum_required (VERSION 3.22)
project(BareMetal_WCH_V307_RISC-V LANGUAGES ASM C CXX)
#
# 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_C_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_VERBOSE_MAKEFILE ON) # Set it to ON to have verbose build output
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)

set(DEPLOY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(EXECUTABLE_NAME "${CMAKE_PROJECT_NAME}")
Expand All @@ -18,7 +19,7 @@ set(MAP_FILE "${DEPLOY_DIR}/${EXECUTABLE_NAME}.map")

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

# Listed flags
Expand Down Expand Up @@ -49,29 +50,29 @@ endif()

# C flags
set(C_FLAGS_LIST
${OPT_FLAGS_LIST}
${OPT_FLAG_LIST}
${ARCH_FLAGS_LIST}
-ffast-math
-g3
-x c
-std=c99
-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
${OPT_FLAGS_LIST}
${ARCH_FLAGS_LIST}
-ffast-math
-g3
-Wall
-Wextra
-fomit-frame-pointer
-gdwarf-2
${C_FLAGS_LIST}
-fno-exceptions
-fno-rtti
-fno-use-cxa-atexit
Expand All @@ -80,42 +81,22 @@ set(CXX_FLAGS_LIST
-fno-enforce-eh-specs
-ftemplate-depth=128
-Wzero-as-null-pointer-constant
-fsingle-precision-constant
)

# Assembler flags
set(ASM_FLAGS_LIST
${OPT_FLAGS_LIST}
${ARCH_FLAGS_LIST}
-ffast-math
-g3
-Wall
-Wextra
-fomit-frame-pointer
-gdwarf-2
-fno-exceptions
-x assembler
-fno-rtti
-fno-use-cxa-atexit
-fno-nonansi-builtins
-fno-threadsafe-statics
-fno-enforce-eh-specs
-ftemplate-depth=128
-Wzero-as-null-pointer-constant
-march=rv32imafc
-alh
)

# LD flags
set(LD_FLAGS_LIST
-nostartfiles
-nostdlib
${ARCH_FLAGS_LIST}
-ffast-math
-Wl,-T ${LD_SCRIPT}
-Wl,--print-memory-usage
-Wl,--print-map
-Wl,-dT ${LD_SCRIPT}
-Wl,-Map=${MAP_FILE}
--specs=nano.specs
--specs=nosys.specs
-Wl,-Map="${MAP_FILE}"
)

# Concatenate listed flags so that they can be parsed when assigned to CMAKE_<X>_FLAGS symbols
Expand All @@ -126,38 +107,48 @@ string(REPLACE ";" " " LD_FLAGS "${LD_FLAGS_LIST}")

# Bind toolchain defintions
set(CMAKE_ASM_COMPILER ${CPP})
set(CMAKE_C_COMPILER ${CPP})
set(CMAKE_C_COMPILER ${CC})
set(CMAKE_CXX_COMPILER ${CPP})
set(CMAKE_ASM_FLAGS "${ASM_FLAGS}")
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}/Appli/main.c
${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}/Appli
${SRC_DIR}/Mcal
${SRC_DIR}/Startup
)

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 ${SIZE} --format=berkeley ${DEPLOY_DIR}/${EXECUTABLE})
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${OBJDUMP} -d --visualize-jumps --wide ${DEPLOY_DIR}/${EXECUTABLE} > ${DEPLOY_DIR}/${EXECUTABLE_NAME}.list.text)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${OBJDUMP} -D --visualize-jumps --wide ${DEPLOY_DIR}/${EXECUTABLE} > ${DEPLOY_DIR}/${EXECUTABLE_NAME}.list.all)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${OBJDUMP} -d --visualize-jumps --wide --disassembler-options=numeric,no-aliases ${DEPLOY_DIR}/${EXECUTABLE} > ${DEPLOY_DIR}/${EXECUTABLE_NAME}.list.text.pure)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${OBJCOPY} -O ihex ${DEPLOY_DIR}/${EXECUTABLE} ${DEPLOY_DIR}/${EXECUTABLE_NAME}.hex)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${READELF} --wide -a ${DEPLOY_DIR}/${EXECUTABLE} > ${DEPLOY_DIR}/${EXECUTABLE_NAME}.readelf)
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("=====================================")
Expand Down
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
2 changes: 1 addition & 1 deletion cmake/toolchain-unix.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(TOOLCHAIN riscv-none-embed)
set(TOOLCHAIN riscv32-unknown-elf)
set(AR ${TOOLCHAIN}-ar)
set(AS ${TOOLCHAIN}-as)
set(CC ${TOOLCHAIN}-gcc)
Expand Down

0 comments on commit bd5db7a

Please sign in to comment.