-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
223 lines (194 loc) · 7.82 KB
/
CMakeLists.txt
File metadata and controls
223 lines (194 loc) · 7.82 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
cmake_minimum_required(VERSION 3.10)
project(ScienceMode
VERSION 4.0.0
DESCRIPTION "ScienceMode communication library for FES devices"
LANGUAGES C
)
set(CMAKE_C_STANDARD 99)
# Options to enable different layers
option(SMPT_LOW_LEVEL "Enable Low-Level module" ON)
option(SMPT_MID_LEVEL "Enable Mid-Level module" ON)
option(SMPT_DYSCOM_LEVEL "Enable Dyscom-Level module" ON)
option(BUILD_SHARED_LIBS "Build shared libraries instead of static" OFF)
# Compiler warning options
option(ENABLE_WERROR "Treat warnings as errors" OFF)
option(ENABLE_STRICT_PROTOTYPE "Enable strict prototype warnings" OFF)
option(ENABLE_ALL_WARNINGS "Enable all warnings" OFF)
# Library source files
file(GLOB_RECURSE LIB_SOURCES
"ScienceMode_Library/src/general/*.c"
"ScienceMode_Library/src/general/packet/*.c"
"ScienceMode_Library/src/general/packet_input_buffer/*.c"
"ScienceMode_Library/src/general/packet_output_buffer/*.c"
"ScienceMode_Library/src/general/serial_port/*.c"
"ScienceMode_Library/src/low-level/*.c"
"ScienceMode_Library/src/mid-level/*.c"
"ScienceMode_Library/src/dyscom-level/*.c"
)
# Library include directories (only used internally)
set(LIBRARY_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general/packet
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general/packet_input_buffer
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general/packet_output_buffer
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general/serial_port
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/low-level
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/mid-level
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/dyscom-level
)
# Use BUILD_SHARED_LIBS option to determine library type
if(BUILD_SHARED_LIBS)
add_library(smpt SHARED ${LIB_SOURCES})
set_target_properties(smpt PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
OUTPUT_NAME "smpt" # Will create libsmpt.so
)
# When building shared libraries on Windows, export symbols
if(WIN32)
target_compile_definitions(smpt PRIVATE SMPT_EXPORTS)
target_compile_definitions(smpt PUBLIC SMPT_DLL)
endif()
else()
add_library(smpt STATIC ${LIB_SOURCES})
set_target_properties(smpt PROPERTIES
OUTPUT_NAME "smpt" # Will create libsmpt.a
)
if(WIN32)
target_compile_definitions(smpt PUBLIC SMPT_STATIC)
endif()
endif()
# Use proper include directories for both building and installing
target_include_directories(smpt
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general/packet>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general/packet_input_buffer>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general/packet_output_buffer>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/general/serial_port>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/low-level>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/mid-level>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library/include/dyscom-level>
$<INSTALL_INTERFACE:include/ScienceMode4>
)
if(NOT WIN32)
target_link_libraries(smpt PUBLIC m) # Link with math library
endif()
# Apply compiler warning flags
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
# Base warning flags that are always enabled
target_compile_options(smpt PRIVATE -Wall)
# Optional warning flags
if(ENABLE_STRICT_PROTOTYPE)
target_compile_options(smpt PRIVATE -Wstrict-prototypes)
endif()
if(ENABLE_ALL_WARNINGS)
target_compile_options(smpt PRIVATE -Wextra -pedantic)
endif()
# Treat warnings as errors if enabled
if(ENABLE_WERROR)
target_compile_options(smpt PRIVATE -Werror)
endif()
elseif(MSVC)
# MSVC warning options
target_compile_options(smpt PRIVATE /W4)
# Treat warnings as errors if enabled
if(ENABLE_WERROR)
target_compile_options(smpt PRIVATE /WX)
endif()
endif()
# Add compile definitions based on options
if(SMPT_LOW_LEVEL)
target_compile_definitions(smpt PUBLIC SMPT_LOW_LEVEL)
endif()
if(SMPT_MID_LEVEL)
target_compile_definitions(smpt PUBLIC SMPT_MID_LEVEL)
endif()
if(SMPT_DYSCOM_LEVEL)
target_compile_definitions(smpt PUBLIC SMPT_DYSCOM_LEVEL)
endif()
# Test source files
file(GLOB_RECURSE TEST_SOURCES
"ScienceMode_Library_Test/src/*.c"
)
# Test include directories
set(TEST_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library_Test/include
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library_Test/include/general
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library_Test/include/low-level
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library_Test/include/mid-level
${CMAKE_CURRENT_SOURCE_DIR}/ScienceMode_Library_Test/include/dyscom-level
# Do not include LIBRARY_INCLUDE_DIRS directly, as they'll be included via the target dependency
)
add_executable(sciencemode_tests ${TEST_SOURCES})
target_include_directories(sciencemode_tests PRIVATE ${TEST_INCLUDE_DIRS})
target_link_libraries(sciencemode_tests PRIVATE smpt)
# Propagate the same compile definitions to the test executable
if(SMPT_LOW_LEVEL)
target_compile_definitions(sciencemode_tests PRIVATE SMPT_LOW_LEVEL)
endif()
if(SMPT_MID_LEVEL)
target_compile_definitions(sciencemode_tests PRIVATE SMPT_MID_LEVEL)
endif()
if(SMPT_DYSCOM_LEVEL)
target_compile_definitions(sciencemode_tests PRIVATE SMPT_DYSCOM_LEVEL)
endif()
# Apply the same compiler warning flags to test executable
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
# Base warning flags that are always enabled
target_compile_options(sciencemode_tests PRIVATE -Wall)
# Optional warning flags
if(ENABLE_STRICT_PROTOTYPE)
target_compile_options(sciencemode_tests PRIVATE -Wstrict-prototypes)
endif()
if(ENABLE_ALL_WARNINGS)
target_compile_options(sciencemode_tests PRIVATE -Wextra -pedantic)
endif()
# Treat warnings as errors if enabled
if(ENABLE_WERROR)
target_compile_options(sciencemode_tests PRIVATE -Werror)
endif()
elseif(MSVC)
# MSVC warning options
target_compile_options(sciencemode_tests PRIVATE /W4)
# Treat warnings as errors if enabled
if(ENABLE_WERROR)
target_compile_options(sciencemode_tests PRIVATE /WX)
endif()
endif()
# Configure a ScienceModeConfig.cmake file
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/ScienceModeConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/ScienceModeConfig.cmake
@ONLY
)
# Install the config file
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/ScienceModeConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ScienceMode
)
# Add export targets for proper installation
include(GNUInstallDirs)
# Install targets
install(TARGETS smpt
EXPORT ScienceModeTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
# Install header files
install(DIRECTORY ScienceMode_Library/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ScienceMode4
)
# Install the export set
install(EXPORT ScienceModeTargets
FILE ScienceModeTargets.cmake
NAMESPACE ScienceMode::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ScienceMode
)
# Add testing support
enable_testing()
add_test(NAME ScienceModeTest COMMAND sciencemode_tests)