CMakeLists.txt 22.4 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
cmake_minimum_required (VERSION 2.6)
2
project(FindPackageTest)
3

4
5
6
# Protect tests from running inside the default install prefix.
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/NotDefaultPrefix")

7
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
8
9
10
11
12

# Look for a package which uses FindPackageHandleStandardArgs.cmake with the
# new (as of cmake 2.8.3) syntax. This works only if CMP0017 is set to NEW,
# because otherwise FindPackageHandleStandardArgs.cmake from the current
# directory is included (via CMAKE_MODULE_PATH).
13
14
cmake_policy(SET CMP0017 NEW)
find_package(ZLIB QUIET)
15

16
# Look for a package that has a find module and may be found.
17
find_package(OpenGL QUIET)
18
19

# Look for a package that has no find module and will not be found.
20
find_package(NotAPackage QUIET)
21
22

# Look for a package that has an advanced find module.
23
find_package(VTK QUIET)
24

25
add_executable(FindPackageTest FindPackageTest.cxx)
26

27
# test behaviour of cmFindBase wrt. the CMAKE_PREFIX_PATH variable
28
29
# foo.h should be found in ${CMAKE_CURRENT_SOURCE_DIR}/include:

30
31
set(CMAKE_PREFIX_PATH /blub /blah "${CMAKE_CURRENT_SOURCE_DIR}")
find_path(FOO_DIR foo.h)
32

33
34
if(NOT FOO_DIR)
  message(FATAL_ERROR "Did not find foo.h which is in ${CMAKE_CURRENT_SOURCE_DIR}/include
35
  CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}")
36
endif()
37

38
39
40
41
find_package(VersionTestA 1)
find_package(VersionTestB 1.2)
find_package(VersionTestC 1.2.3)
find_package(VersionTestD 1.2.3.4)
42

43

44
45
46
47
find_package(LotsOfComponents COMPONENTS AComp OPTIONAL_COMPONENTS BComp CComp)
if(NOT LOTSOFCOMPONENTS_FOUND)
  message(SEND_ERROR "LotsOfComponents not found !")
endif()
48

49
50
find_package(SomePackage)
if(NOT SomePackage_FOUND)
51
52
53
54
  message(SEND_ERROR "SomePackage not found !")
endif()
if(NOT SOMEPACKAGE_FOUND)
  message(SEND_ERROR "SomePackage compatibility name SOMEPACKAGE_FOUND not set!")
55
56
57
endif()

find_package(UpperCasePackage)
58
59
60
if(NOT UpperCasePackage_FOUND)
  message(SEND_ERROR "UpperCasePackage not found!")
endif()
61
if(NOT UPPERCASEPACKAGE_FOUND)
62
  message(SEND_ERROR "SomePackage compatibility name SOMEPACKAGE_FOUND not set!")
63
64
endif()

65
66
#-----------------------------------------------------------------------------
# Test system package registry if possible.
67
68
set(CMakeTestSystemPackage "")
if(WIN32 AND NOT CYGWIN)
69
  # Try writing a value to the system package registry.
70
71
72
73
74
  set(_data "${FindPackageTest_SOURCE_DIR}/SystemPackage")
  set(_key "HKLM\\Software\\Kitware\\CMake\\Packages\\CMakeTestSystemPackage")
  set(_file "${FindPackageTest_BINARY_DIR}/CMakeTestSystemPackage.data")
  file(WRITE ${_file} "${_data}\n")
  execute_process(
75
76
77
78
    COMMAND ${CMAKE_COMMAND} -E md5sum ${_file}
    OUTPUT_VARIABLE _output ERROR_VARIABLE _error RESULT_VARIABLE _failed
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )
79
80
81
  string(REGEX REPLACE " .*" "" _value "${_output}")
  if(NOT _failed AND _value)
    execute_process(
82
83
84
      COMMAND reg add "${_key}" /v "${_value}" /t REG_SZ /d "${_data}" /f
      OUTPUT_VARIABLE _output ERROR_VARIABLE _output RESULT_VARIABLE _failed
      )
85
  endif()
86
87
  # If the above worked, add the rest of the test and a rule to
  # cleanup the value.
88
89
90
91
92
93
94
95
  if(NOT _failed)
    message(STATUS "HKLM is writable: enabling CMakeTestSystemPackage")
    set(CMakeTestSystemPackage_CLEANUP reg delete "${_key}" /v "${_value}" /f)
    set(CMakeTestSystemPackage CMakeTestSystemPackage)
  else()
    message(STATUS "HKLM is readonly: disabling CMakeTestSystemPackage")
  endif()
