uninstall.cmake 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. CMAKE_MINIMUM_REQUIRED(VERSION 3.3)
  2. MESSAGE(STATUS "Attempting to create uninstall target for make")
  3. #Remove all of the files listed in install_manifest.txt
  4. SET(INSTALL_MANIFEST_PATH "${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt")
  5. IF(NOT EXISTS ${INSTALL_MANIFEST_PATH})
  6. MESSAGE(FATAL_ERROR "Could not find install manifest at ${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt\nThis may be because 'make install' has not been run or install_manifest.txt has been deleted")
  7. ENDIF()
  8. MESSAGE(STATUS "install_manifest.txt found")
  9. FILE(STRINGS ${INSTALL_MANIFEST_PATH} FILES_TO_REMOVE)
  10. FOREACH(FILE_TO_REMOVE ${FILES_TO_REMOVE})
  11. IF(NOT EXISTS "${FILE_TO_REMOVE}")
  12. MESSAGE(WARNING "Could not find file ${FILE_TO_REMOVE}")
  13. CONTINUE()
  14. ENDIF()
  15. FILE(REMOVE "${FILE_TO_REMOVE}")
  16. IF(NOT EXISTS "${FILE_TO_REMOVE}")
  17. MESSAGE(STATUS "Successfully removed file ${FILE_TO_REMOVE}")
  18. ELSE()
  19. MESSAGE(FATAL_ERROR "Failed to remove file ${FILE_TO_REMOVE}.")
  20. ENDIF()
  21. ENDFOREACH(FILE_TO_REMOVE)
  22. # Remove empty directories created during installation
  23. # Checks if a directory is empty and saves the result in out_var
  24. FUNCTION(IS_EMPTY_DIR OUT_VAR DIR)
  25. FILE(GLOB FILES "${DIR}/*")
  26. LIST(LENGTH FILES NUM_FILES)
  27. IF(NUM_FILES EQUAL 0)
  28. SET(${OUT_VAR} TRUE PARENT_SCOPE)
  29. ELSE()
  30. SET(${OUT_VAR} FALSE PARENT_SCOPE)
  31. ENDIF()
  32. ENDFUNCTION()
  33. # Recursively append all parent directories of path to out_var
  34. FUNCTION(PARENT_DIRECTORIES OUT_VAR PATH)
  35. GET_FILENAME_COMPONENT(PARENT "${PATH}" DIRECTORY)
  36. IF(PARENT AND NOT PARENT STREQUAL PATH AND NOT PARENT IN_LIST ${OUT_VAR})
  37. LIST(APPEND ${OUT_VAR} ${PARENT})
  38. PARENT_DIRECTORIES(${OUT_VAR} "${PARENT}")
  39. ENDIF()
  40. SET(${OUT_VAR} ${${OUT_VAR}} PARENT_SCOPE)
  41. ENDFUNCTION()
  42. # Removes all empty parent directories of the given files
  43. FUNCTION(REMOVE_EMPTY_DIRECTORIES FILES)
  44. SET(DIRECTORIES)
  45. FOREACH(FILE_TO_REMOVE ${FILES})
  46. PARENT_DIRECTORIES(DIRECTORIES "${FILE_TO_REMOVE}")
  47. endforeach()
  48. LIST(REMOVE_DUPLICATES DIRECTORIES)
  49. # Sort and reverse so we remove subdirectories first
  50. LIST(SORT DIRECTORIES)
  51. LIST(REVERSE DIRECTORIES)
  52. FOREACH(DIR ${DIRECTORIES})
  53. # Skip directories not inside the install prefix
  54. IF(NOT (EXISTS "${DIR}" AND DIR MATCHES "^${CMAKE_INSTALL_PREFIX}/"))
  55. CONTINUE()
  56. ENDIF()
  57. IS_EMPTY_DIR(DIR_EMPTY "${DIR}")
  58. IF(DIR_EMPTY)
  59. MESSAGE(STATUS "Removing empty directory ${DIR}")
  60. FILE(REMOVE_RECURSE "${DIR}")
  61. ELSEIF()
  62. MESSAGE(STATUS "Skipping non-empty directory ${DIR}")
  63. ENDIF()
  64. ENDFOREACH()
  65. ENDFUNCTION()
  66. REMOVE_EMPTY_DIRECTORIES("${FILES_TO_REMOVE}")