Skip to content

Commit b7835b9

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eb98cb8 commit b7835b9

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,13 +2635,9 @@ endif # NO_PERL
26352635
$(SCRIPT_PYTHON_GEN): GIT-BUILD-OPTIONS
26362636

26372637
ifndef NO_PYTHON
2638-
$(SCRIPT_PYTHON_GEN): GIT-CFLAGS GIT-PREFIX GIT-PYTHON-VARS
2638+
$(SCRIPT_PYTHON_GEN): generate-python.sh
26392639
$(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 "$<" "$@"
26452641
else # NO_PYTHON
26462642
$(SCRIPT_PYTHON_GEN): % : unimplemented.sh
26472643
$(QUIET_GEN) \

contrib/buildsystems/CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,17 @@ foreach(script ${git_perl_scripts} ${perl_modules})
899899
endforeach()
900900
add_custom_target(perl-gen ALL DEPENDS ${perl_gen})
901901

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")
906913

907914
#templates
908915
file(GLOB templates "${CMAKE_SOURCE_DIR}/templates/*")

generate-python.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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"

0 commit comments

Comments
 (0)