-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
350 lines (300 loc) · 13.2 KB
/
CMakeLists.txt
File metadata and controls
350 lines (300 loc) · 13.2 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
################################################################################
# CMake Build Configuration for MATLAB/Simulink S-Function
#
# This CMake script compiles a custom S-Function MEX file that integrates
# optical flow tracking capabilities into Simulink models.
#
# CMake variables (optional - FindMatlab will auto-detect):
# MATLAB_ROOT - MATLAB installation root directory
# MATLAB_BIN_DIR - Path to MATLAB bin directory (alternative to MATLAB_ROOT)
#
# Output files:
# - matlab_simulink_s_function.mex* (platform-specific MEX file)
# - include_directories.txt (for Simulink code generation)
# - link_directories.txt (for Simulink code generation)
# - link_libraries.txt (for Simulink code generation)
# - source_files.txt (for Simulink code generation)
################################################################################
cmake_minimum_required(VERSION 3.10)
project(matlab_simulink_s_function)
################################################################################
# Find MATLAB Installation
################################################################################
# Add cmake modules directory to module path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
# Find MATLAB (with MEX compiler required)
# FindMatlab will automatically search common installation paths
# You can override by setting: cmake -DMATLAB_ROOT=/path/to/matlab ..
find_package(Matlab REQUIRED COMPONENTS MEX_COMPILER)
if(NOT Matlab_FOUND)
message(FATAL_ERROR
"MATLAB not found. Please specify MATLAB location:\n"
" cmake -DMATLAB_ROOT=/path/to/matlab ..\n"
" OR\n"
" cmake -DMATLAB_BIN_DIR=/path/to/matlab/bin ..")
endif()
# Use the found MEX compiler
set(MEX_COMPILER "${MATLAB_MEX_COMPILER}")
# Platform-specific compiler flags
# Note: MEX-specific flags are handled in the build section below
set(CUSTOM_COMPILE_FLAGS "")
if(WIN32)
# Windows: /MT flag is handled via COMPFLAGS in build section
# Don't add it here to avoid path parsing issues
elseif(UNIX AND NOT APPLE)
# Linux-specific flags can be added here if needed
elseif(APPLE)
# macOS-specific flags can be added here if needed
else()
message(WARNING "Unsupported platform detected. Build may fail.")
endif()
################################################################################
# External Package Dependencies
################################################################################
set(CUSTOM_PACKAGES
OpenCV
)
foreach(PACKAGE ${CUSTOM_PACKAGES})
find_package(${PACKAGE} REQUIRED)
message(STATUS "Found ${PACKAGE}")
endforeach()
################################################################################
# Source Files and Include Directories
################################################################################
set(SRCS
${CMAKE_SOURCE_DIR}/src/s_function.cpp
${CMAKE_SOURCE_DIR}/src/optical_flow_velocity.cpp
)
set(INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/include")
################################################################################
# Build Compiler and Linker Flags
################################################################################
set(INCLUDE_FLAGS "")
set(LIB_FLAGS "")
set(INCLUDE_FLAGS_RAW "")
set(LIB_FLAGS_RAW "")
set(LINK_DIRS_RAW "")
list(APPEND INCLUDE_FLAGS_RAW "${INCLUDE_DIRS}")
# Collect include directories from all packages
foreach(PACKAGE ${CUSTOM_PACKAGES})
foreach(DIR ${${PACKAGE}_INCLUDE_DIRS})
string(APPEND INCLUDE_FLAGS "-I${DIR} ")
list(APPEND INCLUDE_FLAGS_RAW "${DIR} ")
endforeach()
endforeach()
# Add custom include directory
string(APPEND INCLUDE_FLAGS "-I${INCLUDE_DIRS} ")
# Collect library directories from all packages
foreach(PACKAGE ${CUSTOM_PACKAGES})
# Method 1: Check for explicitly defined library directory variables
if(DEFINED ${PACKAGE}_LIBRARY_DIRS)
foreach(DIR ${${PACKAGE}_LIBRARY_DIRS})
list(APPEND LINK_DIRS_RAW "${DIR}")
endforeach()
elseif(DEFINED ${PACKAGE}_LIBRARY_DIR)
list(APPEND LINK_DIRS_RAW "${${PACKAGE}_LIBRARY_DIR}")
elseif(DEFINED ${PACKAGE}_LIB_DIR)
list(APPEND LINK_DIRS_RAW "${${PACKAGE}_LIB_DIR}")
endif()
# Method 2: Extract directories from absolute library paths
foreach(LIB ${${PACKAGE}_LIBRARIES})
if(IS_ABSOLUTE "${LIB}")
get_filename_component(LIB_DIR "${LIB}" DIRECTORY)
list(APPEND LINK_DIRS_RAW "${LIB_DIR}")
endif()
endforeach()
# Method 3: Try to find libraries and extract their directories
# This handles cases where ${PACKAGE}_LIBRARIES contains only library names
foreach(LIB ${${PACKAGE}_LIBRARIES})
if(NOT IS_ABSOLUTE "${LIB}" AND NOT LIB MATCHES "\\.(so|a|lib|dll|dylib)$")
# It's just a library name, try to find it
set(LIB_SEARCH_NAMES "")
# Platform-specific library naming
if(WIN32)
list(APPEND LIB_SEARCH_NAMES "${LIB}.lib" "lib${LIB}.lib" "${LIB}.dll")
elseif(APPLE)
list(APPEND LIB_SEARCH_NAMES "lib${LIB}.dylib" "lib${LIB}.a" "${LIB}.dylib")
else()
list(APPEND LIB_SEARCH_NAMES "lib${LIB}.so" "lib${LIB}.a" "${LIB}.so")
endif()
# Try to locate the library
find_library(${LIB}_LOCATION
NAMES ${LIB_SEARCH_NAMES}
HINTS ${${PACKAGE}_LIBRARY_DIRS} ${${PACKAGE}_LIBRARY_DIR}
NO_DEFAULT_PATH
)
if(NOT ${LIB}_LOCATION)
# Try system paths if not found
find_library(${LIB}_LOCATION NAMES ${LIB_SEARCH_NAMES})
endif()
if(${LIB}_LOCATION)
get_filename_component(LIB_DIR "${${LIB}_LOCATION}" DIRECTORY)
list(APPEND LINK_DIRS_RAW "${LIB_DIR}")
unset(${LIB}_LOCATION CACHE)
endif()
endif()
endforeach()
endforeach()
# Remove duplicate directories
if(LINK_DIRS_RAW)
list(REMOVE_DUPLICATES LINK_DIRS_RAW)
endif()
# Collect libraries from all packages
foreach(PACKAGE ${CUSTOM_PACKAGES})
foreach(LIB ${${PACKAGE}_LIBRARIES})
# For MEX compilation flags, use -l syntax (library name only)
# Extract just the library name without path or prefix/suffix
get_filename_component(LIB_NAME "${LIB}" NAME_WE)
string(REGEX REPLACE "^lib" "" LIB_NAME_CLEAN "${LIB_NAME}")
string(APPEND LIB_FLAGS "-l${LIB_NAME_CLEAN} ")
# For LIB_FLAGS_RAW, include the full library path with extension
set(LIB_FULL_PATH "")
if(IS_ABSOLUTE "${LIB}")
# LIB is already an absolute path, use as-is
set(LIB_FULL_PATH "${LIB}")
elseif(LIB MATCHES "\\.(so|a|lib|dll|dylib)$")
# LIB has an extension but is relative - use as-is
set(LIB_FULL_PATH "${LIB}")
elseif(EXISTS "${LIB}")
# LIB exists as a file path, use as-is
set(LIB_FULL_PATH "${LIB}")
else()
# LIB is just a library name, try to find the actual file
set(LIB_SEARCH_NAMES "")
# Platform-specific library naming
if(WIN32)
list(APPEND LIB_SEARCH_NAMES "${LIB}.lib" "lib${LIB}.lib" "${LIB}.dll")
elseif(APPLE)
list(APPEND LIB_SEARCH_NAMES "lib${LIB}.dylib" "lib${LIB}.a" "${LIB}.dylib")
else()
list(APPEND LIB_SEARCH_NAMES "lib${LIB}.so" "lib${LIB}.a" "${LIB}.so")
endif()
# Try to locate the library file
find_library(${LIB}_FULL_LOCATION
NAMES ${LIB_SEARCH_NAMES}
HINTS ${${PACKAGE}_LIBRARY_DIRS} ${${PACKAGE}_LIBRARY_DIR}
NO_DEFAULT_PATH
)
if(NOT ${LIB}_FULL_LOCATION)
# Try system paths if not found in package directories
find_library(${LIB}_FULL_LOCATION NAMES ${LIB_SEARCH_NAMES})
endif()
if(${LIB}_FULL_LOCATION)
set(LIB_FULL_PATH "${${LIB}_FULL_LOCATION}")
unset(${LIB}_FULL_LOCATION CACHE)
else()
# If not found, use the library name as-is
set(LIB_FULL_PATH "${LIB}")
endif()
endif()
list(APPEND LIB_FLAGS_RAW "${LIB_FULL_PATH}")
endforeach()
endforeach()
# Collect source files
set(SRC_FLAGS "")
foreach(SRC ${SRCS})
string(APPEND SRC_FLAGS "${SRC} ")
endforeach()
################################################################################
# Compile MEX S-Function
################################################################################
message(STATUS "")
message(STATUS "========================================================================")
message(STATUS "Compiling S-Function MEX file...")
message(STATUS "========================================================================")
message(STATUS "MEX Compiler: ${MEX_COMPILER}")
if(WIN32)
# On Windows, build arguments as a list for proper handling
set(MEX_ARGS -v)
# Add optimization flags for MSVC
list(APPEND MEX_ARGS "COMPFLAGS=\"$COMPFLAGS /O2 /MT\"")
# Add source files
separate_arguments(SRC_LIST WINDOWS_COMMAND ${SRC_FLAGS})
foreach(s IN LISTS SRC_LIST)
# ensure we don't accidentally keep stray semicolons inside items
list(APPEND MEX_ARGS "\"${s}\"")
endforeach()
# Add include directories
separate_arguments(INCLUDE_LIST WINDOWS_COMMAND ${INCLUDE_FLAGS})
foreach(s IN LISTS INCLUDE_LIST)
# ensure we don't accidentally keep stray semicolons inside items
list(APPEND MEX_ARGS "\"${s}\"")
endforeach()
# Add library flags
separate_arguments(LIB_LIST WINDOWS_COMMAND ${LIB_FLAGS})
foreach(s IN LISTS LIB_LIST)
# ensure we don't accidentally keep stray semicolons inside items
list(APPEND MEX_ARGS "\"${s}\"")
endforeach()
LIST(JOIN MEX_ARGS " " MEX_ARGS_RAW)
message(STATUS "Build command: \"${MEX_COMPILER}\" ${MEX_ARGS_RAW}")
message(STATUS "")
execute_process(
COMMAND powershell.exe -Command "& \"${MEX_COMPILER}\" ${MEX_ARGS_RAW}"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
RESULT_VARIABLE MEX_RESULT
OUTPUT_VARIABLE MEX_OUTPUT
ERROR_VARIABLE MEX_ERROR
)
# Show output on failure
if(NOT MEX_RESULT EQUAL "0")
message(STATUS "MEX Output: ${MEX_OUTPUT}")
message(STATUS "MEX Error: ${MEX_ERROR}")
message(STATUS "MEX Result: ${MEX_RESULT}")
endif()
else()
# On Unix, use shell to handle complex quoting
set(BUILD_FLAGS "-v CFLAGS='\$CFLAGS -O3' ${SRC_FLAGS} ${CUSTOM_COMPILE_FLAGS} ${INCLUDE_FLAGS} ${LIB_FLAGS}")
message(STATUS "Build flags: ${BUILD_FLAGS}")
message(STATUS "")
execute_process(
COMMAND bash -c "${MEX_COMPILER} ${BUILD_FLAGS}"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
RESULT_VARIABLE MEX_RESULT
)
endif()
if(NOT MEX_RESULT EQUAL "0")
message(FATAL_ERROR "S-Function compilation failed with exit code: ${MEX_RESULT}")
endif()
################################################################################
# Generate Configuration Files for Simulink Code Generation
################################################################################
# Debug: Show what will be written to files
message(STATUS "")
message(STATUS "Link directories for code generation:")
foreach(LINK_DIR ${LINK_DIRS_RAW})
message(STATUS " ${LINK_DIR}")
endforeach()
message(STATUS "")
message(STATUS "Library files for code generation:")
foreach(LIB_FILE ${LIB_FLAGS_RAW})
message(STATUS " ${LIB_FILE}")
endforeach()
# Write configuration files
write_file("${CMAKE_SOURCE_DIR}/include_directories.txt" "${INCLUDE_FLAGS_RAW}")
write_file("${CMAKE_SOURCE_DIR}/link_directories.txt" "${LINK_DIRS_RAW}")
write_file("${CMAKE_SOURCE_DIR}/link_libraries.txt" "${LIB_FLAGS_RAW}")
write_file("${CMAKE_SOURCE_DIR}/source_files.txt" "${SRC_FLAGS}")
message(STATUS "")
message(STATUS "========================================================================")
message(STATUS "S-Function compilation completed successfully!")
message(STATUS "========================================================================")
message(STATUS "")
message(STATUS "Generated MEX file: ${CMAKE_CURRENT_BINARY_DIR}/matlab_simulink_s_function.mex*")
message(STATUS "")
message(STATUS "Configuration files for Simulink code generation:")
message(STATUS " - include_directories.txt (Include directories)")
message(STATUS " - link_directories.txt (Library search paths)")
message(STATUS " - link_libraries.txt (Libraries with full paths)")
message(STATUS " - source_files.txt (Source files)")
message(STATUS "")
message(STATUS "To use these in Simulink code generation:")
message(STATUS " 1. Open Model Configuration Parameters")
message(STATUS " 2. Navigate to Code Generation > Custom Code")
message(STATUS " 3. Add the contents of these files to:")
message(STATUS " - Include directories -> 'Include directories'")
message(STATUS " - Link directories -> 'Library search path'")
message(STATUS " - Link libraries -> 'Libraries'")
message(STATUS " - Source files -> 'Source files'")
message(STATUS "========================================================================")
message(STATUS "")