Subject: Re: [PATCH v2 4/5] cmake: avoid editing t/test-lib.sh

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Tue, 18 Oct 2022 17:09:44 +0200

On Tue, Oct 18 2022, Johannes Schindelin wrote:

> Hi Ævar,
>
> On Thu, 8 Sep 2022, Ævar Arnfjörð Bjarmason wrote:
>
>> On Tue, Aug 23 2022, Johannes Schindelin via GitGitGadget wrote:
>>
>> > From: Johannes Schindelin <[email-masked]>
>> >
>> > In 7f5397a07c6c (cmake: support for testing git when building out of the
>> > source tree, 2020-06-26), we implemented support for running Git's test
>> > scripts even after building Git in a different directory than the source
>> > directory.
>> >
>> > The way we did this was to edit the file `t/test-lib.sh` to override
>> > `GIT_BUILD_DIR` to point somewhere else than the parent of the `t/`
>> > directory.
>> >
>> > This is unideal because it always leaves a tracked file marked as
>> > modified, and it is all too easy to commit that change by mistake.
>> >
>> > Let's change the strategy by teaching `t/test-lib.sh` to detect the
>> > presence of a file called `GIT-BUILD-DIR` in the source directory. If it
>> > exists, the contents are interpreted as the location to the _actual_
>> > build directory. We then write this file as part of the CTest
>> > definition.
>> >
>> > To support building Git via a regular `make` invocation after building
>> > it using CMake, we ensure that the `GIT-BUILD-DIR` file is deleted (for
>> > convenience, this is done as part of the Makefile rule that is already
>> > run with every `make` invocation to ensure that `GIT-BUILD-OPTIONS` is
>> > up to date).
>> >
>> > Signed-off-by: Johannes Schindelin <[email-masked]>
>> > ---
>> >  .gitignore                          |  1 +
>> >  Makefile                            |  1 +
>> >  contrib/buildsystems/CMakeLists.txt |  7 +------
>> >  t/test-lib.sh                       | 11 ++++++++++-
>> >  4 files changed, 13 insertions(+), 7 deletions(-)
>> >
>> > diff --git a/.gitignore b/.gitignore
>> > index a4522157641..b72ddf09346 100644
>> > --- a/.gitignore
>> > +++ b/.gitignore
>> > @@ -2,6 +2,7 @@
>> >  /fuzz_corpora
>> >  /fuzz-pack-headers
>> >  /fuzz-pack-idx
>> > +/GIT-BUILD-DIR
>> >  /GIT-BUILD-OPTIONS
>> >  /GIT-CFLAGS
>> >  /GIT-LDFLAGS
>> > diff --git a/Makefile b/Makefile
>> > index 04d0fd1fe60..9347ed90da7 100644
>> > --- a/Makefile
>> > +++ b/Makefile
>> > @@ -3028,6 +3028,7 @@ else
>> >  	@echo RUNTIME_PREFIX=\'false\' >>$@+
>> >  endif
>> >  	@if cmp $@+ $@ >/dev/null 2>&1; then $(RM) $@+; else mv $@+ $@; fi
>> > +	@if test -f GIT-BUILD-DIR; then rm GIT-BUILD-DIR; fi
>> >
>> >  ### Detect Python interpreter path changes
>> >  ifndef NO_PYTHON
>> > diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt
>> > index fe606c179f7..29d7e236ae1 100644
>> > --- a/contrib/buildsystems/CMakeLists.txt
>> > +++ b/contrib/buildsystems/CMakeLists.txt
>> > @@ -1067,14 +1067,9 @@ endif()
>> >  #Make the tests work when building out of the source tree
>> >  get_filename_component(CACHE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../CMakeCache.txt ABSOLUTE)
>> >  if(NOT ${CMAKE_BINARY_DIR}/CMakeCache.txt STREQUAL ${CACHE_PATH})
>> > -	file(RELATIVE_PATH BUILD_DIR_RELATIVE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}/CMakeCache.txt)
>> > -	string(REPLACE "/CMakeCache.txt" "" BUILD_DIR_RELATIVE ${BUILD_DIR_RELATIVE})
>> >  	#Setting the build directory in test-lib.sh before running tests
>> >  	file(WRITE ${CMAKE_BINARY_DIR}/CTestCustom.cmake
>> > -		"file(STRINGS ${CMAKE_SOURCE_DIR}/t/test-lib.sh GIT_BUILD_DIR_REPL REGEX \"GIT_BUILD_DIR=(.*)\")\n"
>> > -		"file(STRINGS ${CMAKE_SOURCE_DIR}/t/test-lib.sh content NEWLINE_CONSUME)\n"
>> > -		"string(REPLACE \"\${GIT_BUILD_DIR_REPL}\" \"GIT_BUILD_DIR=\\\"$TEST_DIRECTORY/../${BUILD_DIR_RELATIVE}\\\"\" content \"\${content}\")\n"
>> > -		"file(WRITE ${CMAKE_SOURCE_DIR}/t/test-lib.sh \${content})")
>> > +		"file(WRITE ${CMAKE_SOURCE_DIR}/GIT-BUILD-DIR \"${CMAKE_BINARY_DIR}\")")
>> >  	#misc copies
>> >  	file(COPY ${CMAKE_SOURCE_DIR}/t/chainlint.sed DESTINATION ${CMAKE_BINARY_DIR}/t/)
>> >  	file(COPY ${CMAKE_SOURCE_DIR}/po/is.po DESTINATION ${CMAKE_BINARY_DIR}/po/)
>> > diff --git a/t/test-lib.sh b/t/test-lib.sh
>> > index 55857af601b..4468ac51f25 100644
>> > --- a/t/test-lib.sh
>> > +++ b/t/test-lib.sh
>> > @@ -42,7 +42,16 @@ then
>> >  	TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
>> >  fi
>> >  GIT_BUILD_DIR="${TEST_DIRECTORY%/t}"
>> > -if test "$TEST_DIRECTORY" = "$GIT_BUILD_DIR"
>> > +if test -f "$GIT_BUILD_DIR/GIT-BUILD-DIR"
>> > +then
>> > +	GIT_BUILD_DIR="$(cat "$GIT_BUILD_DIR/GIT-BUILD-DIR")" || exit 1
>> > +	# On Windows, we must convert Windows paths lest they contain a colon
>> > +	case "$(uname -s)" in
>> > +	*MINGW*)
>> > +		GIT_BUILD_DIR="$(cygpath -au "$GIT_BUILD_DIR")"
>> > +		;;
>> > +	esac
>> > +elif test "$TEST_DIRECTORY" = "$GIT_BUILD_DIR"
>> >  then
>> >  	echo "PANIC: Running in a $TEST_DIRECTORY that doesn't end in '/t'?" >&2
>> >  	exit 1
>>
>> As pointed out in the v1 this breaks the cmake<->make interaction in
>> some scenarios, but from some brief testing there seemed to be an easy
>> workaround which didn't suffer from that problem:
>> https://lore.kernel.org/git/[email-masked]/
>
> I do not think that the CMake <-> make interaction will come up in any
> other scenario than your and my tests in the context of this mailing list
> thread.
>
> Therefore, I am certain that we need not cater to that scenario at all.

I run it like that now when I build locally, and it slots nicely in with:

 	1. First run a bunch of 'build' targets
	2. Then run a bunch of 'test' targets

I'm not opposed to having this e.g. as a special-case on Windows, or
that we pick up when you *only* have cmake/ctest unconditionally, and
auto-do the right thing.

But I do think that:

	A. Getting a "run test-lib against this build dir over there" is
	   *different* from the bundled behavior of the auto-flip-flopping.

	B. Your case for doing it this way seems to be the
	   Windows-specific case noted in 1/5, can't we just make that
	   part Windows-specific then? I don't get why it needs to be
	   bundled up with cmake everywhere....