Makefile: extract script to massage Python scripts

Extract a script that massages Python scripts. This provides a couple of benefits: - The build logic is deduplicated across Make, CMake and Meson. - CMake learns to rewrite scripts as-needed at build time instead of only writing them at configure time. Furthermore, we will use this script when introducing Meson to deduplicate the logic across build systems. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 6, 2024 at 14:24 UTC b7835b941bd437aa770ab91d7323ce74d2bd81b7
3 files changed +33 -10
Makefile
+2 -6
@@ -2635,13 +2635,9 @@ endif # NO_PERL
2635 $(SCRIPT_PYTHON_GEN): GIT-BUILD-OPTIONS
2636
2637 ifndef NO_PYTHON
2638 -$(SCRIPT_PYTHON_GEN): GIT-CFLAGS GIT-PREFIX GIT-PYTHON-VARS
2638 +$(SCRIPT_PYTHON_GEN): generate-python.sh
2639 $(SCRIPT_PYTHON_GEN): % : %.py
2640 - $(QUIET_GEN) \
2641 - sed -e '1s|#!.*python|#!$(PYTHON_PATH_SQ)|' \
2642 - $< >$@+ && \
2643 - chmod +x $@+ && \
2644 - mv $@+ $@
2640 + $(QUIET_GEN)$(SHELL_PATH) generate-python.sh ./GIT-BUILD-OPTIONS "$<" "$@"
2641 else # NO_PYTHON
2642 $(SCRIPT_PYTHON_GEN): % : unimplemented.sh
2643 $(QUIET_GEN) \
contrib/buildsystems/CMakeLists.txt
+11 -4
@@ -899,10 +899,17 @@ foreach(script ${git_perl_scripts} ${perl_modules})
899 endforeach()
900 add_custom_target(perl-gen ALL DEPENDS ${perl_gen})
901
902 -#python script
903 -file(STRINGS ${CMAKE_SOURCE_DIR}/git-p4.py content NEWLINE_CONSUME)
904 -string(REPLACE "#!/usr/bin/env python" "#!/usr/bin/python" content "${content}")
905 -file(WRITE ${CMAKE_BINARY_DIR}/git-p4 ${content})
902 +# Python script
903 +add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/git-p4"
904 + COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/generate-python.sh"
905 + "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
906 + "${CMAKE_SOURCE_DIR}/git-p4.py"
907 + "${CMAKE_BINARY_DIR}/git-p4"
908 + DEPENDS "${CMAKE_SOURCE_DIR}/generate-python.sh"
909 + "${CMAKE_SOURCE_DIR}/git-p4.py"
910 + "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
911 + VERBATIM)
912 +add_custom_target(python-gen ALL DEPENDS "${CMAKE_BINARY_DIR}/git-p4")
913
914 #templates
915 file(GLOB templates "${CMAKE_SOURCE_DIR}/templates/*")
generate-python.sh new
+20
@@ -0,0 +1,20 @@
1 +#!/bin/sh
2 +
3 +set -e
4 +
5 +if test $# -ne 3
6 +then
7 + echo >&2 "USAGE: $0 <GIT_BUILD_OPTIONS> <INPUT> <OUTPUT>"
8 + exit 1
9 +fi
10 +
11 +GIT_BUILD_OPTIONS="$1"
12 +INPUT="$2"
13 +OUTPUT="$3"
14 +
15 +. "$GIT_BUILD_OPTIONS"
16 +
17 +sed -e "1s|#!.*python|#!$PYTHON_PATH|" \
18 + "$INPUT" >"$OUTPUT+"
19 +chmod a+x "$OUTPUT+"
20 +mv "$OUTPUT+" "$OUTPUT"