KnownComponents.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. get_filename_component(test_program_name " " PROGRAM)
  67. check("PROGRAM with just a space" "${test_program_name}" "")
  68. get_filename_component(test_program_name "${CMAKE_CURRENT_LIST_FILE}" PROGRAM)
  69. check("PROGRAM specified explicitly without quoting" "${test_program_name}" "${CMAKE_CURRENT_LIST_FILE}")
  70. get_filename_component(test_program_name "\"${CMAKE_CURRENT_LIST_FILE}\" arg1 arg2" PROGRAM
  71. PROGRAM_ARGS test_program_args)
  72. check("PROGRAM specified explicitly with arguments: name" "${test_program_name}" "${CMAKE_CURRENT_LIST_FILE}")
  73. check("PROGRAM specified explicitly with arguments: args" "${test_program_args}" " arg1 arg2")
  74. list(APPEND non_cache_vars test_program_name)
  75. list(APPEND non_cache_vars test_program_args)
  76. # Test CACHE parameter for most component types.
  77. get_filename_component(test_cache "/path/to/filename.ext.in" DIRECTORY CACHE)
  78. check("CACHE 1" "${test_cache}" "/path/to")
  79. # Make sure that the existing CACHE entry from previous is honored:
  80. get_filename_component(test_cache "/path/to/other/filename.ext.in" DIRECTORY CACHE)
  81. check("CACHE 2" "${test_cache}" "/path/to")
  82. unset(test_cache CACHE)
  83. get_filename_component(test_cache "/path/to/other/filename.ext.in" DIRECTORY CACHE)
  84. check("CACHE 3" "${test_cache}" "/path/to/other")
  85. list(APPEND cache_vars test_cache)
  86. # Test the PROGRAM component type with CACHE specified.
  87. # 1. Make sure it makes a cache variable in the first place for basic usage:
  88. get_filename_component(test_cache_program_name_1 "/ arg1 arg2" PROGRAM CACHE)
  89. check("PROGRAM CACHE 1 with no args output" "${test_cache_program_name_1}" "/")
  90. list(APPEND cache_vars test_cache_program_name_1)
  91. # 2. Set some existing cache variables & make sure the function returns them:
  92. set(test_cache_program_name_2 DummyProgramName CACHE FILEPATH "")
  93. get_filename_component(test_cache_program_name_2 "/ arg1 arg2" PROGRAM CACHE)
  94. check("PROGRAM CACHE 2 with no args output" "${test_cache_program_name_2}"
  95. "DummyProgramName")
  96. list(APPEND cache_vars test_cache_program_name_2)
  97. # 3. Now test basic usage when PROGRAM_ARGS is used:
  98. get_filename_component(test_cache_program_name_3 "/ arg1 arg2" PROGRAM
  99. PROGRAM_ARGS test_cache_program_args_3 CACHE)
  100. check("PROGRAM CACHE 3 name" "${test_cache_program_name_3}" "/")
  101. check("PROGRAM CACHE 3 args" "${test_cache_program_args_3}" " arg1 arg2")
  102. list(APPEND cache_vars test_cache_program_name_3)
  103. list(APPEND cache_vars test_cache_program_args_3)
  104. # 4. Test that existing cache variables are returned when PROGRAM_ARGS is used:
  105. set(test_cache_program_name_4 DummyPgm CACHE FILEPATH "")
  106. set(test_cache_program_args_4 DummyArgs CACHE STRING "")
  107. get_filename_component(test_cache_program_name_4 "/ arg1 arg2" PROGRAM
  108. PROGRAM_ARGS test_cache_program_args_4 CACHE)
  109. check("PROGRAM CACHE 4 name" "${test_cache_program_name_4}" "DummyPgm")
  110. check("PROGRAM CACHE 4 args" "${test_cache_program_args_4}" "DummyArgs")
  111. list(APPEND cache_vars test_cache_program_name_4)
  112. list(APPEND cache_vars test_cache_program_name_4)
  113. # Test that ONLY the expected cache variables were created.
  114. get_cmake_property(current_cache_vars CACHE_VARIABLES)
  115. get_cmake_property(current_vars VARIABLES)
  116. foreach(thisVar ${cache_vars})
  117. if(NOT thisVar IN_LIST current_cache_vars)
  118. message(SEND_ERROR "${thisVar} expected in cache but was not found.")
  119. endif()
  120. endforeach()
  121. foreach(thisVar ${non_cache_vars})
  122. if(thisVar IN_LIST current_cache_vars)
  123. message(SEND_ERROR "${thisVar} unexpectedly found in cache.")
  124. endif()
  125. if(NOT thisVar IN_LIST current_vars)
  126. # Catch likely typo when appending to non_cache_vars:
  127. message(SEND_ERROR "${thisVar} not found in regular variable list.")
  128. endif()
  129. endforeach()