flow: Call Srf::init() early in main().
[pulseview.git] / CMakeLists.txt
1 ##
2 ## This file is part of the PulseView project.
3 ##
4 ## Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 ## Copyright (C) 2012-2013 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6 ##
7 ## This program is free software: you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation, either version 2 of the License, or
10 ## (at your option) any later version.
11 ##
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ## GNU General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 ##
20
21 cmake_minimum_required(VERSION 2.8.12)
22
23 include(GNUInstallDirs)
24
25 project(pulseview)
26
27 # Let AUTOMOC and AUTOUIC process GENERATED files.
28 if(POLICY CMP0071)
29         cmake_policy(SET CMP0071 NEW)
30 endif()
31
32 # Only interpret if() arguments as variables or keywords when unquoted.
33 if(POLICY CMP0054)
34         cmake_policy(SET CMP0054 NEW)
35 endif()
36
37 list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")
38
39 #===============================================================================
40 #= User Options
41 #-------------------------------------------------------------------------------
42
43 option(DISABLE_WERROR "Build without -Werror" TRUE)
44 option(ENABLE_SIGNALS "Build with UNIX signals" TRUE)
45 option(ENABLE_STACKTRACE "Enable stack trace when crashing" FALSE)
46 option(ENABLE_DECODE "Build with libsigrokdecode" TRUE)
47 option(ENABLE_FLOW "Build with libsigrokflow" TRUE)
48 option(ENABLE_TESTS "Enable unit tests" FALSE)
49 option(STATIC_PKGDEPS_LIBS "Statically link to (pkg-config) libraries" FALSE)
50
51 if(WIN32)
52         # On Windows/MinGW we need to statically link to libraries.
53         # This option is user configurable, but enable it by default on win32.
54         set(STATIC_PKGDEPS_LIBS TRUE)
55
56         # Windows does not support UNIX signals.
57         set(ENABLE_SIGNALS FALSE)
58 endif()
59
60 if(NOT CMAKE_BUILD_TYPE)
61         set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
62         "Choose the type of build (None, Debug, Release, RelWithDebInfo, MinSizeRel)."
63         FORCE)
64 endif()
65
66 #===============================================================================
67 #= Documentation
68 #-------------------------------------------------------------------------------
69
70 add_subdirectory(manual)
71
72 #===============================================================================
73 #= Dependencies
74 #-------------------------------------------------------------------------------
75
76 list(APPEND PKGDEPS glib-2.0>=2.28.0)
77 list(APPEND PKGDEPS glibmm-2.4>=2.28.0)
78
79 if(ENABLE_FLOW)
80         list(APPEND PKGDEPS gstreamermm-1.0>=1.8.0)
81         list(APPEND PKGDEPS libsigrokflow>=0.1.0)
82 endif()
83
84 set(LIBSR_CXX_BINDING "libsigrokcxx>=0.5.1")
85 list(APPEND PKGDEPS "${LIBSR_CXX_BINDING}")
86
87 if(ENABLE_DECODE)
88         list(APPEND PKGDEPS libsigrokdecode>=0.5.2)
89 endif()
90
91 if(ANDROID)
92         list(APPEND PKGDEPS libsigrokandroidutils>=0.1.0)
93 endif()
94
95 find_package(PkgConfig)
96 pkg_check_modules(LIBSRCXX ${LIBSR_CXX_BINDING})
97 if(NOT LIBSRCXX_FOUND OR NOT LIBSRCXX_VERSION)
98         message(FATAL_ERROR "libsigrok C++ bindings missing, check libsigrok's 'configure' output (missing dependencies?)")
99 endif()
100 pkg_check_modules(PKGDEPS REQUIRED ${PKGDEPS})
101
102 set(CMAKE_AUTOMOC TRUE)
103
104 find_package(Qt5 COMPONENTS Core Gui Widgets Svg REQUIRED)
105
106 if(WIN32)
107         # MXE workaround: Use pkg-config to find Qt5 libs.
108         # https://github.com/mxe/mxe/issues/1642
109         # Not required (and doesn't work) on MSYS2.
110         if(NOT DEFINED ENV{MSYSTEM})
111                 pkg_check_modules(QT5ALL REQUIRED Qt5Widgets Qt5Gui Qt5Svg)
112         endif()
113 endif()
114
115 set(QT_LIBRARIES Qt5::Gui Qt5::Widgets Qt5::Svg)
116
117 set(BOOSTCOMPS filesystem serialization system)
118 if(ENABLE_TESTS)
119         list(APPEND BOOSTCOMPS unit_test_framework)
120 endif()
121
122 if(ENABLE_STACKTRACE)
123         find_package(Boost 1.65.1 COMPONENTS ${BOOSTCOMPS} REQUIRED)
124 else()
125         find_package(Boost 1.55 COMPONENTS ${BOOSTCOMPS} REQUIRED)
126 endif()
127
128 # Find the platform's thread library (needed for C++11 threads).
129 # This will set ${CMAKE_THREAD_LIBS_INIT} to the correct, OS-specific value.
130 find_package(Threads REQUIRED)
131
132 # Check for explicit link against libatomic
133 #
134 # Depending on the toolchain, linking a program using atomic functions may need
135 # "-latomic" explicitly passed to the linker
136 #
137 # This check first tests if atomics are available in the C-library, if not and
138 # libatomic exists, then it runs the same test with -latomic added to the
139 # linker flags.
140
141 # Helper for checking for atomics
142 function(check_working_cxx_atomics varname additional_lib)
143   include(CheckCXXSourceCompiles)
144   include(CMakePushCheckState)
145   cmake_push_check_state()
146   set(CMAKE_REQUIRED_FLAGS "-std=c++11")
147   set(CMAKE_REQUIRED_LIBRARIES "${additional_lib}")
148   set(CMAKE_REQUIRED_QUIET 1)
149   CHECK_CXX_SOURCE_COMPILES("
150 #include <atomic>
151 std::atomic<int> x;
152 int main() {
153   return std::atomic_fetch_add_explicit(&x, 1, std::memory_order_seq_cst);
154 }
155 " ${varname})
156   cmake_pop_check_state()
157 endfunction(check_working_cxx_atomics)
158
159 # First check if atomics work without the library.
160 # If not, check if the library exists, and atomics work with it.
161 check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB "")
162 if(HAVE_CXX_ATOMICS_WITHOUT_LIB)
163   message(STATUS "Atomics provided by the C-library - yes")
164 else()
165   message(STATUS "Atomics provided by the C-library - no")
166   find_library(LIBATOMIC_LIBRARY NAMES atomic PATH_SUFFIXES lib)
167   if(LIBATOMIC_LIBRARY)
168     check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB "${LIBATOMIC_LIBRARY}")
169     if (HAVE_CXX_ATOMICS_WITH_LIB)
170       message(STATUS "Atomics provided by libatomic - yes")
171     else()
172       message(STATUS "Atomics provided by libatomic - no")
173       message(FATAL_ERROR "Compiler must support std::atomic!")
174     endif()
175   else()
176     message(FATAL_ERROR "Compiler appears to require libatomic, but cannot find it.")
177   endif()
178 endif()
179
180 #===============================================================================
181 #= System Introspection
182 #-------------------------------------------------------------------------------
183
184 include(memaccess)
185 memaccess_check_unaligned_le(HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS)
186
187 #===============================================================================
188 #= Config Header
189 #-------------------------------------------------------------------------------
190
191 set(PV_TITLE PulseView)
192 set(PV_VERSION_STRING "0.5.0")
193
194 set(PV_GLIBMM_VERSION ${PKGDEPS_glibmm-2.4_VERSION})
195
196 include(GetGitRevisionDescription)
197
198 # Append the revision hash unless we are exactly on a tagged release.
199 git_describe(PV_TAG_VERSION_STRING --match "pulseview-${PV_VERSION_STRING}" --exact-match)
200 if(NOT PV_TAG_VERSION_STRING)
201         get_git_head_revision(PV_REVSPEC PV_HASH)
202         if(PV_HASH)
203                 string(SUBSTRING "${PV_HASH}" 0 7 PV_SHORTHASH)
204                 set(PV_VERSION_STRING "${PV_VERSION_STRING}-git-${PV_SHORTHASH}")
205         endif()
206
207         # Non-tagged releases use the unstable manual
208         set(PV_MANUAL_VERSION "unstable")
209 else()
210         # Tagged releases use a fixed manual version
211         set(PV_MANUAL_VERSION ${PV_VERSION_STRING})
212 endif()
213
214 if(PV_VERSION_STRING MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[-0-9a-z]*)?$")
215         set(PV_VERSION_MAJOR ${CMAKE_MATCH_1})
216         set(PV_VERSION_MINOR ${CMAKE_MATCH_2})
217         set(PV_VERSION_MICRO ${CMAKE_MATCH_3})
218         set(PV_VERSION_SUFFIX ${CMAKE_MATCH_4})
219 endif()
220
221 message(STATUS "${PV_TITLE} version: ${PV_VERSION_STRING}")
222
223 configure_file (
224         ${PROJECT_SOURCE_DIR}/config.h.in
225         ${PROJECT_BINARY_DIR}/config.h
226 )
227
228 #===============================================================================
229 #= Sources
230 #-------------------------------------------------------------------------------
231
232 set(pulseview_SOURCES
233         main.cpp
234         pv/application.cpp
235         pv/devicemanager.cpp
236         pv/globalsettings.cpp
237         pv/logging.cpp
238         pv/mainwindow.cpp
239         pv/session.cpp
240         pv/storesession.cpp
241         pv/util.cpp
242         pv/binding/binding.cpp
243         pv/binding/inputoutput.cpp
244         pv/binding/device.cpp
245         pv/data/analog.cpp
246         pv/data/analogsegment.cpp
247         pv/data/logic.cpp
248         pv/data/logicsegment.cpp
249         pv/data/signalbase.cpp
250         pv/data/signaldata.cpp
251         pv/data/segment.cpp
252         pv/devices/device.cpp
253         pv/devices/file.cpp
254         pv/devices/hardwaredevice.cpp
255         pv/devices/inputfile.cpp
256         pv/devices/sessionfile.cpp
257         pv/dialogs/connect.cpp
258         pv/dialogs/inputoutputoptions.cpp
259         pv/dialogs/settings.cpp
260         pv/dialogs/storeprogress.cpp
261         pv/popups/deviceoptions.cpp
262         pv/popups/channels.cpp
263         pv/prop/bool.cpp
264         pv/prop/double.cpp
265         pv/prop/enum.cpp
266         pv/prop/int.cpp
267         pv/prop/property.cpp
268         pv/prop/string.cpp
269         pv/toolbars/mainbar.cpp
270         pv/views/trace/analogsignal.cpp
271         pv/views/trace/cursor.cpp
272         pv/views/trace/cursorpair.cpp
273         pv/views/trace/flag.cpp
274         pv/views/trace/header.cpp
275         pv/views/trace/marginwidget.cpp
276         pv/views/trace/logicsignal.cpp
277         pv/views/trace/rowitem.cpp
278         pv/views/trace/ruler.cpp
279         pv/views/trace/signal.cpp
280         pv/views/trace/timeitem.cpp
281         pv/views/trace/timemarker.cpp
282         pv/views/trace/trace.cpp
283         pv/views/trace/tracegroup.cpp
284         pv/views/trace/tracepalette.cpp
285         pv/views/trace/tracetreeitem.cpp
286         pv/views/trace/tracetreeitemowner.cpp
287         pv/views/trace/triggermarker.cpp
288         pv/views/trace/view.cpp
289         pv/views/trace/viewitem.cpp
290         pv/views/trace/viewitemowner.cpp
291         pv/views/trace/viewitempaintparams.cpp
292         pv/views/trace/viewport.cpp
293         pv/views/trace/viewwidget.cpp
294         pv/views/viewbase.cpp
295         pv/views/trace/standardbar.cpp
296         pv/widgets/colorbutton.cpp
297         pv/widgets/colorpopup.cpp
298         pv/widgets/devicetoolbutton.cpp
299         pv/widgets/exportmenu.cpp
300         pv/widgets/importmenu.cpp
301         pv/widgets/popup.cpp
302         pv/widgets/popuptoolbutton.cpp
303         pv/widgets/sweeptimingwidget.cpp
304         pv/widgets/timestampspinbox.cpp
305         pv/widgets/wellarray.cpp
306 )
307
308 # This list includes only QObject derived class headers.
309 set(pulseview_HEADERS
310         pv/logging.hpp
311         pv/globalsettings.hpp
312         pv/mainwindow.hpp
313         pv/session.hpp
314         pv/storesession.hpp
315         pv/binding/device.hpp
316         pv/data/analog.hpp
317         pv/data/analogsegment.hpp
318         pv/data/logic.hpp
319         pv/data/logicsegment.hpp
320         pv/data/signalbase.hpp
321         pv/dialogs/connect.hpp
322         pv/dialogs/inputoutputoptions.hpp
323         pv/dialogs/settings.hpp
324         pv/dialogs/storeprogress.hpp
325         pv/popups/channels.hpp
326         pv/popups/deviceoptions.hpp
327         pv/prop/bool.hpp
328         pv/prop/double.hpp
329         pv/prop/enum.hpp
330         pv/prop/int.hpp
331         pv/prop/property.hpp
332         pv/prop/string.hpp
333         pv/toolbars/mainbar.hpp
334         pv/views/trace/analogsignal.hpp
335         pv/views/trace/cursor.hpp
336         pv/views/trace/flag.hpp
337         pv/views/trace/header.hpp
338         pv/views/trace/logicsignal.hpp
339         pv/views/trace/marginwidget.hpp
340         pv/views/trace/rowitem.hpp
341         pv/views/trace/ruler.hpp
342         pv/views/trace/signal.hpp
343         pv/views/trace/timeitem.hpp
344         pv/views/trace/timemarker.hpp
345         pv/views/trace/trace.hpp
346         pv/views/trace/tracegroup.hpp
347         pv/views/trace/tracetreeitem.hpp
348         pv/views/trace/triggermarker.hpp
349         pv/views/trace/view.hpp
350         pv/views/trace/viewitem.hpp
351         pv/views/trace/viewport.hpp
352         pv/views/trace/viewwidget.hpp
353         pv/views/viewbase.hpp
354         pv/views/trace/standardbar.hpp
355         pv/widgets/colorbutton.hpp
356         pv/widgets/colorpopup.hpp
357         pv/widgets/devicetoolbutton.hpp
358         pv/widgets/exportmenu.hpp
359         pv/widgets/importmenu.hpp
360         pv/widgets/popup.hpp
361         pv/widgets/popuptoolbutton.hpp
362         pv/widgets/sweeptimingwidget.hpp
363         pv/widgets/timestampspinbox.hpp
364         pv/widgets/wellarray.hpp
365 )
366
367 set(pulseview_RESOURCES
368         pulseview.qrc
369 )
370
371 if(ENABLE_SIGNALS)
372         list(APPEND pulseview_SOURCES signalhandler.cpp)
373         list(APPEND pulseview_HEADERS signalhandler.hpp)
374 endif()
375
376 if(ENABLE_DECODE)
377         list(APPEND pulseview_SOURCES
378                 pv/binding/decoder.cpp
379                 pv/data/decodesignal.cpp
380                 pv/data/decode/annotation.cpp
381                 pv/data/decode/decoder.cpp
382                 pv/data/decode/row.cpp
383                 pv/data/decode/rowdata.cpp
384                 pv/views/trace/decodetrace.cpp
385                 pv/widgets/decodergroupbox.cpp
386                 pv/widgets/decodermenu.cpp
387         )
388
389         list(APPEND pulseview_HEADERS
390                 pv/data/decodesignal.hpp
391                 pv/views/trace/decodetrace.hpp
392                 pv/widgets/decodergroupbox.hpp
393                 pv/widgets/decodermenu.hpp
394         )
395 endif()
396
397 if(WIN32)
398         # Use the sigrok icon for the pulseview.exe executable.
399         set(CMAKE_RC_COMPILE_OBJECT "${CMAKE_RC_COMPILER} -O coff -I${CMAKE_CURRENT_SOURCE_DIR} <SOURCE> <OBJECT>")
400         enable_language(RC)
401         list(APPEND pulseview_SOURCES pulseviewico.rc)
402 endif()
403
404 if(ANDROID)
405         list(APPEND pulseview_SOURCES
406                 android/assetreader.cpp
407                 android/loghandler.cpp
408         )
409 endif()
410
411 qt5_add_resources(pulseview_RESOURCES_RCC ${pulseview_RESOURCES})
412
413 #===============================================================================
414 #= Global Definitions
415 #-------------------------------------------------------------------------------
416
417 add_definitions(-DQT_NO_KEYWORDS)
418 add_definitions(-D__STDC_LIMIT_MACROS)
419 add_definitions(-Wall -Wextra)
420 add_definitions(-std=c++11)
421 add_definitions(-DBOOST_MATH_DISABLE_FLOAT128=1)
422
423 if(ENABLE_FLOW)
424         add_definitions(-DENABLE_FLOW)
425 endif()
426
427 if(ENABLE_DECODE)
428         add_definitions(-DENABLE_DECODE)
429 endif()
430
431 if(NOT DISABLE_WERROR)
432         add_definitions(-Werror)
433 endif()
434
435 if(ENABLE_SIGNALS)
436         add_definitions(-DENABLE_SIGNALS)
437 endif()
438
439 if(ENABLE_STACKTRACE)
440         add_definitions(-DENABLE_STACKTRACE)
441 endif()
442
443 #===============================================================================
444 #= Global Include Directories
445 #-------------------------------------------------------------------------------
446
447 include_directories(
448         ${CMAKE_CURRENT_BINARY_DIR}
449         ${CMAKE_CURRENT_SOURCE_DIR}
450         ${Boost_INCLUDE_DIRS}
451 )
452
453 if(STATIC_PKGDEPS_LIBS)
454         include_directories(${PKGDEPS_STATIC_INCLUDE_DIRS})
455 else()
456         include_directories(${PKGDEPS_INCLUDE_DIRS})
457 endif()
458
459 #===============================================================================
460 #= Linker Configuration
461 #-------------------------------------------------------------------------------
462
463 link_directories(${Boost_LIBRARY_DIRS})
464
465 set(PULSEVIEW_LINK_LIBS
466         ${Boost_LIBRARIES}
467         ${QT_LIBRARIES}
468         ${CMAKE_THREAD_LIBS_INIT}
469         ${LIBATOMIC_LIBRARY}
470 )
471
472 if(STATIC_PKGDEPS_LIBS)
473         link_directories(${PKGDEPS_STATIC_LIBRARY_DIRS})
474         list(APPEND PULSEVIEW_LINK_LIBS ${PKGDEPS_STATIC_LDFLAGS})
475 else()
476         link_directories(${PKGDEPS_LIBRARY_DIRS})
477         list(APPEND PULSEVIEW_LINK_LIBS ${PKGDEPS_LIBRARIES})
478 endif()
479
480 if(WIN32)
481         # On Windows we need to statically link the libqsvg imageformat
482         # plugin (and the QtSvg component) for SVG graphics/icons to work.
483         # We also need QWindowsIntegrationPlugin, Qt5PlatformSupport (only for
484         # Qt < 5.8.0), and all Qt libs and their dependencies.
485         add_definitions(-DQT_STATICPLUGIN)
486         list(APPEND PULSEVIEW_LINK_LIBS Qt5::QSvgPlugin)
487         list(APPEND PULSEVIEW_LINK_LIBS Qt5::QWindowsIntegrationPlugin)
488         if(Qt5Gui_VERSION VERSION_LESS 5.8.0)
489                 list(APPEND PULSEVIEW_LINK_LIBS -lQt5PlatformSupport)
490         endif()
491         list(APPEND PULSEVIEW_LINK_LIBS ${QT5ALL_LDFLAGS})
492 endif()
493
494 if(ENABLE_STACKTRACE)
495         # Needed to resolve dladdr.
496         list(APPEND PULSEVIEW_LINK_LIBS "-ldl")
497 endif()
498
499 if(ANDROID)
500         list(APPEND PULSEVIEW_LINK_LIBS "-llog")
501 endif()
502
503 if(ANDROID)
504         add_library(${PROJECT_NAME} SHARED ${pulseview_SOURCES} ${pulseview_RESOURCES_RCC})
505 else()
506         add_executable(${PROJECT_NAME} ${pulseview_SOURCES} ${pulseview_RESOURCES_RCC})
507 endif()
508
509 target_link_libraries(${PROJECT_NAME} ${PULSEVIEW_LINK_LIBS})
510
511 if(WIN32 AND NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
512         # Pass -mwindows so that no "DOS box" opens when PulseView is started.
513         set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-mwindows")
514 endif()
515
516 #===============================================================================
517 #= Installation
518 #-------------------------------------------------------------------------------
519
520 # Install the executable.
521 install(TARGETS ${PROJECT_NAME} DESTINATION bin/)
522
523 # Install the manpage.
524 install(FILES doc/pulseview.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1 COMPONENT doc)
525
526 # Install the desktop file.
527 install(FILES contrib/org.sigrok.PulseView.desktop DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications)
528
529 # Install the AppData/AppStream file.
530 install(FILES contrib/org.sigrok.PulseView.appdata.xml DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/metainfo)
531
532 # Install the PulseView icons.
533 install(FILES icons/pulseview.png DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/48x48/apps)
534 install(FILES icons/pulseview.svg DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/scalable/apps)
535
536 # Generate Windows installer script.
537 configure_file(contrib/pulseview_cross.nsi.in contrib/pulseview_cross.nsi @ONLY)
538
539 #===============================================================================
540 #= Packaging (handled by CPack)
541 #-------------------------------------------------------------------------------
542
543 set(CPACK_PACKAGE_VERSION_MAJOR ${PV_VERSION_MAJOR})
544 set(CPACK_PACKAGE_VERSION_MINOR ${PV_VERSION_MINOR})
545 set(CPACK_PACKAGE_VERSION_PATCH ${PV_VERSION_MICRO})
546 set(CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/README)
547 set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/COPYING)
548 set(CPACK_SOURCE_IGNORE_FILES ${CMAKE_CURRENT_BINARY_DIR} ".gitignore" ".git")
549 set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${PV_VERSION_STRING}")
550 set(CPACK_SOURCE_GENERATOR "TGZ")
551
552 include(CPack)
553
554 #===============================================================================
555 #= Tests
556 #-------------------------------------------------------------------------------
557
558 if(ENABLE_TESTS)
559         add_subdirectory(test)
560         enable_testing()
561         add_test(test ${CMAKE_CURRENT_BINARY_DIR}/test/pulseview-test)
562 endif()