KnownComponents.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Assertion macro
  2. macro(check desc actual expect)
  3. if(NOT "x${actual}" STREQUAL "x${expect}")
  4. message(SEND_ERROR "${desc}: got \"${actual}\", not \"${expect}\"")
  5. endif()
  6. endmacro()
  7. # General test of all component types given an absolute path.
  8. set(filename "/path/to/filename.ext.in")
  9. set(expect_DIRECTORY "/path/to")
  10. set(expect_NAME "filename.ext.in")
  11. set(expect_EXT ".ext.in")
  12. set(expect_NAME_WE "filename")
  13. set(expect_PATH "/path/to")
  14. foreach(c DIRECTORY NAME EXT NAME_WE PATH)
  15. get_filename_component(actual_${c} "${filename}" ${c})
  16. check("${c}" "${actual_${c}}" "${expect_${c}}")
  17. list(APPEND non_cache_vars actual_${c})
  18. endforeach()
  19. # Test Windows paths with DIRECTORY component and an absolute Windows path.
  20. get_filename_component(test_slashes "c:\\path\\to\\filename.ext.in" DIRECTORY)
  21. check("DIRECTORY from backslashes" "${test_slashes}" "c:/path/to")
  22. list(APPEND non_cache_vars test_slashes)
  23. get_filename_component(test_winroot "c:\\filename.ext.in" DIRECTORY)
  24. check("DIRECTORY in windows root" "${test_winroot}" "c:/")
  25. list(APPEND non_cache_vars test_winroot)
  26. # Test finding absolute paths.
  27. get_filename_component(test_absolute "/path/to/a/../filename.ext.in" ABSOLUTE)
  28. check("ABSOLUTE" "${test_absolute}" "/path/to/filename.ext.in")
  29. get_filename_component(test_absolute "/../path/to/filename.ext.in" ABSOLUTE)
  30. check("ABSOLUTE .. in root" "${test_absolute}" "/path/to/filename.ext.in")
  31. get_filename_component(test_absolute "c:/../path/to/filename.ext.in" ABSOLUTE)
  32. check("ABSOLUTE .. in windows root" "${test_absolute}" "c:/path/to/filename.ext.in")
  33. list(APPEND non_cache_vars test_absolute)
  34. # Test finding absolute paths from various base directories.
  35. get_filename_component(test_abs_base "testdir1" ABSOLUTE)
  36. check("ABSOLUTE .. from default base" "${test_abs_base}"
  37. "${CMAKE_CURRENT_SOURCE_DIR}/testdir1")
  38. get_filename_component(test_abs_base "../testdir2" ABSOLUTE
  39. BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dummydir")
  40. check("ABSOLUTE .. from dummy base to parent" "${test_abs_base}"
  41. "${CMAKE_CURRENT_SOURCE_DIR}/testdir2")
  42. get_filename_component(test_abs_base "testdir3" ABSOLUTE
  43. BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dummydir")
  44. check("ABSOLUTE .. from dummy base to child" "${test_abs_base}"
  45. "${CMAKE_CURRENT_SOURCE_DIR}/dummydir/testdir3")
  46. list(APPEND non_cache_vars test_abs_base)
  47. # Test finding absolute paths with CACHE parameter. (Note that more
  48. # rigorous testing of the CACHE parameter comes later with PROGRAM).
  49. get_filename_component(test_abs_base_1 "testdir4" ABSOLUTE CACHE)
  50. check("ABSOLUTE CACHE 1" "${test_abs_base_1}"
  51. "${CMAKE_CURRENT_SOURCE_DIR}/testdir4")
  52. list(APPEND cache_vars test_abs_base_1)
  53. get_filename_component(test_abs_base_2 "testdir5" ABSOLUTE
  54. BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dummydir"
  55. CACHE)
  56. check("ABSOLUTE CACHE 2" "${test_abs_base_2}"
  57. "${CMAKE_CURRENT_SOURCE_DIR}/dummydir/testdir5")
  58. list(APPEND cache_vars test_abs_base_2)
  59. # Test the PROGRAM component type.
  60. get_filename_component(test_program_name "/ arg1 arg2" PROGRAM)
  61. check("PROGRAM with no args output" "${test_program_name}" "/")
  62. get_filename_component(test_program_name "/ arg1 arg2" PROGRAM
  63. PROGRAM_ARGS test_program_args)
  64. check("PROGRAM with args output: name" "${test_program_name}" "/")
  65. check("PROGRAM with args output: args" "${test_program_args}" " arg1 arg2")
  66. list(APPEND non_cache_vars test_program_name)
  67. list(APPEND non_cache_vars test_program_args)
  68. # Test CACHE parameter for most component types.
  69. get_filename_component(test_cache "/path/to/filename.ext.in" DIRECTORY CACHE)
  70. check("CACHE 1" "${test_cache}" "/path/to")
  71. # Make sure that the existing CACHE entry from previous is honored:
  72. get_filename_component(test_cache "/path/to/other/filename.ext.in" DIRECTORY CACHE)
  73. check("CACHE 2" "${test_cache}" "/path/to")
  74. unset(test_cache CACHE)
  75. get_filename_component(test_cache "/path/to/other/filename.ext.in" DIRECTORY CACHE)
  76. check("CACHE 3" "${test_cache}" "/path/to/other")
  77. list(APPEND cache_vars test_cache)
  78. # Test the PROGRAM component type with CACHE specified.
  79. # 1. Make sure it makes a cache variable in the first place for basic usage:
  80. get_filename_component(test_cache_program_name_1 "/ arg1 arg2" PROGRAM CACHE)
  81. check("PROGRAM CACHE 1 with no args output" "${test_cache_program_name_1}" "/")
  82. list(APPEND cache_vars test_cache_program_name_1)
  83. # 2. Set some existing cache variables & make sure the function returns them:
  84. set(test_cache_program_name_2 DummyProgramName CACHE FILEPATH "")
  85. get_filename_component(test_cache_program_name_2 "/ arg1 arg2" PROGRAM CACHE)
  86. check("PROGRAM CACHE 2 with no args output" "${test_cache_program_name_2}"
  87. "DummyProgramName")
  88. list(APPEND cache_vars test_cache_program_name_2)
  89. # 3. Now test basic usage when PROGRAM_ARGS is used:
  90. get_filename_component(test_cache_program_name_3 "/ arg1 arg2" PROGRAM
  91. PROGRAM_ARGS test_cache_program_args_3 CACHE)
  92. check("PROGRAM CACHE 3 name" "${test_cache_program_name_3}" "/")
  93. check("PROGRAM CACHE 3 args" "${test_cache_program_args_3}" " arg1 arg2")
  94. list(APPEND cache_vars test_cache_program_name_3)
  95. list(APPEND cache_vars test_cache_program_args_3)
  96. # 4. Test that existing cache variables are returned when PROGRAM_ARGS is used:
  97. set(test_cache_program_name_4 DummyPgm CACHE FILEPATH "")
  98. set(test_cache_program_args_4 DummyArgs CACHE STRING "")
  99. get_filename_component(test_cache_program_name_4 "/ arg1 arg2" PROGRAM
  100. PROGRAM_ARGS test_cache_program_args_4 CACHE)
  101. check("PROGRAM CACHE 4 name" "${test_cache_program_name_4}" "DummyPgm")
  102. check("PROGRAM CACHE 4 args" "${test_cache_program_args_4}" "DummyArgs")
  103. list(APPEND cache_vars test_cache_program_name_4)
  104. list(APPEND cache_vars test_cache_program_name_4)
  105. # Test that ONLY the expected cache variables were created.
  106. get_cmake_property(current_cache_vars CACHE_VARIABLES)
  107. get_cmake_property(current_vars VARIABLES)
  108. foreach(thisVar ${cache_vars})
  109. if(NOT thisVar IN_LIST current_cache_vars)
  110. message(SEND_ERROR "${thisVar} expected in cache but was not found.")
  111. endif()
  112. endforeach()
  113. foreach(thisVar ${non_cache_vars})
  114. if(thisVar IN_LIST current_cache_vars)
  115. message(SEND_ERROR "${thisVar} unexpectedly found in cache.")
  116. endif()
  117. if(NOT thisVar IN_LIST current_vars)
  118. # Catch likely typo when appending to non_cache_vars:
  119. message(SEND_ERROR "${thisVar} not found in regular variable list.")
  120. endif()
  121. endforeach()