-
Notifications
You must be signed in to change notification settings - Fork 26
/
common.cmake
1610 lines (1200 loc) · 57 KB
/
common.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
MACRO(INITIALISE_PROJECT)
# SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_INCLUDE_CURRENT_DIR ON)
# Make sure that we are using the compiler we support
IF(WIN32)
STRING(REGEX REPLACE "\\..*$" ""
CMAKE_CXX_COMPILER_VERSION_MAJOR "${CMAKE_CXX_COMPILER_VERSION}")
IF( NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC"
OR NOT CMAKE_CXX_COMPILER_VERSION_MAJOR VERSION_EQUAL "19")
MESSAGE(FATAL_ERROR "${CMAKE_PROJECT_NAME} can only be built using MSVC 2015 on Windows...")
ENDIF()
ELSEIF(APPLE)
IF( NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang"
AND NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
MESSAGE(FATAL_ERROR "${CMAKE_PROJECT_NAME} can only be built using (Apple) Clang on macOS...")
ENDIF()
ELSE()
IF( NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"
OR CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9")
MESSAGE(FATAL_ERROR "${CMAKE_PROJECT_NAME} can only be built using GCC/G++ 4.9+ on Linux...")
ENDIF()
ENDIF()
# Make sure that we are building on a supported architecture
# Note: normally, we would check the value of CMAKE_SIZEOF_VOID_P, but in
# some cases it may not be set (e.g. when generating an Xcode project
# file), so we determine and retrieve that value ourselves...
TRY_RUN(ARCHITECTURE_RUN ARCHITECTURE_COMPILE
${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/cmake/architecture.c
RUN_OUTPUT_VARIABLE ARCHITECTURE)
IF(NOT ARCHITECTURE_COMPILE)
MESSAGE(FATAL_ERROR "We could not determine your architecture. Please clean your ${CMAKE_PROJECT_NAME} environment and try again...")
ELSEIF(NOT ${ARCHITECTURE} EQUAL 64)
MESSAGE(FATAL_ERROR "${CMAKE_PROJECT_NAME} can only be built in 64-bit mode...")
ENDIF()
# By default, we are building a release version of OpenCOR, unless we are
# explicitly asked for a debug version
IF( "${CMAKE_BUILD_TYPE}" STREQUAL ""
OR "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
SET(BUILD_INFORMATION "Building a release version of ${CMAKE_PROJECT_NAME}")
SET(RELEASE_MODE TRUE)
ELSEIF("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
SET(BUILD_INFORMATION "Building a debug version of ${CMAKE_PROJECT_NAME}")
SET(RELEASE_MODE FALSE)
ELSE()
MESSAGE(FATAL_ERROR "${CMAKE_PROJECT_NAME} can only be built in release or debug mode...")
ENDIF()
# Required Qt modules and packages
IF(ENABLE_TESTS)
SET(TEST Test)
ELSE()
SET(TEST)
ENDIF()
SET(REQUIRED_QT_MODULES
Network
${TEST}
Widgets
)
FOREACH(REQUIRED_QT_MODULE ${REQUIRED_QT_MODULES})
FIND_PACKAGE(Qt5${REQUIRED_QT_MODULE} REQUIRED)
ENDFOREACH()
# Some initialisations related to our copy of QtWebKit
IF(WIN32)
SET(PLATFORM_DIR windows)
ELSEIF(APPLE)
SET(PLATFORM_DIR macos)
ELSE()
SET(PLATFORM_DIR linux)
ENDIF()
INCLUDE(${CMAKE_SOURCE_DIR}/src/3rdparty/QtWebKit/CMakeLists.txt)
# The WebKit module is also needed on Windows
IF(WIN32)
LIST(APPEND REQUIRED_QT_MODULES WebKit)
FIND_PACKAGE(Qt5WebKit REQUIRED)
ENDIF()
# Keep track of some information about Qt
SET(QT_VERSION ${Qt5Core_VERSION})
SET(QT_VERSION_MAJOR ${Qt5Core_VERSION_MAJOR})
SET(QT_DIR ${_qt5Core_install_prefix})
SET(QT_BINARY_DIR ${QT_DIR}/bin)
SET(QT_LIBRARY_DIR ${QT_DIR}/lib)
SET(QT_PLUGINS_DIR ${QT_DIR}/plugins)
# On macOS, keep track of the Qt libraries against which we need to link
IF(APPLE)
IF(ENABLE_TESTS)
SET(QT_TEST QtTest)
ELSE()
SET(QT_TEST)
ENDIF()
SET(MACOS_QT_LIBRARIES
QtCLucene
QtConcurrent
QtCore
QtGui
QtHelp
QtMacExtras
QtMultimedia
QtMultimediaWidgets
QtNetwork
QtOpenGL
QtPositioning
QtPrintSupport
QtQml
QtQuick
QtSensors
QtSql
QtSvg
${QT_TEST}
QtWebChannel
QtWebKit
QtWebKitWidgets
QtWidgets
QtXml
QtXmlPatterns
)
ENDIF()
# Determine the effective build directory
SET(PROJECT_BUILD_DIR ${CMAKE_BINARY_DIR})
IF(APPLE AND "${CMAKE_GENERATOR}" STREQUAL "Xcode")
# With Xcode, we have a configuration directory, but it messes up our
# build system, so ask for all the binaries to be generated in our build
# folder
SET(XCODE TRUE)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BUILD_DIR})
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BUILD_DIR})
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BUILD_DIR})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BUILD_DIR})
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BUILD_DIR})
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BUILD_DIR})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BUILD_DIR})
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BUILD_DIR})
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BUILD_DIR})
ELSE()
# Check whether there is a configuration directory (the case with MSVC)
# and, if so, make use of it
SET(XCODE FALSE)
IF(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
SET(PROJECT_BUILD_DIR ${PROJECT_BUILD_DIR}/${CMAKE_CFG_INTDIR})
ENDIF()
ENDIF()
# Some general build settings
# Note: MSVC enables C++11 support by default, so we just need to enable it
# on Linux and macOS...
IF(WIN32)
SET(CMAKE_CXX_FLAGS "/DWIN32 /D_WINDOWS /W3 /WX /GR /EHsc")
# Note: MSVC has a /Wall flag, but it results in MSVC being very
# pedantic, so instead we use what MSVC recommends for production
# code, which is /W3 and which is also what CMake uses by
# default...
SET(LINK_FLAGS_PROPERTIES "/STACK:10000000")
ELSE()
SET(CMAKE_CXX_FLAGS "-Wall -W -Werror -std=c++11")
IF(APPLE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
SET(LINK_FLAGS_PROPERTIES "-stdlib=libc++")
ELSE()
SET(LINK_FLAGS_PROPERTIES)
ENDIF()
ENDIF()
# On macOS, we want to be able to access Cocoa
IF(APPLE)
SET(LINK_FLAGS_PROPERTIES "${LINK_FLAGS_PROPERTIES} -framework AppKit")
ENDIF()
# Some build settings that depend on whether we want a release or a debug
# version of OpenCOR
IF(RELEASE_MODE)
# Default compiler and linker settings
IF(WIN32)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DNDEBUG /MD /O2 /Ob2")
ELSE()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -ffast-math")
ENDIF()
IF(NOT WIN32 AND NOT APPLE)
SET(LINK_FLAGS_PROPERTIES "${LINK_FLAGS_PROPERTIES} -Wl,-s")
# Note #1: -Wl,-s strips all the symbols, thus reducing the final
# size of OpenCOR or one its shared libraries...
# Note #2: the above linking option has become obsolete on macOS...
ENDIF()
# Make sure that debugging is off in Qt
ADD_DEFINITIONS(-DQT_NO_DEBUG)
ELSE()
# Default compiler and linker settings
IF(WIN32)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1")
SET(LINK_FLAGS_PROPERTIES "${LINK_FLAGS_PROPERTIES} /DEBUG")
ELSE()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0")
ENDIF()
# Make sure that debugging is on in Qt
ADD_DEFINITIONS(-DQT_DEBUG)
# Make sure that LLVM works fine when in debug mode
# Note: it needs to be done here since, by default, _DEBUG_POINTER_IMPL
# is not defined and will therefore be as soon as we include
# xutility, which will also give it the value _Debug_pointer while
# we want it to be defined but without any value (so that pointer
# testing is disabled)...
ADD_DEFINITIONS(-D_DEBUG_POINTER_IMPL=)
ENDIF()
# Disable a warning that occurs on (the 64-bit version of) Windows
# Note: the warning occurs in (at least) MSVC's algorithm header file. To
# disable it here means that we disable it for everything, but is
# there another solution?...
IF(WIN32)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4267")
ENDIF()
# Ask for Qt deprecated uses to be reported
ADD_DEFINITIONS(-DQT_DEPRECATED_WARNINGS)
# Let OpenCOR know about the options with which it was built
IF(ENABLE_SAMPLES)
ADD_DEFINITIONS(-DENABLE_SAMPLES)
ENDIF()
IF(ENABLE_TESTS)
ADD_DEFINITIONS(-DENABLE_TESTS)
ENDIF()
IF(USE_PREBUILT_LIBGIT2_PLUGIN)
ADD_DEFINITIONS(-DUSE_PREBUILT_LIBGIT2_PLUGIN)
ENDIF()
IF(USE_PREBUILT_LLVM_PLUGIN)
ADD_DEFINITIONS(-DUSE_PREBUILT_LLVM_PLUGIN)
ENDIF()
IF(USE_PREBUILT_QSCINTILLA_PLUGIN)
ADD_DEFINITIONS(-DUSE_PREBUILT_QSCINTILLA_PLUGIN)
ENDIF()
IF(USE_PREBUILT_QWT_PLUGIN)
ADD_DEFINITIONS(-DUSE_PREBUILT_QWT_PLUGIN)
ENDIF()
IF(USE_PREBUILT_SUNDIALS_PLUGIN)
ADD_DEFINITIONS(-DUSE_PREBUILT_SUNDIALS_PLUGIN)
ENDIF()
IF(USE_PREBUILT_ZLIB_PLUGIN)
ADD_DEFINITIONS(-DUSE_PREBUILT_ZLIB_PLUGIN)
ENDIF()
# On macOS, make sure that we support 10.8 and later, unless a specific
# deployment target has been specified
IF(APPLE)
IF("${CMAKE_OSX_DEPLOYMENT_TARGET}" STREQUAL "")
SET(CMAKE_OSX_DEPLOYMENT_TARGET 10.8)
ENDIF()
IF(CMAKE_OSX_DEPLOYMENT_TARGET VERSION_LESS "10.12")
SET(SYSTEM_NAME "OS X")
ELSE()
SET(SYSTEM_NAME "macOS")
ENDIF()
SET(BUILD_INFORMATION "${BUILD_INFORMATION} for ${SYSTEM_NAME} ${CMAKE_OSX_DEPLOYMENT_TARGET} and later")
ENDIF()
# Destination of our plugins so that we don't have to deploy OpenCOR on
# Windows and Linux before being able to test it
IF(APPLE)
SET(DEST_PLUGINS_DIR ${PROJECT_BUILD_DIR}/${CMAKE_PROJECT_NAME}.app/Contents/PlugIns/${CMAKE_PROJECT_NAME})
ELSE()
SET(DEST_PLUGINS_DIR ${PROJECT_BUILD_DIR}/plugins/${CMAKE_PROJECT_NAME})
ENDIF()
# Default location of external dependencies
IF(WIN32)
IF(RELEASE_MODE)
SET(REMOTE_EXTERNAL_BINARIES_DIR ${PLATFORM_DIR}/release)
SET(LOCAL_EXTERNAL_BINARIES_DIR bin/release)
ELSE()
SET(REMOTE_EXTERNAL_BINARIES_DIR ${PLATFORM_DIR}/debug)
SET(LOCAL_EXTERNAL_BINARIES_DIR bin/debug)
ENDIF()
SET(DEST_EXTERNAL_BINARIES_DIR bin)
ELSE()
SET(REMOTE_EXTERNAL_BINARIES_DIR ${PLATFORM_DIR})
SET(LOCAL_EXTERNAL_BINARIES_DIR bin)
IF(APPLE)
SET(DEST_EXTERNAL_BINARIES_DIR ${CMAKE_PROJECT_NAME}.app/Contents/Frameworks)
ELSE()
SET(DEST_EXTERNAL_BINARIES_DIR lib)
ENDIF()
ENDIF()
SET(FULL_DEST_EXTERNAL_BINARIES_DIR ${PROJECT_BUILD_DIR}/${DEST_EXTERNAL_BINARIES_DIR})
IF(NOT EXISTS ${FULL_DEST_EXTERNAL_BINARIES_DIR})
FILE(MAKE_DIRECTORY ${FULL_DEST_EXTERNAL_BINARIES_DIR})
ENDIF()
# Where external packages are installed
IF(APPLE)
SET(FULL_DEST_EXTERNAL_BASE_DIR ${FULL_DEST_EXTERNAL_BINARIES_DIR})
ELSE()
SET(FULL_DEST_EXTERNAL_BASE_DIR ${PROJECT_BUILD_DIR})
ENDIF()
# Set the RPATH (and RPATH link, if needed) information on Linux and macOS
IF(APPLE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_INSTALL_RPATH "@executable_path/../Frameworks;@executable_path/../PlugIns/${CMAKE_PROJECT_NAME}")
ELSEIF(NOT WIN32)
SET(CMAKE_SKIP_RPATH TRUE)
SET(LINK_RPATH_FLAG "-Wl,-rpath,'$ORIGIN/../lib'")
SET(LINK_FLAGS_PROPERTIES "${LINK_FLAGS_PROPERTIES} -Wl,-rpath-link,${QT_LIBRARY_DIR} ${LINK_RPATH_FLAG}")
ENDIF()
# Try to build our runpath2rpath program, if we are on Linux
IF(NOT WIN32 AND NOT APPLE)
SET(RUNPATH2RPATH ${PROJECT_BUILD_DIR}/runpath2rpath)
EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -o ${RUNPATH2RPATH} ${PROJECT_SOURCE_DIR}/cmake/runpath2rpath.c
RESULT_VARIABLE RESULT)
IF(NOT RESULT EQUAL 0)
MESSAGE(FATAL_ERROR "runpath2rpath could not be built...")
ENDIF()
ENDIF()
# We specify where to build external projects because the default
# (to build external projects under their plugin's build directory)
# can result in path names being too long for Windows...
SET_PROPERTY(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/external)
# Show the build information
MESSAGE("${BUILD_INFORMATION} using Qt ${QT_VERSION}...")
ENDMACRO()
#===============================================================================
MACRO(KEEP_TRACK_OF_FILE FILE_NAME)
# Keep track of the given file
# Note: indeed, some files (e.g. versiondate.txt) are 'manually' generated
# and then used to build other files. Now, the 'problem' is that
# Ninja needs to know about those files (see CMake policy CMP0058),
# which we ensure it does through the below...
ADD_CUSTOM_COMMAND(OUTPUT ${FILE_NAME}
COMMAND ${CMAKE_COMMAND} -E sleep 0)
ENDMACRO()
#===============================================================================
MACRO(UPDATE_LANGUAGE_FILES TARGET_NAME)
# Update the translation (.ts) files (if they exist) and generate the
# language (.qm) files, which will later on be embedded in the project
SET(LANGUAGES fr)
SET(INPUT_FILES)
FOREACH(INPUT_FILE ${ARGN})
LIST(APPEND INPUT_FILES ${INPUT_FILE})
ENDFOREACH()
FOREACH(LANGUAGE ${LANGUAGES})
SET(LANGUAGE_FILE ${TARGET_NAME}_${LANGUAGE})
SET(TS_FILE i18n/${LANGUAGE_FILE}.ts)
SET(QM_FILE ${PROJECT_BUILD_DIR}/${LANGUAGE_FILE}.qm)
IF(EXISTS ${PROJECT_SOURCE_DIR}/${TS_FILE})
EXECUTE_PROCESS(COMMAND ${QT_BINARY_DIR}/lupdate -no-obsolete ${INPUT_FILES}
-ts ${TS_FILE}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_QUIET)
EXECUTE_PROCESS(COMMAND ${QT_BINARY_DIR}/lrelease ${PROJECT_SOURCE_DIR}/${TS_FILE}
-qm ${QM_FILE}
OUTPUT_QUIET)
KEEP_TRACK_OF_FILE(${QM_FILE})
ENDIF()
ENDFOREACH()
ENDMACRO()
#===============================================================================
MACRO(ADD_PLUGIN PLUGIN_NAME)
# Various initialisations
SET(PLUGIN_NAME ${PLUGIN_NAME})
SET(OPTIONS
THIRD_PARTY
)
SET(ONE_VALUE_KEYWORDS
EXTERNAL_BINARIES_DIR
EXTERNAL_DEST_DIR
EXTERNAL_SOURCE_DIR
)
SET(MULTI_VALUE_KEYWORDS
SOURCES
HEADERS_MOC
UIS
INCLUDE_DIRS
DEFINITIONS
PLUGINS
PLUGIN_BINARIES
QT_MODULES
EXTERNAL_BINARIES
SYSTEM_BINARIES
TESTS
)
CMAKE_PARSE_ARGUMENTS(ARG "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}" ${ARGN})
# Check whether this is a third-party plugin
IF(ARG_THIRD_PARTY)
# Disable all C/C++ warnings since building a third-party plugin may
# generate some and this has nothing to do with us
# Note: on Windows, we can't simply add /w since it will otherwise
# result in MSVC complaining about /W3 having been overridden by
# /w...
IF(WIN32)
STRING(REPLACE "/W3" "/w"
CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
STRING(REPLACE "/W3" "/w"
CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE()
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")
ENDIF()
# Add a definition in case of compilation from within Qt Creator using
# MSVC and JOM since the latter overrides some of our settings
IF(WIN32)
ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
ENDIF()
# Prevent all debug outputs
ADD_DEFINITIONS(-DQT_NO_DEBUG_OUTPUT)
ENDIF()
# Various include directories
SET(PLUGIN_INCLUDE_DIRS ${ARG_INCLUDE_DIRS} PARENT_SCOPE)
INCLUDE_DIRECTORIES(${ARG_INCLUDE_DIRS})
# Resource files, if any
# Note: ideally, we would have our resource files named i18n.qrc.in and
# ui.qrc for all our plugins, but this is causing problems on Linux
# with Qt apparently accepting only one file called i18n.qrc.in or
# ui.qrc. The end result is that, on a development machine, plugin
# resources (e.g. icons, translations) are not available while,
# strangely enough, they are on a deployment machine...
SET(RESOURCES)
SET(I18N_QRC_IN_FILENAME ${PROJECT_SOURCE_DIR}/res/${PLUGIN_NAME}_i18n.qrc.in)
SET(UI_QRC_FILENAME ${PROJECT_SOURCE_DIR}/res/${PLUGIN_NAME}_ui.qrc)
IF(EXISTS ${I18N_QRC_IN_FILENAME})
STRING(REPLACE "${CMAKE_SOURCE_DIR}" "${PROJECT_BUILD_DIR}"
I18N_QRC_FILENAME "${PROJECT_SOURCE_DIR}/res/${PLUGIN_NAME}_i18n.qrc")
CONFIGURE_FILE(${I18N_QRC_IN_FILENAME}
${I18N_QRC_FILENAME})
LIST(APPEND RESOURCES ${I18N_QRC_FILENAME})
# Update the translation (.ts) files and generate the language (.qm)
# files, which will later on be embedded in the plugin
UPDATE_LANGUAGE_FILES(${PLUGIN_NAME} ${ARG_SOURCES} ${ARG_HEADERS_MOC} ${ARG_UIS})
ENDIF()
IF(EXISTS ${UI_QRC_FILENAME})
LIST(APPEND RESOURCES ${UI_QRC_FILENAME})
ENDIF()
# Definition to make sure that the plugin can be used by other plugins
ADD_DEFINITIONS(-D${PLUGIN_NAME}_PLUGIN)
# Some plugin-specific definitions
FOREACH(ARG_DEFINITION ${ARG_DEFINITIONS})
ADD_DEFINITIONS(-D${ARG_DEFINITION})
ENDFOREACH()
# On Linux, set the RPATH value to use by the plugin
IF(NOT WIN32 AND NOT APPLE)
SET(PLUGIN_LINK_RPATH_FLAG "-Wl,-rpath,'$ORIGIN' -Wl,-rpath,'$ORIGIN/../../lib'")
STRING(REPLACE "${LINK_RPATH_FLAG}" "${PLUGIN_LINK_RPATH_FLAG}"
LINK_FLAGS_PROPERTIES "${LINK_FLAGS_PROPERTIES}")
ENDIF()
# Generate and add the different files needed by the plugin
IF("${ARG_HEADERS_MOC}" STREQUAL "")
SET(SOURCES_MOC)
ELSE()
QT5_WRAP_CPP(SOURCES_MOC ${ARG_HEADERS_MOC})
ENDIF()
IF("${ARG_UIS}" STREQUAL "")
SET(SOURCES_UIS)
ELSE()
QT5_WRAP_UI(SOURCES_UIS ${ARG_UIS})
ENDIF()
IF("${RESOURCES}" STREQUAL "")
SET(SOURCES_RCS)
ELSE()
QT5_ADD_RESOURCES(SOURCES_RCS ${RESOURCES})
ENDIF()
ADD_LIBRARY(${PROJECT_NAME} SHARED
${ARG_SOURCES}
${SOURCES_MOC}
${SOURCES_UIS}
${SOURCES_RCS}
)
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES
OUTPUT_NAME ${PLUGIN_NAME}
LINK_FLAGS "${LINK_FLAGS_PROPERTIES}"
)
# OpenCOR plugins
FOREACH(ARG_PLUGIN ${ARG_PLUGINS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${ARG_PLUGIN}Plugin
)
ENDFOREACH()
# OpenCOR binaries
FOREACH(ARG_PLUGIN_BINARY ${ARG_PLUGIN_BINARIES})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${ARG_PLUGIN_BINARY}
)
ENDFOREACH()
# Qt modules
FOREACH(ARG_QT_MODULE ${ARG_QT_MODULES})
FIND_PACKAGE(Qt5${ARG_QT_MODULE} REQUIRED)
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
Qt5::${ARG_QT_MODULE}
)
ENDFOREACH()
# External binaries
IF(NOT "${ARG_EXTERNAL_BINARIES_DIR}" STREQUAL "")
FOREACH(ARG_EXTERNAL_BINARY ${ARG_EXTERNAL_BINARIES})
# Make sure that the external binary exists
SET(FULL_EXTERNAL_BINARY "${ARG_EXTERNAL_BINARIES_DIR}/${ARG_EXTERNAL_BINARY}")
IF(EXISTS ${FULL_EXTERNAL_BINARY})
SET(COPY_TARGET DIRECT)
ELSE()
SET(COPY_TARGET ${PROJECT_NAME})
ENDIF()
# Copy the external binary to its destination directory, so we can
# test things without first having to deploy OpenCOR
# Note: on Windows, we also need to copy it to the build directory
# so that we can test things from within Qt Creator...
IF(WIN32)
COPY_FILE_TO_BUILD_DIR(${COPY_TARGET} ${ARG_EXTERNAL_BINARIES_DIR} . ${ARG_EXTERNAL_BINARY})
ENDIF()
COPY_FILE_TO_BUILD_DIR(${COPY_TARGET} ${ARG_EXTERNAL_BINARIES_DIR} ${DEST_EXTERNAL_BINARIES_DIR} ${ARG_EXTERNAL_BINARY})
# Strip the external library of all its local symbols, if possible
IF(NOT WIN32 AND RELEASE_MODE)
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND strip -x ${FULL_DEST_EXTERNAL_BINARIES_DIR}/${ARG_EXTERNAL_BINARY})
ENDIF()
# Link the plugin to the external library
IF(WIN32)
STRING(REGEX REPLACE "${CMAKE_SHARED_LIBRARY_SUFFIX}$" "${CMAKE_IMPORT_LIBRARY_SUFFIX}"
IMPORT_EXTERNAL_BINARY "${FULL_EXTERNAL_BINARY}")
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${IMPORT_EXTERNAL_BINARY}
)
ELSEIF(${COPY_TARGET} STREQUAL "DIRECT")
IF(APPLE)
EXECUTE_PROCESS(COMMAND install_name_tool -id @rpath/${ARG_EXTERNAL_BINARY} ${ARG_EXTERNAL_BINARY}
WORKING_DIRECTORY ${FULL_DEST_EXTERNAL_BINARIES_DIR}
)
ENDIF()
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${FULL_DEST_EXTERNAL_BINARIES_DIR}/${ARG_EXTERNAL_BINARY}
)
ELSE()
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${FULL_EXTERNAL_BINARY}
)
IF(APPLE)
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND install_name_tool -id @rpath/${ARG_EXTERNAL_BINARY} ${ARG_EXTERNAL_BINARY}
WORKING_DIRECTORY ${FULL_DEST_EXTERNAL_BINARIES_DIR}
)
ENDIF()
ENDIF()
# Package the external library, if needed
IF(WIN32)
INSTALL(FILES ${FULL_EXTERNAL_BINARY}
DESTINATION bin)
ELSEIF(NOT APPLE)
INSTALL(FILES ${FULL_EXTERNAL_BINARY}
DESTINATION lib)
ENDIF()
ENDFOREACH()
ENDIF()
# Check whether an external package has files to install
IF(NOT "${ARG_EXTERNAL_DEST_DIR}" STREQUAL ""
AND NOT "${ARG_EXTERNAL_SOURCE_DIR}" STREQUAL "")
# Copy the entire source directory to the destination
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${ARG_EXTERNAL_SOURCE_DIR}
${FULL_DEST_EXTERNAL_BASE_DIR}/${ARG_EXTERNAL_DEST_DIR})
ENDIF()
# System binaries
FOREACH(ARG_SYSTEM_BINARY ${ARG_SYSTEM_BINARIES})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${ARG_SYSTEM_BINARY}
)
ENDFOREACH()
# Some settings
IF(XCODE)
SET(PLUGIN_BUILD_DIR ${PROJECT_BUILD_DIR})
ELSE()
STRING(REPLACE "${${CMAKE_PROJECT_NAME}_SOURCE_DIR}/" ""
PLUGIN_BUILD_DIR "${PROJECT_SOURCE_DIR}")
SET(PLUGIN_BUILD_DIR ${CMAKE_BINARY_DIR}/${PLUGIN_BUILD_DIR})
IF(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
SET(PLUGIN_BUILD_DIR ${PLUGIN_BUILD_DIR}/${CMAKE_CFG_INTDIR})
ENDIF()
ENDIF()
SET(PLUGIN_FILENAME ${CMAKE_SHARED_LIBRARY_PREFIX}${PLUGIN_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
# Copy the plugin to our plugins directory
# Note: this is done so that we can, on Windows and Linux, test the use of
# plugins in OpenCOR without first having to package OpenCOR, as well
# as, on Windows, test things from within Qt Creator...
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PLUGIN_BUILD_DIR}/${PLUGIN_FILENAME}
${DEST_PLUGINS_DIR}/${PLUGIN_FILENAME})
# A few macOS specific things
IF(APPLE)
# Clean up our plugin
MACOS_CLEAN_UP_FILE_WITH_QT_DEPENDENCIES(${PROJECT_NAME} ${DEST_PLUGINS_DIR} ${PLUGIN_FILENAME})
ENDIF()
# Package the plugin, but only if we are not on macOS since it will have
# already been copied
IF(NOT APPLE)
INSTALL(FILES ${PLUGIN_BUILD_DIR}/${PLUGIN_FILENAME}
DESTINATION plugins/${CMAKE_PROJECT_NAME})
ENDIF()
# Create some tests, if any and if required
IF(ENABLE_TESTS)
FOREACH(ARG_TEST ${ARG_TESTS})
# Keep track of the test (for later use by our main test program)
FILE(APPEND ${TESTS_LIST_FILENAME} "${PLUGIN_NAME}|${ARG_TEST}|")
# Build our test, if possible
SET(TEST_NAME ${PLUGIN_NAME}_${ARG_TEST})
SET(TEST_SOURCE tests/${ARG_TEST}.cpp)
SET(TEST_HEADER_MOC tests/${ARG_TEST}.h)
IF( EXISTS ${PROJECT_SOURCE_DIR}/${TEST_SOURCE}
AND EXISTS ${PROJECT_SOURCE_DIR}/${TEST_HEADER_MOC})
# The test exists, so build it, but first set the RPATH and
# RPATH link values to use by the test, if on Linux
IF(NOT WIN32 AND NOT APPLE)
STRING(REPLACE "${PLUGIN_LINK_RPATH_FLAG}" "-Wl,-rpath-link,${PROJECT_BUILD_DIR}/lib ${LINK_RPATH_FLAG} -Wl,-rpath,'$ORIGIN/../plugins/${CMAKE_PROJECT_NAME}'"
LINK_FLAGS_PROPERTIES "${LINK_FLAGS_PROPERTIES}")
ENDIF()
SET(TEST_SOURCES_MOC)
# Note: we need to initialise TEST_SOURCES_MOC in case there is
# more than just one test. Indeed, if we were not to do
# that initialisation, then TEST_SOURCES_MOC would include
# the information of all the tests up to the one we want
# build...
QT5_WRAP_CPP(TEST_SOURCES_MOC ${TEST_HEADER_MOC})
QT5_ADD_RESOURCES(TEST_SOURCES_RCS ${TESTS_QRC_FILENAME})
ADD_EXECUTABLE(${TEST_NAME}
../../../tests/src/testsutils.cpp
${ARG_SOURCES}
${SOURCES_MOC}
${SOURCES_UIS}
${SOURCES_RCS}
${TEST_SOURCE}
${TEST_SOURCES_MOC}
${TEST_SOURCES_RCS}
)
SET_TARGET_PROPERTIES(${TEST_NAME} PROPERTIES
OUTPUT_NAME ${TEST_NAME}
LINK_FLAGS "${LINK_FLAGS_PROPERTIES}"
)
# Plugins
FOREACH(ARG_PLUGIN ${ARG_PLUGINS})
TARGET_LINK_LIBRARIES(${TEST_NAME}
${ARG_PLUGIN}Plugin
)
ENDFOREACH()
# OpenCOR binaries
FOREACH(ARG_PLUGIN_BINARY ${ARG_PLUGIN_BINARIES})
TARGET_LINK_LIBRARIES(${TEST_NAME}
${ARG_PLUGIN_BINARY}
)
ENDFOREACH()
# Qt modules
FOREACH(ARG_QT_MODULE ${ARG_QT_MODULES} Test)
TARGET_LINK_LIBRARIES(${TEST_NAME}
Qt5::${ARG_QT_MODULE}
)
ENDFOREACH()
# External binaries
IF(NOT "${ARG_EXTERNAL_BINARIES_DIR}" STREQUAL "")
FOREACH(ARG_EXTERNAL_BINARY ${ARG_EXTERNAL_BINARIES})
IF(WIN32)
STRING(REGEX REPLACE "${CMAKE_SHARED_LIBRARY_SUFFIX}$" "${CMAKE_IMPORT_LIBRARY_SUFFIX}"
IMPORT_EXTERNAL_BINARY "${ARG_EXTERNAL_BINARY}")
TARGET_LINK_LIBRARIES(${TEST_NAME}
${ARG_EXTERNAL_BINARIES_DIR}/${IMPORT_EXTERNAL_BINARY}
)
ELSE()
TARGET_LINK_LIBRARIES(${TEST_NAME}
${FULL_DEST_EXTERNAL_BINARIES_DIR}/${ARG_EXTERNAL_BINARY}
)
ENDIF()
ENDFOREACH()
ENDIF()
# System binaries
FOREACH(ARG_SYSTEM_BINARY ${ARG_SYSTEM_BINARIES})
TARGET_LINK_LIBRARIES(${TEST_NAME}
${ARG_SYSTEM_BINARY}
)
ENDFOREACH()
# Copy the test to our tests directory
# Note: DEST_TESTS_DIR is defined in our main CMake file...
SET(TEST_FILENAME ${TEST_NAME}${CMAKE_EXECUTABLE_SUFFIX})
IF(WIN32)
ADD_CUSTOM_COMMAND(TARGET ${TEST_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PLUGIN_BUILD_DIR}/${TEST_FILENAME}
${PROJECT_BUILD_DIR}/${TEST_FILENAME})
ENDIF()
ADD_CUSTOM_COMMAND(TARGET ${TEST_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PLUGIN_BUILD_DIR}/${TEST_FILENAME}
${DEST_TESTS_DIR}/${TEST_FILENAME})
# A few macOS specific things
IF(APPLE)
# Clean up our plugin's tests
MACOS_CLEAN_UP_FILE_WITH_QT_DEPENDENCIES(${TEST_NAME} ${DEST_TESTS_DIR} ${TEST_FILENAME})
ENDIF()
ELSE()
MESSAGE(AUTHOR_WARNING "The '${ARG_TEST}' test for the '${PLUGIN_NAME}' plugin does not exist...")
ENDIF()
ENDFOREACH()
ENDIF()
ENDMACRO()
#===============================================================================
MACRO(ADD_PLUGIN_BINARY PLUGIN_NAME)
# Various initialisations
SET(PLUGIN_NAME ${PLUGIN_NAME})
SET(OPTIONS)
SET(ONE_VALUE_KEYWORDS)
SET(MULTI_VALUE_KEYWORDS
INCLUDE_DIRS
)
CMAKE_PARSE_ARGUMENTS(ARG "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}" ${ARGN})
# Various include directories
SET(PLUGIN_INCLUDE_DIRS ${ARG_INCLUDE_DIRS} PARENT_SCOPE)
INCLUDE_DIRECTORIES(${ARG_INCLUDE_DIRS})
# Some settings
SET(PLUGIN_BINARY_DIR ${PROJECT_SOURCE_DIR}/${LOCAL_EXTERNAL_BINARIES_DIR})
SET(PLUGIN_FILENAME ${CMAKE_SHARED_LIBRARY_PREFIX}${PLUGIN_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
# Copy the plugin to our plugins directory
# Note: this is done so that we can, on Windows and Linux, test the use of
# plugins in OpenCOR without first having to package and deploy
# everything...
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${PLUGIN_BINARY_DIR}/${PLUGIN_FILENAME}
${DEST_PLUGINS_DIR}/${PLUGIN_FILENAME})
# On macOS, ensure that @rpath is set in the plugin binary's id
IF(APPLE)
EXECUTE_PROCESS(COMMAND install_name_tool -id @rpath/${PLUGIN_FILENAME} ${PLUGIN_FILENAME}
WORKING_DIRECTORY ${DEST_PLUGINS_DIR}
)
ENDIF()
# Package the plugin, but only if we are not on macOS since it will have
# already been copied
IF(NOT APPLE)
INSTALL(FILES ${PLUGIN_BINARY_DIR}/${PLUGIN_FILENAME}
DESTINATION plugins/${CMAKE_PROJECT_NAME})
ENDIF()
# On macOS, and in case we are on Travis CI, make sure that the plugin
# binary refers to the system version of the Qt libraries since we don't
# embed the Qt libraries in that case (see the main CMakeLists.txt file)
IF(APPLE AND ENABLE_TRAVIS_CI)
FOREACH(MACOS_QT_LIBRARY ${MACOS_QT_LIBRARIES})
SET(MACOS_QT_LIBRARY_FILENAME ${MACOS_QT_LIBRARY}.framework/Versions/${QT_VERSION_MAJOR}/${MACOS_QT_LIBRARY})
EXECUTE_PROCESS(COMMAND install_name_tool -change @rpath/${MACOS_QT_LIBRARY_FILENAME}
${QT_LIBRARY_DIR}/${MACOS_QT_LIBRARY_FILENAME}
${DEST_PLUGINS_DIR}/${PLUGIN_FILENAME})
ENDFOREACH()
ENDIF()
ENDMACRO()
#===============================================================================
MACRO(RETRIEVE_CONFIG_FILES)
FOREACH(CONFIG_FILE ${ARGN})
STRING(REPLACE "PLATFORM_DIR/" "${PLATFORM_DIR}/"
CONFIG_FILE_ORIG "${CONFIG_FILE}")
STRING(REPLACE "PLATFORM_DIR/" ""
CONFIG_FILE_DEST "${CONFIG_FILE}")
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/${CONFIG_FILE_ORIG}
${PROJECT_SOURCE_DIR}/${CONFIG_FILE_DEST})
ENDFOREACH()
ENDMACRO()
#===============================================================================
MACRO(COPY_FILE_TO_BUILD_DIR PROJECT_TARGET ORIG_DIRNAME DEST_DIRNAME FILENAME)
# Copy the file (renaming it, if needed) to the destination folder
# Note: DIRECT is used to copy a file that doesn't first need to be built.
# This means that we can then use EXECUTE_PROCESS() rather than
# ADD_CUSTOM_COMMAND(), and thus reduce the length of the
# PROJECT_TARGET command, something that can be useful on Windows
# since the command might otherwise end up being too long for Windows
# to handle...
IF("${ARGN}" STREQUAL "")
IF("${PROJECT_TARGET}" STREQUAL "DIRECT")
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${ORIG_DIRNAME}/${FILENAME}
${PROJECT_BUILD_DIR}/${DEST_DIRNAME}/${FILENAME})
ELSE()
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${ORIG_DIRNAME}/${FILENAME}
${PROJECT_BUILD_DIR}/${DEST_DIRNAME}/${FILENAME})
ENDIF()
ELSE()
# An argument was passed so use it to rename the file, which is to be
# copied
IF("${PROJECT_TARGET}" STREQUAL "DIRECT")
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${ORIG_DIRNAME}/${FILENAME}
${PROJECT_BUILD_DIR}/${DEST_DIRNAME}/${ARGN})
ELSE()
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${ORIG_DIRNAME}/${FILENAME}
${PROJECT_BUILD_DIR}/${DEST_DIRNAME}/${ARGN})
ENDIF()