-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
167 lines (141 loc) Β· 5.9 KB
/
Copy pathCMakeLists.txt
File metadata and controls
167 lines (141 loc) Β· 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
cmake_minimum_required(VERSION 3.20)
# ==============================================================================
# Toolchain & Project Setup
# ==============================================================================
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(TC_PREFIX arm-none-eabi-)
set(CMAKE_C_COMPILER ${TC_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${TC_PREFIX}g++)
set(CMAKE_ASM_COMPILER ${TC_PREFIX}gcc)
set(CMAKE_OBJCOPY ${TC_PREFIX}objcopy)
set(CMAKE_SIZE ${TC_PREFIX}size)
project(mos-renode C CXX ASM)
# ==============================================================================
# Simplified Submodule Automation (Nested Support)
# ==============================================================================
find_package(Git QUIET)
# Define key paths
set(CORE_DIR "${CMAKE_SOURCE_DIR}/core")
set(ETL_DIR "${CORE_DIR}/external/etl")
if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
# Check if 'core' is populated OR if 'etl' inside core is populated
if(NOT EXISTS "${CORE_DIR}/kernel/printf.c" OR NOT EXISTS "${ETL_DIR}/CMakeLists.txt")
message(STATUS "Submodules missing. Initializing recursively...")
# Standard initialization: Pull the specific commit recorded in the superproject
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE git_res
)
# Update to Master: If initialization succeeded, force 'core' to latest master
if(git_res EQUAL 0)
message(STATUS "Switching 'core' submodule to latest master branch...")
# Switch 'core' from detached HEAD to master branch
execute_process(
COMMAND ${GIT_EXECUTABLE} checkout master
WORKING_DIRECTORY ${CORE_DIR}
)
# Pull the latest changes from remote master
execute_process(
COMMAND ${GIT_EXECUTABLE} pull origin master
WORKING_DIRECTORY ${CORE_DIR}
)
# Update nested submodules inside 'core' (in case master has new dependencies)
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CORE_DIR}
)
endif()
# Fallback: If 'etl' was never added to 'core' as a submodule, add it now
if(EXISTS "${CORE_DIR}" AND NOT EXISTS "${ETL_DIR}/CMakeLists.txt")
message(STATUS "Adding ETL as nested submodule in core...")
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule add -f https://github.com/ETLCPP/etl.git external/etl
WORKING_DIRECTORY ${CORE_DIR}
)
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()
endif()
endif()
# Use EXCLUDE_FROM_ALL to prevent building ETL tests or examples
add_subdirectory(${ETL_DIR} EXCLUDE_FROM_ALL)
# ==============================================================================
# Target Configuration
# ==============================================================================
set(TARGET_NAME mos-renode)
# CONFIGURE_DEPENDS: Automatically re-scan for new files during build, no manual refresh needed
file(GLOB_RECURSE DRIVER_SOURCES CONFIGURE_DEPENDS
"vendor/Core/Src/*.c"
"vendor/Drivers/STM32F4xx_HAL_Driver/Src/*.c"
)
add_executable(${TARGET_NAME}.elf
core/kernel/printf.c
core/kernel/syscall.c
app/main.cpp
vendor/startup_stm32f407xx.s
${DRIVER_SOURCES}
)
# ==============================================================================
# Build Options
# ==============================================================================
target_compile_features(${TARGET_NAME}.elf PRIVATE c_std_17 cxx_std_23)
target_compile_definitions(${TARGET_NAME}.elf PRIVATE STM32F407xx USE_FULL_LL_DRIVER USE_HAL_DRIVER)
target_include_directories(${TARGET_NAME}.elf PRIVATE
vendor
vendor/Core/Inc
vendor/Core/Src
vendor/Drivers/STM32F4xx_HAL_Driver/Inc
vendor/Drivers/CMSIS/Include
vendor/Drivers/CMSIS/Device/ST/STM32F4xx/Include
core
core/kernel
core/kernel/data_type
core/arch
app
)
# ==============================================================================
# Hardware Definitions & MCU Flags
# ==============================================================================
set(MCU_FLAGS
-mcpu=cortex-m4
-mthumb
-mfpu=fpv4-sp-d16
-mfloat-abi=hard
) # FPU Enabled
# set(MCU_FLAGS
# -mcpu=cortex-m4
# -mthumb
# ) # FPU Disabled
# Consolidate general compilation flags
target_compile_options(${TARGET_NAME}.elf PRIVATE
${MCU_FLAGS}
-Os -Wall -g
-ffunction-sections -fdata-sections
-funsigned-char -fno-exceptions
--specs=nano.specs
-flto
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti -fcoroutines -fno-use-cxa-atexit>
$<$<COMPILE_LANGUAGE:C>:-Wno-deprecated-volatile>
)
# Linker script configuration
set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/vendor/STM32F407VGTx_FLASH.ld")
target_link_libraries(${TARGET_NAME}.elf PRIVATE etl::etl)
target_link_options(${TARGET_NAME}.elf PRIVATE
${MCU_FLAGS}
-T${LINKER_SCRIPT}
-Wl,--gc-sections --specs=nosys.specs --specs=nano.specs
-Wl,--print-memory-usage
-flto
-Wl,-Map=${CMAKE_BINARY_DIR}/${TARGET_NAME}.map
)
# ==============================================================================
# Post-Build Steps
# ==============================================================================
add_custom_command(TARGET ${TARGET_NAME}.elf POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${TARGET_NAME}.elf> ${TARGET_NAME}.hex
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${TARGET_NAME}.elf> ${TARGET_NAME}.bin
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${TARGET_NAME}.elf>
COMMENT "Generating HEX, BIN, and size report..."
)