From 6feef1cd5f847ac09b33b3cc83af4fc7aab651b8 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 16 Nov 2021 18:10:36 +0100 Subject: [PATCH] add custom uninstall target to CMake --- CMakeLists.txt | 12 ++++++++++++ tools/cmake_uninstall.cmake.in | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tools/cmake_uninstall.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bc44e1..b688d92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -197,5 +197,17 @@ install(TARGETS trexio # Also install trexio_f.f90 file with TREXIO Fortran module. install(FILES ${TREXIO_MOD_FILE} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +# Uninstall target, copied from CMake FAQ: +# https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake +if(NOT TARGET uninstall) + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) + + add_custom_target(uninstall + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) +endif() + # =========================================== diff --git a/tools/cmake_uninstall.cmake.in b/tools/cmake_uninstall.cmake.in new file mode 100644 index 0000000..f779906 --- /dev/null +++ b/tools/cmake_uninstall.cmake.in @@ -0,0 +1,24 @@ +# Needed to make uninstall target in CMake, see the CMake FAQ: +# https://gitlab.kitware.com/cmake/community/wikis/FAQ#can-i-do-make-uninstall-with-cmake + +if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt") + message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt") +endif() + +file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files) +string(REGEX REPLACE "\n" ";" files "${files}") +foreach(file ${files}) + message(STATUS "Uninstalling $ENV{DESTDIR}${file}") + if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") + exec_program( + "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval + ) + if(NOT "${rm_retval}" STREQUAL 0) + message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}") + endif() + else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}") + message(STATUS "File $ENV{DESTDIR}${file} does not exist.") + endif() +endforeach()