forked from cmake-basis/legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommonTools.cmake
3287 lines (3170 loc) · 134 KB
/
CommonTools.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
# ============================================================================
# Copyright (c) 2011-2012 University of Pennsylvania
# Copyright (c) 2013-2014 Carnegie Mellon University
# Copyright (c) 2013-2014 Andreas Schuh
# All rights reserved.
#
# See COPYING file for license information or visit
# http://opensource.andreasschuh.com/cmake-basis/download.html#license
# ============================================================================
##############################################################################
# @file CommonTools.cmake
# @brief Definition of common CMake functions.
#
# @ingroup CMakeTools
##############################################################################
if (__BASIS_COMMONTOOLS_INCLUDED)
return ()
else ()
set (__BASIS_COMMONTOOLS_INCLUDED TRUE)
endif ()
include (CMakeParseArguments)
## @addtogroup CMakeUtilities
# @{
# ============================================================================
# find other packages
# ============================================================================
# ----------------------------------------------------------------------------
## @brief Overloaded find_package() command.
#
# This macro calls CMake's
# <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:find_package">
# find_package()</a> command and converts obsolete all uppercase "<PKG>_<VAR>"
# variables to case-sensitive "<Pkg>_<VAR>" variables.
# It further ensures that the global variables CMAKE_FIND_LIBRARY_SUFFIXES
# and CMAKE_FIND_EXECUTABLE_SUFFIX are reset to the values they had before
# the call to find_package(). This is required if the "Find<Pkg>.cmake" module
# has modified these variables, but not restored their initial value.
macro (find_package)
if (BASIS_DEBUG)
message ("find_package(${ARGV})")
endif ()
# attention: find_package() can be recursive. Hence, use "stack" to keep
# track of library suffixes. Further note that we need to
# maintain a list of lists, which is not supported by CMake.
list (APPEND _BASIS_FIND_LIBRARY_SUFFIXES "{${CMAKE_FIND_LIBRARY_SUFFIXES}}")
list (APPEND _BASIS_FIND_EXECUTABLE_SUFFIX "${CMAKE_FIND_EXECUTABLE_SUFFIX}")
_find_package(${ARGV})
# map obsolete <PKG>_* variables to case-sensitive <Pkg>_*
string (TOUPPER "${ARGV0}" _FP_ARGV0_U)
foreach (_FP_VAR IN ITEMS FOUND DIR USE_FILE
VERSION VERSION_STRING VERSION_MAJOR VERSION_MINOR VERSION_PATCH
INCLUDE_DIR INCLUDE_DIRS INCLUDE_PATH
LIBRARY_DIR LIBRARY_DIRS LIBRARY_PATH)
if (NOT DEFINED ${ARGV0}_${_FP_VAR} AND DEFINED ${_FP_ARGV0_U}_${_FP_VAR})
set (${ARGV0}_${_FP_VAR} "${${_FP_ARGV0_U}_${_FP_VAR}}")
endif ()
endforeach ()
unset (_FP_VAR)
unset (_FP_ARGV0_U)
# restore CMAKE_FIND_LIBRARY_SUFFIXES
string (REGEX REPLACE ";?{([^}]*)}$" "" _BASIS_FIND_LIBRARY_SUFFIXES "${_BASIS_FIND_LIBRARY_SUFFIXES}")
set (CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_MATCH_1}")
# restore CMAKE_FIND_EXECUTABLE_SUFFIX
list (LENGTH _BASIS_FIND_EXECUTABLE_SUFFIX _FP_LAST)
if (_FP_LAST GREATER 0)
math (EXPR _FP_LAST "${_FP_LAST} - 1")
list (REMOVE_AT _BASIS_FIND_EXECUTABLE_SUFFIX ${_FP_LAST})
endif ()
unset (_FP_LAST)
endmacro ()
# ----------------------------------------------------------------------------
## @brief Tokenize dependency specification.
#
# This function parses a dependency specification such as
# "ITK-4.1{TestKernel,IO}" into the package name, i.e., ITK, the requested
# (minimum) package version, i.e., 4.1, and a list of package components, i.e.,
# TestKernel and IO. A valid dependency specification must specify the package
# name of the dependency (case-sensitive). The version and components
# specification are optional. Note that the components specification may
# be separated by an arbitrary number of whitespace characters including
# newlines. The same applies to the specification of the components themselves.
# This allows one to format the dependency specification as follows, for example:
# @code
# ITK {
# TestKernel,
# IO
# }
# @endcode
#
# @param [in] DEP Dependency specification, i.e., "<Pkg>[-<version>][{<Component1>[,...]}]".
# @param [out] PKG Package name.
# @param [out] VER Package version.
# @param [out] CMP List of package components.
function (basis_tokenize_dependency DEP PKG VER CMP)
set (CMPS)
if (DEP MATCHES "^([^ ]+)[ \\n\\t]*{([^}]*)}$")
set (DEP "${CMAKE_MATCH_1}")
string (REPLACE "," ";" COMPONENTS "${CMAKE_MATCH_2}")
foreach (C IN LISTS COMPONENTS)
string (STRIP "${C}" C)
list (APPEND CMPS ${C})
endforeach ()
endif ()
if (DEP MATCHES "^(.*)-([0-9]+)(\\.[0-9]+)?(\\.[0-9]+)?(\\.[0-9]+)?$")
set (${PKG} "${CMAKE_MATCH_1}" PARENT_SCOPE)
set (${VER} "${CMAKE_MATCH_2}${CMAKE_MATCH_3}${CMAKE_MATCH_4}${CMAKE_MATCH_5}" PARENT_SCOPE)
else ()
set (${PKG} "${DEP}" PARENT_SCOPE)
set (${VER} "" PARENT_SCOPE)
endif ()
set (${CMP} "${CMPS}" PARENT_SCOPE)
endfunction ()
# ----------------------------------------------------------------------------
## @brief Find external software package or other project module.
#
# This function replaces CMake's
# <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:find_package">
# find_package()</a> command and extends its functionality.
# In particular, if the given package name is the name of another module
# of this project (the top-level project), it ensures that this module is
# found instead of an external package.
#
# If the package is found, but only optionally used, i.e., the @c REQUIRED
# argument was not given to this macro, a <tt>USE_<Pkg></tt> option is
# added by this macro which is by default @c ON. This option can be set to
# @c OFF by the user in order to force the <tt><Pkg>_FOUND</tt> variable
# to be set to @c FALSE again even if the package was found. This allows the
# user to specify which of the optional dependencies should actually not be
# used for the build of the software even though these packages are installed
# on their system.
#
# @param [in] PACKAGE Name of other package. Optionally, the package name
# can include a version specification as suffix which
# is separated by the package name using a dash (-), i.e.,
# <Package>[-major[.minor[.patch[.tweak]]]].
# If a version specification is given, it is passed on as
# @c version argument to CMake's
# <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:find_package">
# find_package()</a> command.
# @param [in] ARGN Advanced arguments for
# <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:find_package">
# find_package()</a>.
#
# @retval <PACKAGE>_FOUND Whether the given package was found.
#
# @sa http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:find_package
#
# @ingroup CMakeAPI
macro (basis_find_package PACKAGE)
# parse arguments
CMAKE_PARSE_ARGUMENTS (
ARGN
"EXACT;QUIET;REQUIRED"
""
"COMPONENTS"
${ARGN}
)
# --------------------------------------------------------------------------
# tokenize dependency specification
basis_tokenize_dependency ("${PACKAGE}" PKG VER CMPS)
list (APPEND ARGN_COMPONENTS ${CMPS})
unset (CMPS)
if (ARGN_UNPARSED_ARGUMENTS MATCHES "^[0-9]+(\\.[0-9]+)*$" AND VER)
message (FATAL_ERROR "Cannot use both version specification as part of "
"package name and explicit version argument.")
else ()
set (VER "${CMAKE_MATCH_0}")
endif ()
# --------------------------------------------------------------------------
# preserve <PKG>_DIR variable which might get reset if different versions
# of the package are searched or if package is optional and deselected
set (PKG_DIR "${${PKG}_DIR}")
# --------------------------------------------------------------------------
# some debugging output
if (BASIS_DEBUG)
message ("** basis_find_package()")
message ("** Package: ${PKG}")
if (VER)
message ("** Version: ${VER}")
endif ()
if (ARGN_COMPONENTS)
message ("** Components: [${ARGN_COMPONENTS}]")
endif ()
endif ()
# --------------------------------------------------------------------------
# find other modules of same project
set (PKG_IS_MODULE FALSE)
if (PROJECT_IS_MODULE)
# allow modules to specify top-level project as dependency
if ("^${PKG}$" STREQUAL "^${TOPLEVEL_PROJECT_NAME}$")
if (BASIS_DEBUG)
message ("** This is the top-level project.")
endif ()
set (${PKG}_FOUND TRUE)
# look for other module of top-level project
else ()
list (FIND PROJECT_MODULES "${PKG}" PKGIDX)
if (NOT PKGIDX EQUAL -1)
set (PKG_IS_MODULE TRUE)
list (FIND PROJECT_MODULES_ENABLED "${PKG}" PKGIDX)
if (NOT PKGIDX EQUAL -1)
if (BASIS_DEBUG)
message ("** Identified it as other module of this project.")
endif ()
include ("${${PKG}_DIR}/${TOPLEVEL_PROJECT_PACKAGE_CONFIG_PREFIX}${PKG}Config.cmake")
set (${PKG}_FOUND TRUE)
else ()
set (${PKG}_FOUND FALSE)
endif ()
endif ()
unset (PKGIDX)
endif ()
# --------------------------------------------------------------------------
# find bundled packages
else ()
list (FIND BUNDLE_PROJECTS "${PKG}" PKGIDX)
if (NOT PKGIDX EQUAL -1)
if (EXISTS "${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIG_DIR}/${PKG}Config.cmake")
set (PKG_CONFIG_FILE "${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIG_DIR}/${PKG}Config.cmake")
else ()
string (TOLOWER "${PKG}" PKG_L)
if (EXISTS "${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIG_DIR}/${PKG_L}-config.cmake")
set (PKG_CONFIG_FILE "${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIG_DIR}/${PKG_L}-config.cmake")
else ()
set (PKG_CONFIG_FILE)
endif ()
unset (PKG_L)
endif ()
if (PKG_CONFIG_FILE)
if (BASIS_DEBUG)
message ("** Identified it as other package of this bundle.")
endif ()
get_filename_component (PKG_CONFIG_DIR "${PKG_CONFIG_FILE}" PATH)
basis_set_or_update_value (${PKG}_DIR "${PKG_CONFIG_DIR}")
include ("${PKG_CONFIG_FILE}")
set (${PKG}_FOUND TRUE)
unset (PKG_CONFIG_DIR)
endif ()
unset (PKG_CONFIG_FILE)
endif ()
unset (PKGIDX)
endif ()
# --------------------------------------------------------------------------
# otherwise, look for external package
if (NOT PKG_IS_MODULE)
# ------------------------------------------------------------------------
# make <PKG>_DIR variable visible in GUI by caching it if not done yet
basis_is_cached (_BFP_CACHED ${PKG}_DIR)
if (DEFINED ${PKG}_DIR AND NOT _BFP_CACHED)
set (${PKG}_DIR "${${PKG}_DIR}" CACHE PATH "Installation directory of ${PKG}.")
endif ()
unset (_BFP_CACHED)
# ------------------------------------------------------------------------
# determine if additional components of found package should be discovered
set (FIND_ADDITIONAL_COMPONENTS FALSE)
if (${PKG}_FOUND)
if (${PKG}_FOUND_COMPONENTS AND ARGN_COMPONENTS)
foreach (_C ${ARGN_COMPONENTS})
list (FIND ${PKG}_FOUND_COMPONENTS "${_C}" _IDX)
if (_IDX EQUAL -1)
set (FIND_ADDITIONAL_COMPONENTS TRUE)
break ()
endif ()
endforeach ()
elseif (${PKG}_FOUND_COMPONENTS OR ARGN_COMPONENTS)
set (FIND_ADDITIONAL_COMPONENTS TRUE)
endif ()
endif ()
# ------------------------------------------------------------------------
# look for external package if not found or additional components needed
if (NOT ${PKG}_FOUND OR FIND_ADDITIONAL_COMPONENTS)
set (_${PKG}_FOUND "${${PKG}_FOUND}") # used to decide what the intersection of
# of multiple find invocations for the same
# package with different components will be
# ----------------------------------------------------------------------
# reset other <PKG>[_-]* variables if <PKG>_DIR changed
if (_${PKG}_DIR AND ${PKG}_DIR) # internal _<PKG>_DIR cache entry set below
basis_sanitize_for_regex (_BFP_RE "${${PKG}_DIR}")
if (NOT _${PKG}_DIR MATCHES "^${_BFP_RE}$")
get_cmake_property (_BFP_VARS VARIABLES)
basis_sanitize_for_regex (PKG_RE "${PKG}")
foreach (_BFP_VAR IN LISTS _BFP_VARS)
if (_BFP_VAR MATCHES "^${PKG_RE}[_-]" AND NOT "^${_BFP_VAR}$" STREQUAL "^${PKG}_DIR$")
basis_is_cached (_BFP_CACHED ${_BFP_VAR})
if (_BFP_CACHED)
get_property (_BFP_TYPE CACHE ${_BFP_VAR} PROPERTY TYPE)
if (NOT _BFP_TYPE MATCHES INTERNAL)
set_property (CACHE ${_BFP_VAR} PROPERTY VALUE "${_BFP_VAR}-NOTFOUND")
set_property (CACHE ${_BFP_VAR} PROPERTY TYPE INTERNAL)
endif ()
endif ()
endif ()
endforeach ()
unset (PKG_RE)
unset (_BFP_VAR)
unset (_BFP_VARS)
unset (_BFP_CACHED)
unset (_BFP_TYPE)
endif ()
unset (_BFP_RE)
endif ()
# ----------------------------------------------------------------------
# hide or show already defined <PKG>_DIR cache entry
if (DEFINED ${PKG}_DIR AND DEFINED USE_${PKG})
if (USE_${PKG})
mark_as_advanced (CLEAR ${PKG}_DIR)
else ()
mark_as_advanced (FORCE ${PKG}_DIR)
endif ()
endif ()
# ----------------------------------------------------------------------
# find external packages
if (DEFINED USE_${PKG} AND NOT USE_${PKG})
set (${PKG}_FOUND FALSE)
else ()
# circumvent issue with CMake's find_package() interpreting these variables
# relative to the current binary directory instead of the top-level directory
if (${PKG}_DIR AND NOT IS_ABSOLUTE "${${PKG}_DIR}")
set (${PKG}_DIR "${CMAKE_BINARY_DIR}/${${PKG}_DIR}")
get_filename_component (${PKG}_DIR "${${PKG}_DIR}" ABSOLUTE)
endif ()
# moreover, users tend to specify the installation prefix instead of the
# actual directory containing the package configuration file
if (IS_DIRECTORY "${${PKG}_DIR}")
list (INSERT CMAKE_PREFIX_PATH 0 "${${PKG}_DIR}")
endif ()
# now look for the package
set (FIND_ARGN)
if (ARGN_EXACT)
list (APPEND FIND_ARGN "EXACT")
endif ()
if (ARGN_QUIET)
list (APPEND FIND_ARGN "QUIET")
endif ()
if (ARGN_COMPONENTS)
list (APPEND FIND_ARGN "COMPONENTS" ${ARGN_COMPONENTS})
elseif (ARGN_REQUIRED)
list (APPEND FIND_ARGN "REQUIRED")
endif ()
if (PKG MATCHES "^(MFC|wxWidgets)$")
# if Find<Pkg>.cmake prints status message, don't do it here
find_package (${PKG} ${VER} ${FIND_ARGN})
else ()
set (_STATUS "Looking for ${PKG}")
if (VER)
set (_STATUS "${_STATUS} ${VER}")
endif ()
if (ARGN_COMPONENTS)
set (_STATUS "${_STATUS} [${ARGN_COMPONENTS}]")
endif ()
if (NOT ARGN_REQUIRED)
set (_STATUS "${_STATUS} (optional)")
endif ()
set (_STATUS "${_STATUS}...")
message (STATUS "${_STATUS}")
find_package (${PKG} ${VER} ${FIND_ARGN})
# set common <Pkg>_VERSION_STRING variable if possible and not set
if (NOT DEFINED ${PKG}_VERSION_STRING)
if ("^${PKG}$" STREQUAL "^PythonInterp$")
set (${PKG}_VERSION_STRING ${PYTHON_VERSION_STRING})
elseif ("^${PKG}$" STREQUAL "^JythonInterp$")
set (${PKG}_VERSION_STRING ${JYTHON_VERSION_STRING})
elseif (DEFINED ${PKG}_VERSION_MAJOR)
set (${PKG}_VERSION_STRING ${${PKG}_VERSION_MAJOR})
if (DEFINED ${PKG}_VERSION_MINOR)
set (${PKG}_VERSION_STRING ${${PKG}_VERSION_STRING}.${${PKG}_VERSION_MINOR})
if (DEFINED ${PKG}_VERSION_PATCH)
set (${PKG}_VERSION_STRING ${${PKG}_VERSION_STRING}.${${PKG}_VERSION_PATCH})
endif ()
endif ()
elseif (DEFINED ${PKG}_VERSION)
set (${PKG}_VERSION_STRING ${${PKG}_VERSION})
endif ()
endif ()
# verbose output of information about found package
if (${PKG}_FOUND)
set (_STATUS "${_STATUS} - found")
if (BASIS_VERBOSE)
if (DEFINED ${PKG}_VERSION_STRING AND NOT ${PKG}_VERSION_STRING MATCHES "^0.0.0$")
set (_STATUS "${_STATUS} v${${PKG}_VERSION_STRING}")
endif ()
if (${PKG}_DIR)
set (_STATUS "${_STATUS} at ${${PKG}_DIR}")
endif ()
endif ()
else ()
set (_STATUS "${_STATUS} - not found")
endif ()
message (STATUS "${_STATUS}")
endif ()
# remember which components where found already
if (${PKG}_FOUND AND ARGN_COMPONENTS)
if (${PKG}_FOUND_COMPONENTS)
list (APPEND ARGN_COMPONENTS ${${PKG}_FOUND_COMPONENTS})
list (REMOVE_DUPLICATES ARGN_COMPONENTS)
endif ()
set (${PKG}_FOUND_COMPONENTS "${ARGN_COMPONENTS}")
endif ()
# if previously components of this package where found and the additional
# components are only optional, set <PKG>_FOUND to TRUE again
if (_${PKG}_FOUND AND NOT ARGN_REQUIRED)
set (${PKG}_FOUND TRUE)
endif ()
endif ()
# ----------------------------------------------------------------------
# reset <PKG>_DIR variable for possible search of different package version
if (PKG_DIR AND NOT ${PKG}_DIR)
basis_set_or_update_value (${PKG}_DIR "${PKG_DIR}")
endif ()
# ----------------------------------------------------------------------
# remember current/previous <PKG>_DIR
# (used above to reset other <PKG>_* variables whenever <PKG>_DIR changed)
if (DEFINED ${PKG}_DIR)
set (_${PKG}_DIR "${${PKG}_DIR}" CACHE INTERNAL "(Previous) Installation directory of ${PKG}." FORCE)
endif ()
endif ()
# ------------------------------------------------------------------------
# provide option which allows users to disable use of optional packages
if (${PKG}_FOUND AND NOT ARGN_REQUIRED)
if (NOT DEFINED USE_${PKG})
option (USE_${PKG} "Enable/disable use of package ${PKG}." ON)
mark_as_advanced (USE_${PKG})
endif ()
if (NOT USE_${PKG})
set (${PKG}_FOUND FALSE)
endif ()
endif ()
endif ()
# --------------------------------------------------------------------------
# unset locally used variables
unset (PACKAGE_DIR)
unset (PKG)
unset (VER)
unset (USE_PKG_OPTION)
unset (PKG_IS_MODULE)
unset (FIND_ADDITIONAL_COMPONENTS)
endmacro ()
# ----------------------------------------------------------------------------
## @brief Use found package.
#
# This macro includes the package's use file if the variable @c <Pkg>_USE_FILE
# is defined. Otherwise, it adds the include directories to the search path
# for include paths if possible. Therefore, the corresponding package
# configuration file has to set the proper CMake variables, i.e.,
# either @c <Pkg>_INCLUDES, @c <Pkg>_INCLUDE_DIRS, or @c <Pkg>_INCLUDE_DIR.
#
# If the given package name is the name of another module of this project
# (the top-level project), this function includes the use file of the specified
# module.
#
# @note As some packages still use all captial variables instead of ones
# prefixed by a string that follows the same capitalization as the
# package's name, this function also considers these if defined instead.
# Hence, if @c <PKG>_INCLUDES is defined, but not @c <Pkg>_INCLUDES, it
# is used in place of the latter.
#
# @note According to an email on the CMake mailing list, it is not a good idea
# to use basis_link_directories() any more given that the arguments to
# basis_target_link_libraries() are absolute paths to the library files.
# Therefore, this code is commented and not used. It remains here as a
# reminder only.
#
# @param [in] PACKAGE Name of other package. Optionally, the package name
# can include a version specification as suffix which
# is separated by the package name using a dash (-), i.e.,
# <Package>[-major[.minor[.patch[.tweak]]]].
# A version specification is simply ignored by this macro.
#
# @ingroup CMakeAPI
macro (basis_use_package PACKAGE)
# tokenize package specification
basis_tokenize_dependency ("${PACKAGE}" PKG VER CMPS)
# use package
foreach (A IN ITEMS "WORKAROUND FOR NOT BEING ABLE TO USE RETURN")
if (BASIS_DEBUG)
message ("** basis_use_package()")
message ("** Package: ${PKG}")
endif ()
if (PROJECT_IS_MODULE)
# ignore BASIS as module dependency
# important if BASIS itself is a project module
if ("^${PKG}$" STREQUAL "^BASIS$")
if (BASIS_DEBUG)
message ("** Ignoring BASIS dependency as it clearly is used already by the top-level project.")
endif ()
break ()
# allow modules to specify top-level project as dependency
elseif ("^${PKG}$" STREQUAL "^${TOPLEVEL_PROJECT_NAME}$")
if (BASIS_DEBUG)
message ("** This is the top-level project.")
endif ()
break () # instead of return()
# use other module of top-level project
else ()
list (FIND PROJECT_MODULES "${PKG}" PKGIDX)
if (NOT PKGIDX EQUAL -1)
if (${PKG}_FOUND)
if (BASIS_DEBUG)
message ("** Include package use file of other module.")
endif ()
include ("${${PKG}_USE_FILE}")
break () # instead of return()
else ()
message (FATAL_ERROR "Module ${PKG} not found! This must be an error in BASIS."
" Report this issue to the maintainer of this package.")
endif ()
endif ()
unset (PKGIDX)
endif ()
endif ()
# if this package is an external project, i.e., a project build as part
# of the same superbuild as this project, set BUNDLE_PROJECT to TRUE.
# it is used by (basis_)link_directories() and add_library() to mark
# the imported link directories and target as belonging to the same
# installation. this is in particular important for the RPATH settings.
# whether this package is an external project or not, is decided by the
# BUNDLE_PROJECTS variable which must be set using the -D option of
# cmake to a list naming all the other packages which are part of the
# superbuild.
if (BUNDLE_PROJECTS)
list (FIND BUNDLE_PROJECTS "${PKG}" PKGIDX)
if (PKGIDX EQUAL -1)
set (BUNDLE_PROJECT FALSE)
else ()
set (BUNDLE_PROJECT TRUE)
endif ()
unset (PKGIDX)
endif ()
# use external package
if (${PKG}_FOUND)
# use package only if basis_use_package() not invoked before
if (BASIS_USE_${PKG}_INCLUDED)
if (BASIS_DEBUG)
message ("** External package used before already.")
endif ()
break ()
endif ()
if (${PKG}_USE_FILE)
if (BASIS_DEBUG)
message ("** Include package use file of external package.")
endif ()
if ("^${PKG}$" STREQUAL "^BASIS$")
include ("${${PKG}_USE_FILE}" NO_POLICY_SCOPE)
else ()
include ("${${PKG}_USE_FILE}")
endif ()
else ()
if (BASIS_DEBUG)
message ("** Use variables which were set by basis_find_package().")
endif ()
# OpenCV
if ("^${PKG}$" STREQUAL "^OpenCV$")
# the cv.h may be found as part of PerlLibs, the include path of
# which is added at first by BASISConfig.cmake
if (OpenCV_INCLUDE_DIRS)
basis_include_directories (BEFORE ${OpenCV_INCLUDE_DIRS})
elseif (OpenCV_INCLUDE_DIR)
basis_include_directories (BEFORE ${OpenCV_INCLUDE_DIR})
endif ()
# generic
else ()
if (${PKG}_INCLUDE_DIRS)
basis_include_directories (${${PKG}_INCLUDE_DIRS})
elseif (${PKG}_INCLUDES)
basis_include_directories (${${PKG}_INCLUDES})
elseif (${PKG}_INCLUDE_PATH)
basis_include_directories (${${PKG}_INCLUDE_PATH})
elseif (${PKG}_INCLUDE_DIR)
basis_include_directories (${${PKG}_INCLUDE_DIR})
endif ()
endif ()
endif ()
set (BASIS_USE_${PKG}_INCLUDED TRUE)
elseif (ARGC GREATER 1 AND "^${ARGV1}$" STREQUAL "^REQUIRED$")
if (BASIS_DEBUG)
basis_dump_variables ("${PROJECT_BINARY_DIR}/VariablesAfterFind${PKG}.cmake")
endif ()
message (FATAL_ERROR "Package ${PACKAGE} not found!")
endif ()
# reset switch that identifies currently imported targets and link directories
# as belonging to an external project which is part of the same superbuild
set (BUNDLE_PROJECT FALSE)
endforeach ()
endmacro ()
# ============================================================================
# basis_get_filename_component / basis_get_relative_path
# ============================================================================
# ----------------------------------------------------------------------------
## @brief Fixes CMake's
# <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:get_filename_component">
# get_filename_component()</a> command.
#
# The get_filename_component() command of CMake returns the entire portion
# after the first period (.) [including the period] as extension. However,
# only the component following the last period (.) [including the period]
# should be considered to be the extension.
#
# @note Consider the use of the basis_get_filename_component() macro as
# an alias to emphasize that this function is different from CMake's
# <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:get_filename_component">
# get_filename_component()</a> command.
#
# @todo Fix issue http://public.kitware.com/Bug/view.php?id=15743 which
# affects also basis_get_relative_path.
#
# @param [in,out] ARGN Arguments as accepted by get_filename_component().
#
# @returns Sets the variable named by the first argument to the requested
# component of the given file path.
#
# @sa http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:get_filename_component
# @sa basis_get_filename_component()
function (get_filename_component)
if (ARGC LESS 3)
message (FATAL_ERROR "[basis_]get_filename_component(): At least three arguments required!")
elseif (ARGC GREATER 4)
message (FATAL_ERROR "[basis_]get_filename_component(): Too many arguments!")
endif ()
list (GET ARGN 0 VAR)
list (GET ARGN 1 STR)
list (GET ARGN 2 CMD)
if (CMD MATCHES "^EXT")
_get_filename_component (${VAR} "${STR}" ${CMD})
string (REGEX MATCHALL "\\.[^.]*" PARTS "${${VAR}}")
list (LENGTH PARTS LEN)
if (LEN GREATER 1)
math (EXPR LEN "${LEN} - 1")
list (GET PARTS ${LEN} ${VAR})
endif ()
elseif (CMD MATCHES "NAME_WE")
_get_filename_component (${VAR} "${STR}" NAME)
string (REGEX REPLACE "\\.[^.]*$" "" ${VAR} ${${VAR}})
else ()
_get_filename_component (${VAR} "${STR}" ${CMD})
endif ()
if (ARGC EQUAL 4)
if (NOT "^${ARGV3}$" STREQUAL "^CACHE$")
message (FATAL_ERROR "[basis_]get_filename_component(): Invalid fourth argument: ${ARGV3}!")
else ()
set (${VAR} "${${VAR}}" CACHE STRING "")
endif ()
else ()
set (${VAR} "${${VAR}}" PARENT_SCOPE)
endif ()
endfunction ()
# ----------------------------------------------------------------------------
## @brief Alias for the overwritten get_filename_component() function.
#
# @sa get_filename_component()
#
# @ingroup CMakeAPI
macro (basis_get_filename_component)
get_filename_component (${ARGN})
endmacro ()
# ----------------------------------------------------------------------------
## @brief Get path relative to a given base directory.
#
# Unlike the file(RELATIVE_PATH ...) command of CMake which if @p PATH and
# @p BASE are the same directory returns an empty string, this function
# returns a dot (.) in this case instead.
#
# @param [out] REL @c PATH relative to @c BASE.
# @param [in] BASE Path of base directory. If a relative path is given, it
# is made absolute using basis_get_filename_component()
# with ABSOLUTE as last argument.
# @param [in] PATH Absolute or relative path. If a relative path is given
# it is made absolute using basis_get_filename_component()
# with ABSOLUTE as last argument.
#
# @returns Sets the variable named by the first argument to the relative path.
#
# @sa http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:file
#
# @ingroup CMakeAPI
function (basis_get_relative_path REL BASE PATH)
if (BASE MATCHES "^$")
message (FATAL_ERROR "Empty string given where (absolute) base directory path expected!")
endif ()
if (PATH MATCHES "^$")
set (PATH ".")
endif ()
# Attention: http://public.kitware.com/Bug/view.php?id=15743
basis_get_filename_component (PATH "${PATH}" ABSOLUTE)
basis_get_filename_component (BASE "${BASE}" ABSOLUTE)
if (NOT PATH)
message (FATAL_ERROR "basis_get_relative_path(): No PATH given!")
endif ()
if (NOT BASE)
message (FATAL_ERROR "basis_get_relative_path(): No BASE given!")
endif ()
file (RELATIVE_PATH P "${BASE}" "${PATH}")
if ("${P}" STREQUAL "")
set (P ".")
endif ()
set (${REL} "${P}" PARENT_SCOPE)
endfunction ()
## @brief Create a string from a list of variables indicating if they are defined and their values.
#
# Useful for debug and user errors, for example:
# @code
# set(VAR1 "I'm a string")
# set(VAR2 2)
# basis_variable_value_status(VAR_INFO_STRING VAR1 VAR2 VAR3)
# message(STATUS ${VAR_INFO_STRING})
# @endcode
#
# @param[out] VAR_INFO_STRING The output string variable that will set with the debug string.
# @param[in] ARGN List of variables to be put into a string along with their value.
function(basis_variable_value_status VAR_INFO_STRING)
set (OUTPUT_STRING)
foreach (VARIABLE_NAME IN ITEMS ${ARGN})
if (DEFINED ${VARIABLE_NAME})
set (OUTPUT_STRING "${OUTPUT_STRING}\n variable name: ${VARIABLE_NAME} value: ${${VARIABLE_NAME}}")
else ()
set (OUTPUT_STRING "${OUTPUT_STRING}\n variable name: ${VARIABLE_NAME} value is not defined")
endif ()
endforeach ()
set (${VAR_INFO_STRING} ${OUTPUT_STRING} PARENT_SCOPE)
endfunction()
## @brief Checks for a list of variables required later in the script.
#
# Produces a clear error message explaining the problem and how to fix it if they are not present.
#
# @code
# basis_variable_check(
# REQUIRED
# LIBRARY1_INCLUDE_DIRS
# LIBRARY2_INCLUDE_DIRS
# LIBRARY2_LIBRARIES
# OPTIONAL
# LIBRARY3_INCLUDE_DIRS
# LIBRARY3_LIBRARIES
# OPTIONAL_PATH
#
# )
# @endcode
#
# @param [in] ARGN This argument list is parsed and the following
# arguments are extracted.
# @par
# <table border="0">
# <tr>
# @tp @b REQUIRED var... @endtp
# <td>List of variables that MUST be set to run this script correctly.
# Will produce a FATAL_ERROR message explaining which variables
# are misisng and exit the cmake script.</td>
# </tr>
# <tr>
# @tp @b OPTIONAL var... @endtp
# <td>List of variables need not be set to run this script correctly.</td>
# </tr>
# <tr>
# @tp @b PATH_EXISTS var... @endtp
# <td>List of path variables that MUST be set to a location that exists.</td>
# </tr>
# <tr>
# @tp @b OPTIONAL_PATH_EXISTS var... @endtp
# <td>List of path variables that are optional, but once set must be empty
# or provide a path to location that exists.</td>
# </tr>
# </table>
function(basis_variable_check)
set(options ) # currently none
set(oneValueArgs ) # currently none
set(multiValueArgs REQUIRED OPTIONAL PATH_EXISTS OPTIONAL_PATH_EXISTS )
cmake_parse_arguments(VARIABLE_CONFIGURATION "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
#-------------------------------------------
# Create the error strings for missing REQUIRED variables
set(MISSING_REQUIRED_VARIABLES)
foreach(VARIABLE_NAME IN LISTS VARIABLE_CONFIGURATION_REQUIRED)
if(NOT ${VARIABLE_NAME})
list(APPEND MISSING_REQUIRED_VARIABLES ${VARIABLE_NAME})
endif()
endforeach()
if(MISSING_REQUIRED_VARIABLES)
basis_variable_value_status(MISSING_REQUIRED_VARIABLES_STATUS ${MISSING_REQUIRED_VARIABLES})
set(MISSING_VARIABLES_ERROR "\nThe following variables are marked as REQUIRED but they are not set to a valid value. Please define the variables correctly in your cmake script or on the command line using -D. ${MISSING_REQUIRED_VARIABLES_STATUS}")
endif(MISSING_REQUIRED_VARIABLES)
#-------------------------------------------
# Create and print the warning strings for missing OPTIONAL variables
set(MISSING_OPTIONAL_VARIABLES)
foreach(VARIABLE_NAME IN LISTS VARIABLE_CONFIGURATION_OPTIONAL)
if(NOT ${VARIABLE_NAME})
list(APPEND MISSING_OPTIONAL_VARIABLES ${VARIABLE_NAME})
endif()
endforeach()
if(MISSING_OPTIONAL_VARIABLES)
basis_variable_value_status(MISSING_OPTIONAL_VARIABLES_STATUS ${MISSING_OPTIONAL_VARIABLES})
set(MISSING_VARIABLES_WARNING "\nThe following variables are marked as OPTIONAL but they are not set to a valid value. Please define the variables correctly in your cmake script or on the command line using -D. ${MISSING_OPTIONAL_VARIABLES_STATUS}")
message(AUTHOR_WARNING "${MISSING_VARIABLES_WARNING}")
endif(MISSING_OPTIONAL_VARIABLES)
#-------------------------------------------
# Create the error strings for missing or nonexistant REQUIRED PATH variables
set(MISSING_PATH_EXISTS)
foreach(VARIABLE_NAME IN LISTS VARIABLE_CONFIGURATION_PATH_EXISTS)
if(NOT DEFINED ${VARIABLE_NAME} OR NOT EXISTS ${${VARIABLE_NAME}})
list(APPEND MISSING_PATH_EXISTS ${VARIABLE_NAME})
endif()
endforeach()
if(MISSING_PATH_EXISTS)
basis_variable_value_status(MISSING_PATH_EXISTS_STATUS ${MISSING_PATH_EXISTS})
set(PATH_EXISTS_ERROR "\nThe following PATH variables are marked as REQUIRED but they are not set to a valid location. Please define the variables correctly in your cmake script or on the command line using -D. ${MISSING_PATH_EXISTS_STATUS}")
endif(MISSING_PATH_EXISTS)
#-------------------------------------------
# Create the error strings for missing or nonexistant OPTIONAL PATH variables
set(MISSING_OPTIONAL_PATH_EXISTS)
foreach(VARIABLE_NAME IN LISTS VARIABLE_CONFIGURATION_OPTIONAL_PATH_EXISTS)
if(DEFINED ${VARIABLE_NAME} AND NOT ${${VARIABLE_NAME}} STREQUAL "" AND NOT EXISTS ${${VARIABLE_NAME}})
# add VARIABLE_NAME to error list if a nonempty path is defined, but does not point to a real location
list(APPEND MISSING_OPTIONAL_PATH_EXISTS ${VARIABLE_NAME})
endif()
endforeach()
if(MISSING_OPTIONAL_PATH_EXISTS)
basis_variable_value_status(MISSING_OPTIONAL_PATH_EXISTS_STATUS ${MISSING_OPTIONAL_PATH_EXISTS})
#debug:
#message(STATUS "MISSING_OPTIONAL_PATH_EXISTS: ${MISSING_OPTIONAL_PATH_EXISTS}")
#message(STATUS "MISSING_OPTIONAL_PATH_EXISTS_STATUS: ${MISSING_OPTIONAL_PATH_EXISTS_STATUS}")
set(OPTIONAL_PATH_EXISTS_ERROR "\nThe following PATH variables are marked as OPTIONAL but they are not set to a valid location. Please define the variables correctly in your cmake script or on the command line using -D. ${MISSING_OPTIONAL_PATH_EXISTS_STATUS}")
endif(MISSING_OPTIONAL_PATH_EXISTS)
#-------------------------------------------
# Print all fatal errors at once, because the script will halt
if(MISSING_VARIABLES_ERROR OR PATH_EXISTS_ERROR OR OPTIONAL_PATH_EXISTS_ERROR)
message(FATAL_ERROR "${MISSING_VARIABLES_ERROR}${PATH_EXISTS_ERROR}${OPTIONAL_PATH_EXISTS_ERROR}\n")
endif(MISSING_VARIABLES_ERROR OR PATH_EXISTS_ERROR OR OPTIONAL_PATH_EXISTS_ERROR)
endfunction(basis_variable_check)
# ============================================================================
# name / version
# ============================================================================
# ----------------------------------------------------------------------------
## @brief Convert string to lowercase only or mixed case.
#
# Strings in all uppercase or all lowercase are converted to all lowercase
# letters because these are usually used for acronymns. All other strings
# are returned unmodified with the one exception that the first letter has
# to be uppercase for mixed case strings.
#
# This function is in particular used to normalize the project name for use
# in installation directory paths and namespaces.
#
# @param [out] OUT String in CamelCase.
# @param [in] STR String.
function (basis_normalize_name OUT STR)
# strings in all uppercase or all lowercase such as acronymns are an
# exception and shall be converted to all lowercase instead
string (TOLOWER "${STR}" L)
string (TOUPPER "${STR}" U)
if ("${STR}" STREQUAL "${L}" OR "${STR}" STREQUAL "${U}")
set (${OUT} "${L}" PARENT_SCOPE)
# change first letter to uppercase
else ()
string (SUBSTRING "${U}" 0 1 A)
string (SUBSTRING "${STR}" 1 -1 B)
set (${OUT} "${A}${B}" PARENT_SCOPE)
endif ()
endfunction ()
# ----------------------------------------------------------------------------
## @brief Extract version numbers from version string.
#
# @param [in] VERSION Version string in the format "MAJOR[.MINOR[.PATCH]]".
# @param [out] MAJOR Major version number if given or 0.
# @param [out] MINOR Minor version number if given or 0.
# @param [out] PATCH Patch number if given or 0.
#
# @returns See @c [out] parameters.
function (basis_version_numbers VERSION MAJOR MINOR PATCH)
if (VERSION MATCHES "([0-9]+)(\\.[0-9]+)?(\\.[0-9]+)?(rc[1-9][0-9]*|[a-z]+)?")
if (CMAKE_MATCH_1)
set (VERSION_MAJOR ${CMAKE_MATCH_1})
else ()
set (VERSION_MAJOR 0)
endif ()
if (CMAKE_MATCH_2)
set (VERSION_MINOR ${CMAKE_MATCH_2})
string (REGEX REPLACE "^\\." "" VERSION_MINOR "${VERSION_MINOR}")
else ()
set (VERSION_MINOR 0)
endif ()
if (CMAKE_MATCH_3)
set (VERSION_PATCH ${CMAKE_MATCH_3})
string (REGEX REPLACE "^\\." "" VERSION_PATCH "${VERSION_PATCH}")
else ()
set (VERSION_PATCH 0)
endif ()
else ()
set (VERSION_MAJOR 0)
set (VERSION_MINOR 0)
set (VERSION_PATCH 0)
endif ()
set ("${MAJOR}" "${VERSION_MAJOR}" PARENT_SCOPE)
set ("${MINOR}" "${VERSION_MINOR}" PARENT_SCOPE)
set ("${PATCH}" "${VERSION_PATCH}" PARENT_SCOPE)
endfunction ()
# ============================================================================
# set
# ============================================================================
# ----------------------------------------------------------------------------
## @brief Set flag given mutually exclusive
# ARGN_<FLAG> and ARGN_NO<FLAG> function arguments.
#
# @param [in] PREFIX Prefix of function arguments. Set to the first argument
# of the CMAKE_PARSE_ARGUMENTS() command.
# @param [out] FLAG Name of flag.
# @param [in] DEFAULT Default flag value if neither <tt>ARGN_<FLAG;gt;</tt>
# nor <tt>ARGN_NO<FLAG;gt;</tt> evaluates to true.
macro (basis_set_flag PREFIX FLAG DEFAULT)
if (${PREFIX}_${FLAG} AND ${PREFIX}_NO${FLAG})
message (FATAL_ERROR "Options ${FLAG} and NO${FLAG} are mutually exclusive!")
endif ()
if (${PREFIX}_${FLAG})
set (${FLAG} TRUE)
elseif (${PREFIX}_NO${FLAG})
set (${FLAG} FALSE)
else ()
set (${FLAG} ${DEFAULT})
endif ()
endmacro ()
# ----------------------------------------------------------------------------
## @brief Determine if cache entry exists.
#
# @param [out] VAR Name of boolean result variable.
# @param [in] ENTRY Name of cache entry.
macro (basis_is_cached VAR ENTRY)
if (DEFINED ${ENTRY})
get_property (${VAR} CACHE ${ENTRY} PROPERTY TYPE SET)
else ()
set (${VAR} FALSE)
endif ()
endmacro ()
# ----------------------------------------------------------------------------
## @brief Set type of variable.
#
# If the variable is cached, the type is updated, otherwise, a cache entry
# of the given type with the current value of the variable is added.
#
# @param [in] VAR Name of variable.
# @param [in] TYPE Desired type of variable.
# @param [in] ARGN Optional DOC string used if variable was not cached before.
macro (basis_set_or_update_type VAR TYPE)
basis_is_cached (_CACHED ${VAR})
if (_CACHED)
set_property (CACHE ${VAR} PROPERTY TYPE ${TYPE})
else ()
set (${VAR} "${${VAR}}" CACHE ${TYPE} "${ARGN}" FORCE)
endif ()
unset (_CACHED)
endmacro ()
# ----------------------------------------------------------------------------
## @brief Change type of cached variable.
#
# If the variable is not cached, nothing is done.
macro (basis_update_type_of_variable VAR TYPE)
basis_is_cached (_CACHED ${VAR})
if (_CACHED)
set_property (CACHE ${VAR} PROPERTY TYPE ${TYPE})
endif ()
unset (_CACHED)
endmacro ()
# ----------------------------------------------------------------------------