Skip to content

Commit 233d78c

Browse files
authored
CDRIVER-6062 fix Windows uninstall batch script behavior (#2075)
1 parent d4e08e5 commit 233d78c

File tree

2 files changed

+36
-61
lines changed

2 files changed

+36
-61
lines changed

.evergreen/scripts/compile-std.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ touch "${mongoc_install_dir:?}/${lib_dir:?}/canary.txt"
210210

211211
# Linux/MacOS: uninstall.sh
212212
# Windows: uninstall.cmd
213-
"${cmake_binary}" --build "${mongoc_build_dir:?}" --target uninstall || true # CDRIVER-6062
213+
"${cmake_binary}" --build "${mongoc_build_dir:?}" --target uninstall
214214

215215
# No files should remain except canary.txt.
216216
# No directories except top-level directories should remain.

build/cmake/GenerateUninstaller.cmake

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -120,53 +120,6 @@ limitations under the License.]])
120120
string(STRIP header "${header}")
121121
string(REPLACE "\n" ";" header_lines "${header}")
122122

123-
# Prefix for the Batch script:
124-
set(bat_preamble [[
125-
call :init
126-
127-
:print
128-
<nul set /p_=%~1
129-
exit /b
130-
131-
:rmfile
132-
set f=%__prefix%\%~1
133-
call :print "Remove file %f% "
134-
if EXIST "%f%" (
135-
del /Q /F "%f%" || exit /b %errorlevel%
136-
call :print " - ok"
137-
) else (
138-
call :print " - skipped: not present"
139-
)
140-
echo(
141-
exit /b
142-
143-
:rmdir
144-
set f=%__prefix%\%~1
145-
call :print "Remove directory: %f% "
146-
if EXIST "%f%" (
147-
rmdir /Q "%f%" 2>nul
148-
if ERRORLEVEL 0 (
149-
call :print "- ok"
150-
) else (
151-
call :print "- skipped (non-empty?)"
152-
)
153-
) else (
154-
call :print " - skipped: not present"
155-
)
156-
echo(
157-
exit /b
158-
159-
:init
160-
setlocal EnableDelayedExpansion
161-
setlocal EnableExtensions
162-
if /i "%~dp0" NEQ "%TEMP%\" (
163-
set tmpfile=%TEMP%\mongoc-%~nx0
164-
copy "%~f0" "!tmpfile!" >nul
165-
call "!tmpfile!" & del "!tmpfile!"
166-
exit /b
167-
)
168-
]])
169-
170123
# Prefix for the shell script:
171124
set(sh_preamble [[
172125
set -eu
@@ -225,15 +178,9 @@ if(UNINSTALL_IS_WIN32)
225178
""
226179
"${header_lines}"
227180
""
228-
"${bat_preamble}"
229-
"if \"%DESTDIR%\"==\"\" ("
230-
" set __prefix=${install_prefix}"
231-
") else ("
232-
" set __prefix=!DESTDIR!\\${relative_prefix}"
233-
")"
234-
"")
235-
set(__rmfile "call :rmfile")
236-
set(__rmdir "call :rmdir")
181+
"if \"%DESTDIR%\"==\"\" (set __prefix=${install_prefix}) else (set __prefix=!DESTDIR!\\${relative_prefix})"
182+
""
183+
"(GOTO) 2>nul & (")
237184
else()
238185
# Comment the header:
239186
list(TRANSFORM header_lines PREPEND "# " REGEX "^.+$")
@@ -247,8 +194,6 @@ else()
247194
"${sh_preamble}"
248195
"__prefix=\${DESTDIR:-}${install_prefix}"
249196
"")
250-
set(__rmfile "__rmfile")
251-
set(__rmdir "__rmdir")
252197
endif()
253198

254199
# Add the first lines to the file:
@@ -258,13 +203,37 @@ append_line("${init}")
258203
# Generate a "remove a file" command
259204
function(add_rmfile filename)
260205
file(TO_NATIVE_PATH "${filename}" native)
261-
append_line("${__rmfile} '${native}'")
206+
if(WIN32)
207+
set(file "%__prefix%\\${native}")
208+
set(rmfile_lines
209+
" <nul set /p \"=Remove file: ${file} \""
210+
" if EXIST \"${file}\" ("
211+
" del /Q /F \"${file}\" && echo - ok"
212+
" ) ELSE echo - skipped: not present"
213+
") && (")
214+
string(REPLACE ";" "\n" rmfile "${rmfile_lines}")
215+
append_line("${rmfile}")
216+
else()
217+
append_line("__rmfile '${native}'")
218+
endif()
262219
endfunction()
263220

264221
# Generate a "remove a directory" command
265222
function(add_rmdir dirname)
266223
file(TO_NATIVE_PATH "${dirname}" native)
267-
append_line("${__rmdir} '${native}'")
224+
if(WIN32)
225+
set(dir "%__prefix%\\${native}")
226+
set(rmdir_lines
227+
" <nul set /p \"=Remove directory: ${dir} \""
228+
" if EXIST \"${dir}\" ("
229+
" rmdir /Q \"${dir}\" 2>nul && echo - ok || echo - skipped ^(non-empty?^)"
230+
" ) ELSE echo - skipped: not present"
231+
") && (")
232+
string(REPLACE ";" "\n" rmdir "${rmdir_lines}")
233+
append_line("${rmdir}")
234+
else()
235+
append_line("__rmdir '${native}'")
236+
endif()
268237
endfunction()
269238

270239
set(script_self "${install_prefix}/${UNINSTALL_SCRIPT_SELF}")
@@ -298,4 +267,10 @@ foreach(dir IN LISTS dirs_to_remove)
298267
add_rmdir("${relpath}")
299268
endforeach()
300269

270+
# Allow the batch script delete itself without error.
271+
if(WIN32)
272+
append_line(" ver>nul")
273+
append_line(")")
274+
endif()
275+
301276
message(STATUS "Generated uninstaller: ${UNINSTALL_WRITE_FILE}")

0 commit comments

Comments
 (0)