diff mbox series

[RFC,v4,04/19] Makefile: extract script to massage Perl scripts

Message ID 50b607a412afea051a7839b9f3f1b4519b58721a.1729771605.git.ps@pks.im (mailing list archive)
State Superseded
Headers show
Series Modernize the build system | expand

Commit Message

Patrick Steinhardt Oct. 24, 2024, 12:39 p.m. UTC
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>
---
 Makefile                            | 12 ++----------
 contrib/buildsystems/CMakeLists.txt | 20 +++++++++++++++-----
 generate-perl.sh                    | 26 ++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 15 deletions(-)
 create mode 100755 generate-perl.sh

Comments

Phillip Wood Nov. 10, 2024, 2:36 p.m. UTC | #1
Hi Patrick

On 24/10/2024 13:39, Patrick Steinhardt wrote:
>
> +	add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${perl_gen_path}
> +		COMMAND ${CMAKE_SOURCE_DIR}/generate-perl.sh

This is missing ${SH_EXE} in order to work with cmd.exe on windows when 
running the build from Visual Studio. Also do we want to quote the 
command arguments in case there is a ';' in the path - we seem to be a 
bit inconsistant in about that in CMakeLists.txt. Also we should add 
VERBATIM so that special characters in the arguments are quoted when 
generating the build rules.

Best Wishes

Phillip

> +			${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS
> +			${PROJECT_VERSION}
> +			${CMAKE_BINARY_DIR}/PERL-HEADER
> +			${CMAKE_SOURCE_DIR}/${script}
> +			${CMAKE_BINARY_DIR}/${perl_gen_path}
> +		DEPENDS ${CMAKE_SOURCE_DIR}/generate-perl.sh
> +			${CMAKE_SOURCE_DIR}/${script})
> +	list(APPEND perl_gen ${CMAKE_BINARY_DIR}/${perl_gen_path})
>   endforeach()
> +add_custom_target(perl-gen ALL DEPENDS ${perl_gen})
Patrick Steinhardt Nov. 11, 2024, 10:36 a.m. UTC | #2
On Sun, Nov 10, 2024 at 02:36:49PM +0000, Phillip Wood wrote:
> Hi Patrick
> 
> On 24/10/2024 13:39, Patrick Steinhardt wrote:
> > 
> > +	add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${perl_gen_path}
> > +		COMMAND ${CMAKE_SOURCE_DIR}/generate-perl.sh
> 
> This is missing ${SH_EXE} in order to work with cmd.exe on windows when
> running the build from Visual Studio. Also do we want to quote the command
> arguments in case there is a ';' in the path - we seem to be a bit
> inconsistant in about that in CMakeLists.txt. Also we should add VERBATIM so
> that special characters in the arguments are quoted when generating the
> build rules.

Indeed, I also had it in my mind to revisit these patches and fix them
up based on Dscho's findings in the other thread. Thanks for being
proactive and pointing them out!

Patrick
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 22ed53f39e7..e04a381e8f0 100644
--- a/Makefile
+++ b/Makefile
@@ -2604,16 +2604,8 @@  endif
 
 PERL_DEFINES += $(gitexecdir) $(perllibdir) $(localedir)
 
-$(SCRIPT_PERL_GEN): % : %.perl GIT-PERL-DEFINES GIT-PERL-HEADER GIT-VERSION-FILE
-	$(QUIET_GEN) \
-	sed -e '1{' \
-	    -e '	s|#!.*perl|#!$(PERL_PATH_SQ)|' \
-	    -e '	r GIT-PERL-HEADER' \
-	    -e '	G' \
-	    -e '}' \
-	    -e 's/@GIT_VERSION@/$(GIT_VERSION)/g' \
-	    $< >$@+ && \
-	chmod +x $@+ && \
+$(SCRIPT_PERL_GEN): % : %.perl generate-perl.sh GIT-PERL-DEFINES GIT-PERL-HEADER GIT-VERSION-FILE
+	$(QUIET_GEN)$(SHELL_PATH) generate-perl.sh ./GIT-BUILD-OPTIONS $(GIT_VERSION) GIT-PERL-HEADER "$<" "$@+" && \
 	mv $@+ $@
 
 PERL_DEFINES := $(subst $(space),:,$(PERL_DEFINES))
diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt
index 608ad9714d4..7fb6a149f21 100644
--- a/contrib/buildsystems/CMakeLists.txt
+++ b/contrib/buildsystems/CMakeLists.txt
@@ -848,19 +848,29 @@  foreach(script ${git_shell_scripts})
 endforeach()
 
 #perl scripts
-parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" ".perl")
+parse_makefile_for_scripts(git_perl_scripts "SCRIPT_PERL" "")
 
 #create perl header
 file(STRINGS ${CMAKE_SOURCE_DIR}/perl/header_templates/fixed_prefix.template.pl perl_header )
 string(REPLACE "@PATHSEP@" ":" perl_header "${perl_header}")
 string(REPLACE "@INSTLIBDIR@" "${INSTLIBDIR}" perl_header "${perl_header}")
+file(WRITE ${CMAKE_BINARY_DIR}/PERL-HEADER ${perl_header})
 
 foreach(script ${git_perl_scripts})
-	file(STRINGS ${CMAKE_SOURCE_DIR}/${script}.perl content NEWLINE_CONSUME)
-	string(REPLACE "#!/usr/bin/perl" "#!/usr/bin/perl\n${perl_header}\n" content "${content}")
-	string(REPLACE "@GIT_VERSION@" "${PROJECT_VERSION}" content "${content}")
-	file(WRITE ${CMAKE_BINARY_DIR}/${script} ${content})
+	string(REPLACE ".perl" "" perl_gen_path "${script}")
+
+	add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${perl_gen_path}
+		COMMAND ${CMAKE_SOURCE_DIR}/generate-perl.sh
+			${CMAKE_BINARY_DIR}/GIT-BUILD-OPTIONS
+			${PROJECT_VERSION}
+			${CMAKE_BINARY_DIR}/PERL-HEADER
+			${CMAKE_SOURCE_DIR}/${script}
+			${CMAKE_BINARY_DIR}/${perl_gen_path}
+		DEPENDS ${CMAKE_SOURCE_DIR}/generate-perl.sh
+			${CMAKE_SOURCE_DIR}/${script})
+	list(APPEND perl_gen ${CMAKE_BINARY_DIR}/${perl_gen_path})
 endforeach()
+add_custom_target(perl-gen ALL DEPENDS ${perl_gen})
 
 #python script
 file(STRINGS ${CMAKE_SOURCE_DIR}/git-p4.py content NEWLINE_CONSUME)
diff --git a/generate-perl.sh b/generate-perl.sh
new file mode 100755
index 00000000000..12e116b76e5
--- /dev/null
+++ b/generate-perl.sh
@@ -0,0 +1,26 @@ 
+#!/bin/sh
+
+set -e
+
+if test $# -ne 5
+then
+	echo "USAGE: $0 <GIT_BUILD_OPTIONS> <GIT_VERSION> <PERL_HEADER> <INPUT> <OUTPUT>" >&2
+	exit 1
+fi
+
+GIT_BUILD_OPTIONS="$1"
+GIT_VERSION="$2"
+PERL_HEADER="$3"
+INPUT="$4"
+OUTPUT="$5"
+
+. "$GIT_BUILD_OPTIONS"
+
+sed -e '1{' \
+    -e "	s|#!.*perl|#!$PERL_PATH|" \
+    -e "	r $PERL_HEADER" \
+    -e '	G' \
+    -e '}' \
+    -e "s/@GIT_VERSION@/$GIT_VERSION/g" \
+    "$INPUT" >"$OUTPUT"
+chmod a+x "$OUTPUT"