-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonTools.cmake
3823 lines (3704 loc) · 160 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-2016 Andreas Schuh
# All rights reserved.
#
# See COPYING file for license information or visit
# https://cmake-basis.github.io/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 common uppercase <PKG>_* variables to case-preserving <Pkg>_*
string (TOUPPER "${ARGV0}" _FP_ARGV0_U)
foreach (_FP_VAR IN ITEMS FOUND DIR USE_FILE
VERSION VERSION_STRING
VERSION_MAJOR VERSION_MINOR VERSION_SUBMINOR VERSION_PATCH
MAJOR_VERSION MINOR_VERSION SUBMINOR_VERSION PATCH_VERSION
INCLUDE_DIR INCLUDE_DIRS INCLUDE_PATH
LIBRARY_DIR LIBRARY_DIRS LIBRARY_PATH
EXECUTABLE COMPILER CONVERTER)
if (NOT ${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(s), 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
#
# VTK-7|6{}
#
# @param [in] DEP Dependency specification, i.e., "<Pkg>[-<version>[|...]][{<Component1>[,...]}]".
# @param [out] PKG Package name.
# @param [out] VER Package version(s).
# @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]+(\\.[0-9]+)?(\\.[0-9]+)?)*)$")
string (REPLACE "|" ";" CMAKE_MATCH_2 "${CMAKE_MATCH_2}")
set (${PKG} "${CMAKE_MATCH_1}" PARENT_SCOPE)
set (${VER} "${CMAKE_MATCH_2}" PARENT_SCOPE)
else ()
set (${PKG} "${DEP}" PARENT_SCOPE)
set (${VER} "" PARENT_SCOPE)
endif ()
set (${CMP} "${CMPS}" PARENT_SCOPE)
endfunction ()
## @brief Get installation prefix given path of <PKG>Config directory
function (_basis_config_to_prefix_dir PKG PKG_DIR PREFIX)
if (PKG_DIR)
if (APPLE)
if (PKG_DIR MATCHES "^(.*)/([^/]+)\\.(framework|app)(/|$)")
set (${PREFIX} "${CMAKE_MATCH_1}/${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" PARENT_SCOPE)
return ()
endif ()
endif ()
string (REGEX REPLACE "/+(cmake|CMake)/*$" "" prefix "${PKG_DIR}")
set (SUBDIR_RE "/+(lib[0-9]*(/[^/]+)?|share)(/+cmake)?")
if (UNIX AND prefix MATCHES "${SUBDIR_RE}(/+[^/]+|/*$)")
get_filename_component (subdir "${prefix}" NAME)
string (TOLOWER "${subdir}" subdir)
string (TOLOWER "${PKG}" pkg)
if ("^${subdir}$" STREQUAL "^${pkg}$")
get_filename_component (prefix "${prefix}" PATH)
endif ()
string(REGEX REPLACE "${SUBDIR_RE}/*$" "" prefix "${prefix}")
endif ()
else ()
set(prefix "NOTFOUND")
endif ()
set(${PREFIX} "${prefix}" PARENT_SCOPE)
endfunction ()
# ----------------------------------------------------------------------------
## @brief Find external software package or other project module.
#
# This macro replaces CMake's
# <a href="https://cmake.org/cmake/help/v2.8.12/cmake.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>WITH_<PACKAGE></tt> option
# is added by this macro which is by default @c OFF. This option can be set to
# @c ON by the user in order to require the discovery of the package.
# When this option is OFF, the (non-cached) <tt>USE_<PACKAGE></tt> variable
# can be used by project developers to still request the discovery of the
# optional package, but no error is raised when the package is not found.
# This allows a project to use an optional dependency when an installation
# is found regardless of the <tt>WITH_<PACKAGE></tt> option. Note
# that when <tt>USE_<PACKAGE></tt> is defined, no <tt>WITH_<PACKAGE></tt>
# entry is added by this macro to the cache.
#
# @param [in] PACKAGE Name of software package or other project module.
# Optionally, the package name can include a version
# specification as suffix which is separated from the
# package name using a dash (-), i.e., <Package>[-major[.minor[.patch[.tweak]]]].
# Multiple alternative versions have to be separated by
# a pipe character "|", the logical OR.
# If a version specification is given, it is passed on as
# @c version argument to CMake's
# <a href="https://cmake.org/cmake/help/v2.8.12/cmake.html#command:find_package">
# find_package()</a> command. The discovery of multiple
# alternative versions is only supported for the CONFIG
# mode of the find_package command.
# @param [in] ARGN Additional arguments for
# <a href="https://cmake.org/cmake/help/v2.8.12/cmake.html#command:find_package">
# find_package()</a>.
#
# @retval <PACKAGE>_FOUND Whether the given package was found.
# @retval <PACKAGE>_COMPONENTS_FOUND Names of found components.
# Optional components are only included when
# "<PACKAGE>_<COMPONENT>_FOUND" is set to @c TRUE
# for each found component by the find_package call,
# i.e., either the "Find<PACKAGE>" module or the
# "<PACKAGE>Config" file.
#
# @sa https://cmake.org/cmake/help/v2.8.12/cmake.html#command:find_package
#
# @ingroup CMakeAPI
macro (basis_find_package PACKAGE)
# Note that this MUST be a macro such that non-cached variables
# set by find_package are set within the callers scope
# ------------------------------------------------------------------------
# parse arguments
set (_BFP_OPTIONS
QUIET
REQUIRED
MODULE
NO_MODULE
CONFIG
NO_NOTFOUND_ERROR
)
set (_BFP_MULTI_ARGS
COMPONENTS
OPTIONAL_COMPONENTS
)
cmake_parse_arguments (
_BFP_ARGN
"${_BFP_OPTIONS}"
""
"${_BFP_MULTI_ARGS}"
${ARGN}
)
# ------------------------------------------------------------------------
# tokenize dependency specification
basis_tokenize_dependency ("${PACKAGE}" PKG _BFP_VERSIONS _BFP_COMPONENTS)
list (GET _BFP_ARGN_UNPARSED_ARGUMENTS 0 _BFP_VERSION)
if (_BFP_VERSION MATCHES "^[0-9]+(\\.[0-9]+)*(\\|[0-9]+(\\.[0-9]+)*)*$")
if (_BFP_VERSIONS)
message (FATAL_ERROR "Cannot use both version specification as part of "
"package name and explicit version argument.")
endif ()
list (REMOVE_AT _BFP_ARGN_UNPARSED_ARGUMENTS 0)
string(REPLACE "|" ";" _BFP_VERSIONS ${_BFP_VERSION})
endif ()
list (LENGTH _BFP_VERSIONS _BFP_VERSIONS_COUNT)
if (_BFP_ARGN_MODULE AND _BFP_VERSIONS GREATER 1)
message (FATAL_ERROR "Cannot look for multiple alternative package versions"
" in MODULE mode. The CONFIG|NO_MODULE mode of find_package"
" is used in this case. When MODULE mode is required"
" by package ${PKG}, only one version can be specified.")
endif ()
string (TOLOWER "${PKG}" PKG_L)
string (TOUPPER "${PKG}" PKG_U)
# ------------------------------------------------------------------------
# set <PKG>_FIND_REQUIRED_<CMP>
foreach (_BFP_CMP IN LISTS _BFP_COMPONENTS)
set (${PKG}_FIND_REQUIRED_${_BFP_CMP} ${_BFP_ARGN_REQUIRED})
endforeach ()
foreach (_BFP_CMP IN LISTS _BFP_ARGN_COMPONENTS)
set (${PKG}_FIND_REQUIRED_${_BFP_CMP} TRUE)
endforeach ()
foreach (_BFP_CMP IN LISTS _BFP_ARGN_OPTIONAL_COMPONENTS)
set (${PKG}_FIND_REQUIRED_${_BFP_CMP} FALSE)
endforeach ()
list (APPEND _BFP_ARGN_COMPONENTS ${_BFP_COMPONENTS})
if (_BFP_ARGN_COMPONENTS)
list (REMOVE_DUPLICATES _BFP_ARGN_COMPONENTS)
endif ()
# ------------------------------------------------------------------------
# prefix of package variable names set by Find<Pkg> or <Pkg>Config
if (PKG MATCHES "^((P|J)ython)Interp$")
string (TOUPPER "${CMAKE_MATCH_1}" _BFP_NS)
else ()
set (_BFP_NS "${PKG}")
endif ()
# ------------------------------------------------------------------------
# some debugging output
if (BASIS_DEBUG)
set (_BFP_ARGS)
if (_BFP_ARGN_REQUIRED)
list (APPEND _BFP_ARGS REQUIRED)
endif ()
if (_BFP_ARGN_QUIET)
list (APPEND _BFP_ARGS QUIET)
endif ()
if (_BFP_ARGN_MODULE)
list (APPEND _BFP_ARGS MODULE)
endif ()
if (_BFP_ARGN_NO_MODULE OR _BFP_ARGN_CONFIG)
list (APPEND _BFP_ARGS CONFIG)
endif ()
list (APPEND _BFP_ARGS ${_BFP_ARGN_UNPARSED_ARGUMENTS})
set (_BFP_INFO "** basis_find_package()")
set (_BFP_INFO "${_BFP_INFO}\n** Package: ${PKG}")
if (_BFP_VERSIONS)
set (_BFP_INFO "${_BFP_INFO}\n** Versions: ${_BFP_VERSIONS}")
endif ()
set (_BFP_INFO "${_BFP_INFO}\n** Arguments: [${_BFP_ARGS}]")
if (_BFP_ARGN_COMPONENTS OR _BFP_ARGN_OPTIONAL_COMPONENTS)
set (_BFP_INFO "${_BFP_INFO}\n** Components: ")
if (_BFP_ARGN_COMPONENTS AND _BFP_ARGN_OPTIONAL_COMPONENTS)
set (_BFP_INFO "${_BFP_INFO}[${_BFP_ARGN_COMPONENTS}] and [${_BFP_ARGN_OPTIONAL_COMPONENTS}] (optional)")
elseif (_BFP_ARGN_COMPONENTS)
set (_BFP_INFO "${_BFP_INFO}[${_BFP_ARGN_COMPONENTS}]")
else ()
set (_BFP_INFO "${_BFP_INFO}[${_BFP_ARGN_OPTIONAL_COMPONENTS}] (optional)")
endif ()
endif ()
message ("${_BFP_INFO}")
unset (_BFP_INFO)
unset (_BFP_ARGS)
endif ()
# ------------------------------------------------------------------------
# find other modules of same project
set (_BFP_IS_PROJECT FALSE)
set (_BFP_IS_MODULE FALSE)
if (PROJECT_IS_MODULE)
# allow modules to specify top-level project as dependency,
# respectively, other modules as components of top-level project
if ("^${PKG}$" STREQUAL "^${TOPLEVEL_PROJECT_NAME}$")
if (_BFP_ARGN_COMPONENTS OR _BFP_ARGN_OPTIONAL_COMPONENTS)
if (BASIS_DEBUG)
message ("** This is the top-level project. Components must be other modules of this project.")
endif ()
foreach (_BFP_CMP IN LISTS _BFP_ARGN_COMPONENTS _BFP_ARGN_OPTIONAL_COMPONENTS)
list (FIND PROJECT_MODULES "${_BFP_CMP}" _BFP_CMPIDX)
if (_BFP_CMPIDX EQUAL -1)
message (FATAL_ERROR "Module ${PROJECT_NAME} has module ${_BFP_CMP} of top-level project ${PKG}"
" as dependency, but no such module exists.")
endif ()
list (FIND PROJECT_MODULES_ENABLED "${_BFP_CMP}" _BFP_CMPIDX)
if (_BFP_CMPIDX EQUAL -1)
if (BASIS_DEBUG)
message ("** Identified it as disabled module of this project.")
endif ()
if (${PKG}_FIND_REQUIRED_${_BFP_CMP})
if (_BFP_ARGN_REQUIRED)
message (FATAL_ERROR
"Module ${PROJECT_NAME} requires module ${_BFP_CMP} of top-level project ${PKG},"
" but module ${_BFP_CMP} is not enabled."
)
elseif (NOT _BFP_ARGN_QUIET)
message (STATUS
"Module ${PROJECT_NAME} optionally uses module ${_BFP_CMP} of top-level project ${PKG},"
" but module ${_BFP_CMP} is not enabled."
)
endif ()
endif ()
set (${PKG}_${_BFP_CMP}_FOUND FALSE)
else ()
if (BASIS_DEBUG)
message ("** Identified it as enabled module of this project.")
endif ()
include ("${BINARY_LIBCONF_DIR}/${TOPLEVEL_PROJECT_PACKAGE_CONFIG_PREFIX}${_BFP_CMP}Config.cmake")
set (${PKG}_${_BFP_CMP}_FOUND TRUE)
endif ()
endforeach ()
else ()
if (BASIS_DEBUG)
message ("** This is the top-level project.")
endif ()
endif ()
set (${PKG}_FOUND TRUE)
set (_BFP_IS_PROJECT TRUE)
# look for other module of top-level project
else ()
list (FIND PROJECT_MODULES "${PKG}" _BFP_PKGIDX)
if (NOT _BFP_PKGIDX EQUAL -1)
set (_BFP_IS_MODULE TRUE)
list (FIND PROJECT_MODULES_ENABLED "${PKG}" _BFP_PKGIDX)
if (_BFP_PKGIDX EQUAL -1)
if (BASIS_DEBUG)
message ("** Identified it as disable module of this project.")
endif ()
if (_BFP_ARGN_REQUIRED)
message (FATAL_ERROR
"Module ${PROJECT_NAME} requires module ${_BFP_CMP} of top-level project ${PKG},"
" but module ${_BFP_CMP} is not enabled."
)
elseif (NOT _BFP_ARGN_QUIET)
message (STATUS
"Module ${PROJECT_NAME} optionally uses module ${_BFP_CMP} of top-level project ${PKG},"
" but module ${_BFP_CMP} is not enabled."
)
endif ()
set (${PKG}_FOUND FALSE)
else ()
if (BASIS_DEBUG)
message ("** Identified it as enabled module of this project.")
endif ()
include ("${BINARY_LIBCONF_DIR}/${TOPLEVEL_PROJECT_PACKAGE_CONFIG_PREFIX}${PKG}Config.cmake")
set (${PKG}_FOUND TRUE)
endif ()
endif ()
endif ()
# --------------------------------------------------------------------------
# find bundled packages
else ()
list (FIND BUNDLE_PROJECTS "${PKG}" _BFP_PKGIDX)
if (NOT _BFP_PKGIDX EQUAL -1)
if (EXISTS "${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIG_DIR}/${PKG}Config.cmake")
set (_BFP_CONFIG_FILE "${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIG_DIR}/${PKG}Config.cmake")
else ()
if (EXISTS "${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIG_DIR}/${PKG_L}-config.cmake")
set (_BFP_CONFIG_FILE "${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIG_DIR}/${PKG_L}-config.cmake")
else ()
set (_BFP_CONFIG_FILE)
endif ()
endif ()
if (_BFP_CONFIG_FILE)
if (BASIS_DEBUG)
message ("** Identified it as other package of this bundle.")
endif ()
get_filename_component (_BFP_CONFIG_DIR "${_BFP_CONFIG_FILE}" PATH)
_basis_config_to_prefix_dir (${PKG} "${_BFP_CONFIG_DIR}" _BFP_PREFIX)
basis_set_or_update_value (DEPENDS_${PKG}_DIR "${_BFP_PREFIX}" PATH)
include ("${_BFP_CONFIG_FILE}")
set (${PKG}_FOUND TRUE)
unset (_BFP_CONFIG_DIR)
endif ()
unset (_BFP_CONFIG_FILE)
endif ()
endif ()
# --------------------------------------------------------------------------
# otherwise, look for external package
if (NOT _BFP_IS_PROJECT AND NOT _BFP_IS_MODULE)
# ------------------------------------------------------------------------
# provide option which allows users to request use of optional packages
#
# - WITH_<PKG> == ON: optional dependency is required
# - WITH_<PKG> == OFF:
# - USE_<PKG> == ON: dependency is looked for and used if available
# - USE_<PKG> == OFF: dependency is ignored
#
# The default of the WITH_<PKG> option is OFF unless WITH_<PKG>_DEFAULT
# is set in the Depends.cmake, the CMake command-line using the -D switch
# or the root CMakeLists.txt file before the basis_project_begin call.
# The (uncached) WITH_${PKG}_DEFAULT variable can be used by a project
# to require optional dependencies by default, e.g., to enable optional
# program features that depend on these external packages.
if (NOT _BFP_ARGN_REQUIRED)
if (NOT DEFINED WITH_${PKG}_DEFAULT)
set (WITH_${PKG}_DEFAULT OFF)
endif ()
if (DEFINED USE_${PKG} AND NOT DEFINED WITH_${PKG})
set (WITH_${PKG} ${WITH_${PKG}_DEFAULT})
else ()
option (WITH_${PKG} "Build with optional ${PKG} dependency" ${WITH_${PKG}_DEFAULT})
endif ()
endif ()
# look for external package only if required, built with optional dependency
# enabled by user (cf. WITH_<PKG> option above) or deprecated -DUSE_<PKG>=ON
if (_BFP_ARGN_REQUIRED OR WITH_${PKG} OR USE_${PKG})
# ----------------------------------------------------------------------
# Use more user friendly hybrid DEPENDS_<PKG>_DIR cache variable which
# allows grouping of DEPENDS paths cache entry, but still consider more
# common variables named <PKG>_DIR, <PKG>_ROOT, <PKG>ROOT, or <PKG>_ROOT_DIR
# set in the user shell environment or on the CMake command-line using -D.
set (
DEPENDS_${PKG}_DIR "${DEPENDS_${PKG}_DIR}" CACHE PATH
"Top-level installation directory of ${PKG} or directory containing ${PKG}Config.cmake file."
)
if (DEPENDS_${PKG}_DIR)
file (TO_CMAKE_PATH "${DEPENDS_${PKG}_DIR}" _BFP_PREFIX)
if (NOT "^${DEPENDS_${PKG}_DIR}$" STREQUAL "^${_BFP_PREFIX}$")
basis_update_value (DEPENDS_${PKG}_DIR "${_BFP_PREFIX}")
endif ()
endif ()
# Names of considered alternative find search path variables excl. <PKG>_DIR
set (_BFP_PKG_DIR_VARS
${PKG}_ROOT ${PKG_U}_ROOT
${PKG}ROOT ${PKG_U}ROOT
${PKG}_ROOT_DIR ${PKG_U}_ROOT_DIR
)
list (REMOVE_DUPLICATES _BFP_PKG_DIR_VARS)
# Override DEPENDS_<PKG>_DIR by alternative search path variable value if these
# were specified on the command line using the -D option. Note that these variables
# cannot be set in the CMake GUI because their type is changed here to INTERNAL.
# This has two reasons, firstly to not have duplicate variables with different
# names for the same purpose, and secondly to be able to recognize when their
# value is changed using the -D command line option of the cmake command.
#
# Order of precedence:
# 1. <PKG>_DIR
# 2. <PKG>_ROOT... CMake variable
# 3. <PKG>_ROOT... environment variable
foreach (_BFP_VAR IN LISTS _BFP_PKG_DIR_VARS)
file (TO_CMAKE_PATH "${${_BFP_VAR}}" _BFP_PREFIX) # CMake (cache) variable
if (_BFP_PREFIX)
# first configure run or new value specified using -D option of cmake command
if (NOT DEFINED _DEPENDS_${PKG}_DIR OR (DEFINED _DEPENDS_${PKG}_DIR AND NOT "^${_BFP_PREFIX}$" STREQUAL "^${_DEPENDS_${PKG}_DIR}$"))
basis_update_value (DEPENDS_${PKG}_DIR "${_BFP_PREFIX}")
break ()
endif ()
endif ()
endforeach ()
if (${PKG}_DIR) # find_package CONFIG mode variable
# first configure run or new value specified using -D option of cmake command
if (NOT DEFINED _${PKG}_DIR OR (DEFINED _${PKG}_DIR AND NOT "^${${PKG}_DIR}$" STREQUAL "^${_${PKG}_DIR}$"))
file (TO_CMAKE_PATH "${${PKG}_DIR}" _BFP_PREFIX)
_basis_config_to_prefix_dir(${PKG} "${_BFP_PREFIX}" _BFP_PREFIX)
basis_update_value (DEPENDS_${PKG}_DIR "${_BFP_PREFIX}")
endif ()
endif ()
# mark alternatives as internal cache entries
foreach (_BFP_VAR IN LISTS _BFP_PKG_DIR_VARS)
basis_is_cached (_BFP_CACHED ${_BFP_VAR})
if (_BFP_CACHED)
basis_update_type_of_variable (${_BFP_VAR} INTERNAL)
endif ()
endforeach ()
# if still not set, use common environment variables to set DEPENDS_<PKG>_DIR
if (NOT DEPENDS_${PKG}_DIR)
foreach (_BFP_VAR IN LISTS _BFP_PKG_DIR_VARS)
file (TO_CMAKE_PATH "$ENV{${_BFP_VAR}}" _BFP_PREFIX) # shell environment variable
if (_BFP_PREFIX)
basis_update_value (DEPENDS_${PKG}_DIR "${_BFP_PREFIX}")
break()
endif ()
endforeach ()
endif ()
# circumvent issue with find_package interpreting <PKG>_DIR relative
# to the current binary directory instead of the top-level directory
if (DEPENDS_${PKG}_DIR AND NOT IS_ABSOLUTE "${DEPENDS_${PKG}_DIR}")
get_filename_component (_BFP_PREFIX "${CMAKE_BINARY_DIR}/${DEPENDS_${PKG}_DIR}" ABSOLUTE)
basis_update_value (DEPENDS_${PKG}_DIR "${_BFP_PREFIX}")
endif ()
# --------------------------------------------------------------------
# reset _?<PKG>[_-]* variables when DEPENDS_<PKG>_DIR has changed
if (_DEPENDS_${PKG}_DIR AND DEPENDS_${PKG}_DIR)
if (NOT "^${_DEPENDS_${PKG}_DIR}$" STREQUAL "^${DEPENDS_${PKG}_DIR}$")
get_cmake_property (_BFP_VARS VARIABLES)
basis_sanitize_for_regex (PKG_RE "${PKG}")
basis_sanitize_for_regex (PKG_U_RE "${PKG_U}")
basis_sanitize_for_regex (_BFP_NS_RE "${_BFP_NS}")
foreach (_BFP_VAR IN LISTS _BFP_VARS)
if (_BFP_VAR MATCHES "^_?(${_BFP_NS_RE}|${PKG_RE}|${PKG_U_RE})[_-]")
basis_is_cached (_BFP_CACHED ${_BFP_VAR})
if (_BFP_CACHED)
set_property (CACHE ${_BFP_VAR} PROPERTY VALUE "${_BFP_VAR}-NOTFOUND")
set_property (CACHE ${_BFP_VAR} PROPERTY TYPE INTERNAL)
elseif (NOT _BFP_VAR MATCHES "^${PKG_RE}_FIND_")
unset (${_BFP_VAR})
endif ()
endif ()
endforeach ()
unset (PKG_RE)
unset (PKG_U_RE)
unset (_BFP_NS_RE)
endif ()
endif ()
# ----------------------------------------------------------------------
# determine if additional components of found package should be discovered
if (${PKG}_FOUND)
set (_BFP_NO_FIND_PACKAGE TRUE)
if (${PKG}_COMPONENTS_FOUND)
# previously called with COMPONENTS
set (_BFP_FIND_COMPONENTS)
set (_BFP_FIND_OPTIONAL_COMPONENTS)
if (_BFP_ARGN_COMPONENTS OR _BFP_ARGN_OPTIONAL_COMPONENTS)
foreach (_BFP_CMP IN LISTS _BFP_ARGN_COMPONENTS _BFP_ARGN_OPTIONAL_COMPONENTS)
list (FIND ${PKG}_COMPONENTS_FOUND "${_BFP_CMP}" _BFP_CMPIDX)
if (_BFP_CMPIDX EQUAL -1)
if (${PKG}_FIND_REQUIRED_${_BFP_CMP})
list (APPEND _BFP_FIND_COMPONENTS "${_BFP_CMP}")
elseif (NOT DEFINED ${PKG}_${_BFP_CMP}_FOUND)
list (APPEND _BFP_FIND_OPTIONAL_COMPONENTS "${_BFP_CMP}")
endif ()
endif ()
endforeach ()
else ()
# Not sure if "default" components were found when find_package
# was previously invoked with the COMPONENTS argument, but
# now without it. This depends on the Find<PKG> module.
set (_BFP_NO_FIND_PACKAGE FALSE)
endif ()
else ()
# previously called without COMPONENTS
set (_BFP_FIND_COMPONENTS ${_BFP_ARGN_COMPONENTS})
set (_BFP_FIND_OPTIONAL_COMPONENTS ${_BFP_ARGN_OPTIONAL_COMPONENTS})
endif ()
if (_BFP_FIND_COMPONENTS OR _BFP_FIND_OPTIONAL_COMPONENTS)
set (_BFP_NO_FIND_PACKAGE FALSE)
endif ()
else ()
set (_BFP_NO_FIND_PACKAGE FALSE)
set (_BFP_FIND_COMPONENTS ${_BFP_ARGN_COMPONENTS})
set (_BFP_FIND_OPTIONAL_COMPONENTS ${_BFP_ARGN_OPTIONAL_COMPONENTS})
endif ()
# ----------------------------------------------------------------------
# look for external package if not found or additional components needed
if (NOT ${PKG}_FOUND AND NOT _BFP_ARGN_REQUIRED AND NOT WITH_${PKG} AND ((DEFINED USE_${PKG} AND NOT USE_${PKG}) OR NOT DEFINED USE_${PKG}))
# skip if package is optional and user did not ask us to look for it
# when package was found before, still perform steps below to ensure
# that everything is set properly even when find_package was called
mark_as_advanced (FORCE DEPENDS_${PKG}_DIR)
else ()
# status message with information what we are looking for
if (_BFP_ARGN_QUIET)
set (_BFP_STATUS)
else ()
set (_BFP_STATUS "Looking for ${PKG}")
if (_BFP_VERSIONS)
string (REPLACE ";" " or " _BFP_VERSION_STRING "${_BFP_VERSIONS}")
set (_BFP_STATUS "${_BFP_STATUS} ${_BFP_VERSION_STRING}")
unset (_BFP_VERSION_STRING)
endif ()
if (_BFP_FIND_COMPONENTS)
set (_BFP_STATUS "${_BFP_STATUS} [${_BFP_FIND_COMPONENTS}]")
endif ()
if (_BFP_FIND_OPTIONAL_COMPONENTS)
set (_BFP_STATUS "${_BFP_STATUS}, optional components [${_BFP_FIND_OPTIONAL_COMPONENTS}]")
endif ()
if (NOT _BFP_ARGN_REQUIRED)
set (_BFP_STATUS "${_BFP_STATUS} (optional)")
endif ()
set (_BFP_STATUS "${_BFP_STATUS}...")
message (STATUS "${_BFP_STATUS}")
endif ()
# make copy of previous <Pkg>_VERSION_STRING if already set which is used
# as "last resort" when variable not set by the following find_package
set (_BFP_VERSION_STRING "${${_BFP_NS}_VERSION_STRING}")
unset (${_BFP_NS}_VERSION_STRING)
# set internal <Pkg>_DIR used by find_package to locate <Pkg>Config
set (${PKG}_DIR "${DEPENDS_${PKG}_DIR}" CACHE INTERNAL "Directory containing ${PKG}Config.cmake file." FORCE)
# call find_package if not all components found yet
set (_BFP_FOUND "${${PKG}_FOUND}") # used to decide what the intersection of
# multiple find invocations for the same
# package with different components will be
# for the setting of <PKG>_FOUND
if (NOT _BFP_NO_FIND_PACKAGE)
# reset <PKG>_FOUND
set (${PKG}_FOUND FALSE)
set (${PKG_U}_FOUND FALSE)
# make copy of find_* search path variables
foreach (_BFP_VAR IN ITEMS CMAKE_PREFIX_PATH CMAKE_PROGRAM_PATH)
set (_BFP_${_BFP_VAR} "${${_BFP_VAR}}")
endforeach ()
# insert DEPENDS_<PKG>_DIR into find_* search paths
if (IS_DIRECTORY "${DEPENDS_${PKG}_DIR}")
# add directory to CMAKE_PROGRAM_PATH when the name
# of the last subdirectory is "bin", "Bin", "sbin",
# or "texbin" (i.e., MacTeX's "/Library/TeX/texbin" path)
if (DEPENDS_${PKG}_DIR MATCHES "/([bB]in|sbin|texbin)/+$")
list (INSERT CMAKE_PROGRAM_PATH 0 "${DEPENDS_${PKG}_DIR}")
list (REMOVE_DUPLICATES CMAKE_PROGRAM_PATH)
# add directory to CMAKE_PREFIX_PATH otherwise as users
# tend to specify the installation prefix instead of the
# actual directory containing the package configuration file
else ()
list (INSERT CMAKE_PREFIX_PATH 0 "${DEPENDS_${PKG}_DIR}")
list (REMOVE_DUPLICATES CMAKE_PREFIX_PATH)
endif ()
endif ()
# now look for the package using find_package
set (_BFP_FIND_PACKAGE_ARGS ${_BFP_ARGN_UNPARSED_ARGUMENTS})
if (_BFP_ARGN_QUIET OR "^${PKG}$" STREQUAL "^Boost$")
list (APPEND _BFP_FIND_PACKAGE_ARGS "QUIET")
endif ()
if (_BFP_FIND_COMPONENTS OR _BFP_FIND_OPTIONAL_COMPONENTS)
if (${PKG}_COMPONENTS_FOUND OR _BFP_FIND_COMPONENTS)
list (APPEND _BFP_FIND_PACKAGE_ARGS "COMPONENTS" ${${PKG}_COMPONENTS_FOUND} ${_BFP_FIND_COMPONENTS})
endif ()
if (_BFP_FIND_OPTIONAL_COMPONENTS)
list (APPEND _BFP_FIND_PACKAGE_ARGS "OPTIONAL_COMPONENTS" ${_BFP_FIND_OPTIONAL_COMPONENTS})
endif ()
endif ()
if (${_BFP_VERSIONS_COUNT} GREATER 1)
list (APPEND _BFP_FIND_PACKAGE_ARGS CONFIG)
if (DEPENDS_${PKG}_DIR)
foreach (_BFP_VERSION ${_BFP_VERSIONS})
find_package (${PKG} ${_BFP_VERSION} ${_BFP_FIND_PACKAGE_ARGS} QUIET
PATHS ${DEPENDS_${PKG}_DIR} NO_DEFAULT_PATH
)
if (${PKG}_FOUND)
break ()
endif ()
endforeach ()
endif ()
if (NOT ${PKG}_FOUND)
foreach (_BFP_VERSION ${_BFP_VERSIONS})
find_package (${PKG} ${_BFP_VERSION} ${_BFP_FIND_PACKAGE_ARGS} QUIET)
if (${PKG}_FOUND)
break ()
endif ()
endforeach ()
endif ()
else ()
if (_BFP_ARGN_MODULE)
list (APPEND _BFP_FIND_PACKAGE_ARGS MODULE)
endif ()
if (_BFP_ARGN_NO_MODULE OR _BFP_ARGN_CONFIG)
list (APPEND _BFP_FIND_PACKAGE_ARGS CONFIG)
endif ()
find_package (${PKG} ${_BFP_VERSIONS} ${_BFP_FIND_PACKAGE_ARGS} QUIET)
endif ()
unset (_BFP_FIND_PACKAGE_ARGS)
# restore find_* search path variables
foreach (_BFP_VAR IN ITEMS CMAKE_PREFIX_PATH CMAKE_PROGRAM_PATH)
set (${_BFP_VAR} "${_BFP_${_BFP_VAR}}")
unset (_BFP_${_BFP_VAR})
endforeach ()
# ensure that <PKG>_DIR is still an internal cache entry
basis_update_type_of_variable (${PKG}_DIR INTERNAL)
# force reinclusion of package use file
if (${PKG}_FOUND)
if (${PKG}_USE_FILE_INCLUDED)
set (${PKG}_USE_FILE_INCLUDED 0)
endif ()
if (BASIS_USE_${PKG}_INCLUDED)
set (BASIS_USE_${PKG}_INCLUDED FALSE)
endif ()
endif ()
endif () # NOT _BFP_NO_FIND_PACKAGE
# set common <Pkg>_VERSION_STRING variable if possible and not set
if (NOT DEFINED ${_BFP_NS}_VERSION_STRING)
if (DEFINED ${_BFP_NS}_VERSION_MAJOR)
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_VERSION_MAJOR})
if (DEFINED ${_BFP_NS}_VERSION_MINOR)
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_VERSION_STRING}.${${_BFP_NS}_VERSION_MINOR})
if (DEFINED ${_BFP_NS}_VERSION_PATCH)
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_VERSION_STRING}.${${_BFP_NS}_VERSION_PATCH})
elseif (DEFINED ${_BFP_NS}_SUBMINOR_VERSION) # e.g., FindBoost
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_VERSION_STRING}.${${_BFP_NS}_SUBMINOR_VERSION})
endif ()
endif ()
elseif (DEFINED ${_BFP_NS}_MAJOR_VERSION)
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_MAJOR_VERSION})
if (DEFINED ${_BFP_NS}_MINOR_VERSION)
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_VERSION_STRING}.${${_BFP_NS}_MINOR_VERSION})
if (DEFINED ${PKG}_PATCH_VERSION)
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_VERSION_STRING}.${${_BFP_NS}_PATCH_VERSION})
elseif (DEFINED ${_BFP_NS}_SUBMINOR_VERSION) # e.g., FindBoost
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_VERSION_STRING}.${${_BFP_NS}_SUBMINOR_VERSION})
endif ()
endif ()
elseif (DEFINED ${_BFP_NS}_VERSION)
set (${_BFP_NS}_VERSION_STRING ${${_BFP_NS}_VERSION})
else ()
set (${_BFP_NS}_VERSION_STRING "${_BFP_VERSION_STRING}")
endif ()
endif ()
unset (_BFP_VERSION_STRING)
# update DEPENDS_<PKG>_DIR from variables set by find_package
if (${PKG}_FOUND)
if (${PKG}_DIR AND IS_ABSOLUTE "${${PKG}_DIR}" AND
(EXISTS "${${PKG}_DIR}/${PKG}Config.cmake" OR
EXISTS "${${PKG}_DIR}/${PKG_L}-config.cmake"))
_basis_config_to_prefix_dir(${PKG} "${${PKG}_DIR}" _BFP_PREFIX)
basis_update_value (DEPENDS_${PKG}_DIR "${_BFP_PREFIX}")
elseif (NOT DEPENDS_${PKG}_DIR)
if (${_BFP_NS}_INCLUDE_DIR)
list (GET ${_BFP_NS}_INCLUDE_DIR 0 _BFP_PREFIX)
string (REGEX REPLACE "^(.*)/[iI]ncludes?(/.*)?$" "\\1" _BFP_PREFIX "${_BFP_PREFIX}")
elseif (${_BFP_NS}_LIBRARY_DIR)
list (GET ${_BFP_NS}_LIBRARY_DIR 0 _BFP_PREFIX)
string (REGEX REPLACE "^(.*)/[lL]ib(s|exec|64)?(/.*)?$" "\\1" _BFP_PREFIX "${_BFP_PREFIX}")
else ()
set (_BFP_VARS
${_BFP_NS}_EXECUTABLE # e.g., FindBASH
${_BFP_NS}_COMPILER # e.g., FindLATEX
${_BFP_NS}_CONVERTER # e.g., FindLATEX
)
foreach (_BFP_VAR IN LISTS _BFP_VARS)
if (${_BFP_VAR})
get_filename_component (_BFP_PREFIX "${${_BFP_VAR}}" PATH)
break ()
endif ()
endforeach ()
endif ()
basis_update_value (DEPENDS_${PKG}_DIR "${_BFP_PREFIX}")
endif ()
else ()
basis_update_value (DEPENDS_${PKG}_DIR "NOTFOUND")
endif ()
# make internal copy of DEPENDS_<PKG>_DIR used to detect change
set (_DEPENDS_${PKG}_DIR "${DEPENDS_${PKG}_DIR}" CACHE INTERNAL "(Previous) DEPENDS_${PKG}_DIR value." FORCE)
# make internal search path cache entries consistent with DEPENDS_<PKG>_DIR
foreach (_BFP_VAR IN LISTS _BFP_PKG_DIR_VARS)
basis_is_cached (_BFP_CACHED ${_BFP_VAR})
if (_BFP_CACHED)
set (${_BFP_VAR} "${DEPENDS_${PKG}_DIR}" CACHE INTERNAL "Installation prefix of ${PKG}." FORCE)
endif ()
endforeach ()
# make internal copy of <PKG>_DIR used to detect change via -D option
#
# Note: All other alternative variables such as <PKG>_ROOT are forced to
# be equal DEPENDS_<PKG>_DIR. Only <PKG>_DIR usually points to the
# <PKG>Config, while DEPENDS_<PKG>_DIR is the installation prefix.
set (_${PKG}_DIR "${${PKG}_DIR}" CACHE INTERNAL "(Previous) ${PKG}_DIR value." FORCE)
# status message with information about found package
if (_BFP_STATUS)
if (${PKG}_FOUND)
if (_BFP_NO_FIND_PACKAGE)
set (_BFP_STATUS "${_BFP_STATUS} - already found")
else ()
set (_BFP_STATUS "${_BFP_STATUS} - found")
endif ()
if ("^${PKG}$" STREQUAL "^Boost$")
set (_BFP_STATUS "${_BFP_STATUS} v${${_BFP_NS}_MAJOR_VERSION}.${${_BFP_NS}_MINOR_VERSION}.${${_BFP_NS}_SUBMINOR_VERSION}")
elseif (DEFINED ${_BFP_NS}_VERSION_STRING AND NOT ${_BFP_NS}_VERSION_STRING MATCHES "^(0(\\.0)?(\\.0)?)?$")
set (_BFP_STATUS "${_BFP_STATUS} v${${_BFP_NS}_VERSION_STRING}")
endif ()
if (BASIS_VERBOSE AND DEPENDS_${PKG}_DIR)
set (_BFP_STATUS "${_BFP_STATUS} at ${DEPENDS_${PKG}_DIR}")
endif ()
else ()
set (_BFP_STATUS "${_BFP_STATUS} - not found")
endif ()
message (STATUS "${_BFP_STATUS}")
endif ()
unset (_BFP_STATUS)
# raise error when a required package was not found
if (NOT ${PKG}_FOUND AND NOT _BFP_ARGN_NO_NOTFOUND_ERROR AND (_BFP_ARGN_REQUIRED OR WITH_${PKG}))
set (_BFP_ERROR)
if (PROJECT_IS_MODULE)
set (_BFP_ERROR "Module")
else ()
set (_BFP_ERROR "Project")
endif ()
set (_BFP_ERROR "${_BFP_ERROR} ${PROJECT_NAME}")
if (_BFP_ARGN_REQUIRED)
set (_BFP_ERROR "${_BFP_ERROR} requires ${PKG}")
else ()
set (_BFP_ERROR "${_BFP_ERROR} was requested to be build with ${PKG}")
endif ()
if (_BFP_VERSIONS)
set (_BFP_ERROR "${_BFP_ERROR} version ${_BFP_VERSIONS}")
endif ()
set (_BFP_ERROR "${_BFP_ERROR}. Please ensure that the package is installed in a"
" standard system location or set DEPENDS_${PKG}_DIR to the"
" installation prefix (i.e., root directory of the installation).")
if (NOT _BFP_ARGN_REQUIRED AND DEFINED WITH_${PKG})
set (_BFP_ERROR "${_BFP_ERROR} To disable features which require this optional dependency,"
" set the WITH_${PKG} option to OFF and try again.")
endif ()
if (DEFINED ${PKG}_DIR)
set (_BFP_ERROR "${_BFP_ERROR}\nThe DEPENDS_${PKG}_DIR variable can alternatively be set"
" to the directory containing a ${PKG}Config.cmake or ${PKG_L}-config.cmake"
" file. If no such file exists, contact either the developer of"
" this project or CMake BASIS to provide a Find${PKG}.cmake file.")
endif ()
basis_list_to_string(_BFP_ERROR ${_BFP_ERROR})
message (FATAL_ERROR "\n${_BFP_ERROR}\n")
endif ()
# update list of found components
if (${PKG}_FOUND)
if (_BFP_FIND_COMPONENTS)
list (APPEND ${PKG}_COMPONENTS_FOUND ${_BFP_FIND_COMPONENTS})
endif ()
foreach (_BFP_CMP IN LISTS _BFP_FIND_OPTIONAL_COMPONENTS)
if (${PKG}_${_BFP_CMP}_FOUND)
list (APPEND ${PKG}_COMPONENTS_FOUND ${_BFP_CMP})
endif ()
endforeach ()
if (${PKG}_COMPONENTS_FOUND)
list (REMOVE_DUPLICATES ${PKG}_COMPONENTS_FOUND)
endif ()
endif ()
# if previously this package or components of it where found and the
# re-discovery of the package or additional components is only optional,
# set <PKG>_FOUND to TRUE again
if (_BFP_FOUND AND NOT _BFP_ARGN_REQUIRED AND NOT WITH_${PKG})
set (${PKG}_FOUND TRUE)
endif ()
unset (_BFP_FOUND)
endif ()
endif ()
endif ()
# --------------------------------------------------------------------------
# unset locally used variables
foreach (_BFP_CMP IN LISTS _BFP_ARGN_COMPONENTS _BFP_ARGN_OPTIONAL_COMPONENTS)
unset (${PKG}_FIND_REQUIRED_${_BFP_CMP})
endforeach ()
foreach (_BFP_VAR IN LISTS _BFP_OPTIONS _BFP_MULTI_ARGS)
unset (_BFP_ARGN_${_BFP_VAR})
endforeach ()
unset (_BFP_COMPONENTS)
unset (_BFP_ARGN_UNPARSED_ARGUMENTS)
unset (_BFP_OPTIONS)
unset (_BFP_MULTI_ARGS)
unset (_BFP_IS_MODULE)
unset (_BFP_IS_PROJECT)
unset (_BFP_VERSION)
unset (_BFP_VERSIONS)
unset (_BFP_VERSIONS_COUNT)
unset (_BFP_PKGIDX)
unset (_BFP_CMPIDX)
unset (_BFP_CMP)
unset (_BFP_VAR)
unset (_BFP_VARS)
unset (_BFP_CACHED)
unset (_BFP_TYPE)
unset (_BFP_PREFIX)
unset (_BFP_PKG_DIR_VARS)
unset (_BFP_FIND_COMPONENTS)
unset (_BFP_FIND_OPTIONAL_COMPONENTS)
unset (_BFP_NO_FIND_PACKAGE)
unset (_BFP_NS)
unset (PKG)
unset (PKG_L)
unset (PKG_U)
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 (CMPS)
if (BASIS_DEBUG)
message ("** These are other modules of the top-level project.")
endif ()
foreach (CMP IN LISTS CMPS)
list (FIND PROJECT_MODULES "${CMP}" CMPIDX)
if (CMPIDX EQUAL -1)
message (FATAL_ERROR "Module ${PROJECT_NAME} has module ${CMP} of project ${TOPLEVEL_PROJECT_NAME}"
" as dependency, but no such module exists.")
endif ()
list (FIND PROJECT_MODULES_ENABLED "${CMP}" CMPIDX)
if (NOT CMPIDX EQUAL -1)
if (BASIS_DEBUG)
message ("** Include package use file of module ${CMP}.")
endif ()
include ("${${${CMP}_CONFIG_PREFIX}_USE_FILE}")
if (PROJECT_PACKAGE_NAME)
add_definitions(-DHAVE_${PROJECT_PACKAGE_NAME}_${CMP})
else ()
add_definitions(-DHAVE_${TOPLEVEL_PROJECT_NAME}_${CMP})
endif ()
endif ()
endforeach ()
unset (CMPIDX)
unset (CMP)
else ()
if (BASIS_DEBUG)
message ("** This is the top-level project.")
endif ()
endif ()
break () # instead of return()
# use other module of top-level project
else ()
list (FIND PROJECT_MODULES "${PKG}" PKGIDX)
if (NOT PKGIDX EQUAL -1 AND ${PKG}_FOUND)
if (BASIS_DEBUG)
message ("** Include package use file of other module.")
endif ()
include ("${${PKG}_USE_FILE}")
add_definitions(-DHAVE_${PKG})
unset (PKGIDX)
break () # instead of return()
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