Makefile: extract script to massage Perl scripts

Extract the script to inject various build-time parameters into our Perl scripts into a standalone script. This is done such that we can reuse it in other 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 e4b488049a5faa9b8c7255f483a89cea414d5eb4
3 files changed +56 -15
Makefile
+2 -10
@@ -2606,16 +2606,8 @@ endif
2606
2607 PERL_DEFINES += $(gitexecdir) $(perllibdir) $(localedir)
2608
2609 -$(SCRIPT_PERL_GEN): % : %.perl GIT-PERL-DEFINES GIT-PERL-HEADER GIT-VERSION-FILE
2610 - $(QUIET_GEN) \
2611 - sed -e '1{' \
2612 - -e ' s|#!.*perl|#!$(PERL_PATH_SQ)|' \
2613 - -e ' r GIT-PERL-HEADER' \
2614 - -e ' G' \
2615 - -e '}' \
2616 - -e 's/@GIT_VERSION@/$(GIT_VERSION)/g' \
2617 - $< >$@+ && \
2618 - chmod +x $@+ && \
2609 +$(SCRIPT_PERL_GEN): % : %.perl generate-perl.sh GIT-PERL-DEFINES GIT-PERL-HEADER GIT-VERSION-FILE
2610 + $(QUIET_GEN)$(SHELL_PATH) generate-perl.sh ./GIT-BUILD-OPTIONS ./GIT-VERSION-FILE GIT-PERL-HEADER "$<" "$@+" && \
2611 mv $@+ $@
2612
2613 PERL_DEFINES := $(subst $(space),:,$(PERL_DEFINES))
contrib/buildsystems/CMakeLists.txt
+27 -5
@@ -852,19 +852,41 @@ foreach(script ${git_shell_scripts})
852 endforeach()
853
854 #perl scripts
855 -parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" ".perl")
855 +parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" "")
856
857 #create perl header
858 file(STRINGS ${CMAKE_SOURCE_DIR}/perl/header_templates/fixed_prefix.template.pl perl_header )
859 string(REPLACE "@PATHSEP@" ":" perl_header "${perl_header}")
860 string(REPLACE "@INSTLIBDIR@" "${INSTLIBDIR}" perl_header "${perl_header}")
861 +file(WRITE ${CMAKE_BINARY_DIR}/PERL-HEADER ${perl_header})
862 +
863 +add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
864 + COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
865 + "${CMAKE_SOURCE_DIR}"
866 + "${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE.in"
867 + "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
868 + DEPENDS ${SH_EXE} "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
869 + "${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE.in"
870 + VERBATIM)
871
872 foreach(script ${git_perl_scripts})
863 - file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.perl content NEWLINE_CONSUME)
864 - string(REPLACE "#!/usr/bin/perl" "#!/usr/bin/perl\n${perl_header}\n" content "${content}")
865 - string(REPLACE "@GIT_VERSION@" "${PROJECT_VERSION}" content "${content}")
866 - file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
873 + string(REPLACE ".perl" "" perl_gen_path "${script}")
874 +
875 + add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/${perl_gen_path}"
876 + COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/generate-perl.sh"
877 + "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
878 + "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
879 + "${CMAKE_BINARY_DIR}/PERL-HEADER"
880 + "${CMAKE_SOURCE_DIR}/${script}"
881 + "${CMAKE_BINARY_DIR}/${perl_gen_path}"
882 + DEPENDS "${CMAKE_SOURCE_DIR}/generate-perl.sh"
883 + "${CMAKE_SOURCE_DIR}/${script}"
884 + "${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS"
885 + "${CMAKE_BINARY_DIR}/GIT-VERSION-FILE"
886 + VERBATIM)
887 + list(APPEND perl_gen ${CMAKE_BINARY_DIR}/${perl_gen_path})
888 endforeach()
889 +add_custom_target(perl-gen ALL DEPENDS ${perl_gen})
890
891 #python script
892 file(STRINGS ${CMAKE_SOURCE_DIR}/git-p4.py content NEWLINE_CONSUME)
generate-perl.sh new
+27
@@ -0,0 +1,27 @@
1 +#!/bin/sh
2 +
3 +set -e
4 +
5 +if test $# -ne 5
6 +then
7 + echo >&2 "USAGE: $0 <GIT_BUILD_OPTIONS> <GIT_VERSION_FILE> <PERL_HEADER> <INPUT> <OUTPUT>"
8 + exit 1
9 +fi
10 +
11 +GIT_BUILD_OPTIONS="$1"
12 +GIT_VERSION_FILE="$2"
13 +PERL_HEADER="$3"
14 +INPUT="$4"
15 +OUTPUT="$5"
16 +
17 +. "$GIT_BUILD_OPTIONS"
18 +. "$GIT_VERSION_FILE"
19 +
20 +sed -e '1{' \
21 + -e " s|#!.*perl|#!$PERL_PATH|" \
22 + -e " r $PERL_HEADER" \
23 + -e ' G' \
24 + -e '}' \
25 + -e "s/@GIT_VERSION@/$GIT_VERSION/g" \
26 + "$INPUT" >"$OUTPUT"
27 +chmod a+x "$OUTPUT"