This repository was archived by the owner on Feb 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
373 lines (291 loc) · 14 KB
/
CMakeLists.txt
File metadata and controls
373 lines (291 loc) · 14 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
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
cmake_minimum_required(VERSION 3.14)
set(CMAKE_CXX_STANDARD 20) # CMake decays this to the highest supported by the compiler
set(CMAKE_C_STANDARD 11)
project(
"MIP_SDK"
VERSION "4.0.0"
DESCRIPTION "MicroStrain Communications Library for embedded systems"
LANGUAGES C CXX
)
set_property(GLOBAL PROPERTY
USE_FOLDERS ON
)
set(MICROSTRAIN_SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/src")
# Add our cmake directory to the available cmake modules path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
#
# Build options
#
include(check_cxx_support)
set(MIP_LOGGING_MAX_LEVEL "MICROSTRAIN_LOGGING_LEVEL_TRACE_" CACHE STRING "Max log level the SDK is allowed to log. If this is defined, any call to logging functions with a higher level than this will be compiled out.")
set(MIP_LOGGING_ENABLED OFF)
if(MIP_LOGGING_MAX_LEVEL)
if(NOT "${MIP_LOGGING_MAX_LEVEL}" MATCHES "^MICROSTRAIN_LOGGING_LEVEL_[A-Z][A-Z][A-Z][A-Z]?[A-Z]?_$" AND NOT "${MIP_LOGGING_MAX_LEVEL}" MATCHES "^[0-9]$")
message(FATAL_ERROR "MIP_LOGGING_MAX_LEVEL must be MICROSTRAIN_LOGGING_LEVEL_*_ or a positive number")
endif()
if(NOT "${MIP_LOGGING_MAX_LEVEL}" MATCHES "^MICROSTRAIN_LOGGING_LEVEL_OFF_$" AND NOT "${MIP_LOGGING_MAX_LEVEL}" MATCHES "^0$")
set(MIP_LOGGING_ENABLED ON)
message("MIP logging is enabled. Max level = '${MIP_LOGGING_MAX_LEVEL}'")
endif()
endif()
option(MICROSTRAIN_ENABLE_CPP "Enables the C++ API. Turn off to avoid compiling the C++ API." ON)
#option(MICROSTRAIN_ENABLE_CPP_C_NAMESPACE "Wraps the C api in a C++ namespace to avoid global namespace pollution (e.g. microstrain::C::microstrain_serial_port)" ${MICROSTRAIN_ENABLE_CPP})
message(DEBUG "MICROSTRAIN_ENABLE_CPP=${MICROSTRAIN_ENABLE_CPP}")
if(MICROSTRAIN_ENABLE_CPP)
option(MICROSTRAIN_USE_STD_ENDIAN "Use std::endian from C++20 for microstrain::Endian." ${MICROSTRAIN_COMPILER_SUPPORTS_BIT})
message(DEBUG "MICROSTRAIN_USE_STD_ENDIAN=${MICROSTRAIN_USE_STD_ENDIAN}")
endif()
option(MICROSTRAIN_ENABLE_EXTRAS "Build extra support into the library including some things that might work at a higher level and use dynamically allocated memory" ON)
option(MICROSTRAIN_ENABLE_SERIAL "Build serial connection support into the library and examples" ON)
option(MICROSTRAIN_ENABLE_TCP "Build TCP connection support into the library and examples" ON)
option(MICROSTRAIN_BUILD_PACKAGE "Whether to build a package from the resulting binaries" OFF)
option(MICROSTRAIN_BUILD_EXAMPLES "Builds the example programs." OFF)
# CTest defines this option to ON by default, so override it to OFF here.
option(MICROSTRAIN_BUILD_TESTS "Build the testing tree." OFF)
option(MICROSTRAIN_BUILD_DOCUMENTATION "Build the documentation." OFF)
option(MICROSTRAIN_BUILD_DOCUMENTATION_FULL "Build the full (internal) documentation." OFF)
option(MICROSTRAIN_BUILD_DOCUMENTATION_QUIET "Suppress doxygen standard output." ON)
option(MICROSTRAIN_CMAKE_DEBUG "If set, prints build system debug info such as source files and preprocessor definitions." ON) # TODO: off by default
mark_as_advanced(MICROSTRAIN_CMAKE_DEBUG)
# MIP options
option(MIP_ENABLE_DIAGNOSTICS "Enable various diagnostic counters in the mip library for debugging." OFF)
if(NOT DEFINED MIP_ENABLE_METADATA)
message(STATUS "MIP_ENABLE_METADATA not set - compiler support: ${MIP_COMPILER_SUPPORTS_METADATA}")
endif()
option(MIP_ENABLE_METADATA "Build support for MIP protocol metadata in C++ (requires c++20)" ${MIP_COMPILER_SUPPORTS_METADATA})
option(MIP_ENABLE_EXTRAS "Build extra support into the library including some things that might work at a higher level and use dynamically allocated memory" ${MICROSTRAIN_ENABLE_EXTRAS})
# Include some utilities used for MicroStrain projects
include(microstrain_utilities)
#
# Common preprocessor definitions
#
if(WIN32)
if(MSVC)
set(MIP_PRIVATE_COMPILE_OPTIONS
"/external:anglebrackets" # Treat angle brackets as system includes
"/external:W0" # No warnings from system includes
"/W4" # Enable warnings
"/WX" # Warnings as errors
"/Zc:__cplusplus" # Enable updated __cplusplus value
"/MP" # Multi-process compilation
)
if(${MSVC_TOOLSET_VERSION} LESS 143)
list(APPEND MIP_PRIVATE_COMPILE_OPTIONS
"/wd4201" # Suppress warnings related to nameless structs/unions
)
endif()
endif()
# Disable windows defined min/max
# Set Windows header version (0x0501 is _WIN32_WINNT_WINXP, required for TCP)
set(MIP_PRIVATE_COMPILE_DEFINITIONS
"NOMINMAX=1"
"_WIN32_WINNT=_WIN32_WINNT_WINXP"
)
else()
set(MIP_PRIVATE_COMPILE_OPTIONS
"-Wall" # Enable warnings
"-Wextra" # Enable extra warnings
"-Werror" # Warnings as errors
)
set(MIP_PRIVATE_COMPILE_DEFINITIONS "")
endif()
# TODO: Move to the MicroStrain SDK project when separated
set(MICROSTRAIN_PRIVATE_COMPILE_OPTIONS "${MIP_PRIVATE_COMPILE_OPTIONS}")
set(MICROSTRAIN_PRIVATE_COMPILE_DEFINITIONS "${MIP_PRIVATE_COMPILE_DEFINITIONS}")
list(APPEND MIP_PRIVATE_COMPILE_DEFINITIONS
"MICROSTRAIN_LOGGING_MAX_LEVEL=${MIP_LOGGING_MAX_LEVEL}"
)
# Suppress warnings due to adding files with target_sources.
cmake_policy(SET CMP0076 NEW)
# Suppress warnings due to linking libraries across directories
cmake_policy(SET CMP0079 NEW)
#
# Version numbering
#
microstrain_get_git_version(MICROSTRAIN_GIT_VERSION MICROSTRAIN_GIT_VERSION_CLEAN)
# Add all the C files (required even for C++ API)
set(MICROSTRAIN_SRC_C_DIR "${MICROSTRAIN_SRC_DIR}/c")
add_subdirectory(${MICROSTRAIN_SRC_C_DIR}/microstrain)
add_subdirectory(${MICROSTRAIN_SRC_C_DIR}/mip)
# Add all the C++ files (if C++ enabled)
if(MICROSTRAIN_ENABLE_CPP)
set(MICROSTRAIN_SRC_CPP_DIR "${MICROSTRAIN_SRC_DIR}/cpp")
add_subdirectory(${MICROSTRAIN_SRC_CPP_DIR}/microstrain)
add_subdirectory(${MICROSTRAIN_SRC_CPP_DIR}/mip)
#target_compile_definitions(${MICROSTRAIN_LIBRARY} PUBLIC "MICROSTRAIN_ENABLE_CPP_C_NAMESPACE=${MICROSTRAIN_ENABLE_CPP_C_NAMESPACE}")
endif()
# Create list of requested libraries for easier linking
# Use the PARENT_SCOPE option when adding new modules and make sure it propagates up all levels
set(MICROSTRAIN_LIBRARIES ${MICROSTRAIN_LIBRARIES_TMP} CACHE STRING "List of all requested MicroStrain libraries to make linking easier" FORCE)
set(MIP_LIBRARIES ${MIP_LIBRARIES_TMP} CACHE STRING "List of all requested MIP libraries to make linking easier" FORCE)
#
# Libraries
#
#
# TESTING
#
if(MICROSTRAIN_BUILD_TESTS)
include(CTest)
enable_testing()
add_subdirectory("test")
endif()
#
# EXAMPLES
#
if(MICROSTRAIN_BUILD_EXAMPLES)
add_subdirectory("examples")
endif()
#
# DOCUMENTATION
#
if(MICROSTRAIN_BUILD_DOCUMENTATION)
find_package(Doxygen)
if(NOT DOXYGEN_FOUND)
message(FATAL_ERROR "Doxygen is required to build documentation.")
endif()
set(DOXYGEN_PROJECT_NUMBER "${MICROSTRAIN_GIT_VERSION}")
set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/documentation")
set(DOXYGEN_IMAGE_PATH "${CMAKE_CURRENT_LIST_DIR}/docs")
set(DOXYGEN_WARN_IF_UNDOCUMENTED NO)
if(MICROSTRAIN_ENABLE_CPP)
list(APPEND DOXYGEN_PREDEFINED "__cplusplus")
endif()
if(MIP_LOGGING_ENABLED)
list(APPEND DOXYGEN_PREDEFINED "MICROSTRAIN_LOGGING_MAX_LEVEL=${MIP_LOGGING_MAX_LEVEL}")
endif()
set(DOXYGEN_CASE_SENSE_NAMES YES)
set(DOXYGEN_EXTRACT_ALL YES)
set(DOXYGEN_EXTRACT_STATIC YES)
set(DOXYGEN_SORT_MEMBER_DOCS NO) # Keep the order that things are defined instead of alphabetical
set(DOXYGEN_REPEAT_BRIEF NO) # Don't copy the brief in the details
if(MICROSTRAIN_BUILD_DOCUMENTATION_FULL)
set(DOXYGEN_INTERNAL_DOCS YES)
# set(DOXYGEN_WARN_AS_ERROR YES)
else()
set(DOXYGEN_HIDE_UNDOC_MEMBERS YES)
set(DOXYGEN_HIDE_UNDOC_CLASSES YES)
endif()
if(MICROSTRAIN_BUILD_DOCUMENTATION_QUIET)
set(DOXYGEN_QUIET YES)
endif()
set(DOXYGEN_HTML_EXTRA_STYLESHEET "${CMAKE_CURRENT_LIST_DIR}/docs/style.css")
set(MICROSTRAIN_EXAMPLES_DIR "${CMAKE_CURRENT_LIST_DIR}/examples")
doxygen_add_docs(docs
"${MICROSTRAIN_SRC_DIR}"
"${CMAKE_CURRENT_LIST_DIR}/docs"
"${MICROSTRAIN_EXAMPLES_DIR}"
COMMENT "Generating documentation."
)
# Fix Doxygen inline HTML parsing within the examples README.md file
# This is needed to tell Doxygen to treat the HTML table as HTML and not convert it to a Doxygen table
set(EXAMPLES_README_FILE "${MICROSTRAIN_EXAMPLES_DIR}/README")
set(EXAMPLES_README_CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/examples_readme.cmake")
file(CONFIGURE OUTPUT "${EXAMPLES_README_CONFIG_FILE}" CONTENT
"file(READ \"${EXAMPLES_README_FILE}.md\" CONTENT)\n
# Wrap all <table> blocks with @htmlonly/@endhtmlonly
string(REGEX REPLACE \"(<table)\" \"@htmlonly\\n\\\\1\" CONTENT \"\${CONTENT}\")\n
string(REGEX REPLACE \"(</table>)\" \"\\\\1\\n@endhtmlonly\" CONTENT \"\${CONTENT}\")\n
# Update the example page references
string(REGEX REPLACE \"(<a href=\\\")([a-z]+)/([57]+)_([a-z]+)/([a-z]+)_([a-z]+)(\\\">)\" \"\\\\1group____\\\\3__\\\\4__\\\\5__\\\\6__example__\\\\2.html\\\\7\" CONTENT \"\${CONTENT}\")\n
string(REGEX REPLACE \"(<a href=\\\")([a-z]+)/([57]+)_([a-z]+)/([a-z]+)(\\\">)\" \"\\\\1group____\\\\3__\\\\4__\\\\5__example__\\\\2.html\\\\6\" CONTENT \"\${CONTENT}\")\n
string(REGEX REPLACE \"(<a href=\\\")([a-z]+)/([a-z]+)_([a-z]+)(\\\">)\" \"\\\\1group__\\\\3__\\\\4__example__\\\\2.html\\\\5\" CONTENT \"\${CONTENT}\")\n
file(WRITE \"${EXAMPLES_README_FILE}.md\" \"\${CONTENT}\")"
@ONLY
)
set(MIP_PREPARE_EXAMPLES_README_TARGET "doxygen_prepare_examples_readme")
add_custom_target("${MIP_PREPARE_EXAMPLES_README_TARGET}"
COMMAND ${CMAKE_COMMAND} -E copy "${EXAMPLES_README_FILE}.md" "${EXAMPLES_README_FILE}.bak"
COMMAND ${CMAKE_COMMAND} -P "${EXAMPLES_README_CONFIG_FILE}"
COMMENT "Preparing examples README.md for Doxygen"
)
add_dependencies(docs "${MIP_PREPARE_EXAMPLES_README_TARGET}")
add_custom_command(TARGET docs POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${EXAMPLES_README_FILE}.bak" "${EXAMPLES_README_FILE}.md"
COMMAND ${CMAKE_COMMAND} -E remove "${EXAMPLES_README_FILE}.bak"
COMMENT "Restoring original examples README.md"
)
# Add a target to enable users to zip up the docs
add_custom_target(package_docs
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/documentation/html
DEPENDS docs
COMMAND ${CMAKE_COMMAND} -E tar "cf" "${CMAKE_CURRENT_BINARY_DIR}/mipsdk_${MICROSTRAIN_GIT_VERSION}_Documentation.zip" --format=zip "."
)
endif()
#
# Packaging
#
# If we were asked to package, find the generators we can use
if(MICROSTRAIN_BUILD_PACKAGE)
microstrain_get_architecture(MICROSTRAIN_ARCH)
set(MIP_FILE_NAME_PREFIX "mipsdk_${MICROSTRAIN_GIT_VERSION}_${MICROSTRAIN_ARCH}")
set(FOUND_CPACK_GENERATORS "")
set(DPKG_ROOT "" CACHE STRING "Location of the dpkg executable")
find_program(DPKG_EXECUTABLE
NAMES dpkg
PATHS ${DPKG_ROOT}
DOC "dpkg command line client"
)
if(NOT ${DPKG_EXECUTABLE} STREQUAL "DPKG_EXECUTABLE-NOTFOUND")
list(APPEND FOUND_CPACK_GENERATORS "DEB")
# DEB specific configuration
# Build different deb packages for each target
set(CPACK_DEBIAN_MIP_FILE_NAME "${MIP_FILE_NAME_PREFIX}.deb")
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
endif()
set(RPMBUILD_ROOT "" CACHE STRING "Location of the rpmbuild executable")
find_program(RPMBUILD_EXECUTABLE
NAMES rpmbuild
PATHS ${RPMBUILD_ROOT}
DOC "rpmbuild command line client"
)
if(NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
list(APPEND FOUND_CPACK_GENERATORS "RPM")
# RPM specific configuration
# Build different RPM packages for each target
set(CPACK_RPM_MIP_FILE_NAME "${MIP_FILE_NAME_PREFIX}.rpm")
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_RPM_PACKAGE_AUTOREQ ON)
endif()
# Windows always has zip installed, so only look for it on linux and mac
if(WIN32)
list(APPEND FOUND_CPACK_GENERATORS "ZIP")
else()
set(ZIP_ROOT "" CACHE STRING "Location of the zip executable")
find_program(ZIP_EXECUTABLE
NAMES zip
PATHS ${ZIP_ROOT}
DOC "zip command line client"
)
if(NOT ${ZIP_EXECUTABLE} STREQUAL "ZIP_EXECUTABLE-NOTFOUND")
list(APPEND FOUND_CPACK_GENERATORS "ZIP")
endif()
endif()
if(NOT FOUND_CPACK_GENERATORS)
message(FATAL_ERROR "Unable to find a suitable package generator, but we were requested to build a package.")
else()
message(STATUS "Packaging using the following generators: ${FOUND_CPACK_GENERATORS}")
endif()
# Packaging
# NOTE: CPack requires all these variables to be set before importing the module. Do not move them after the include(CPack) line
set(CPACK_GENERATOR "${FOUND_CPACK_GENERATORS}")
set(CPACK_PACKAGE_VENDOR "MicroStrain by HBK")
set(CPACK_PACKAGE_CONTACT "MicroStrain Support <microstrainsupport@hbkworld.com>")
set(CPACK_PACKAGE_VERSION ${MICROSTRAIN_GIT_VERSION_CLEAN})
# Zip specific configuration
# Build different zip packages for each target
if(MSVC)
set(CPACK_PACKAGE_FILE_NAME "${MIP_FILE_NAME_PREFIX}_MSVC_v${MSVC_TOOLSET_VERSION}")
elseif(APPLE)
set(CPACK_PACKAGE_FILE_NAME "${MIP_FILE_NAME_PREFIX}_OSX")
elseif(UNIX)
set(CPACK_PACKAGE_FILE_NAME "${MIP_FILE_NAME_PREFIX}_Linux")
else()
set(CPACK_PACKAGE_FILE_NAME "${MIP_FILE_NAME_PREFIX}")
endif()
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
# Package everything into a single group (includes all MIP and MicroStrain SDK files/libs in one package)
set(CPACK_COMPONENTS_GROUPING ALL_COMPONENTS_IN_ONE)
# Finally include cpack which should have taken all of the previous variables into consideration
include(CPack)
endif()