Makefile: refactor generators to be PWD-independent

We have multiple scripts that generate headers from other data. All of these scripts have the assumption built-in that they are executed in the current source directory, which makes them a bit unwieldy to use during out-of-tree builds. Refactor them to instead take the source directory as well as the output file as arguments. 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 3f145a4fe37099c55c7596e93a4cfd850662f88a
5 files changed +68 -34
Makefile
+3 -3
@@ -2523,17 +2523,17 @@ $(BUILT_INS): git$X
2523 config-list.h: generate-configlist.sh
2524
2525 config-list.h: Documentation/*config.txt Documentation/config/*.txt
2526 - $(QUIET_GEN)$(SHELL_PATH) ./generate-configlist.sh >$@
2526 + $(QUIET_GEN)$(SHELL_PATH) ./generate-configlist.sh . $@
2527
2528 command-list.h: generate-cmdlist.sh command-list.txt
2529
2530 command-list.h: $(wildcard Documentation/git*.txt)
2531 $(QUIET_GEN)$(SHELL_PATH) ./generate-cmdlist.sh \
2532 $(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \
2533 - command-list.txt >$@
2533 + . $@
2534
2535 hook-list.h: generate-hooklist.sh Documentation/githooks.txt
2536 - $(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh >$@
2536 + $(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh . $@
2537
2538 SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):\
2539 $(localedir_SQ):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\
contrib/buildsystems/CMakeLists.txt
+10 -9
@@ -638,23 +638,24 @@ set(EXCLUSION_PROGS_CACHE ${EXCLUSION_PROGS} CACHE STRING "Programs not built" F
638 if(NOT EXISTS ${CMAKE_BINARY_DIR}/command-list.h OR NOT EXCLUSION_PROGS_CACHE STREQUAL EXCLUSION_PROGS)
639 list(REMOVE_ITEM EXCLUSION_PROGS empty)
640 message("Generating command-list.h")
641 - execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-cmdlist.sh ${EXCLUSION_PROGS} command-list.txt
642 - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
643 - OUTPUT_FILE ${CMAKE_BINARY_DIR}/command-list.h)
641 + execute_process(COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/generate-cmdlist.sh"
642 + ${EXCLUSION_PROGS}
643 + "${CMAKE_SOURCE_DIR}"
644 + "${CMAKE_BINARY_DIR}/command-list.h")
645 endif()
646
647 if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h)
648 message("Generating config-list.h")
648 - execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-configlist.sh
649 - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
650 - OUTPUT_FILE ${CMAKE_BINARY_DIR}/config-list.h)
649 + execute_process(COMMAND "${SH_EXE}" "${CMAKE_SOURCE_DIR}/generate-configlist.sh"
650 + "${CMAKE_SOURCE_DIR}"
651 + "${CMAKE_BINARY_DIR}/config-list.h")
652 endif()
653
654 if(NOT EXISTS ${CMAKE_BINARY_DIR}/hook-list.h)
655 message("Generating hook-list.h")
655 - execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-hooklist.sh
656 - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
657 - OUTPUT_FILE ${CMAKE_BINARY_DIR}/hook-list.h)
656 + execute_process(COMMAND "${SH_EXE}" ${CMAKE_SOURCE_DIR}/generate-hooklist.sh
657 + "${CMAKE_SOURCE_DIR}"
658 + "${CMAKE_BINARY_DIR}/hook-list.h")
659 endif()
660
661 include_directories(${CMAKE_BINARY_DIR})
generate-cmdlist.sh
+26 -16
@@ -64,7 +64,7 @@ define_category_names () {
64 print_command_list () {
65 echo "static struct cmdname_help command_list[] = {"
66
67 - echo "$1" |
67 + echo "$2" |
68 while read cmd rest
69 do
70 synopsis=
@@ -76,7 +76,7 @@ print_command_list () {
76 break
77 ;;
78 esac
79 - done <"Documentation/$cmd.txt"
79 + done <"$1/Documentation/$cmd.txt"
80
81 printf '\t{ "%s", N_("%s"), 0' "$cmd" "$synopsis"
82 printf " | CAT_%s" $rest
@@ -93,18 +93,28 @@ do
93 shift
94 done
95
96 -commands="$(command_list "$1")"
97 -categories="$(category_list "$commands")"
96 +if test "$#" -ne 2
97 +then
98 + die "USAGE: $0 <SOURCE_DIR> <OUTPUT>"
99 +fi
100 +
101 +SOURCE_DIR="$1"
102 +OUTPUT="$2"
103 +
104 +{
105 + commands="$(command_list "$SOURCE_DIR"/command-list.txt)"
106 + categories="$(category_list "$commands")"
107
99 -echo "/* Automatically generated by generate-cmdlist.sh */
100 -struct cmdname_help {
101 - const char *name;
102 - const char *help;
103 - uint32_t category;
104 -};
105 -"
106 -define_categories "$categories"
107 -echo
108 -define_category_names "$categories"
109 -echo
110 -print_command_list "$commands"
108 + echo "/* Automatically generated by generate-cmdlist.sh */
109 + struct cmdname_help {
110 + const char *name;
111 + const char *help;
112 + uint32_t category;
113 + };
114 + "
115 + define_categories "$categories"
116 + echo
117 + define_category_names "$categories"
118 + echo
119 + print_command_list "$SOURCE_DIR" "$commands"
120 +} >"$OUTPUT"
generate-configlist.sh
+15 -5
@@ -1,13 +1,19 @@
1 #!/bin/sh
2
3 -echo "/* Automatically generated by generate-configlist.sh */"
4 -echo
3 +SOURCE_DIR="$1"
4 +OUTPUT="$2"
5 +
6 +if test -z "$SOURCE_DIR" || ! test -d "$SOURCE_DIR" || test -z "$OUTPUT"
7 +then
8 + echo >&2 "USAGE: $0 <SOURCE_DIR> <OUTPUT>"
9 + exit 1
10 +fi
11
12 print_config_list () {
13 cat <<EOF
14 static const char *config_name_list[] = {
15 EOF
10 - grep -h '^[a-zA-Z].*\..*::$' Documentation/*config.txt Documentation/config/*.txt |
16 + grep -h '^[a-zA-Z].*\..*::$' "$SOURCE_DIR"/Documentation/*config.txt "$SOURCE_DIR"/Documentation/config/*.txt |
17 sed '/deprecated/d; s/::$//; s/, */\n/g' |
18 sort |
19 sed 's/^.*$/ "&",/'
@@ -17,5 +23,9 @@ EOF
23 EOF
24 }
25
20 -echo
21 -print_config_list
26 +{
27 + echo "/* Automatically generated by generate-configlist.sh */"
28 + echo
29 + echo
30 + print_config_list
31 +} >"$OUTPUT"
generate-hooklist.sh
+14 -1
@@ -2,6 +2,17 @@
2 #
3 # Usage: ./generate-hooklist.sh >hook-list.h
4
5 +SOURCE_DIR="$1"
6 +OUTPUT="$2"
7 +
8 +if test -z "$SOURCE_DIR" || ! test -d "$SOURCE_DIR" || test -z "$OUTPUT"
9 +then
10 + echo >&2 "USAGE: $0 <SOURCE_DIR> <OUTPUT>"
11 + exit 1
12 +fi
13 +
14 +{
15 +
16 cat <<EOF
17 /* Automatically generated by generate-hooklist.sh */
18
@@ -11,10 +22,12 @@ EOF
22 sed -n \
23 -e '/^~~~~*$/ {x; s/^.*$/ "&",/; p;}' \
24 -e 'x' \
14 - <Documentation/githooks.txt |
25 + <"$SOURCE_DIR"/Documentation/githooks.txt |
26 LC_ALL=C sort
27
28 cat <<EOF
29 NULL,
30 };
31 EOF
32 +
33 +} >"$OUTPUT"