Skip to content

Commit 6567fed

Browse files
committed
[sw] Apply common CMake conventions
- Set the target compiler and flags in a toolchain file - Enable assembly language support - Simplify linking the common library
1 parent 4ea1060 commit 6567fed

File tree

9 files changed

+30
-29
lines changed

9 files changed

+30
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ to a verilator simulation model to be simulated on a PC.
396396
```
397397
mkdir sw/build
398398
pushd sw/build
399-
cmake ../
399+
cmake ..
400400
make
401401
popd
402402
```

sw/CMakeLists.txt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
cmake_minimum_required(VERSION 3.10)
2-
project(demo_system_sw)
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
4+
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/gcc_toolchain.cmake")
5+
endif()
6+
7+
project(demo_system_sw LANGUAGES C ASM)
8+
9+
if(CMAKE_BUILD_TYPE STREQUAL "")
10+
get_property(helpstring CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING)
11+
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "${helpstring}" FORCE)
12+
endif()
313

414
option(SIM_CTRL_OUTPUT
515
"Send string output to simulator control rather than UART")
@@ -8,19 +18,6 @@ if(SIM_CTRL_OUTPUT)
818
add_compile_definitions(SIM_CTRL_OUTPUT)
919
endif()
1020

11-
set(COMMON_DIR "${CMAKE_CURRENT_LIST_DIR}/common")
12-
set(LINKER_SCRIPT "${COMMON_DIR}/link.ld")
13-
set(CMAKE_C_COMPILER riscv32-unknown-elf-gcc)
14-
set(CMAKE_EXE_LINKER_FLAGS "-nostartfiles -T${LINKER_SCRIPT}")
15-
set(CMAKE_C_FLAGS "-march=rv32imc -mabi=ilp32 -static -mcmodel=medany -Wall -g \
16-
-fvisibility=hidden -ffreestanding")
17-
1821
add_subdirectory(common)
19-
20-
function(add_prog prog_name srcs)
21-
add_executable(${prog_name} ${srcs} $<TARGET_OBJECTS:common>)
22-
target_include_directories(${prog_name} PRIVATE $<TARGET_PROPERTY:common,INCLUDE_DIRECTORIES>)
23-
endfunction()
24-
2522
add_subdirectory(demo)
2623
add_subdirectory(blank)

sw/blank/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
add_executable(blank blank.S)
2-
set_property(SOURCE blank.S PROPERTY LANGUAGE C)
32

43
add_custom_command(
54
TARGET blank POST_BUILD
6-
COMMAND riscv32-unknown-elf-objcopy -O binary "$<TARGET_FILE:blank>" "$<TARGET_FILE:blank>.bin"
5+
COMMAND ${CMAKE_OBJCOPY} -O binary "$<TARGET_FILE:blank>" "$<TARGET_FILE:blank>.bin"
76
COMMAND srec_cat "$<TARGET_FILE:blank>.bin" -binary -offset 0x0000 -byte-swap 4 -o "$<TARGET_FILE:blank>.vmem" -vmem
87
VERBATIM)

sw/common/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
add_library(common OBJECT demo_system.c uart.c timer.c gpio.c pwm.c spi.c crt0.S)
2-
set_property(SOURCE crt0.S PROPERTY LANGUAGE C)
3-
target_include_directories(common PUBLIC "${CMAKE_CURRENT_LIST_DIR}")
2+
target_include_directories(common INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")

sw/demo/basic-passwdcheck/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/newae/simpleseri
66
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../common)
77

88

9-
add_prog(basic-passwdcheck ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/newae/basic-passwdcheck/basic-passwdcheck.c)
9+
add_executable(basic-passwdcheck ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/newae/basic-passwdcheck/basic-passwdcheck.c)
1010

11-
target_link_libraries(basic-passwdcheck simpleserial)
11+
target_link_libraries(basic-passwdcheck common simpleserial)
1212

sw/demo/hello_world/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
add_prog(demo main.c)
1+
add_executable(demo main.c)
2+
target_link_libraries(demo common)

sw/demo/lcd_st7735/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/display_drivers/core/lucida_console_
66
${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/display_drivers/st7735/lcd_st7735.c
77
)
88

9-
string(APPEND CMAKE_C_FLAGS " -O2")
10-
119
# add_executable(lcd_st7735 main.c)
12-
add_prog(lcd_st7735 "main.c;lcd.c;fractal_fixed.c;fractal_float.c;fractal_palette.c")
10+
add_executable(lcd_st7735 main.c lcd.c fractal_fixed.c fractal_float.c fractal_palette.c)
1311

1412
# pull in core dependencies and additional i2c hardware support
15-
target_link_libraries(lcd_st7735 lcd_st7735_lib)
13+
target_link_libraries(lcd_st7735 common lcd_st7735_lib)
1614

1715
target_include_directories(lcd_st7735 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/display_drivers)

sw/demo/simpleserial-aes/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/newae/crypto/aes-independant.c
1616
${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/newae/crypto/tiny-AES128-C/aes.c
1717
)
1818

19-
add_prog(simpleserial-aes ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/newae/simpleserial-aes/simpleserial-aes.c)
19+
add_executable(simpleserial-aes ${CMAKE_CURRENT_SOURCE_DIR}/../../../vendor/newae/simpleserial-aes/simpleserial-aes.c)
2020

21-
target_link_libraries(simpleserial-aes simpleserial tiny-AES128)
21+
target_link_libraries(simpleserial-aes common simpleserial tiny-AES128)
2222

sw/gcc_toolchain.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set(LINKER_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/common/link.ld")
2+
set(CMAKE_SYSTEM_NAME Generic)
3+
set(CMAKE_C_COMPILER riscv32-unknown-elf-gcc)
4+
set(CMAKE_C_FLAGS_INIT
5+
"-march=rv32imc -mabi=ilp32 -mcmodel=medany -Wall -fvisibility=hidden -ffreestanding")
6+
set(CMAKE_ASM_FLAGS_INIT "-march=rv32imc")
7+
set(CMAKE_EXE_LINKER_FLAGS_INIT "-nostartfiles -T \"${LINKER_SCRIPT}\"")

0 commit comments

Comments
 (0)