forked from sccn/liblsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSLCMake.cmake
365 lines (336 loc) · 13 KB
/
LSLCMake.cmake
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
# Common functions and settings for LSL
message(STATUS "Included LSL CMake helpers, rev. 9")
# set build type and default install dir if not done already
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "CMAKE_BUILD_TYPE was default initialized to Release")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
endif()
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
# OR ((${MSVC_VERSION} GREATER_EQUAL 1910) AND ("${CMAKE_GENERATOR}" STREQUAL "Ninja"))
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH
"Where to put redistributable binaries" FORCE)
message(WARNING "CMAKE_INSTALL_PREFIX default initialized to ${CMAKE_INSTALL_PREFIX}")
endif()
# Try to find the labstreaminglayer library and enable
# the imported target LSL::lsl
#
# Use it with
# target_link_libraries(your_target_app PRIVATE LSL::lsl)
if(TARGET lsl)
add_library(LSL::lsl ALIAS lsl)
message(STATUS "Found target lsl in current build tree")
else()
message(STATUS "Trying to find package LSL")
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_LIST_DIR})
find_package(LSL REQUIRED)
endif()
# Generate folders for IDE targets (e.g., VisualStudio solutions)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Set runtime path, i.e. where shared libs are searched relative to the exe
if(APPLE)
list(APPEND CMAKE_INSTALL_RPATH "@executable_path/../LSL/lib")
list(APPEND CMAKE_INSTALL_RPATH "@executable_path/../lib")
list(APPEND CMAKE_INSTALL_RPATH "@executable_path/")
elseif(UNIX)
list(APPEND CMAKE_INSTALL_RPATH "\$ORIGIN/../LSL/lib:\$ORIGIN/../lib/:\$ORIGIN")
endif()
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "limited configs" FORCE)
# Qt5
set(CMAKE_INCLUDE_CURRENT_DIR ON) # Because the ui_mainwindow.h file.
# Enable automatic compilation of .cpp->.moc, xy.ui->ui_xy.h and resource files
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
# Boost
#SET(Boost_DEBUG OFF) #Switch this and next to ON for help debugging Boost problems.
#SET(Boost_DETAILED_FAILURE_MSG ON)
if(WIN32)
set(Boost_USE_STATIC_LIBS ON)
endif()
# LSL functions, mostly for Apps
# installs additional files (configuration etc.)
function(installLSLAuxFiles target)
get_target_property(is_bundle ${target} MACOSX_BUNDLE)
set(destdir ${PROJECT_NAME})
if(LSL_UNIXFOLDERS)
set(destdir bin/)
elseif(is_bundle AND APPLE)
set(destdir ${destdir}/${target}.app/Contents/MacOS)
endif()
if("${ARGV1}" STREQUAL "directory")
install(DIRECTORY ${ARGV2} DESTINATION ${destdir} COMPONENT "${PROJECT_NAME}")
else()
install(FILES ${ARGN} DESTINATION ${destdir} COMPONENT "${PROJECT_NAME}")
endif()
endfunction()
# installLSLApp: adds the specified target to the install list
#
# there are two install types for different use cases
# Windows users mostly want a folder per app that contains
# everything needed to run the app, whereas on Linux,
# OS X (homebrew) and Conda the libraries are installed
# separately to save space, ease upgrading and distribution
function(installLSLApp target)
set(CPACK_COMPONENT_${PROJECT_NAME}_DEPENDS liblsl PARENT_SCOPE)
if(LSL_UNIXFOLDERS)
install(TARGETS ${target}
COMPONENT "${PROJECT_NAME}"
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
)
else()
installLSLAppSingleFolder(${target})
endif()
endfunction()
macro(findQtInstallationTool qtdeploytoolname)
if(QT_QMAKE_EXE AND EXISTS ${QT_QMAKE_EXE})
message(STATUS "Qt deploy tool found at ${QT_QMAKE_EXE}")
return()
endif()
get_target_property(QT_QMAKE_EXE Qt5::qmake IMPORTED_LOCATION)
execute_process(COMMAND ${QT_QMAKE_EXE} -query QT_VERSION OUTPUT_VARIABLE QT_VERSION)
get_filename_component(QT_BIN_DIR "${QT_QMAKE_EXE}" DIRECTORY)
find_program (QT_DEPLOYQT_EXECUTABLE ${qtdeploytoolname} HINTS "${QT_BIN_DIR}")
if (NOT QT_DEPLOYQT_EXECUTABLE)
message(WARNING "Windeployqt wasn't found, installing ${PROJECT_NAME} will fail!")
return()
endif()
endmacro()
# installLSLAppSingleFolder: installs the app its folder and copies needed libraries
#
# when calling make install / ninja install the executable is installed to
# CMAKE_INSTALL_PREFIX/PROJECT_NAME/TARGET_NAME
# e.g. C:/LSL/BrainAmpSeries/BrainAmpSeries.exe
function(installLSLAppSingleFolder target)
install(TARGETS ${target}
COMPONENT "${PROJECT_NAME}"
BUNDLE DESTINATION ${PROJECT_NAME}
RUNTIME DESTINATION ${PROJECT_NAME}
LIBRARY DESTINATION ${PROJECT_NAME}/lib
)
set(appbin "${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/${target}${CMAKE_EXECUTABLE_SUFFIX}")
# Copy lsl library for WIN32 or MacOS.
# On Mac, dylib is only needed for macdeployqt and for non bundles when not using system liblsl.
# Copy anyway, and fixup_bundle can deal with the dylib already being present.
if(WIN32 OR APPLE)
installLSLAuxFiles(${target} $<TARGET_FILE:LSL::lsl>)
endif()
# do we need to install with Qt5?
get_target_property(TARGET_LIBRARIES ${target} LINK_LIBRARIES)
if(";${TARGET_LIBRARIES}" MATCHES ";Qt5::")
if(WIN32)
findQtInstallationTool("windeployqt")
if (QT_DEPLOYQT_EXECUTABLE)
file (TO_NATIVE_PATH "${QT_BIN_DIR}" QT_BIN_DIR_NATIVE)
# It's safer to use `\` separators in the Path, but we need to escape them
string (REPLACE "\\" "\\\\" QT_BIN_DIR_NATIVE "${QT_BIN_DIR_NATIVE}")
set(QT_DEPLOYQT_FLAGS --no-translations --no-system-d3d-compiler --no-opengl-sw --no-compiler-runtime)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${target}_$<CONFIG>_path"
CONTENT "$<TARGET_FILE:${target}>" # Full path to .exe file
)
get_filename_component(appdir ${appbin} DIRECTORY CACHE)
install (CODE "
file(READ \"${CMAKE_CURRENT_BINARY_DIR}/${target}_${CMAKE_BUILD_TYPE}_path\" _file)
message (STATUS \"Running Qt Deploy Tool for \${_file}\")
if (CMAKE_INSTALL_CONFIG_NAME STREQUAL \"Debug\")
set(QT_DEPLOYQT_FLAGS \"\${QT_DEPLOYQT_FLAGS} --debug\")
else ()
set(QT_DEPLOYQT_FLAGS \"\${QT_DEPLOYQT_FLAGS} --release\")
endif ()
execute_process(COMMAND
\"${CMAKE_COMMAND}\" -E env
\"Path=${QT_BIN_DIR_NATIVE};\$ENV{SystemRoot}\\\\System32;\$ENV{SystemRoot}\"
\"${QT_DEPLOYQT_EXECUTABLE}\"
${QT_DEPLOYQT_FLAGS} --dry-run --list mapping
\"${appbin}\"
OUTPUT_VARIABLE output
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REPLACE \"\\\\\" \"/\" _output \${output})
separate_arguments(_files WINDOWS_COMMAND \${_output})
while(_files)
list(GET _files 0 _src)
list(GET _files 1 _dest)
execute_process(
COMMAND \"${CMAKE_COMMAND}\" -E
copy \${_src} \"\${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/$\{_dest}\"
)
list(REMOVE_AT _files 0 1)
endwhile()
" COMPONENT ${target})
#add_custom_command(TARGET ${target} POST_BUILD
# COMMAND "${CMAKE_COMMAND}" -E env PATH="${QT_BIN_DIR}" "${QT_DEPLOYQT_EXECUTABLE}"
# ${QT_DEPLOYQT_FLAGS}
# \"$<TARGET_FILE:${target}>\")
endif()
set_property(GLOBAL APPEND PROPERTY
LSLMENU "${PROJECT_NAME}/${target}.exe" "${target}")
message(STATUS "MENU appended: ${target}")
elseif(APPLE)
# It should be enough to call fixup_bundle (see below),
# but this fails to install qt plugins (cocoa).
# Use macdeployqt instead (but this is bad at grabbing lsl dylib, so we did that above)
findQtInstallationTool("macdeployqt")
if(QT_DEPLOYQT_EXECUTABLE)
set(QT_DEPLOYQT_FLAGS "-verbose=1") # Adding -libpath=${CMAKE_INSTALL_PREFIX}/LSL/lib seems to do nothing, maybe deprecated
install(CODE "
message(STATUS \"Running Qt Deploy Tool...\")
#list(APPEND QT_DEPLOYQT_FLAGS -dmg)
if(CMAKE_INSTALL_CONFIG_NAME STREQUAL \"Debug\")
list(APPEND QT_DEPLOYQT_FLAGS -use-debug-libs)
endif()
execute_process(COMMAND
\"${QT_DEPLOYQT_EXECUTABLE}\"
\"${appbin}.app\"
${QT_DEPLOYQT_FLAGS}
)
")
endif()
endif()
elseif(APPLE)
# fixup_bundle appears to be broken for Qt apps. Use only for non-Qt.
get_target_property(target_is_bundle ${target} MACOSX_BUNDLE)
if(target_is_bundle)
install(CODE "
include(BundleUtilities)
set(BU_CHMOD_BUNDLE_ITEMS ON)
fixup_bundle(
${appbin}.app
\"\"
\"${CMAKE_INSTALL_PREFIX}/LSL/lib\"
)
"
COMPONENT ${PROJECT_NAME}
)
endif()
endif()
endfunction()
# default paths, versions and magic to guess it on windows
# guess default paths for Windows / VC
set(LATEST_QT_VERSION "5.11.2")
# Boost autoconfig:
# Original author: Ryan Pavlik <[email protected]> <[email protected]
# Released with the same license as needed to integrate into CMake.
# Modified by Chadwick Boulay Jan 2018
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(lslplatform 64)
else()
set(lslplatform 32)
endif()
if(WIN32 AND MSVC)
# see https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html
if(MSVC_VERSION EQUAL 1500)
set(VCYEAR 2008)
set(_vs_ver 9.0)
elseif(MSVC_VERSION EQUAL 1600)
set(VCYEAR 2010)
set(_vs_ver 10.0)
elseif(MSVC_VERSION EQUAL 1700)
set(VCYEAR 2012)
set(_vs_ver 11.0)
elseif(MSVC_VERSION EQUAL 1800)
set(VCYEAR 2013)
set(_vs_ver 12.0)
elseif(MSVC_VERSION EQUAL 1900)
set(VCYEAR 2015)
set(_vs_ver 14.0)
elseif(MSVC_VERSION GREATER 1910 AND MSVC_VERSION LESS 1929)
set(VCYEAR 2017)
set(_vs_ver 14.1)
# Also VS 2019, but it's binary compatible with 2017 so Boost
# and Qt still use the 2017 binaries
else()
message(WARNING "You're using an untested Visual C++ compiler (MSVC_VERSION: ${MSVC_VERSION}).")
endif()
if(NOT Qt5_DIR)
set(Qt5_DIR "C:/Qt/${LATEST_QT_VERSION}/msvc${VCYEAR}_${lslplatform}/lib/cmake/Qt5")
message(STATUS "You didn't specify a Qt5_DIR. I'm guessing it's ${Qt5_DIR}.")
message(STATUS "If you are building Apps that require Qt and if this is wrong then please add the correct dir:")
message(STATUS " -DQt5_DIR=/path/to/Qt5/5.x.y/msvc_20xy_z/lib/cmake/Qt5")
endif()
if((NOT BOOST_ROOT) AND (NOT Boost_INCLUDE_DIR) AND (NOT Boost_LIBRARY_DIR))
message(STATUS "Attempting to find Boost, whether or not you need it.")
set(_libdir "lib${lslplatform}-msvc-${_vs_ver}")
set(_haslibs)
if(EXISTS "c:/local")
file(GLOB _possibilities "c:/local/boost*")
list(REVERSE _possibilities)
foreach(DIR ${_possibilities})
if(EXISTS "${DIR}/${_libdir}")
list(APPEND _haslibs "${DIR}")
endif()
endforeach()
if(_haslibs)
list(APPEND CMAKE_PREFIX_PATH ${_haslibs})
find_package(Boost QUIET)
if(Boost_FOUND AND NOT Boost_LIBRARY_DIR)
set(BOOST_ROOT "${Boost_INCLUDE_DIR}" CACHE PATH "")
set(BOOST_LIBRARYDIR "${Boost_INCLUDE_DIR}/${_libdir}" CACHE PATH "")
endif()
endif()
endif()
endif()
if(NOT BOOST_ROOT)
message(STATUS "Did not find Boost. If you need it then set BOOST_ROOT and/or BOOST_LIBRARYDIR")
endif()
endif()
# CPack configuration
include(CPackComponent)
cpack_add_component(${PROJECT_NAME}
DISPLAY_NAME ${PROJECT_NAME}
DESCRIPTION "${PROJECT_NAME} ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
macro(LSLGenerateCPackConfig)
# top level CMakeLists.txt?
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
# CPack configuration
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(CPACK_STRIP_FILES ON)
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
set(CPACK_PACKAGE_NAME lsl)
if(APPLE)
set(CPACK_GENERATOR "TBZ2")
if(DEFINED ENV{OSXVER})
# Configured by Travis-CI for multi-osx builds.
set(LSL_OS "$ENV{OSXVER}")
else()
set(LSL_OS "OSX${lslplatform}")
endif(DEFINED ENV{OSXVER})
elseif(WIN32)
set(CPACK_GENERATOR "7Z") # you can create NSIS packages by calling 'cpack -G NSIS'
set(CPACK_NSIS_MODIFY_PATH ON)
set(LSL_OS "Win${lslplatform}")
get_property(CPACK_NSIS_MENU_LINKS GLOBAL PROPERTY LSLMENU)
message(STATUS "MENU: ${CPACK_NSIS_MENU_LINKS}")
elseif(UNIX)
set(CPACK_GENERATOR DEB)
set(CPACK_SET_DESTDIR 1)
set(CPACK_INSTALL_PREFIX "/usr")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Tristan Stenner <[email protected]>")
set(CPACK_DEBIAN_ENABLE_COMPONENT_DEPENDS 1)
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_PACKAGE_PRIORITY optional)
# include distribution name (e.g. trusty or xenial) in the file name
find_program(LSB_RELEASE lsb_release)
execute_process(COMMAND ${LSB_RELEASE} -cs
OUTPUT_VARIABLE LSB_RELEASE_CODENAME
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(CPACK_DEBIAN_PACKAGE_RELEASE ${LSB_RELEASE_CODENAME})
set(LSL_OS "Linux${lslplatform}-${LSB_RELEASE_CODENAME}")
endif()
get_cmake_property(CPACK_COMPONENTS_ALL COMPONENTS)
foreach(component ${CPACK_COMPONENTS_ALL})
string(TOUPPER ${component} COMPONENT)
message(STATUS "Setting packages name for ${COMPONENT}")
set(LSL_CPACK_FILENAME "${component}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}-${LSL_OS}")
message(STATUS "File name: ${LSL_CPACK_FILENAME}")
set("CPACK_DEBIAN_${COMPONENT}_FILE_NAME" "${LSL_CPACK_FILENAME}.deb")
set("CPACK_ARCHIVE_${COMPONENT}_FILE_NAME" ${LSL_CPACK_FILENAME})
#set(CPACK_DEBIAN_${component}_FILE_NAME "${FILENAME}.deb")
endforeach()
message(STATUS "Installing Components: ${CPACK_COMPONENTS_ALL}")
include(CPack)
endif()
endmacro()