-
Notifications
You must be signed in to change notification settings - Fork 9
/
CMakeLists.txt
422 lines (357 loc) · 13.7 KB
/
CMakeLists.txt
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# The MIT License (MIT)
#
# Copyright (c) 2020, National Cybersecurity Agency of France (ANSSI)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author: - Guillaume Bouffard <guillaume.bouffard@ssi.gouv.fr>
cmake_minimum_required(VERSION 3.13)
project(CHOUPI)
include(CheckIPOSupported)
enable_language(ASM)
#
# OPTIONS
#
# List of options that can be tweaked by final user. Warning: if you want to
# change the default behavior, do not edit this file directly. Run cmake and
# then edit the CMakeCache.txt of use a tool like ccmake.
# Enable link time optimizations for all targets.
option(CHOUPI_ENABLE_LTO "Enable link time optimisations" ON)
option(CHOUPI_OS_DEBUG "Build choupi-os with debug output" ON)
option(CHOUPI_JCVM_DEBUG "Build choupi with debug output" OFF)
option(CHOUPI_TARGET_PC "PC Version" ON)
option(CHOUPI_TARGET_STM32 "STM32 Version" OFF)
if(CHOUPI_TARGET_STM32)
set(CHOUPI_TARGET_PC OFF)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR ARM)
set(OCD "/usr/share/openocd/scripts/board/st_nucleo_f4.cfg")
set(TOOLCHAIN_PREFIX arm-none-eabi-)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_SIZE ${TOOLCHAIN_PREFIX}size)
set(CMAKE_NM ${TOOLCHAIN_PREFIX}nm)
set(CMAKE_GDB ${TOOLCHAIN_PREFIX}gdb)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_C_FLAGS
"-mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork \
-finline-small-functions -findirect-inlining \
-fno-common ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS
"${CMAKE_C_FLAGS} -fexceptions -fno-threadsafe-statics ${CMAKE_CXX_FLAGS}"
)
add_link_options("LINKER:--gc-section")
add_link_options("LINKER:--wrap,__malloc_r")
add_link_options("LINKER:--wrap,__free_r")
add_link_options("LINKER:--wrap,__calloc_r")
add_link_options("LINKER:--wrap,__realloc_r")
# add_link_options("LINKER:-Map=map.map")
set(CMAKE_EXE_LINKER_FLAGS_INIT
"--specs=nano.specs" "--specs=nosys.specs"
CACHE INTERNAL "")
add_link_options("LINKER:-T${CMAKE_SOURCE_DIR}/stm32f401xe.ld")
endif(CHOUPI_TARGET_STM32)
if(CHOUPI_TARGET_PC)
set(CHOUPI_TARGET_STM32 OFF)
endif(CHOUPI_TARGET_PC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -ggdb")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -ggdb")
set(CMAKE_BUILD_TYPE MinSizeRel)
if(CHOUPI_TARGET_STM32)
function(firmware_size target)
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND ${CMAKE_SIZE} -B
"${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}")
endfunction()
function(generate_firmware target suffix type)
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND
${CMAKE_OBJCOPY} --remove-section=.flashloader -O ${type}
"${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}"
"${CMAKE_CURRENT_BINARY_DIR}/${target}${suffix}")
endfunction()
function(generate_loader target)
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND
${CMAKE_OBJCOPY} --only-section=.flashloader -O ihex
"${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}"
"${CMAKE_CURRENT_BINARY_DIR}/loader.hex")
endfunction()
function(generate_code target)
add_custom_command(
TARGET ${target}
POST_BUILD
COMMAND
${CMAKE_OBJCOPY} --remove-section=.flashloader -O ihex
"${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}"
"${CMAKE_CURRENT_BINARY_DIR}/code.hex")
endfunction()
endif(CHOUPI_TARGET_STM32)
#
# COMPILE INFO
#
message("Link-time optimizations: ${CHOUPI_ENABLE_LTO}")
if(CHOUPI_TARGET_PC)
message("Target: Computer version")
elseif(CHOUPI_TARGET_STM32)
message("Target: STM32")
endif(CHOUPI_TARGET_PC)
#
# CHOUPI
#
if(CHOUPI_JCVM_DEBUG)
add_compile_definitions(DEBUG)
endif(CHOUPI_JCVM_DEBUG)
if(CHOUPI_TARGET_PC)
if(CHOUPI_OS_DEBUG)
add_custom_target(
javacardos
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/os"
COMMENT "Building Java Card OS with debug output"
BYPRODUCTS ${CMAKE_BINARY_DIR}/libjavacard_os.a
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/javacard-os
COMMAND
cargo build --no-default-features --features host,big_ram
--manifest-path=${CMAKE_SOURCE_DIR}/os/Cargo.toml -Z unstable-options
--out-dir ${CMAKE_BINARY_DIR} --target-dir
${CMAKE_BINARY_DIR}/javacard-os/
VERBATIM)
else(CHOUPI_OS_DEBUG)
add_custom_target(
javacardos
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/os"
COMMENT "Building Java Card OS"
BYPRODUCTS ${CMAKE_BINARY_DIR}/libjavacard_os.a
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/javacard-os
COMMAND
cargo build --release --no-default-features --features host,big_ram
--manifest-path=${CMAKE_SOURCE_DIR}/os/Cargo.toml -Z unstable-options
--out-dir ${CMAKE_BINARY_DIR} --target-dir
${CMAKE_BINARY_DIR}/javacard-os/
VERBATIM)
endif(CHOUPI_OS_DEBUG)
set(CHOUPI_JAVACARDOS_LIB "${CMAKE_BINARY_DIR}")
elseif(CHOUPI_TARGET_STM32)
if(CHOUPI_OS_DEBUG)
set(ENV{CARGOPROFILE} "debug")
add_custom_target(
javacardos
COMMENT "Building Java Card OS in DEBUG mode"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/os"
BYPRODUCTS ${CMAKE_SOURCE_DIR}/libjavacard_os.a
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/javacard-os
COMMAND
xargo build --features stm32f401re --target thumbv7em-none-eabi -Z
unstable-options --out-dir ${CMAKE_BINARY_DIR} --target-dir
${CMAKE_BINARY_DIR}/javacard-os/
VERBATIM)
else(CHOUPI_OS_DEBUG)
set(ENV{CARGOPROFILE} "release")
add_custom_target(
javacardos
COMMENT "Building Java Card OS in RELEASE mode"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/os"
BYPRODUCTS ${CMAKE_SOURCE_DIR}/libjavacard_os.a
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/javacard-os
COMMAND
xargo build --release --features stm32f401re --target
thumbv7em-none-eabi -Z unstable-options --out-dir ${CMAKE_BINARY_DIR}
--target-dir ${CMAKE_BINARY_DIR}/javacard-os/
VERBATIM)
endif(CHOUPI_OS_DEBUG)
set(CHOUPI_JAVACARDOS_LIB "${CMAKE_BINARY_DIR}")
endif(CHOUPI_TARGET_PC)
add_custom_target(
javacard-sdk
COMMENT "Building Java Card API"
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/javacard-api/
COMMAND ant -f ${CMAKE_SOURCE_DIR}/javacard-sdk/javacard-api/build.xml
-Dbuild.dir=${CMAKE_BINARY_DIR}/javacard-api/ clean build
VERBATIM)
if(CHOUPI_TARGET_PC)
add_custom_target(
rommask
DEPENDS javacard-sdk
COMMENT "Generating Java Card ROM mask"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/javacard-sdk/rommask"
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/rommask/
COMMAND make
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/rommask/
COMMAND
${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/javacard-sdk/rommask/target/rommask-1.0-jar-with-dependencies.jar
${CMAKE_BINARY_DIR}/rommask/rommask.jar
COMMAND
java -jar ${CMAKE_BINARY_DIR}/rommask/rommask.jar
${CMAKE_BINARY_DIR}/javacard-api ${CMAKE_BINARY_DIR}/flash
${CMAKE_BINARY_DIR}/jni.hpp fr.gouv.ssi.starter.Starter.run
VERBATIM)
elseif(CHOUPI_TARGET_STM32)
add_custom_target(
rommask
DEPENDS javacard-sdk
COMMENT "Generating Java Card ROM mask"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/javacard-sdk/rommask"
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/rommask/
COMMAND make
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/rommask/
COMMAND
${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/javacard-sdk/rommask/target/rommask-1.0-jar-with-dependencies.jar
${CMAKE_BINARY_DIR}/rommask/rommask.jar
COMMAND
java -jar ${CMAKE_BINARY_DIR}/rommask/rommask.jar --compactHex 08020000
${CMAKE_BINARY_DIR}/javacard-api ${CMAKE_BINARY_DIR}/flash.hex
${CMAKE_BINARY_DIR}/jni.hpp fr.gouv.ssi.starter.Starter.run
VERBATIM)
endif(CHOUPI_TARGET_PC)
# JCVM source files
file(GLOB_RECURSE JCVM_CORE_SOURCES_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
if(CHOUPI_TARGET_PC)
add_executable(choupi "${JCVM_CORE_SOURCES_FILES}")
target_include_directories(
choupi PUBLIC ${CMAKE_BINARY_DIR} "${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/os/src")
find_package(
Boost 1.67
COMPONENTS program_options
REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(choupi ${Boost_LIBRARIES})
endif(CHOUPI_TARGET_PC)
if(CHOUPI_TARGET_STM32)
add_executable(
choupi
"${JCVM_CORE_SOURCES_FILES}"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/startup_stm32f401xe.s"
"${CMAKE_SOURCE_DIR}/os/src/malloc.c"
"${CMAKE_SOURCE_DIR}/os/src/stm32f4xx_it.c"
"${CMAKE_SOURCE_DIR}/os/src/system_stm32f4xx.c"
"${CMAKE_SOURCE_DIR}/os/src/newlib_stubs.c"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c"
)
target_include_directories(
choupi
PUBLIC
${CMAKE_BINARY_DIR}
"${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/os/src"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/STM32F4xx_HAL_Driver/Inc/"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/CMSIS/Device/ST/STM32F4xx/Include/"
"${CMAKE_SOURCE_DIR}/os/STM32CubeF4/Drivers/CMSIS/Include/")
# -DSTM32F401xE
add_compile_definitions(STM32F401xE)
endif(CHOUPI_TARGET_STM32)
set_property(TARGET choupi PROPERTY CXX_STANDARD 17)
if(CHOUPI_ENABLE_LTO)
check_ipo_supported(RESULT result OUTPUT output)
if(result)
set_target_properties(choupi PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO is not supported: ${output}")
endif()
endif(CHOUPI_ENABLE_LTO)
add_dependencies(choupi rommask)
add_dependencies(choupi javacardos)
if(CHOUPI_TARGET_PC)
# -DPC_VERSION
add_compile_definitions(PC_VERSION)
# -lutil
target_link_libraries(choupi util)
# -ldl
target_link_libraries(choupi dl)
# -lrt
target_link_libraries(choupi rt)
# -lpthread
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(choupi Threads::Threads)
# -lgcc_s
target_link_libraries(choupi gcc_s)
# -lc
target_link_libraries(choupi c)
# -lm
target_link_libraries(choupi m)
endif()
target_link_libraries(choupi -L${CHOUPI_JAVACARDOS_LIB})
target_link_libraries(choupi javacard_os)
if(CHOUPI_TARGET_STM32)
generate_loader(choupi)
generate_code(choupi)
generate_firmware(choupi .bin binary)
firmware_size(choupi)
endif(CHOUPI_TARGET_STM32)
#
# CUSTOM COMMANDS
#
# Custom command to beautify the sources
add_custom_target(
beautify
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND
astyle -r -n --style=kr -s2 --attach-namespaces --add-brackets
--close-templates --indent-modifiers --indent-switches --break-blocks
--indent-col1-comments --indent-preproc-block --indent-preproc-define
--indent-classes --indent-namespaces --min-conditional-indent=0
--lineend=linux --pad-oper --pad-comma --pad-header --unpad-paren
--align-pointer=name --align-reference=name 'src/*.cpp' 'src/*.h*'
'test/*.cpp')
if(CHOUPI_TARGET_STM32)
add_custom_target(
ocd
COMMENT "Starting opencd"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND openocd -f ${OCD}
VERBATIM)
add_custom_target(
load
COMMENT "Starting rlwrap"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND echo
"To flash, run the following command in the next-to-come shell:"
COMMAND
echo
"reset halt; flash write_image erase loader.hex; flash write_image erase code.hex; flash write_image erase flash.hex; reset run"
COMMAND rlwrap nc localhost 4444
VERBATIM)
add_custom_target(
gdb
COMMENT "Starting debugging"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_GDB} choupi -ex "target extended localhost:3333"
VERBATIM)
endif(CHOUPI_TARGET_STM32)