diff --git a/CMakeLists.txt b/CMakeLists.txt index d3d99f6e7c..b80a0e16d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,6 +167,8 @@ include(AMReXInstallHelpers) install_amrex_targets(${_amrex_targets}) if(AMReX_INSTALL) + add_uninstall_target(${CMAKE_CURRENT_LIST_DIR}) + # Add a test_install target to smoke-test # the installation add_test_install_target( diff --git a/Docs/sphinx_documentation/source/BuildingAMReX.rst b/Docs/sphinx_documentation/source/BuildingAMReX.rst index bcbc84e116..3315829df8 100644 --- a/Docs/sphinx_documentation/source/BuildingAMReX.rst +++ b/Docs/sphinx_documentation/source/BuildingAMReX.rst @@ -383,6 +383,7 @@ The CMake build process is summarized as follows: cmake [options] -DCMAKE_BUILD_TYPE=[Debug|Release|RelWithDebInfo|MinSizeRel] -DCMAKE_INSTALL_PREFIX=/path/to/installdir /path/to/amrex make install make test_install # optional step to test if the installation is working + make uninstall # optional step to remove files from install_manifest.txt In the above snippet, ``[options]`` indicates one or more options for the customization of the build, as described in the subsection on @@ -391,6 +392,10 @@ customization of the build, as described in the subsection on build directory, we advise against doing so. After the installation is complete, ``builddir`` can be removed. +The ``uninstall`` target removes files listed in ``install_manifest.txt``. +For staged installs, use the same ``DESTDIR`` setting that was used for the +install step. + .. _sec:build:cmake:options: diff --git a/Tools/CMake/AMReXInstallHelpers.cmake b/Tools/CMake/AMReXInstallHelpers.cmake index 5c7d556a02..2dc8f257ad 100644 --- a/Tools/CMake/AMReXInstallHelpers.cmake +++ b/Tools/CMake/AMReXInstallHelpers.cmake @@ -185,3 +185,22 @@ macro( add_test_install_target _dir _amrex_root ) ) endmacro() + +# +# Add uninstall target +# +# _amrex_root: Root directory of AMReX (contains Tools/CMake/) +# +macro(add_uninstall_target _amrex_root) + if(NOT TARGET uninstall) + configure_file( + "${_amrex_root}/Tools/CMake/AMReX_cmake_uninstall.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 + COMMENT "Uninstalling files listed in install_manifest.txt" + ) + endif() +endmacro() diff --git a/Tools/CMake/AMReX_cmake_uninstall.in b/Tools/CMake/AMReX_cmake_uninstall.in new file mode 100644 index 0000000000..830a6c6230 --- /dev/null +++ b/Tools/CMake/AMReX_cmake_uninstall.in @@ -0,0 +1,25 @@ +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}") + execute_process( + COMMAND "@CMAKE_COMMAND@" -E remove "$ENV{DESTDIR}${file}" + RESULT_VARIABLE rm_retval + OUTPUT_VARIABLE rm_out + ERROR_VARIABLE rm_err + ) + if(NOT "${rm_retval}" STREQUAL "0") + message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}: ${rm_err}") + endif() + else() + message(STATUS "File $ENV{DESTDIR}${file} does not exist.") + endif() +endforeach() + +message(STATUS "Uninstall complete") \ No newline at end of file