Tie the "always zoom to fit" setting in with the settings mgmt
[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.6)
22
23 include(GNUInstallDirs)
24
25 project(pulseview)
26
27 list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")
28
29 #===============================================================================
30 #= User Options
31 #-------------------------------------------------------------------------------
32
33 option(DISABLE_WERROR "Build without -Werror" FALSE)
34 option(ENABLE_SIGNALS "Build with UNIX signals" TRUE)
35 option(ENABLE_DECODE "Build with libsigrokdecode" TRUE)
36 option(ENABLE_TESTS "Enable unit tests" TRUE)
37 option(STATIC_PKGDEPS_LIBS "Statically link to (pkg-config) libraries" FALSE)
38 option(FORCE_QT4 "Force use of Qt4 even if Qt5 is available" FALSE)
39
40 if(WIN32)
41         # On Windows/MinGW we need to statically link to libraries.
42         # This option is user configurable, but enable it by default on win32.
43         set(STATIC_PKGDEPS_LIBS TRUE)
44
45         # For boost-thread we need two additional settings on win32:
46         set(Boost_USE_STATIC_LIBS ON)
47         add_definitions(-DBOOST_THREAD_USE_LIB)
48
49         # On Windows/MinGW we need to use 'thread_win32' instead of 'thread'.
50         # The library is named libboost_thread_win32* (not libboost_thread*).
51         set(Boost_THREADAPI win32)
52
53         # Windows does not support UNIX signals.
54         set(ENABLE_SIGNALS FALSE)
55 endif()
56
57 if(NOT CMAKE_BUILD_TYPE)
58         set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
59         "Choose the type of build (None, Debug, Release, RelWithDebInfo, MinSizeRel)."
60         FORCE)
61 endif()
62
63 #===============================================================================
64 #= Dependencies
65 #-------------------------------------------------------------------------------
66
67 list(APPEND PKGDEPS libsigrokcxx>=0.4.0)
68
69 if(ENABLE_DECODE)
70         list(APPEND PKGDEPS libsigrokdecode>=0.4.0)
71 endif()
72
73 if(ANDROID)
74         list(APPEND PKGDEPS libsigrokandroidutils>=0.1.0)
75 endif()
76
77 find_package(PkgConfig)
78 pkg_check_modules(PKGDEPS REQUIRED ${PKGDEPS})
79
80 if(FORCE_QT4)
81         set(Qt5Core_FOUND FALSE)
82 else()
83         find_package(Qt5Core QUIET)
84 if(WIN32)
85         # MXE workaround: Use pkg-config to find Qt5 libs.
86         # https://github.com/mxe/mxe/issues/1642
87         pkg_check_modules(QT5ALL REQUIRED Qt5Widgets Qt5Gui Qt5Svg)
88 endif()
89 endif()
90
91 if(Qt5Core_FOUND)
92         message("-- Using Qt5")
93         find_package(Qt5Widgets REQUIRED)
94         find_package(Qt5Gui REQUIRED)
95         find_package(Qt5Svg REQUIRED)
96         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
97         set(QT_INCLUDE_DIRS ${Qt5Gui_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS})
98         set(QT_LIBRARIES Qt5::Gui Qt5::Widgets Qt5::Svg)
99         add_definitions(${Qt5Gui_DEFINITIONS} ${Qt5Widgets_DEFINITIONS})
100 else()
101         find_program(QT_QMAKE_EXECUTABLE NAMES qmake4 qmake-qt4 qmake-mac)
102         find_package(Qt4 REQUIRED QtCore QtGui QtSvg)
103 endif()
104
105 set(BOOSTCOMPS filesystem serialization system thread)
106 if(ENABLE_TESTS)
107         list(APPEND BOOSTCOMPS unit_test_framework)
108 endif()
109 find_package(Boost 1.55 COMPONENTS ${BOOSTCOMPS} REQUIRED)
110
111 # Find the platform's thread library (needed for C++11 threads).
112 # This will set ${CMAKE_THREAD_LIBS_INIT} to the correct, OS-specific value.
113 find_package(Threads REQUIRED)
114
115
116 # Check for explicit link against libatomic
117 #
118 # Depending on the toolchain, linking a program using atomic functions may need
119 # "-latomic" explicitly passed to the linker
120 #
121 # This check first tests if atomics are available in the C-library, if not and
122 # libatomic exists, then it runs the same test with -latomic added to the
123 # linker flags.
124
125 # Helper for checking for atomics
126 function(check_working_cxx_atomics varname additional_lib)
127   include(CheckCXXSourceCompiles)
128   include(CMakePushCheckState)
129   cmake_push_check_state()
130   set(CMAKE_REQUIRED_FLAGS "-std=c++11")
131   set(CMAKE_REQUIRED_LIBRARIES "${additional_lib}")
132   set(CMAKE_REQUIRED_QUIET 1)
133   CHECK_CXX_SOURCE_COMPILES("
134 #include <atomic>
135 std::atomic<int> x;
136 int main() {
137   return std::atomic_fetch_add_explicit(&x, 1, std::memory_order_seq_cst);
138 }
139 " ${varname})
140   cmake_pop_check_state()
141 endfunction(check_working_cxx_atomics)
142
143 # First check if atomics work without the library.
144 # If not, check if the library exists, and atomics work with it.
145 check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB "")
146 if(HAVE_CXX_ATOMICS_WITHOUT_LIB)
147   message(STATUS "Atomics provided by the C-library - yes")
148 else()
149   message(STATUS "Atomics provided by the C-library - no")
150   find_library(LIBATOMIC_LIBRARY NAMES atomic PATH_SUFFIXES lib)
151   if(LIBATOMIC_LIBRARY)
152     check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB "${LIBATOMIC_LIBRARY}")
153     if (HAVE_CXX_ATOMICS_WITH_LIB)
154       message(STATUS "Atomics provided by libatomic - yes")
155     else()
156       message(STATUS "Atomics provided by libatomic - no")
157       message(FATAL_ERROR "Compiler must support std::atomic!")
158     endif()
159   else()
160     message(FATAL_ERROR "Compiler appears to require libatomic, but cannot find it.")
161   endif()
162 endif()
163
164 #===============================================================================
165 #= System Introspection
166 #-------------------------------------------------------------------------------
167
168 include(memaccess)
169 memaccess_check_unaligned_le(HAVE_UNALIGNED_LITTLE_ENDIAN_ACCESS)
170
171 #===============================================================================
172 #= Config Header
173 #-------------------------------------------------------------------------------
174
175 set(PV_TITLE PulseView)
176 set(PV_DESCRIPTION "A GUI for sigrok")
177 set(PV_VERSION_STRING "0.4.0")
178
179 include(GetGitRevisionDescription)
180
181 # Append the revision hash unless we are exactly on a tagged release.
182 git_describe(PV_TAG_VERSION_STRING --match "pulseview-${PV_VERSION_STRING}" --exact-match)
183 if(NOT PV_TAG_VERSION_STRING)
184         get_git_head_revision(PV_REVSPEC PV_HASH)
185         if(PV_HASH)
186                 string(SUBSTRING "${PV_HASH}" 0 7 PV_SHORTHASH)
187                 set(PV_VERSION_STRING "${PV_VERSION_STRING}-git-${PV_SHORTHASH}")
188         endif()
189 endif()
190
191 if(PV_VERSION_STRING MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(-[-0-9a-z]*)?$")
192         set(PV_VERSION_MAJOR ${CMAKE_MATCH_1})
193         set(PV_VERSION_MINOR ${CMAKE_MATCH_2})
194         set(PV_VERSION_MICRO ${CMAKE_MATCH_3})
195         set(PV_VERSION_SUFFIX ${CMAKE_MATCH_4})
196 endif()
197
198 message("-- ${PV_TITLE} version: ${PV_VERSION_STRING}")
199
200 configure_file (
201         ${PROJECT_SOURCE_DIR}/config.h.in
202         ${PROJECT_BINARY_DIR}/config.h
203 )
204
205 #===============================================================================
206 #= Sources
207 #-------------------------------------------------------------------------------
208
209 set(pulseview_SOURCES
210         main.cpp
211         pv/application.cpp
212         pv/devicemanager.cpp
213         pv/globalsettings.cpp
214         pv/mainwindow.cpp
215         pv/session.cpp
216         pv/storesession.cpp
217         pv/util.cpp
218         pv/binding/binding.cpp
219         pv/binding/inputoutput.cpp
220         pv/binding/device.cpp
221         pv/data/analog.cpp
222         pv/data/analogsegment.cpp
223         pv/data/logic.cpp
224         pv/data/logicsegment.cpp
225         pv/data/signalbase.cpp
226         pv/data/signaldata.cpp
227         pv/data/segment.cpp
228         pv/devices/device.cpp
229         pv/devices/file.cpp
230         pv/devices/hardwaredevice.cpp
231         pv/devices/inputfile.cpp
232         pv/devices/sessionfile.cpp
233         pv/dialogs/about.cpp
234         pv/dialogs/connect.cpp
235         pv/dialogs/inputoutputoptions.cpp
236         pv/dialogs/settings.cpp
237         pv/dialogs/storeprogress.cpp
238         pv/popups/deviceoptions.cpp
239         pv/popups/channels.cpp
240         pv/prop/bool.cpp
241         pv/prop/double.cpp
242         pv/prop/enum.cpp
243         pv/prop/int.cpp
244         pv/prop/property.cpp
245         pv/prop/string.cpp
246         pv/toolbars/mainbar.cpp
247         pv/view/analogsignal.cpp
248         pv/view/cursor.cpp
249         pv/view/cursorpair.cpp
250         pv/view/flag.cpp
251         pv/view/header.cpp
252         pv/view/marginwidget.cpp
253         pv/view/logicsignal.cpp
254         pv/view/rowitem.cpp
255         pv/view/ruler.cpp
256         pv/view/signal.cpp
257         pv/view/signalscalehandle.cpp
258         pv/view/timeitem.cpp
259         pv/view/timemarker.cpp
260         pv/view/trace.cpp
261         pv/view/tracegroup.cpp
262         pv/view/tracepalette.cpp
263         pv/view/tracetreeitem.cpp
264         pv/view/tracetreeitemowner.cpp
265         pv/view/triggermarker.cpp
266         pv/view/view.cpp
267         pv/view/viewitem.cpp
268         pv/view/viewitemowner.cpp
269         pv/view/viewitempaintparams.cpp
270         pv/view/viewport.cpp
271         pv/view/viewwidget.cpp
272         pv/views/viewbase.cpp
273         pv/views/trace/standardbar.cpp
274         pv/widgets/colourbutton.cpp
275         pv/widgets/colourpopup.cpp
276         pv/widgets/devicetoolbutton.cpp
277         pv/widgets/exportmenu.cpp
278         pv/widgets/importmenu.cpp
279         pv/widgets/popup.cpp
280         pv/widgets/popuptoolbutton.cpp
281         pv/widgets/sweeptimingwidget.cpp
282         pv/widgets/timestampspinbox.cpp
283         pv/widgets/wellarray.cpp
284 )
285
286 # This list includes only QObject derived class headers.
287 set(pulseview_HEADERS
288         pv/globalsettings.hpp
289         pv/mainwindow.hpp
290         pv/session.hpp
291         pv/storesession.hpp
292         pv/binding/device.hpp
293         pv/data/analog.hpp
294         pv/data/analogsegment.hpp
295         pv/data/logic.hpp
296         pv/data/logicsegment.hpp
297         pv/data/signalbase.hpp
298         pv/dialogs/about.hpp
299         pv/dialogs/connect.hpp
300         pv/dialogs/inputoutputoptions.hpp
301         pv/dialogs/settings.hpp
302         pv/dialogs/storeprogress.hpp
303         pv/popups/channels.hpp
304         pv/popups/deviceoptions.hpp
305         pv/prop/bool.hpp
306         pv/prop/double.hpp
307         pv/prop/enum.hpp
308         pv/prop/int.hpp
309         pv/prop/property.hpp
310         pv/prop/string.hpp
311         pv/toolbars/mainbar.hpp
312         pv/view/analogsignal.hpp
313         pv/view/cursor.hpp
314         pv/view/flag.hpp
315         pv/view/header.hpp
316         pv/view/logicsignal.hpp
317         pv/view/marginwidget.hpp
318         pv/view/rowitem.hpp
319         pv/view/ruler.hpp
320         pv/view/signal.hpp
321         pv/view/signalscalehandle.hpp
322         pv/view/timeitem.hpp
323         pv/view/timemarker.hpp
324         pv/view/trace.hpp
325         pv/view/tracegroup.hpp
326         pv/view/tracetreeitem.hpp
327         pv/view/triggermarker.hpp
328         pv/view/view.hpp
329         pv/view/viewitem.hpp
330         pv/view/viewport.hpp
331         pv/view/viewwidget.hpp
332         pv/views/viewbase.hpp
333         pv/views/trace/standardbar.hpp
334         pv/widgets/colourbutton.hpp
335         pv/widgets/colourpopup.hpp
336         pv/widgets/devicetoolbutton.hpp
337         pv/widgets/exportmenu.hpp
338         pv/widgets/importmenu.hpp
339         pv/widgets/popup.hpp
340         pv/widgets/popuptoolbutton.hpp
341         pv/widgets/sweeptimingwidget.hpp
342         pv/widgets/timestampspinbox.hpp
343         pv/widgets/wellarray.hpp
344 )
345
346 set(pulseview_FORMS
347         pv/dialogs/about.ui
348 )
349
350 set(pulseview_RESOURCES
351         pulseview.qrc
352 )
353
354 if(ENABLE_SIGNALS)
355         list(APPEND pulseview_SOURCES signalhandler.cpp)
356         list(APPEND pulseview_HEADERS signalhandler.hpp)
357 endif()
358
359 if(ENABLE_DECODE)
360         list(APPEND pulseview_SOURCES
361                 pv/binding/decoder.cpp
362                 pv/data/decoderstack.cpp
363                 pv/data/decode/annotation.cpp
364                 pv/data/decode/decoder.cpp
365                 pv/data/decode/row.cpp
366                 pv/data/decode/rowdata.cpp
367                 pv/view/decodetrace.cpp
368                 pv/widgets/decodergroupbox.cpp
369                 pv/widgets/decodermenu.cpp
370         )
371
372         list(APPEND pulseview_HEADERS
373                 pv/data/decoderstack.hpp
374                 pv/view/decodetrace.hpp
375                 pv/widgets/decodergroupbox.hpp
376                 pv/widgets/decodermenu.hpp
377         )
378 endif()
379
380 if(WIN32)
381         # Use the sigrok icon for the pulseview.exe executable.
382         set(CMAKE_RC_COMPILE_OBJECT "${CMAKE_RC_COMPILER} -O coff -I${CMAKE_CURRENT_SOURCE_DIR} <SOURCE> <OBJECT>")
383         enable_language(RC)
384         list(APPEND pulseview_SOURCES pulseviewico.rc)
385 endif()
386
387 if(ANDROID)
388         list(APPEND pulseview_SOURCES
389                 android/assetreader.cpp
390                 android/loghandler.cpp
391         )
392 endif()
393
394 if(Qt5Core_FOUND)
395         qt5_wrap_cpp(pulseview_HEADERS_MOC ${pulseview_HEADERS})
396         qt5_wrap_ui(pulseview_FORMS_HEADERS ${pulseview_FORMS})
397         qt5_add_resources(pulseview_RESOURCES_RCC ${pulseview_RESOURCES})
398 else()
399         # Workaround for QTBUG-22829: -DBOOST_NEXT_PRIOR_HPP_INCLUDED.
400         # https://bugreports.qt.io/browse/QTBUG-22829
401         qt4_wrap_cpp(pulseview_HEADERS_MOC ${pulseview_HEADERS}
402                 OPTIONS -DBOOST_NEXT_PRIOR_HPP_INCLUDED)
403         qt4_wrap_ui(pulseview_FORMS_HEADERS ${pulseview_FORMS})
404         qt4_add_resources(pulseview_RESOURCES_RCC ${pulseview_RESOURCES})
405         include(${QT_USE_FILE})
406 endif()
407
408 #===============================================================================
409 #= Global Definitions
410 #-------------------------------------------------------------------------------
411
412 add_definitions(${QT_DEFINITIONS} -DQT_NO_KEYWORDS)
413 add_definitions(-D__STDC_LIMIT_MACROS)
414 add_definitions(-Wall -Wextra)
415 add_definitions(-std=c++11)
416 add_definitions(-DBOOST_MATH_DISABLE_FLOAT128=1)
417
418 if(ENABLE_DECODE)
419         add_definitions(-DENABLE_DECODE)
420 endif()
421
422 if(NOT DISABLE_WERROR)
423         add_definitions(-Werror)
424 endif()
425
426 if(ENABLE_SIGNALS)
427         add_definitions(-DENABLE_SIGNALS)
428 endif()
429
430 #===============================================================================
431 #= Global Include Directories
432 #-------------------------------------------------------------------------------
433
434 include_directories(
435         ${CMAKE_CURRENT_BINARY_DIR}
436         ${CMAKE_CURRENT_SOURCE_DIR}
437         ${Boost_INCLUDE_DIRS}
438         ${QT_INCLUDE_DIRS}
439 )
440
441 if(STATIC_PKGDEPS_LIBS)
442         include_directories(${PKGDEPS_STATIC_INCLUDE_DIRS})
443 else()
444         include_directories(${PKGDEPS_INCLUDE_DIRS})
445 endif()
446
447 #===============================================================================
448 #= Linker Configuration
449 #-------------------------------------------------------------------------------
450
451 link_directories(${Boost_LIBRARY_DIRS})
452
453 set(PULSEVIEW_LINK_LIBS
454         ${Boost_LIBRARIES}
455         ${QT_LIBRARIES}
456         ${CMAKE_THREAD_LIBS_INIT}
457         ${LIBATOMIC_LIBRARY}
458 )
459
460 if(STATIC_PKGDEPS_LIBS)
461         link_directories(${PKGDEPS_STATIC_LIBRARY_DIRS})
462         list(APPEND PULSEVIEW_LINK_LIBS ${PKGDEPS_STATIC_LDFLAGS})
463 else()
464         link_directories(${PKGDEPS_LIBRARY_DIRS})
465         list(APPEND PULSEVIEW_LINK_LIBS ${PKGDEPS_LIBRARIES})
466 endif()
467
468 if(WIN32)
469         # On Windows we need to statically link the libqsvg imageformat
470         # plugin (and the QtSvg component) for SVG graphics/icons to work.
471         # We also need QWindowsIntegrationPlugin, Qt5PlatformSupport, and all
472         # Qt libs and their dependencies.
473         add_definitions(-DQT_STATICPLUGIN)
474         list(APPEND PULSEVIEW_LINK_LIBS Qt5::QSvgPlugin)
475         list(APPEND PULSEVIEW_LINK_LIBS Qt5::QWindowsIntegrationPlugin)
476         list(APPEND PULSEVIEW_LINK_LIBS -lQt5PlatformSupport ${QT5ALL_LDFLAGS})
477 endif()
478
479 if(ANDROID)
480         list(APPEND PULSEVIEW_LINK_LIBS "-llog")
481 endif()
482
483 if(ANDROID)
484 add_library(${PROJECT_NAME} SHARED
485         ${pulseview_SOURCES}
486         ${pulseview_HEADERS_MOC}
487         ${pulseview_FORMS_HEADERS}
488         ${pulseview_RESOURCES_RCC}
489 )
490 else()
491 add_executable(${PROJECT_NAME}
492         ${pulseview_SOURCES}
493         ${pulseview_HEADERS_MOC}
494         ${pulseview_FORMS_HEADERS}
495         ${pulseview_RESOURCES_RCC}
496 )
497 endif()
498
499 target_link_libraries(${PROJECT_NAME} ${PULSEVIEW_LINK_LIBS})
500
501 if(WIN32)
502         # Pass -mwindows so that no "DOS box" opens when PulseView is started.
503         set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-mwindows")
504 endif()
505
506 #===============================================================================
507 #= Installation
508 #-------------------------------------------------------------------------------
509
510 # Install the executable.
511 install(TARGETS ${PROJECT_NAME} DESTINATION bin/)
512
513 # Install the manpage.
514 install(FILES doc/pulseview.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1 COMPONENT doc)
515
516 # Generate Windows installer script.
517 configure_file(contrib/pulseview_cross.nsi.in contrib/pulseview_cross.nsi @ONLY)
518
519 #===============================================================================
520 #= Packaging (handled by CPack)
521 #-------------------------------------------------------------------------------
522
523 set(CPACK_PACKAGE_VERSION_MAJOR ${PV_VERSION_MAJOR})
524 set(CPACK_PACKAGE_VERSION_MINOR ${PV_VERSION_MINOR})
525 set(CPACK_PACKAGE_VERSION_PATCH ${PV_VERSION_MICRO})
526 set(CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/README)
527 set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/COPYING)
528 set(CPACK_SOURCE_IGNORE_FILES ${CMAKE_CURRENT_BINARY_DIR} ".gitignore" ".git")
529 set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${PV_VERSION_STRING}")
530 set(CPACK_SOURCE_GENERATOR "TGZ")
531
532 include(CPack)
533
534 #===============================================================================
535 #= Tests
536 #-------------------------------------------------------------------------------
537
538 if(ENABLE_TESTS)
539         add_subdirectory(test)
540         enable_testing()
541         add_test(test ${CMAKE_CURRENT_BINARY_DIR}/test/pulseview-test)
542 endif()