endif()
96

97
98
#-----------------------------------------------------------------------------

99
#set(CMAKE_FIND_DEBUG_MODE 1)
100
101

# For purposes of the test wipe out previous find results.
102
set(PACKAGES
103
  foo Foo Bar Blub TFramework Tframework TApp Tapp Special
104
  VersionedA VersionedB VersionedC VersionedD VersionedE
105
  VersionedF VersionedG VersionedH
106
  WrongA WrongB WrongC WrongD
107
  wibbleA wibbleB
108
  RecursiveA RecursiveB RecursiveC
109
  ArchA ArchB ArchC ArchD
110
  EnvA EnvB
111
  SetFoundTRUE SetFoundFALSE
112
  ${CMakeTestSystemPackage}
113
  )
114
115
foreach(p ${PACKAGES})
  set(${p}_DIR "" CACHE FILEPATH "Wipe out find results for testing." FORCE)
116
endforeach()
117
118
119

# Enable framework and bundle searching.  Make sure bundles are found
# before unix-syle packages.
120
121
set(CMAKE_FIND_FRAMEWORK LAST)
set(CMAKE_FIND_APPBUNDLE FIRST)
122

123
124
125
# Set the wrong answer for a find to make sure it re-finds.
set(VersionedA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/cmake/zot-4.0)

126
127
128
129
# Test that CMAKE_IGNORE_PATH can ignore the purposely bad package
# files in the lib/cmake/zot-3.1 directory.
set(CMAKE_IGNORE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib/cmake/zot-3.1)

130
# Look for packages with new-style signatures.
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
find_package(foo NO_MODULE)
find_package(Foo CONFIGS FooConfig.cmake)
find_package(Bar)
set(CMAKE_DISABLE_FIND_PACKAGE_Blub TRUE)
find_package(Blub NO_MODULE)
find_package(TFramework CONFIGS TFrameworkConfig.cmake)
find_package(Tframework)
find_package(TApp)
find_package(Tapp CONFIGS tapp-config.cmake)
find_package(Special NAMES Suffix SuffixTest PATH_SUFFIXES test)
find_package(VersionedA 2 NAMES zot)
find_package(VersionedB 3.1 EXACT NAMES zot)
find_package(VersionedC 4.0 EXACT NAMES zot)
find_package(VersionedD 1.1 EXACT NAMES Baz)
find_package(VersionedE 1.2 EXACT NAMES Baz)
146
147
148
149
find_package(VersionedF 1.3 EXACT NAMES Baz)
find_package(VersionedG 2.0 EXACT NAMES Baz)
find_package(VersionedH 2.1 EXACT NAMES Baz)

150

151
# Test Config files which set Xyz_FOUND themselves:
152
153
find_package(SetFoundTRUE NO_MODULE)
find_package(SetFoundFALSE NO_MODULE)
154

155
# Test wrong initial path when result is present.
156
157
set(WrongA_DIR "${VersionedD_DIR}")
find_package(WrongA 1.2 EXACT NAMES Baz)
158
159

# Test wrong initial cache entry of UNINITIALIZED type when result is present.
160
161
162
set(WrongB_DIR "${VersionedD_DIR}" CACHE UNINITIALIZED "Wrong Value" FORCE)
get_property(type CACHE WrongB_DIR PROPERTY TYPE)
find_package(WrongB 1.2 EXACT NAMES Baz)
163
164

# Test wrong initial path when result is missing.
165
set(WrongC_DIR "${VersionedD_DIR}")
166
find_package(WrongC 1.4 EXACT QUIET NAMES Baz)
167
168

# Test wrong initial cache entry of UNINITIALIZED type when result is missing.
169
170
set(WrongD_DIR "${VersionedD_DIR}" CACHE UNINITIALIZED "Wrong Value" FORCE)
get_property(type CACHE WrongD_DIR PROPERTY TYPE)
171
find_package(WrongD 1.4 EXACT QUIET NAMES Baz)
172

