From 3557ab833545e5f4657d3391f3b833ba4aaaeacc Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sat, 4 Apr 2026 14:34:57 -0400 Subject: [PATCH] build: + --clobber feature for .build/reconfigure script copies reconfigure script to /tmp before killing build directory. --- share/xo-macros/xo-reconfigure.in | 76 ++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/share/xo-macros/xo-reconfigure.in b/share/xo-macros/xo-reconfigure.in index d3e55e55..9078b4d3 100644 --- a/share/xo-macros/xo-reconfigure.in +++ b/share/xo-macros/xo-reconfigure.in @@ -3,20 +3,35 @@ # Generated by cmake - rerun to reconfigure dry_run_flag=false +clobber=false testing=@ENABLE_TESTING@ +orig_path=@CMAKE_BINARY_DIR@/reconfigure +self_path=$(cd $(dirname $0) && pwd)/$(basename $0) + # Parse arguments while [[ $# -gt 0 ]]; do case "$1" in -n|--dry-run) dry_run_flag=true ;; + --print-source-dir) + echo @CMAKE_SOURCE_DIR@ + exit 0 + ;; + --print-build-dir) + echo @CMAKE_BINARY_DIR@ + exit 0 + ;; --enable-testing) testing=1 ;; --disable-testing) testing=0 ;; + --clobber) + clobber=true + ;; --) break; ;; @@ -24,8 +39,14 @@ while [[ $# -gt 0 ]]; do echo "error: xo-reconfigure: unexpected argument [$1]" echo "usage: xo-reconfigure" echo " xo-reconfigure [-n|--dry-run]" + echo " [--print-source-dir|--print-build-dir]" + echo " [--clobber]" echo " [--enable-testing|--disable-testing]" echo " -- CMAKE_ARGS" + echo "" + echo "1. clobber ignores --enable-testing/--disable-testing flags" + echo " use to cleanse build directory when stale build artifacts" + echo " are confusing cmake" exit 1 esac @@ -54,11 +75,54 @@ CMAKE_CMD=( "${@}" ) -if [[ ${dry_run_flag} = true ]]; then - # Print the command with proper quoting for copy-paste - printf '%q ' "${CMAKE_CMD[@]}" | sed -e 's: -D: \\\n -D:g' - printf '\n' +if [[ ${clobber} == true ]]; then + if [[ ${dry_run_flag} == true ]]; then + 2>&1 echo "would clobber directory [@CMAKE_BINARY_DIR@], then run reconfigure:" + 2>&1 echo + else + if [[ ${orig_path} == ${self_path} ]]; then + # must copy self out of doomed build directory before we clobber + + # 1. rescue reconfigure script from build directory + tmpfile=$(mktemp /tmp/reconfigure-XXX) + cp ${self_path} ${tmpfile} + + # 2. copy needs to be executable + chmod +x ${tmpfile} + + # 2. run reconfigure from non-build directory + exec ${tmpfile} --clobber + else + # running outside build directory + + 2>&1 echo "removing build directory [@CMAKE_BINARY_DIR@]" + rm -rf @CMAKE_BINARY_DIR@ + + 2>&1 echo "run cmake:" + 2>&1 echo "${CMAKE_CMD[@]}" + 2>&1 echo + + # run reconfigure + "${CMAKE_CMD[@]}" + + if [[ $(realpath ${self_path}) == /tmp/* ]]; then + # since we're in /tmp, delete ourselves as final step + rm -f ${self_path} + fi + + # rehearse cmake command for visibility + 2>&1 echo "" + 2>&1 echo "cmake command was:" + 2>&1 echo "${CMAKE_CMD[@]}" + fi + fi else - # Execute the command - "${CMAKE_CMD[@]}" + if [[ ${dry_run_flag} = true ]]; then + # Print the command with proper quoting for copy-paste + printf '%q ' "${CMAKE_CMD[@]}" | sed -e 's: -D: \\\n -D:g' + printf '\n' + else + # Execute the command + "${CMAKE_CMD[@]}" + fi fi