forked from canokeys/canokey-nrf52
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
133 lines (101 loc) · 4.22 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
cmake_minimum_required(VERSION 3.18)
option(ENABLE_DEBUG_OUTPUT "Print debug messages" OFF)
project(canokey-nrf52 C ASM)
set(TARGET canokey)
# board selection, see config/board_selection.h for avaliable boards
set(CANOKEY_BOARD OPT_BOARD_NRF52840_DONGLE)
# set(CANOKEY_BOARD OPT_BOARD_MAKERDIARY_M2_DOCK)
set(BASE_DIR ${CMAKE_CURRENT_LIST_DIR}/)
set(NRF5_SDK_DIR ${BASE_DIR}/nrf_sdk/nRF5_SDK_17.1.0_ddde560)
set(LINKER_SCRIPT "${BASE_DIR}/nrf52840.ld")
add_definitions(-DNRF52840_XXAA)
add_definitions(-DMBR_PRESENT)
add_definitions(-DCFG_BOARD=${CANOKEY_BOARD})
add_definitions(-DCFG_TUSB_MCU=OPT_MCU_NRF5X)
add_definitions(-DMBEDTLS_CONFIG_FILE="${BASE_DIR}/config/mbedtls_config.h")
# nrf5 sdk configs
add_definitions(-DCUSTOM_BOARD_INC=board_selection)
add_definitions(-DBSP_DEFINES_ONLY)
add_definitions(-DCONFIG_GPIO_AS_PINRESET)
add_definitions(-DFLOAT_ABI_HARD)
set(CMAKE_C_STANDARD 11)
set(COMMON_COMPILE_FLAGS "-mcpu=cortex-m4 -mthumb -mabi=aapcs -mfloat-abi=hard -mfpu=fpv4-sp-d16")
set(EXTRA_COMPILE_FLAGS "-ffunction-sections -fdata-sections -fno-strict-aliasing -fno-builtin -fshort-enums")
set(WARNING_FLAGS "-Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_COMPILE_FLAGS} ${WARNING_FLAGS} ${EXTRA_COMPILE_FLAGS} ${DEFINITIONS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_COMPILE_FLAGS} ${WARNING_FLAGS} ${EXTRA_COMPILE_FLAGS} ${DEFINITIONS}")
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${COMMON_COMPILE_FLAGS}")
set(CMAKE_C_FLAGS_DEBUG "-g3 -Og")
set(CMAKE_C_FLAGS_RELEASE "-Os")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L${NRF5_SDK_DIR}/modules/nrfx/mdk -T ${LINKER_SCRIPT} -Wl,--gc-sections -Wl,-Map=${TARGET}.map --specs=nano.specs")
# Enable export of compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(${BASE_DIR}/nrf_sdk/nRF5_SDK.cmake)
file(GLOB SRCS
"${BASE_DIR}/src/*.c"
)
set(SOURCES
${NRF5_SDK_SOURCES}
${SRCS}
)
add_executable(${TARGET} ${SOURCES})
# Include dirs
target_include_directories(${TARGET} PRIVATE ${NRF5_SDK_INCLUDES})
target_include_directories(${TARGET} PRIVATE ${BASE_DIR}/include)
target_include_directories(${TARGET} PRIVATE ${BASE_DIR}/config)
# nrf sdk binary libraries
target_link_libraries(${TARGET} PRIVATE ${NRF5_SDK_LIBRARIES})
# TinyUSB DCD configure
set(DCD_INCLUDE_DIRS
${NRF5_SDK_INCLUDES}
${BASE_DIR}/config
)
set(TINYUSB_MCU "OPT_MCU_NRF5X" CACHE STRING "TinyUSB MCU name")
set(TINYUSB_DCD_INCLUDES ${DCD_INCLUDE_DIRS} CACHE STRING "TinyUSB DCD include paths")
# CanoKey Core
add_subdirectory(canokey-core)
target_link_libraries(${TARGET} PRIVATE
-Wl,--whole-archive
canokey-core
-Wl,--no-whole-archive
)
target_link_libraries(${TARGET} PRIVATE c m nosys)
# Generate Git version header
add_dependencies(${TARGET} gitrev)
set(gitrev_in include/git-rev.h.in)
set(gitrev include/git-rev.h)
add_custom_target(gitrev
${CMAKE_COMMAND} -E remove -f ${CMAKE_CURRENT_SOURCE_DIR}/${gitrev}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${gitrev_in} ${CMAKE_CURRENT_SOURCE_DIR}/${gitrev}
COMMAND git describe --always --tags --long --abbrev=8 --dirty >> ${CMAKE_CURRENT_SOURCE_DIR}/${gitrev}
)
# Create a binary from the elf
add_custom_target(${TARGET}.bin
DEPENDS ${TARGET}
COMMAND "${CROSS_COMPILE}objcopy" -O binary -S ${TARGET} ${TARGET}.bin
)
add_custom_target(${TARGET}.hex
DEPENDS ${TARGET}
COMMAND "${CROSS_COMPILE}objcopy" -O ihex -S ${TARGET} ${TARGET}.hex
)
# Print size of the elf
add_custom_command(TARGET ${TARGET} POST_BUILD
COMMAND ${CROSS_COMPILE}size ${TARGET}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
# Generate UF2 package for Adafruit bootloader
add_custom_target(${TARGET}_flash.uf2
DEPENDS ${TARGET}.hex
COMMAND python ${BASE_DIR}/utils/uf2/utils/uf2conv.py --convert --family 0xADA52840 -o ${TARGET}_flash.uf2 ${TARGET}.hex
)
# NRF sdk config tool
set(NRF5_SDK_CONFIG_FILE ${BASE_DIR}/config/sdk_config.h)
set(CMSIS_CONFIG_TOOL ${NRF5_SDK_DIR}/external_tools/cmsisconfig/CMSIS_Configuration_Wizard.jar)
add_custom_target(sdk_config
COMMAND java -jar ${CMSIS_CONFIG_TOOL} ${NRF5_SDK_CONFIG_FILE}
)
# auto-ignore build directory
if(NOT EXISTS ${PROJECT_BINARY_DIR}/.gitignore)
file(WRITE ${PROJECT_BINARY_DIR}/.gitignore "*")
endif()
# vim: set ts=4 sw=4 sts=4 et smarttab :