173
# HINTS should override the system but PATHS should not
174
175
176
list(INSERT CMAKE_SYSTEM_PREFIX_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/A")
find_package(wibbleA NAMES wibble PATHS B)
find_package(wibbleB NAMES wibble HINTS B)
177

178
# Look for package with recursive find-modules.
179
180
181
find_package(RecursiveA COMPONENTS A)
find_package(RecursiveB 2)
find_package(RecursiveC 3.1 EXACT)
182

183
# Test architecture-specific search directories.
184
185
186
187
188
189
set(CMAKE_LIBRARY_ARCHITECTURE arch)
find_package(ArchA NAMES Bar)
find_package(ArchB NAMES Foo CONFIGS FooConfig.cmake)
find_package(ArchC 3.1 EXACT NAMES zot)
find_package(ArchD 4.0 EXACT NAMES zot)
unset(CMAKE_LIBRARY_ARCHITECTURE)
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Test find_package() with CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS enabled
if(UNIX)
  # Create ./symlink pointing back here.
  execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
      . "${CMAKE_CURRENT_SOURCE_DIR}/symlink")
  # Make find_package search through the symlink
  set(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/symlink")

  # First, test the default behavior where symlinks are preserved.
  set(SetFoundResolved_DIR "")
  find_package(SetFoundResolved)
  # The result must preserve the /symlink/ path.
  set(SetFoundResolved_EXPECTED "${CMAKE_CURRENT_SOURCE_DIR}/symlink/cmake")
  if(NOT "${SetFoundResolved_DIR}" STREQUAL "${SetFoundResolved_EXPECTED}")
    message(SEND_ERROR "SetFoundResolved_DIR set by find_package() is set to \"${SetFoundResolved_DIR}\" (expected \"${SetFoundResolved_EXPECTED}\")")
  endif()

208
209
210
211
212
213
214
215
216
217
218
219
  # This part of the test only works if there are no symlinks in our path.
  get_filename_component(real_src_dir "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH)
  if(real_src_dir STREQUAL CMAKE_CURRENT_SOURCE_DIR)
    # Resolve symlinks when finding the package.
    set(CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS TRUE)
    set(SetFoundResolved_DIR "")
    find_package(SetFoundResolved)
    # ./symlink points back here so it should be gone when resolved.
    set(SetFoundResolved_EXPECTED "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
    if(NOT "${SetFoundResolved_DIR}" STREQUAL "${SetFoundResolved_EXPECTED}")
      message(SEND_ERROR "SetFoundResolved_DIR set by find_package() is set to \"${SetFoundResolved_DIR}\" (expected \"${SetFoundResolved_EXPECTED}\")")
    endif()
220
  endif()
221

222
223
224
225
226
  # Cleanup.
  unset(CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS)
  file(REMOVE "${CMAKE_CURRENT_SOURCE_DIR}/symlink")
endif()

227
# Test <PackageName>_DIR environment variable.
228
# We erase the main prefix path to ensure the env var is used.
229
230
231
232
set(CMAKE_PREFIX_PATH)
set(ENV{EnvA_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/lib/zot-3.1")
find_package(EnvA 3.1 EXACT QUIET NAMES zot) # Should Work
find_package(EnvB 3.1 EXACT QUIET NAMES zot) # Should Fail
233

234
# Test system package registry if available.
235
236
237
if(CMakeTestSystemPackage)
  find_package(CMakeTestSystemPackage)
  execute_process(COMMAND ${CMakeTestSystemPackage_CLEANUP}
238
    OUTPUT_VARIABLE _output ERROR_VARIABLE _error)
239
endif()
240

241
# Expected locations at which packages should be found.
242
243
244
245
246
247
set(foo_EXPECTED "lib/foo-1.2/foo-config.cmake")
set(Foo_EXPECTED "lib/foo-1.2/CMake/FooConfig.cmake")
set(Bar_EXPECTED "lib/Bar/BarConfig.cmake")
set(Blub_MISSING "")
set(Special_EXPECTED "lib/suffix/test/SuffixTestConfig.cmake")
set(TFramework_EXPECTED
248
  "TFramework.framework/Versions/A/Resources/CMake/TFrameworkConfig.cmake")
249
set(Tframework_EXPECTED
250
  "TFramework.framework/Versions/A/Resources/tframework-config.cmake")
251
set(TApp_EXPECTED
252
  "TApp.app/Contents/Resources/TAppConfig.cmake")
253
set(Tapp_EXPECTED
254
  "TApp.app/Contents/Resources/cmake/tapp-config.cmake")
255
256
257
258
259
set(VersionedA_EXPECTED "lib/zot-2.0/zot-config.cmake")
set(VersionedB_EXPECTED "lib/zot-3.1/zot-config.cmake")
set(VersionedC_EXPECTED "lib/cmake/zot-4.0/zot-config.cmake")
set(VersionedD_EXPECTED "Baz 1.1/BazConfig.cmake")
set(VersionedE_EXPECTED "Baz 1.2/CMake/BazConfig.cmake")
260
261
262
set(VersionedF_EXPECTED "Baz 1.3/lib/cmake/Baz/BazConfig.cmake")
set(VersionedG_EXPECTED "Baz 2.0/share/Baz 2/BazConfig.cmake")
set(VersionedH_EXPECTED "Baz 2.1/lib/Baz 2/cmake/BazConfig.cmake")
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
set(WrongA_EXPECTED "${VersionedE_EXPECTED}")
set(WrongB_EXPECTED "${VersionedE_EXPECTED}")
set(WrongC_MISSING "WrongC_DIR-NOTFOUND")
set(WrongD_MISSING "WrongD_DIR-NOTFOUND")
set(wibbleA_EXPECTED "A/wibble-config.cmake")
set(wibbleB_EXPECTED "B/wibble-config.cmake")
set(RecursiveA_EXPECTED "lib/RecursiveA/recursivea-config.cmake")
set(RecursiveB_EXPECTED "lib/zot-2.0/zot-config.cmake")
set(RecursiveC_EXPECTED "lib/zot-3.1/zot-config.cmake")
set(ArchA_EXPECTED "lib/arch/Bar/BarConfig.cmake")
set(ArchB_EXPECTED "lib/arch/foo-1.2/CMake/FooConfig.cmake")
set(ArchC_EXPECTED "lib/arch/zot-3.1/zot-config.cmake")
set(ArchD_EXPECTED "lib/arch/cmake/zot-4.0/zot-config.cmake")
set(EnvA_EXPECTED "lib/zot-3.1/zot-config.cmake")
set(EnvB_MISSING "EnvB_DIR-NOTFOUND")
set(SetFoundTRUE_EXPECTED "cmake/SetFoundTRUEConfig.cmake")
set(SetFoundFALSE_MISSING "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMakeTestSystemPackage_EXPECTED "SystemPackage/CMakeTestSystemPackageConfig.cmake")
281
282

# Check the results.
283
284
foreach(p ${PACKAGES})
  if(DEFINED ${p}_MISSING)
285
    # Check and report failure.
286
287
    if(NOT "${${p}_DIR}" STREQUAL "${${p}_MISSING}")
      message(SEND_ERROR
288
289
        "Package ${p} should have been [${${p}_MISSING}] but "
        "was [${${p}_DIR}]")
290
291
292
    endif()
    if(${p}_FOUND)
      message(SEND_ERROR
293
294
        "Package ${p} should not have been found, but ${p}_FOUND is set to "
        "\"${${p}_FOUND}\"")
295
296
    endif()
  elseif(${p}_FOUND)
297
    # Convert to relative path for comparison to expected location.
298
    file(RELATIVE_PATH REL_${p}_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}"
299
300
301
      "${${p}_CONFIG}")

    # Debugging output.
302
303
    if(CMAKE_FIND_DEBUG_MODE)
      message("Package ${p} found [${REL_${p}_CONFIG}]")
304
    endif()
305
306

    # Check and report failure.
307
308
    if(NOT "${REL_${p}_CONFIG}" STREQUAL "${${p}_EXPECTED}")
      message(SEND_ERROR
309
310
        "Package ${p} should have been [${${p}_EXPECTED}] but "
        "was [${REL_${p}_CONFIG}]")
311
    endif()
312
313
314
  else()
    message(SEND_ERROR "Package ${p} not found!")
  endif()
315
endforeach()
316
317

# Check that version information was extracted.
318
319
if(NOT "${VersionedA_VERSION}" STREQUAL "2.0")
  message(SEND_ERROR
320
    "Package VersionedA is version [${VersionedA_VERSION}], not [2.0]")
321
endif()
322
323
if(NOT "${VersionedA_VERSION_MAJOR}" STREQUAL "2")
  message(SEND_ERROR
324
    "Package VersionedA is major version [${VersionedA_VERSION_MAJOR}], not [2]")
325
endif()
326
327
if(NOT "${VersionedA_VERSION_MINOR}" STREQUAL "0")
  message(SEND_ERROR
328
    "Package VersionedA is minor version [${VersionedA_VERSION_MINOR}], not [0]")
329
endif()
330

331
332
if(NOT "${VersionedB_VERSION}" STREQUAL "3.1")
  message(SEND_ERROR
333
    "Package VersionedB is version [${VersionedB_VERSION}], not [3.1]")
334
endif()
335
336
if(NOT "${VersionedB_VERSION_MAJOR}" STREQUAL "3")
  message(SEND_ERROR
337
    "Package VersionedB is major version [${VersionedB_VERSION_MAJOR}], not [3]")
338
endif()
339
340
if(NOT "${VersionedB_VERSION_MINOR}" STREQUAL "1")
  message(SEND_ERROR
341
    "Package VersionedB is minor version [${VersionedB_VERSION_MINOR}], not [1]")
342
endif()
343

344
345
if(NOT "${Special_VERSION}" STREQUAL "1.2")
  message(SEND_ERROR
346
    "Package Special is version [${Special_VERSION}], not [1.2]")
347
endif()
348
349
if(NOT "${Special_VERSION_MAJOR}" STREQUAL "1")
  message(SEND_ERROR
350
    "Package Special is major version [${Special_VERSION_MAJOR}], not [1]")
351
endif()
352
353
if(NOT "${Special_VERSION_MINOR}" STREQUAL "2")
  message(SEND_ERROR
354
    "Package Special is minor version [${Special_VERSION_MINOR}], not [2]")
355
endif()
356
357

# Test version number comparison.
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
if(NOT "1.2.3.4" VERSION_LESS "1.2.3.5")
  message(SEND_ERROR "1.2.3.4 VERSION_LESS 1.2.3.5 is not true!")
endif()
if(NOT "1.2" VERSION_LESS "1.10")
  message(SEND_ERROR "1.2 VERSION_LESS 1.10 is not true!")
endif()
if(NOT "1.02" VERSION_GREATER "1.1")
  message(SEND_ERROR "1.02 VERSION_GREATER 1.1 is not true!")
endif()
if("1.2.3" VERSION_GREATER "1.2.3.4")
  message(SEND_ERROR "1.2.3 VERSION_GREATER 1.2.3.4 is not false!")
endif()
if(NOT "1.2" VERSION_EQUAL "1.2.0.0")
  message(SEND_ERROR "1.2 VERSION_EQUAL 1.2.0.0 is not true!")
endif()
Brad King's avatar
Brad King committed
373
374
375

#-----------------------------------------------------------------------------
# Test export(PACKAGE) with find_package.
376
377
378
379

# Choose a unique version.
string(REGEX REPLACE "-.*$" "" version ${CMAKE_VERSION})
string(RANDOM LENGTH 4 ALPHABET "0123456789" v)
Daniel Pfeifer's avatar
Daniel Pfeifer committed
380
string(APPEND version ".${v}")
381

382
383
message(STATUS "Preparing export(PACKAGE) test project")
try_compile(EXPORTER_COMPILED
384
  ${FindPackageTest_BINARY_DIR}/Exporter-build
Brad King's avatar
Brad King committed
385
386
  ${FindPackageTest_SOURCE_DIR}/Exporter
  CMakeTestExportPackage dummy
387
  CMAKE_FLAGS "-UCMAKE_EXPORT_NO_PACKAGE_REGISTRY"
388
    "-DCMAKE_POLICY_DEFAULT_CMP0090:STRING=OLD"
389
    -Dversion=${version}
Brad King's avatar
Brad King committed
390
  OUTPUT_VARIABLE output)
391
392
message(STATUS "Searching for export(PACKAGE) test project")
set(CMakeTestExportPackage_DIR "" CACHE FILEPATH
Brad King's avatar
Brad King committed
393
  "Wipe out find results for testing." FORCE)
394
find_package(CMakeTestExportPackage 1.${version} EXACT REQUIRED)
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
message(STATUS "Searching for export(PACKAGE) test project with CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=TRUE")
set(CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY TRUE)
set(CMakeTestExportPackage_DIR "" CACHE FILEPATH
  "Wipe out find results for testing." FORCE)
find_package(CMakeTestExportPackage  1.${version} EXACT QUIET)
if(CMakeTestExportPackage_FOUND)
  message(SEND_ERROR "CMakeTestExportPackage should not be FOUND!")
endif()
unset(CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY)

message(STATUS "Remove export(PACKAGE) test project")
file(REMOVE_RECURSE ${FindPackageTest_BINARY_DIR}/Exporter-build)
set(CMakeTestExportPackage_DIR "" CACHE FILEPATH
  "Wipe out find results for testing." FORCE)
find_package(CMakeTestExportPackage QUIET) # Should clean the user package cache
#
message(STATUS "Preparing export(PACKAGE) test project with CMAKE_EXPORT_NO_PACKAGE_REGISTRY=TRUE")
try_compile(EXPORTER_COMPILED
  ${FindPackageTest_BINARY_DIR}/Exporter-build
  ${FindPackageTest_SOURCE_DIR}/Exporter
  CMakeTestExportPackage dummy
  CMAKE_FLAGS "-DCMAKE_EXPORT_NO_PACKAGE_REGISTRY:BOOL=TRUE"
    -Dversion=${version}
  OUTPUT_VARIABLE output)
message(STATUS "Searching for export(PACKAGE) test project")
find_package(CMakeTestExportPackage 1.${version} EXACT QUIET)
if(CMakeTestExportPackage_FOUND)
  message(SEND_ERROR "CMakeTestExportPackage should not be FOUND!")
endif()

426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
message(STATUS "Remove export(PACKAGE) test project")
file(REMOVE_RECURSE ${FindPackageTest_BINARY_DIR}/Exporter-build)

message(STATUS "Preparing export(PACKAGE) test project with POLICY CMP0090=NEW")
try_compile(EXPORTER_COMPILED
  ${FindPackageTest_BINARY_DIR}/Exporter-build
  ${FindPackageTest_SOURCE_DIR}/Exporter
  CMakeTestExportPackage dummy
  CMAKE_FLAGS
   "-DCMAKE_POLICY_DEFAULT_CMP0090:STRING=NEW"
    -Dversion=${version}
  OUTPUT_VARIABLE output)
message(STATUS "Searching for export(PACKAGE) test project")
find_package(CMakeTestExportPackage 1.${version} EXACT QUIET)
if(CMakeTestExportPackage_FOUND)
  message(SEND_ERROR "CMakeTestExportPackage should not be FOUND!")
endif()


445
446
447
448
449
#-----------------------------------------------------------------------------
# Test configure_package_config_file().

include(CMakePackageConfigHelpers)

450
# Generate a config file ready to be installed.
451
452
453
454
455
456
457
458
459
set(INCLUDE_INSTALL_DIR include )
set(SHARE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/share/" )
set(CURRENT_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}" )

configure_package_config_file(RelocatableConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/RelocatableConfig.cmake"
                              INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}"
                              PATH_VARS INCLUDE_INSTALL_DIR SHARE_INSTALL_DIR CURRENT_BUILD_DIR
                             )

460
461
set(Relocatable_FIND_COMPONENTS AComp BComp CComp)
set(Relocatable_FIND_REQUIRED_BComp 1)
462
463
464
465
466
467
468
469
470
471
472
473
474
475
include("${CMAKE_CURRENT_BINARY_DIR}/RelocatableConfig.cmake")

if(NOT "${RELOC_INCLUDE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/include")
  message(SEND_ERROR "RELOC_INCLUDE_DIR set by configure_package_config_file() is set to \"${RELOC_INCLUDE_DIR}\" (expected \"${CMAKE_CURRENT_BINARY_DIR}/include\")")
endif()

if(NOT "${RELOC_SHARE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/share/")
  message(SEND_ERROR "RELOC_SHARE_DIR set by configure_package_config_file() is set to \"${RELOC_SHARE_DIR}\" (expected \"${CMAKE_CURRENT_BINARY_DIR}/share/\")")
endif()

if(NOT "${RELOC_BUILD_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
  message(SEND_ERROR "RELOC_BUILD_DIR set by configure_package_config_file() is set to \"${RELOC_BUILD_DIR}\" (expected \"${CMAKE_CURRENT_BINARY_DIR}\")")
endif()

476
477
478
479
480
481
482
483
if(NOT DEFINED Relocatable_FOUND)
  message(SEND_ERROR "Relocatable_FOUND not defined !")
endif()

if(Relocatable_FOUND)
  message(SEND_ERROR "Relocatable_FOUND set to TRUE !")
endif()

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
# Generate a config file for the build tree.
set(INCLUDE_INSTALL_DIR include )
set(SHARE_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/share/" )
set(CURRENT_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}" )

configure_package_config_file(RelocatableConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/RelocatableConfig.cmake"
                              INSTALL_DESTINATION "${CMAKE_CURRENT_BINARY_DIR}"
                              PATH_VARS INCLUDE_INSTALL_DIR SHARE_INSTALL_DIR CURRENT_BUILD_DIR
                              INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
                             )

set(Relocatable_FIND_COMPONENTS AComp BComp CComp)
set(Relocatable_FIND_REQUIRED_BComp 1)
include("${CMAKE_CURRENT_BINARY_DIR}/RelocatableConfig.cmake")

if(NOT "${RELOC_INCLUDE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/include")
  message(SEND_ERROR "RELOC_INCLUDE_DIR set by configure_package_config_file() is set to \"${RELOC_INCLUDE_DIR}\" (expected \"${CMAKE_CURRENT_BINARY_DIR}/include\")")
endif()

if(NOT "${RELOC_SHARE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/share/")
  message(SEND_ERROR "RELOC_SHARE_DIR set by configure_package_config_file() is set to \"${RELOC_SHARE_DIR}\" (expected \"${CMAKE_CURRENT_BINARY_DIR}/share/\")")
endif()

if(NOT "${RELOC_BUILD_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
  message(SEND_ERROR "RELOC_BUILD_DIR set by configure_package_config_file() is set to \"${RELOC_BUILD_DIR}\" (expected \"${CMAKE_CURRENT_BINARY_DIR}\")")
endif()

if(NOT DEFINED Relocatable_FOUND)
  message(SEND_ERROR "Relocatable_FOUND not defined !")
endif()

if(Relocatable_FOUND)
  message(SEND_ERROR "Relocatable_FOUND set to TRUE !")
endif()


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
############################################################################
##Test FIND_PACKAGE using sorting
set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_FIND_PACKAGE_SORT_ORDER NAME)
SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION ASC)

set(SortLib_DIR "" CACHE FILEPATH "Wipe out find results for testing." FORCE)
FIND_PACKAGE(SortLib CONFIG)
IF (NOT "${SortLib_VERSION}" STREQUAL "3.1.1")
  message(SEND_ERROR "FIND_PACKAGE_SORT_ORDER Name Asc! ${SortLib_VERSION}")
endif()
unset(SortLib_VERSION)


set(SortLib_DIR "" CACHE FILEPATH "Wipe out find results for testing." FORCE)
SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
FIND_PACKAGE(SortLib CONFIG)
IF (NOT "${SortLib_VERSION}" STREQUAL "3.10.1")
  message(SEND_ERROR "FIND_PACKAGE_SORT_ORDER Natural! Dec ${SortLib_VERSION}")
endif()
set(SortLib_DIR "" CACHE FILEPATH "Wipe out find results for testing." FORCE)
unset(SortLib_VERSION)

unset(CMAKE_FIND_PACKAGE_SORT_ORDER)
unset(CMAKE_FIND_PACKAGE_SORT_DIRECTION)
set(CMAKE_PREFIX_PATH )
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

############################################################################
##Test FIND_PACKAGE CMAKE_FIND_PACKAGE_PREFER_CONFIG

set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/PreferConfig)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/PreferConfig)

# prefer module mode
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG OFF)
unset(ABC_FOUND)
unset(ABC_CONFIG)

find_package(ABC)
if(NOT ABC_FOUND)
    message(SEND_ERROR "Did not find ABC package")
endif()
if(ABC_CONFIG)
    message(SEND_ERROR "Incorrectly found ABC in CONFIG mode, expected to find it with MODULE mode")
endif()

# Now prefer config mode
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
unset(ABC_FOUND)
unset(ABC_CONFIG)

find_package(ABC)
if(NOT ABC_FOUND)
    message(SEND_ERROR "Did not find ABC package")
endif()
if(NOT ABC_CONFIG)
    message(SEND_ERROR "Incorrectly found ABC in MODULE mode, expected to find it with CONFIG mode")
endif()

set(CMAKE_FIND_PACKAGE_PREFER_CONFIG OFF)
set(CMAKE_PREFIX_PATH)
582
583
584
585
586
587
588
589
590
591
592
593
594

############################################################################
##Test find_package CMAKE_FIND_PACKAGE_PREFER_CONFIG with module fallback

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/PreferConfigOnlyModule)

set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)

find_package(ACME REQUIRED)

if(NOT ACME_FOUND)
    message(SEND_ERROR "Did not find ACME package")
endif()