generated from pbosetti/xtemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
353 lines (329 loc) · 13 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
cmake_minimum_required(VERSION 3.16)
# _____ _ _ _
# | __ \ | (_) (_)
# | |__) | __ ___| |_ _ __ ___ _ _ __ __ _ _ __ _ _
# | ___/ '__/ _ \ | | '_ ` _ \| | '_ \ / _` | '__| | | |
# | | | | | __/ | | | | | | | | | | | (_| | | | |_| |
# |_| |_| \___|_|_|_| |_| |_|_|_| |_|\__,_|_| \__, |
# __/ |
# |___/
# GIT versioning - It requires at leas one git tag e.g. 0.0.1
# create it with: git tag -am " " 0.0.1
execute_process(
COMMAND git describe --long --dirty=X
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(
COMMAND git describe --abbrev=0 --tags
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_VERSION_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if("${GIT_VERSION_TAG}" STREQUAL "")
message(WARNING "Could not figure out tag")
project(Mightex1304 LANGUAGES C)
else()
project(Mightex1304 VERSION "${GIT_VERSION_TAG}" LANGUAGES C)
endif()
# Override build type (Debug or Release)
# set(CMAKE_BUILD_TYPE Debug)
# set(CMAKE_BUILD_TYPE Release)
message(STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
if(NOT WIN32)
# Libusb
set(LIBUSB_VERSION 1.0.24)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(LIBUSB_NO_DEBUG --disable-log)
endif()
include(ExternalProject)
ExternalProject_Add(libusb_prj
URL https://github.com/libusb/libusb/releases/download/v${LIBUSB_VERSION}/libusb-${LIBUSB_VERSION}.tar.bz2
PREFIX ${CMAKE_SOURCE_DIR}/vendor
BUILD_IN_SOURCE TRUE
CONFIGURE_COMMAND ./configure --prefix=${CMAKE_SOURCE_DIR}/vendor --enable-udev=no --with-pic -enable-shared=no ${LIBUSB_NO_DEBUG}
BUILD_COMMAND make
INSTALL_COMMAND make install
)
ExternalProject_Get_Property(libusb_prj INSTALL_DIR)
message(STATUS "Installing libusb in ${INSTALL_DIR}")
add_library(libusb STATIC IMPORTED)
set_property(TARGET libusb PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libusb-1.0.a)
endif()
# _____ _____
# / ____| / ____|
# | | _ __ ___ ___ ___ ______| | ___ _ __ ___ _ __
# | | | '__/ _ \/ __/ __|______| | / _ \| '_ ` _ \| '_ \
# | |____| | | (_) \__ \__ \ | |___| (_) | | | | | | |_) |
# \_____|_| \___/|___/___/ \_____\___/|_| |_| |_| .__/
# | |
# |_|
# Find out target platform
if (NOT WIN32)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpmachine OUTPUT_VARIABLE TARGET_PLATFORM) #put into TARGET_PLATFORM result of $ gcc -dumpmachine
string(REPLACE "\n" "" TARGET_PLATFORM ${TARGET_PLATFORM})
string(REGEX MATCH mipsel-openwrt-linux-musl IS_MIPSEL ${TARGET_PLATFORM})
string(REGEX MATCH armv7-unknown-linux-gnueabi IS_ARM7 ${TARGET_PLATFORM})
string(REGEX MATCH arm-linux-gnueabihf IS_ARM6 ${TARGET_PLATFORM})
string(REGEX MATCH arm-cortexa8_neon-linux-gnueabihf IS_ARM7A ${TARGET_PLATFORM})
string(REGEX MATCH aarch64-unknown-linux-gnueabi IS_ARM64 ${TARGET_PLATFORM})
if (IS_ARM6 OR IS_ARM7 OR IS_ARM7A OR IS_ARM64)
set(IS_ARM TRUE)
endif()
# SETUP PATHS AND VARIABLES
if(IS_MIPSEL)
message(STATUS "Cross-building for MIPSEL (cross_root: ${cross_root})")
set(LUA_LIB lua)
add_definitions(-D_OPENWRT_LINUX=1)
include_directories(${cross_root}/include)
link_directories(${cross_root}/lib)
elseif(IS_ARM)
message(STATUS "Cross-building for ARM (cross_root: ${cross_root})")
set(LINUX TRUE)
set(LUA_LIB lua)
include_directories(${cross_root}/include ${cross_root}/include/libusb-1.0)
link_directories(${cross_root}/lib)
add_definitions(-D__ARM7__)
elseif(UNIX AND NOT APPLE)
message(STATUS "Building on Linux")
set(LINUX TRUE)
set(NATIVE TRUE)
set(LUA_LIB lua5.3)
include_directories(/usr/include/${LUA_LIB} /usr/local/include ${CMAKE_SOURCE_DIR}/vendor/include)
link_directories(/usr/lib/x86_64-linux-gnu ${CMAKE_SOURCE_DIR}/vendor/lib)
elseif(APPLE)
message(STATUS "Building on Apple OS X")
set(LUA_LIB lua)
set(NATIVE TRUE)
include_directories(/usr/local/include /usr/local/include/lua ${CMAKE_SOURCE_DIR}/vendor/include)
link_directories(${CMAKE_CURRENT_LIST_DIR}/vendor/lib)
set(FRAMEWORKS "-framework CoreFoundation -framework IOKit")
endif()
# COMPILE OPTIONS
add_compile_options(-std=gnu11 -fPIC -D_GNU_SOURCE)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
message(STATUS "Debug mode, enabling all warnings")
add_compile_options(-Wall -Wno-comment)
endif()
set(LIBUSB_NAME libusb)
else() #WIN32
include_directories(${CMAKE_SOURCE_DIR}/win/include)
link_directories(${CMAKE_SOURCE_DIR}/win/lib)
set(LIBUSB_NAME libusb-1.0.lib)
set(TARGET_PLATFORM "Windows")
set(NATIVE TRUE)
endif()
include_directories("${CMAKE_SOURCE_DIR}/src")
# _____ _ _ _____ _ _ _
# | __ \ (_) | | | __ \ | | (_) |
# | |__) | __ ___ _ ___ ___| |_ | | | | ___| |_ __ _ _| |___
# | ___/ '__/ _ \| |/ _ \/ __| __| | | | |/ _ \ __/ _` | | / __|
# | | | | | (_) | | __/ (__| |_ | |__| | __/ || (_| | | \__ \
# |_| |_| \___/| |\___|\___|\__| |_____/ \___|\__\__,_|_|_|___/
# _/ |
# |__/
# FILES
# expect all sources in /src, except mains
set(SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
set(VENDOR ${CMAKE_CURRENT_LIST_DIR}/vendor)
# generate defines.h, which also contains version numbers matching git tags
configure_file(
${SOURCE_DIR}/defines.h.in
${SOURCE_DIR}/defines.h
)
file(GLOB LIB_SOURCES "${SOURCE_DIR}/*.c")
file(GLOB HEADERS "${SOURCE_DIR}/*.h" "${SOURCE_DIR}/*.hh")
if(WIN32) # On windows, getopt is missing, provide local implementation
file(GLOB WIN_LIB_SOURCES "${CMAKE_CURRENT_LIST_DIR}/win/src/*.c")
file(GLOB WIN_HEADERS "${CMAKE_CURRENT_LIST_DIR}/win/include/*.h")
list(APPEND LIB_SOURCES ${WIN_LIB_SOURCES})
list(APPEND HEADERS ${WIN_HEADERS})
endif()
if(NATIVE)
execute_process(COMMAND swig -lua -c++ -o ${SOURCE_DIR}/wrappers/mightex_lua.cpp ${SOURCE_DIR}/mightex.hh)
execute_process(COMMAND swig -python -c++ -o ${SOURCE_DIR}/wrappers/mightex_py.cpp ${SOURCE_DIR}/mightex.hh)
execute_process(COMMAND swig -ruby -c++ -o ${SOURCE_DIR}/wrappers/mightex_rb.cpp ${SOURCE_DIR}/mightex.hh)
endif()
# libs
include(CheckLibraryExists)
check_library_exists(m sqrt "" HAVE_LIB_M)
if(HAVE_LIB_M)
message(STATUS "Including libm")
list(APPEND EXTRA_LIBS m)
else()
message(STATUS "libm not needed")
endif()
include(FindThreads)
if(Threads_FOUND)
if("${CMAKE_THREAD_LIBS_INIT}" STREQUAL "")
message(STATUS "Threads library don't need link flag")
else()
message(STATUS "Found Threads library, linking with " ${CMAKE_THREAD_LIBS_INIT})
endif()
else()
message(ERROR "Thread library not found!")
endif()
# TARGETS
add_library(mightex_static STATIC ${LIB_SOURCES})
target_link_libraries(mightex_static ${LIBUSB_NAME} ${FRAMEWORKS} ${CMAKE_THREAD_LIBS_INIT})
add_library(mightex_shared SHARED ${LIB_SOURCES})
target_link_libraries(mightex_shared ${LIBUSB_NAME} ${FRAMEWORKS})
set_target_properties(mightex_shared PROPERTIES PREFIX "lib" OUTPUT_NAME "mightex")
set_target_properties(mightex_shared PROPERTIES PUBLIC_HEADER "${HEADERS}")
add_executable(grab ${SOURCE_DIR}/main/grab.c)
target_link_libraries(grab mightex_static ${EXTRA_LIBS})
add_executable(listusb ${SOURCE_DIR}/main/listusb.c)
target_link_libraries(listusb ${LIBUSB_NAME} ${FRAMEWORKS} ${CMAKE_THREAD_LIBS_INIT})
if (NOT WIN32)
add_dependencies(mightex_static libusb libusb_prj)
add_dependencies(mightex_shared libusb libusb_prj)
add_dependencies(listusb libusb libusb_prj)
else()
set_target_properties(grab PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT")
set_target_properties(listusb PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT")
set_target_properties(mightex_shared PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT")
endif()
list(APPEND TARGETS_LIST
grab listusb
mightex_static mightex_shared
)
# _____ _ _ _
# |_ _| | | | | |
# | | _ __ ___| |_ __ _| | |
# | | | '_ \/ __| __/ _` | | |
# _| |_| | | \__ \ || (_| | | |
# |_____|_| |_|___/\__\__,_|_|_|
if(NATIVE)
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR}/products_host)
else()
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR}/products)
endif()
install(TARGETS ${TARGETS_LIST}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/matlab
DESTINATION interfaces
PATTERN "*.asv" EXCLUDE
PATTERN "*.pdf" EXCLUDE
)
install(DIRECTORY ${SOURCE_DIR}/wrappers
DESTINATION interfaces
)
if(WIN32)
install(FILES ${CMAKE_CURRENT_LIST_DIR}/win/lib/libusb-1.0.lib
DESTINATION lib
)
else()
install(FILES ${CMAKE_CURRENT_LIST_DIR}/vendor/lib/libusb-1.0.a
DESTINATION lib
)
endif()
install(FILES ${SOURCE_DIR}/main/grab.c
DESTINATION examples
)
install(FILES ${CMAKE_CURRENT_LIST_DIR}/README_binary.md ${CMAKE_CURRENT_LIST_DIR}/LICENSE
DESTINATION .
)
if(BUILD_DOC)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/doc
DESTINATION .
)
endif()
# ____
# | _ \ ___ ___ ___
# | | | |/ _ \ / __/ __|
# | |_| | (_) | (__\__ \
# |____/ \___/ \___|___/
option(BUILD_DOC "Build documentation" OFF)
find_package(Doxygen)
if (BUILD_DOC AND DOXYGEN_FOUND)
set(DOXYGEN_IN ${CMAKE_CURRENT_LIST_DIR}/Doxyfile.conf)
set(DOXYGEN_OUT ${CMAKE_CURRENT_LIST_DIR}/doc/Doxyfile)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
message(STATUS "Doxygen documentation can be build with `make doc`")
add_custom_target(doc
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM
)
elseif (BUILD_DOC AND NOT DOXYGEN_FOUND)
message(ERROR "Doxygen need to be installed to generate the doxygen documentation")
endif (BUILD_DOC AND DOXYGEN_FOUND)
# _____ _ _
# | __ \ | | (_)
# | |__) |_ _ ___| | ____ _ __ _ _ _ __ __ _
# | ___/ _` |/ __| |/ / _` |/ _` | | '_ \ / _` |
# | | | (_| | (__| < (_| | (_| | | | | | (_| |
# |_| \__,_|\___|_|\_\__,_|\__, |_|_| |_|\__, |
# __/ | __/ |
# |___/ |___/
if(WIN32)
set(CPACK_GENERATOR "ZIP")
else()
set(CPACK_GENERATOR "TGZ")
endif()
if("${GIT_COMMIT_HASH}" STREQUAL "")
set(CPACK_PACKAGE_VERSION "no_version")
else()
set(CPACK_PACKAGE_VERSION ${GIT_COMMIT_HASH})
endif()
set(CPACK_PACKAGE_NAME "mightex1304-${CMAKE_BUILD_TYPE}")
set(CPACK_PACKAGE_DIRECTORY ${CMAKE_SOURCE_DIR}/packages)
set(CPACK_STRIP_FILES TRUE)
include(CPack)
# _____ _
# |_ _|__ ___| |_ ___
# | |/ _ \/ __| __/ __|
# | | __/\__ \ |_\__ \
# |_|\___||___/\__|___/
enable_testing()
add_test(grab_help ${CMAKE_CURRENT_BINARY_DIR}/grab -h)
add_test(listusb_help ${CMAKE_CURRENT_BINARY_DIR}/listusb -h)
# _____ _ __ _ _
# | __ \ | | / _(_) |
# | | | | ___ ___| | _____ _ __| |_ _| | ___
# | | | |/ _ \ / __| |/ / _ \ '__| _| | |/ _ \
# | |__| | (_) | (__| < __/ | | | | | | __/
# |_____/ \___/ \___|_|\_\___|_| |_| |_|_|\___|
#
# if not cross-compiling, create Dockerfile for the target platform
if(NATIVE AND NOT WIN32)
option(ENABLE_MRUBY "Enable build of mruby in container" OFF)
if(NOT ENABLE_MRUBY)
message(STATUS "mruby is NOT compiled in the container")
set(COMMENT_ENABLE_MRUBY "#")
endif()
option(ENABLE_OPENBLAS "Enable build of OpenBLAS in container" OFF)
if(NOT ENABLE_OPENBLAS)
message(STATUS "OpenBLAS is NOT compiled in the container")
set(COMMENT_ENABLE_OPENBLAS "#")
endif()
option(KEEP_BUILD_DIR "Do not delete build dir in Docker container" OFF)
if(KEEP_BUILD_DIR)
message(STATUS "Build dir is NOT deleted after compilation")
set(COMMENT_BUILD_DIR "#")
endif()
set(TARGET_NAME CACHE STRING "Select TARGET platform")
if(NOT TARGET_NAME)
set(TARGET_NAME "armv7")
endif()
set_property(CACHE TARGET_NAME PROPERTY STRINGS mipsel armv6 armv7 armv7a arm64)
configure_file(
${CMAKE_HOME_DIRECTORY}/Dockerfile.in
${CMAKE_HOME_DIRECTORY}/Dockerfile
@ONLY
)
message(STATUS "Host Dockerfile generated for target ${TARGET_NAME}")
message(STATUS "Building of mruby (libmruby.a) is: ${ENABLE_MRUBY}")
message(STATUS "Keep the build root directory: ${KEEP_BUILD_DIR}")
message(STATUS "Build with: docker build -t ${TARGET_NAME} .")
message(STATUS "then: docker run --rm ${TARGET_NAME} > ${TARGET_NAME} && chmod a+x ${TARGET_NAME}")
endif()