GetFilenameComponentRealpathTest.cmake 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. set(bindir ${CMAKE_CURRENT_BINARY_DIR})
  2. #
  3. # Test nonexistent REALPATH & ABSOLUTE resolution
  4. #
  5. get_filename_component(nonexistent1 ${bindir}/THIS_IS_A_NONEXISTENT_FILE REALPATH)
  6. get_filename_component(nonexistent2 ${bindir}/THIS_IS_A_NONEXISTENT_FILE ABSOLUTE)
  7. if(NOT nonexistent1 STREQUAL "${bindir}/THIS_IS_A_NONEXISTENT_FILE")
  8. message(FATAL_ERROR "REALPATH is not preserving nonexistent files")
  9. endif()
  10. if(NOT nonexistent2 STREQUAL "${bindir}/THIS_IS_A_NONEXISTENT_FILE")
  11. message(FATAL_ERROR "ABSOLUTE is not preserving nonexistent files")
  12. endif()
  13. #
  14. # Test treatment of relative paths
  15. #
  16. foreach(c REALPATH ABSOLUTE)
  17. get_filename_component(dir "subdir/THIS_IS_A_NONEXISTENT_FILE" ${c})
  18. if(NOT "${dir}" STREQUAL "${bindir}/subdir/THIS_IS_A_NONEXISTENT_FILE")
  19. message(FATAL_ERROR
  20. "${c} does not handle relative paths. Expected:\n"
  21. " ${bindir}/subdir/THIS_IS_A_NONEXISTENT_FILE\n"
  22. "but got:\n"
  23. " ${nonexistent1}\n"
  24. )
  25. endif()
  26. endforeach()
  27. #
  28. # Test symbolic link resolution
  29. #
  30. if(UNIX)
  31. # file1 => file2 => file3 (real)
  32. file(WRITE ${bindir}/file3 "test file")
  33. find_program(LN NAMES "ln")
  34. if(LN)
  35. # Create symlinks using "ln -s"
  36. if(NOT EXISTS ${bindir}/file2)
  37. execute_process(COMMAND ${LN} "-s" "${bindir}/file3" "${bindir}/file2")
  38. endif()
  39. if(NOT EXISTS ${bindir}/file1)
  40. execute_process(COMMAND ${LN} "-s" "${bindir}/file2" "${bindir}/file1")
  41. endif()
  42. get_filename_component(file1 ${bindir}/file1 REALPATH)
  43. get_filename_component(file2 ${bindir}/file2 REALPATH)
  44. get_filename_component(file3 ${bindir}/file3 REALPATH)
  45. if(NOT file3 STREQUAL "${bindir}/file3")
  46. message(FATAL_ERROR "CMake fails resolving REALPATH file file3")
  47. endif()
  48. if(NOT file2 STREQUAL "${bindir}/file3")
  49. message(FATAL_ERROR "CMake fails resolving simple symlink")
  50. endif()
  51. if(NOT file1 STREQUAL "${bindir}/file3")
  52. message(FATAL_ERROR "CMake fails resolving double symlink")
  53. endif()
  54. # cleanup
  55. file(REMOVE ${bindir}/file1)
  56. file(REMOVE ${bindir}/file2)
  57. if(EXISTS file1 OR EXISTS file2)
  58. message(FATAL_ERROR "removal of file1 or file2 failed")
  59. endif()
  60. endif()
  61. file(REMOVE ${bindir}/file3)
  62. endif()