Skip to content

Commit 6f6e939

Browse files
acxzhenryiii
andauthored
feat: add uninstall target for CMake (#2265)
* add uninstall target for cmake * only add target when built as master project Co-authored-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent 94db5c5 commit 6f6e939

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ MANIFEST
3232
.DS_Store
3333
/dist
3434
/build
35-
/cmake/
3635
.cache/
3736
sosize-*.txt
3837
pybind11Config*.cmake

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,12 @@ if(PYBIND11_INSTALL)
246246
NAMESPACE "pybind11::"
247247
DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
248248
endif()
249+
250+
# Uninstall target
251+
if(PYBIND11_MASTER_PROJECT)
252+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
253+
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)
254+
255+
add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P
256+
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
257+
endif()

cmake/cmake_uninstall.cmake.in

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Source: https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#can-i-do-make-uninstall-with-cmake
2+
3+
if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
4+
message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt")
5+
endif()
6+
7+
file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files)
8+
string(REGEX REPLACE "\n" ";" files "${files}")
9+
foreach(file ${files})
10+
message(STATUS "Uninstalling $ENV{DESTDIR}${file}")
11+
if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
12+
exec_program(
13+
"@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
14+
OUTPUT_VARIABLE rm_out
15+
RETURN_VALUE rm_retval
16+
)
17+
if(NOT "${rm_retval}" STREQUAL 0)
18+
message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}")
19+
endif()
20+
else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
21+
message(STATUS "File $ENV{DESTDIR}${file} does not exist.")
22+
endif()
23+
endforeach()

0 commit comments

Comments
 (0)