-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CMakeLists.txt
400 lines (327 loc) · 13.6 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
###
# General cmake settings
###
cmake_minimum_required(VERSION 3.16.3)
cmake_policy(SET CMP0042 NEW)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
###
#
# Default cmake directories:
#
# | Target Type | GNUInstallDirs Variable | Built-In Default |
# | --- | --- | --- |
# | RUNTIME | ${CMAKE_INSTALL_BINDIR} | bin |
# | LIBRARY | ${CMAKE_INSTALL_LIBDIR} | lib |
# | ARCHIVE | ${CMAKE_INSTALL_LIBDIR} | lib |
# | PRIVATE_HEADER | ${CMAKE_INSTALL_INCLUDEDIR} | include |
# | PUBLIC_HEADER | ${CMAKE_INSTALL_INCLUDEDIR} | include |
# | FILE_SET (type HEADERS) | ${CMAKE_INSTALL_INCLUDEDIR} | include |
#
# | TYPE Argument | GNUInstallDirs Variable | Built-In Default |
# | --- | --- | --- |
# | BIN | ${CMAKE_INSTALL_BINDIR} | bin |
# | SBIN | ${CMAKE_INSTALL_SBINDIR} | sbin |
# | LIB | ${CMAKE_INSTALL_LIBDIR} | lib |
# | INCLUDE | ${CMAKE_INSTALL_INCLUDEDIR} | include |
# | SYSCONF | ${CMAKE_INSTALL_SYSCONFDIR} | etc |
# | SHAREDSTATE | ${CMAKE_INSTALL_SHARESTATEDIR} | com |
# | LOCALSTATE | ${CMAKE_INSTALL_LOCALSTATEDIR} | var |
# | RUNSTATE | ${CMAKE_INSTALL_RUNSTATEDIR} | <LOCALSTATE dir>/run |
# | DATA | ${CMAKE_INSTALL_DATADIR} | <DATAROOT dir> |
# | INFO | ${CMAKE_INSTALL_INFODIR} | <DATAROOT dir>/info |
# | LOCALE | ${CMAKE_INSTALL_LOCALEDIR} | <DATAROOT dir>/locale |
# | MAN | ${CMAKE_INSTALL_MANDIR} | <DATAROOT dir>/man |
# | DOC | ${CMAKE_INSTALL_DOCDIR} | <DATAROOT dir>/doc |
#
# ${CMAKE_BINARY_DIR}
# This is the full path to the top level of the current CMake build tree.
# For an in-source build, this would be the same as CMAKE_SOURCE_DIR.
#
# ${CMAKE_SOURCE_DIR}
# This is the full path to the top level of the current CMake source tree.
# For an in-source build, this would be the same as CMAKE_BINARY_DIR.
#
# ${CMAKE_CURRENT_BINARY_DIR}
# The path to the binary directory currently being processed.
# This is the full path to the build directory that is currently being processed by cmake.
# Each directory added by add_subdirectory() will create a binary directory in the build tree,
# and as it is being processed this variable will be set.
# For in-source builds this is the current source directory being processed.
#
# ${CMAKE_CURRENT_SOURCE_DIR}
# The path to the source directory currently being processed.
# This is the full path to the source directory that is currently being processed by cmake.
#
###
###
# General Project Settings
###
project(stlink C)
set(PROJECT_DESCRIPTION "Open source version of the STMicroelectronics ST-LINK Tools")
include(${CMAKE_MODULE_PATH}/get_version.cmake) # Determine project version
include(GNUInstallDirs) # Define GNU standard installation directories
# Define install directory for st-link shared files
cmake_host_system_information(RESULT OS_NAME QUERY OS_NAME)
message(STATUS "Checking for OS_NAME: ${OS_NAME}")
## Set C build flags
if (NOT MSVC)
include(${CMAKE_MODULE_PATH}/c_flags.cmake)
else ()
message(STATUS "MSVC C Flags override to /MT")
set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_C_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
endif()
###
# Dependencies
###
find_package(libusb REQUIRED)
## Check for system-specific additional header files and libraries
include(CheckIncludeFile)
include(CheckLibraryExists)
CHECK_LIBRARY_EXISTS(ssp __stack_chk_fail "" _stack_chk_fail_exists)
if (_stack_chk_fail_exists)
if (WIN32)
set(SSP_LIB -static ssp)
else ()
set(SSP_LIB ssp)
endif()
else ()
set(SSP_LIB "")
endif()
CHECK_INCLUDE_FILE(sys/mman.h STLINK_HAVE_SYS_MMAN_H)
if (STLINK_HAVE_SYS_MMAN_H)
add_definitions(-DSTLINK_HAVE_SYS_MMAN_H)
endif()
CHECK_INCLUDE_FILE(sys/time.h STLINK_HAVE_SYS_TIME_H)
if (STLINK_HAVE_SYS_TIME_H)
add_definitions(-DSTLINK_HAVE_SYS_TIME_H)
endif()
CHECK_INCLUDE_FILE(unistd.h STLINK_HAVE_UNISTD_H)
if (STLINK_HAVE_UNISTD_H)
add_definitions(-DSTLINK_HAVE_UNISTD_H)
endif()
CHECK_INCLUDE_FILE(dirent.h STLINK_HAVE_DIRENT_H)
if (STLINK_HAVE_DIRENT_H)
add_definitions(-DSTLINK_HAVE_DIRENT_H)
endif()
if (MSVC)
# Use string.h rather than strings.h and disable annoying warnings
add_definitions(-DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS /wd4710)
endif()
###
# Main build process
###
## Define include directories to avoid absolute paths for header defines
include_directories(${LIBUSB_INCLUDE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/inc) # contains top-level header files
include_directories(${PROJECT_BINARY_DIR}/inc) # contains version.h
include_directories(src)
include_directories(src/st-flash)
include_directories(src/st-info)
include_directories(src/st-trace)
include_directories(src/st-util)
include_directories(src/stlink-lib)
## Set installation directory for header files
if (WIN32)
set(STLINK_INCLUDE_PATH ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Main include install directory")
else ()
set(STLINK_INCLUDE_PATH ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} CACHE PATH "Main include install directory")
endif()
## Subordinate CMakeLists for version config & header installation
add_subdirectory(inc)
## Define source- and headerfiles for stlink library
set(STLINK_HEADERS
inc/backend.h
inc/stlink.h
inc/stm32.h
inc/stm32flash.h
src/stlink-lib/calculate.h
src/stlink-lib/chipid.h
src/stlink-lib/commands.h
src/stlink-lib/common_flash.h
src/stlink-lib/flash_loader.h
src/stlink-lib/helper.h
src/stlink-lib/libusb_settings.h
src/stlink-lib/lib_md5.h
src/stlink-lib/logging.h
src/stlink-lib/map_file.h
src/stlink-lib/md5.h
src/stlink-lib/option_bytes.h
src/stlink-lib/register.h
src/stlink-lib/sg.h
src/stlink-lib/usb.h
)
set(STLINK_SOURCE
src/stlink-lib/calculate.c
src/stlink-lib/chipid.c
src/stlink-lib/common_flash.c
src/stlink-lib/common.c
src/stlink-lib/flash_loader.c
src/stlink-lib/helper.c
src/stlink-lib/logging.c
src/stlink-lib/map_file.c
src/stlink-lib/lib_md5.c
src/stlink-lib/md5.c
src/stlink-lib/option_bytes.c
src/stlink-lib/read_write.c
src/stlink-lib/sg.c
src/stlink-lib/usb.c
)
if (WIN32)
include_directories(src/win32)
set(STLINK_SOURCE "${STLINK_SOURCE};src/win32/win32_socket.c")
set(STLINK_HEADERS "${STLINK_HEADERS};src/win32/win32_socket.h")
if (MSVC)
# Add drop-in replacement for unistd.h to sources
include_directories(src/win32/unistd)
set(STLINK_HEADERS "${STLINK_HEADERS};src/win32/unistd/unistd.h")
endif()
if (NOT STLINK_HAVE_SYS_MMAN_H)
include_directories(src/win32/mmap)
set(STLINK_SOURCE "${STLINK_SOURCE};src/win32/mmap.c")
set(STLINK_HEADERS "${STLINK_HEADERS};src/win32/mmap.h")
endif()
if (NOT STLINK_HAVE_SYS_TIME_H)
set(STLINK_SOURCE "${STLINK_SOURCE};src/win32/sys_time.c")
set(STLINK_HEADERS "${STLINK_HEADERS};src/win32/sys_time.h")
endif()
endif()
## Include test execution for test-targets for target Debug
if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
include(CTest)
endif()
###
# Libraries
###
# Set the environment variable LD_LIBRARY_PATH to point to /usr/local/lib (per default).
execute_process(COMMAND bash -c "export LD_LIBRARY_PATH=${CMAKE_INSTALL_LIBDIR}")
###
# Shared library
###
# Set library name
set(STLINK_LIB_SHARED ${PROJECT_NAME}-shared)
add_library(${STLINK_LIB_SHARED} SHARED ${STLINK_HEADERS} ${STLINK_SOURCE})
set(STLINK_SHARED_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
message(STATUS "STLINK_LIB_SHARED: ${STLINK_LIB_SHARED}")
message(STATUS "VERSION: ${STLINK_SHARED_VERSION}")
set_target_properties(${STLINK_LIB_SHARED} PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${STLINK_SHARED_VERSION}
OUTPUT_NAME ${PROJECT_NAME}
)
# Link shared library
if (WIN32)
target_link_libraries(${STLINK_LIB_SHARED} ${LIBUSB_LIBRARY} ${SSP_LIB} wsock32 ws2_32)
else ()
target_link_libraries(${STLINK_LIB_SHARED} ${LIBUSB_LIBRARY} ${SSP_LIB})
endif()
install(TARGETS ${STLINK_LIB_SHARED} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
###
# Static library
###
# Set library name
set(STLINK_LIB_STATIC ${PROJECT_NAME}-static)
set(STLINK_LIB_STATIC_OUTPUT_NAME ${PROJECT_NAME})
if (MSVC)
set(STLINK_LIB_STATIC_OUTPUT_NAME ${PROJECT_NAME}-static)
endif()
add_library(${STLINK_LIB_STATIC} STATIC ${STLINK_HEADERS} ${STLINK_SOURCE})
set(STLINK_STATIC_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
message(STATUS "STLINK_LIB_STATIC: ${STLINK_LIB_STATIC}")
message(STATUS "VERSION: ${STLINK_STATIC_VERSION}")
set_target_properties(${STLINK_LIB_STATIC} PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${STLINK_STATIC_VERSION}
OUTPUT_NAME ${STLINK_LIB_STATIC_OUTPUT_NAME}
)
# Link static library
if (WIN32)
target_link_libraries(${STLINK_LIB_STATIC} ${LIBUSB_LIBRARY} ${SSP_LIB} wsock32 ws2_32)
else ()
target_link_libraries(${STLINK_LIB_STATIC} ${LIBUSB_LIBRARY} ${SSP_LIB})
endif()
install(TARGETS ${STLINK_LIB_STATIC} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
###
# Build toolset executables
###
set(ST-FLASH_SOURCES src/st-flash/flash.c src/st-flash/flash_opts.c)
set(ST-INFO_SOURCES src/st-info/info.c)
set(ST-UTIL_SOURCES src/st-util/gdb-remote.c src/st-util/gdb-server.c src/st-util/semihosting.c)
set(ST-TRACE_SOURCES src/st-trace/trace.c)
if (MSVC)
# Add getopt to sources
include_directories(src/win32/getopt)
set(ST-UTIL_SOURCES "${ST-UTIL_SOURCES};src/win32/getopt/getopt.c")
set(ST-TRACE_SOURCES "${ST-TRACE_SOURCES};src/win32/getopt/getopt.c")
endif()
add_executable(st-flash ${ST-FLASH_SOURCES})
add_executable(st-info ${ST-INFO_SOURCES})
add_executable(st-util ${ST-UTIL_SOURCES})
add_executable(st-trace ${ST-TRACE_SOURCES})
if (WIN32)
target_link_libraries(st-flash ${STLINK_LIB_STATIC})
target_link_libraries(st-info ${STLINK_LIB_STATIC})
target_link_libraries(st-util ${STLINK_LIB_STATIC})
target_link_libraries(st-trace ${STLINK_LIB_STATIC})
else ()
target_link_libraries(st-flash ${STLINK_LIB_SHARED})
target_link_libraries(st-info ${STLINK_LIB_SHARED})
target_link_libraries(st-util ${STLINK_LIB_SHARED})
target_link_libraries(st-trace ${STLINK_LIB_SHARED})
endif()
install(TARGETS st-flash DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS st-info DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS st-util DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS st-trace DESTINATION ${CMAKE_INSTALL_BINDIR})
###
# Device configuration (Linux only)
###
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
## Install modprobe.d conf files to /etc/modprobe.d/ (explicitly hardcoded)
set(STLINK_MODPROBED_DIR "/etc/modprobe.d" CACHE PATH "modprobe.d directory")
install(FILES ${CMAKE_SOURCE_DIR}/config/modprobe.d/stlink_v1.conf DESTINATION ${STLINK_MODPROBED_DIR})
## Install udev rules files to /lib/udev/rules.d/ (explicitly hardcoded)
set(STLINK_UDEV_RULES_DIR "/lib/udev/rules.d" CACHE PATH "udev rules directory")
file(GLOB RULES_FILES ${CMAKE_SOURCE_DIR}/config/udev/rules.d/*.rules)
install(FILES ${RULES_FILES} DESTINATION ${STLINK_UDEV_RULES_DIR})
endif()
###
# Additional build tasks
###
# MCU configuration files
if (WIN32)
set(CMAKE_CHIPS_DIR ${CMAKE_INSTALL_PREFIX}/config/chips)
else ()
set(CMAKE_CHIPS_DIR ${CMAKE_INSTALL_FULL_DATADIR}/${PROJECT_NAME}/config/chips)
endif()
add_definitions( -DSTLINK_CHIPS_DIR="${CMAKE_CHIPS_DIR}" )
file(GLOB CHIP_FILES ${CMAKE_SOURCE_DIR}/config/chips/*.chip)
install(FILES ${CHIP_FILES} DESTINATION ${CMAKE_CHIPS_DIR})
# Documentation / manpages
option(STLINK_GENERATE_MANPAGES "Generate manpages with pandoc" OFF)
add_subdirectory(doc/man) # contains subordinate CMakeLists to generate manpages
add_subdirectory(src/stlink-gui) # contains subordinate CMakeLists to build GUI
add_subdirectory(tests) # contains subordinate CMakeLists to build test executables
add_subdirectory(cmake/packaging) # contains subordinate CMakeLists to build packages
###
# Uninstall target
###
if (NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake"
IMMEDIATE @ONLY
)
add_custom_target(
uninstall COMMAND ${CMAKE_COMMAND}
-P ${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake
)
endif()