-
Notifications
You must be signed in to change notification settings - Fork 454
Refactor uninstall-script generation #1380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vector-of-bool
merged 10 commits into
mongodb:master
from
vector-of-bool:cleanup-uninstall
Aug 16, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
58fb873
New CMake-based uninstall generation
vector-of-bool 3ce5c91
Windows Batch and DESTDIR compatibility
vector-of-bool f18552f
Formatting tweaks
vector-of-bool d42c688
Uninstaller needs to remove itself.
vector-of-bool acf8ef3
Fix env expansion order, messaging
vector-of-bool e7f89c5
No longer used
vector-of-bool ee41027
Spell and format
vector-of-bool 5c0b5e7
Fix: Remove symlinks during uninstall
vector-of-bool e097eda
Fix: Don't use genex in places where old CMake can't use it
vector-of-bool 6479103
PR review: Spellingg
vector-of-bool File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,271 @@ | ||
cmake_policy(VERSION 3.15) | ||
|
||
if(NOT CMAKE_SCRIPT_MODE_FILE) | ||
# We are being included from within a project, so we should generate the install rules | ||
# The script name is "uninstall" by default: | ||
if(NOT DEFINED UNINSTALL_SCRIPT_NAME) | ||
set(UNINSTALL_SCRIPT_NAME "uninstall") | ||
endif() | ||
# We need a directory where we should install the script: | ||
if(NOT UNINSTALL_PROG_DIR) | ||
message(SEND_ERROR "We require an UNINSTALL_PROG_DIR to be defined") | ||
endif() | ||
# Platform dependent values: | ||
if(WIN32) | ||
set(_script_ext "cmd") | ||
set(_script_runner cmd.exe /c) | ||
else() | ||
set(_script_ext "sh") | ||
set(_script_runner sh -e -u) | ||
endif() | ||
# The script filename and path: | ||
set(_script_filename "${UNINSTALL_SCRIPT_NAME}.${_script_ext}") | ||
get_filename_component(_uninstaller_script "${CMAKE_CURRENT_BINARY_DIR}/${_script_filename}" ABSOLUTE) | ||
# Code that will do the work at install-time: | ||
string(CONFIGURE [==[ | ||
function(__generate_uninstall) | ||
set(UNINSTALL_IS_WIN32 "@WIN32@") | ||
set(UNINSTALL_WRITE_FILE "@_uninstaller_script@") | ||
set(UNINSTALL_SCRIPT_SELF "@UNINSTALL_PROG_DIR@/@_script_filename@") | ||
include("@CMAKE_CURRENT_LIST_FILE@") | ||
endfunction() | ||
__generate_uninstall() | ||
]==] code @ONLY ESCAPE_QUOTES) | ||
install(CODE "${code}") | ||
# Add a rule to install that file: | ||
install( | ||
FILES "${_uninstaller_script}" | ||
DESTINATION "${UNINSTALL_PROG_DIR}" | ||
PERMISSIONS | ||
OWNER_READ OWNER_WRITE OWNER_EXECUTE | ||
GROUP_READ GROUP_EXECUTE | ||
WORLD_READ WORLD_EXECUTE | ||
) | ||
|
||
# If applicable, generate an "uninstall" target to run the uninstaller: | ||
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR OR PROJECT_IS_TOP_LEVEL) | ||
add_custom_target( | ||
uninstall | ||
COMMAND ${_script_runner} "${_uninstaller_script}" | ||
COMMENT Uninstalling... | ||
) | ||
endif() | ||
# Stop here: The rest of the file is for install-time | ||
return() | ||
endif() | ||
|
||
# We get here if running in script mode (e.g. at CMake install-time) | ||
if(NOT DEFINED CMAKE_INSTALL_MANIFEST_FILES) | ||
message(FATAL_ERROR "This file is only for use with CMake's install(CODE/SCRIPT) command") | ||
endif() | ||
if(NOT DEFINED UNINSTALL_WRITE_FILE) | ||
message(FATAL_ERROR "Expected a variable “UNINSTALL_WRITE_FILE” to be defined") | ||
endif() | ||
|
||
# Clear out the uninstall script before we begin writing: | ||
file(WRITE "${UNINSTALL_WRITE_FILE}" "") | ||
|
||
# Append a line to the uninstall script file. Single quotes will be replaced with doubles, | ||
# and an appropriate newline will be added. | ||
function(append_line line) | ||
string(REPLACE "'" "\"" line "${line}") | ||
file(APPEND "${UNINSTALL_WRITE_FILE}" "${line}\n") | ||
endfunction() | ||
|
||
# The copyright header: | ||
set(header [[ | ||
Mongo C Driver uninstall program, generated with CMake | ||
|
||
Copyright 2018-present MongoDB, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the \"License\"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an \"AS IS\" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
]]) | ||
string(STRIP header "${header}") | ||
string(REPLACE "\n" ";" header_lines "${header}") | ||
|
||
# Prefix for the Batch script: | ||
set(bat_preamble [[ | ||
call :init | ||
|
||
<nul set /p_=%~1 | ||
exit /b | ||
|
||
:rmfile | ||
set f=%__prefix%\%~1 | ||
call :print "Remove file %f% " | ||
if EXIST "%f%" ( | ||
del /Q /F "%f%" || exit /b %errorlevel% | ||
call :print " - ok" | ||
) else ( | ||
call :print " - skipped: not present" | ||
) | ||
echo( | ||
exit /b | ||
|
||
:rmdir | ||
set f=%__prefix%\%~1 | ||
call :print "Remove directory: %f% " | ||
if EXIST "%f%" ( | ||
rmdir /Q "%f%" 2>nul | ||
if ERRORLEVEL 0 ( | ||
call :print "- ok" | ||
) else ( | ||
call :print "- skipped (non-empty?)" | ||
) | ||
) else ( | ||
call :print " - skipped: not present" | ||
) | ||
echo( | ||
exit /b | ||
|
||
:init | ||
setlocal EnableDelayedExpansion | ||
setlocal EnableExtensions | ||
if /i "%~dp0" NEQ "%TEMP%\" ( | ||
set tmpfile=%TEMP%\mongoc-%~nx0 | ||
copy "%~f0" "!tmpfile!" >nul | ||
call "!tmpfile!" & del "!tmpfile!" | ||
exit /b | ||
) | ||
]]) | ||
|
||
# Prefix for the shell script: | ||
set(sh_preamble [[ | ||
set -eu | ||
|
||
__rmfile() { | ||
set -eu | ||
abs=$__prefix/$1 | ||
printf "Remove file %s: " "$abs" | ||
if test -f "$abs" || test -L "$abs" | ||
then | ||
rm -- "$abs" | ||
echo "ok" | ||
else | ||
echo "skipped: not present" | ||
fi | ||
} | ||
|
||
__rmdir() { | ||
set -eu | ||
abs=$__prefix/$1 | ||
printf "Remove directory %s: " "$abs" | ||
if test -d "$abs" | ||
then | ||
list=$(ls --almost-all "$abs") | ||
if test "$list" = "" | ||
then | ||
rmdir -- "$abs" | ||
echo "ok" | ||
else | ||
echo "skipped: not empty" | ||
fi | ||
else | ||
echo "skipped: not present" | ||
fi | ||
} | ||
]]) | ||
|
||
# Convert the install prefix to an absolute path with the native path format: | ||
get_filename_component(install_prefix "${CMAKE_INSTALL_PREFIX}" ABSOLUTE) | ||
file(TO_NATIVE_PATH "${install_prefix}" install_prefix) | ||
# Handling DESTDIR requires careful handling of root path redirection: | ||
set(root_path) | ||
set(relative_prefix "${install_prefix}") | ||
if(COMMAND cmake_path) | ||
cmake_path(GET install_prefix ROOT_PATH root_path) | ||
cmake_path(GET install_prefix RELATIVE_PART relative_prefix) | ||
endif() | ||
|
||
# The first lines that will be written to the script: | ||
set(init_lines) | ||
|
||
if(UNINSTALL_IS_WIN32) | ||
# Comment the header: | ||
list(TRANSFORM header_lines PREPEND "rem ") | ||
# Add the preamble | ||
list(APPEND init_lines | ||
"@echo off" | ||
"${header_lines}" | ||
"${bat_preamble}" | ||
"if \"%DESTDIR%\"==\"\" (" | ||
" set __prefix=${install_prefix}" | ||
") else (" | ||
" set __prefix=!DESTDIR!\\${relative_prefix}" | ||
")" | ||
"") | ||
set(__rmfile "call :rmfile") | ||
set(__rmdir "call :rmdir") | ||
else() | ||
# Comment the header: | ||
list(TRANSFORM header_lines PREPEND "# * ") | ||
# Add the preamble | ||
list(APPEND init_lines | ||
"#!/bin/sh" | ||
"${header_lines}" | ||
"${sh_preamble}" | ||
"__prefix=\${DESTDIR:-}${install_prefix}" | ||
"") | ||
set(__rmfile "__rmfile") | ||
set(__rmdir "__rmdir") | ||
endif() | ||
|
||
# Add the first lines to the file: | ||
string(REPLACE ";" "\n" init "${init_lines}") | ||
append_line("${init}") | ||
|
||
# Generate a "remove a file" command | ||
function(add_rmfile filename) | ||
file(TO_NATIVE_PATH "${filename}" native) | ||
append_line("${__rmfile} '${native}'") | ||
endfunction() | ||
|
||
# Generate a "remove a directory" command | ||
function(add_rmdir dirname) | ||
file(TO_NATIVE_PATH "${dirname}" native) | ||
append_line("${__rmdir} '${native}'") | ||
endfunction() | ||
|
||
set(script_self "${install_prefix}/${UNINSTALL_SCRIPT_SELF}") | ||
set(dirs_to_remove) | ||
foreach(installed IN LISTS CMAKE_INSTALL_MANIFEST_FILES script_self) | ||
# Get the relative path from the prefix (the uninstaller will fix it up later) | ||
file(RELATIVE_PATH relpath "${install_prefix}" "${installed}") | ||
# Add a removal: | ||
add_rmfile("${relpath}") | ||
# Climb the path and collect directories: | ||
while("1") | ||
get_filename_component(installed "${installed}" DIRECTORY) | ||
file(TO_NATIVE_PATH "${installed}" installed) | ||
get_filename_component(parent "${installed}" DIRECTORY) | ||
file(TO_NATIVE_PATH "${parent}" parent) | ||
# Don't account for the prefix or direct children of the prefix: | ||
if(installed STREQUAL install_prefix OR parent STREQUAL install_prefix) | ||
break() | ||
endif() | ||
# Keep track of this directory for later: | ||
list(APPEND dirs_to_remove "${installed}") | ||
endwhile() | ||
endforeach() | ||
|
||
# Now generate commands to remove (empty) directories: | ||
list(REMOVE_DUPLICATES dirs_to_remove) | ||
# Order them by depth so that we remove subdirectories before their parents: | ||
list(SORT dirs_to_remove ORDER DESCENDING) | ||
foreach(dir IN LISTS dirs_to_remove) | ||
file(RELATIVE_PATH relpath "${install_prefix}" "${dir}") | ||
add_rmdir("${relpath}") | ||
endforeach() | ||
|
||
message(STATUS "Generated uninstaller: ${UNINSTALL_WRITE_FILE}") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this how a collision is prevented between the uninstall generated in this project and, for instance, the uninstall generated in the C++ driver when it includes the C driver directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. This is mostly the same as what was done previously, but with the addition of the newer CMake feature
PROJECT_IS_TOP_LEVEL
, which was added to address this exact use case (on older CMake, that variable check will be a no-op).