Skip to content

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
merged 10 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -550,26 +550,16 @@ install (FILES COPYING NEWS README.rst THIRD_PARTY_NOTICES
)

if (ENABLE_UNINSTALL)
if (WIN32)
if (ENABLE_MONGOC)
set (UNINSTALL_PROG "uninstall.cmd")
else ()
set (UNINSTALL_PROG "uninstall-bson.cmd")
endif ()
else ()
if (ENABLE_MONGOC)
set (UNINSTALL_PROG "uninstall.sh")
else ()
set (UNINSTALL_PROG "uninstall-bson.sh")
endif ()
endif ()
set (UNINSTALL_PROG_DIR "${CMAKE_INSTALL_DATADIR}/mongo-c-driver")

# Create uninstall program and associated uninstall target
#
# This needs to be last (after all other add_subdirectory calls) to ensure that
# the generated uninstall program is complete and correct
add_subdirectory (generate_uninstall)
if (NOT ENABLE_MONGOC)
# Generate a different script name for uninstalling libbson only:
set (UNINSTALL_SCRIPT_NAME "uninstall-bson")
endif ()
set (UNINSTALL_PROG_DIR "${CMAKE_INSTALL_DATADIR}/mongo-c-driver")
include (GenerateUninstaller)
endif ()

# Spit out some information regarding the generated build system
Expand Down
17 changes: 16 additions & 1 deletion build/cmake/GeneratePkgConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ All named parameters accept generator expressions.

]==]
function(mongo_generate_pkg_config target)
list(APPEND CMAKE_MESSAGE_CONTEXT "mongo_generate_pkg_config" "${target}")
# Collect some target properties:
# The name:
_genex_escape(proj_name "${PROJECT_NAME}")
Expand Down Expand Up @@ -148,17 +149,20 @@ function(mongo_generate_pkg_config target)
set(ARG_FILENAME "$<TARGET_FILE_BASE_NAME:${target}>.pc")
endif()
endif()
message(DEBUG "FILENAME: ${ARG_FILENAME}")

# The defalut CONDITION is just "1" (true)
if(NOT DEFINED ARG_CONDITION)
set(ARG_CONDITION 1)
endif()
message(DEBUG "CONDITION: ${ARG_CONDITION}")
_bind_genex_to_target(gx_cond ${target} "${ARG_CONDITION}")

# The default LIBDIR comes from GNUInstallDirs.cmake
if(NOT ARG_LIBDIR)
set(ARG_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
endif()
message(DEBUG "LIBDIR: ${ARG_LIBDIR}")
_bind_genex_to_target(gx_libdir ${target} "${ARG_LIBDIR}")

# Evaluate the filename genex in the context of the target:
Expand All @@ -168,6 +172,7 @@ function(mongo_generate_pkg_config target)
else()
get_filename_component(gx_output "${CMAKE_CURRENT_BINARY_DIR}/${gx_filename}" ABSOLUTE)
endif()
message(DEBUG "Generating build-tree file: ${gx_output}")

# Generate the content of the file:
_generate_pkg_config_content(content
Expand All @@ -187,13 +192,15 @@ function(mongo_generate_pkg_config target)
CONDITION "${gx_cond}")
if(NOT "INSTALL" IN_LIST ARGN)
# Nothing more to do here.
message(DEBUG "(Not installing)")
return()
endif()

# Installation handling:
# Use file(GENERATE) to generate a temporary file to be picked up at install-time.
# (For some reason, injecting the content directly into install(CODE) fails in corner cases)
set(gx_tmpfile "${CMAKE_CURRENT_BINARY_DIR}/_pkgconfig/${target}-$<LOWER_CASE:$<CONFIG>>-for-install.txt")
message(DEBUG "Generate for-install: ${gx_tmpfile}")
file(GENERATE OUTPUT "${gx_tmpfile}"
CONTENT "${gx_content}"
CONDITION "${gx_cond}")
Expand All @@ -206,6 +213,8 @@ function(mongo_generate_pkg_config target)
if(NOT DEFINED inst_RENAME)
set(inst_RENAME "${ARG_FILENAME}")
endif()
message(DEBUG "INSTALL DESTINATION: ${inst_DESTINATION}")
message(DEBUG "INSTALL RENAME: ${inst_RENAME}")
# install(CODE) will write a simple temporary file:
set(inst_tmp "${CMAKE_CURRENT_BINARY_DIR}/${target}-pkg-config-tmp.txt")
_genex_escape(esc_cond "${ARG_CONDITION}")
Expand All @@ -226,7 +235,13 @@ function(mongo_generate_pkg_config target)
]==] code @ONLY)
install(CODE "${code}")
_bind_genex_to_target(gx_dest ${target} "${inst_DESTINATION}")
_bind_genex_to_target(gx_rename ${target} "${inst_RENAME}")
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20")
_bind_genex_to_target(gx_rename ${target} "${inst_RENAME}")
else()
# Note: CMake 3.20 is required for using generator expresssions in install(RENAME).
# if we are older than that, just treat RENAME as a plain value.
set(gx_rename "${inst_RENAME}")
endif()
# Wrap the filename to install with the same condition used to generate it. If the condition
# is not met, then the FILES list will be empty, and nothing will be installed.
install(FILES "$<${gx_cond}:${inst_tmp}>"
Expand Down
271 changes: 271 additions & 0 deletions build/cmake/GenerateUninstaller.cmake
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)
Copy link
Contributor

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?

Copy link
Contributor Author

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).

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

:print
<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}")
Loading