From 0cc56f31647581554972e4b69c10a5a3e22cc3b3 Mon Sep 17 00:00:00 2001 From: k9er Date: Mon, 13 Jan 2025 20:50:05 +0000 Subject: [PATCH] Merge adjacent identical ifdefs in genmod --- qcsrc/common/monsters/_mod.inc | 4 -- qcsrc/common/monsters/_mod.qh | 4 -- .../common/mutators/mutator/overkill/_mod.inc | 2 - qcsrc/tools/genmod.sh | 59 ++++++++++++++++--- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/qcsrc/common/monsters/_mod.inc b/qcsrc/common/monsters/_mod.inc index 31ff90be6..f2315fc6c 100644 --- a/qcsrc/common/monsters/_mod.inc +++ b/qcsrc/common/monsters/_mod.inc @@ -2,11 +2,7 @@ #include #ifdef SVQC #include -#endif -#ifdef SVQC #include -#endif -#ifdef SVQC #include #endif diff --git a/qcsrc/common/monsters/_mod.qh b/qcsrc/common/monsters/_mod.qh index 890edfced..73167f84f 100644 --- a/qcsrc/common/monsters/_mod.qh +++ b/qcsrc/common/monsters/_mod.qh @@ -2,11 +2,7 @@ #include #ifdef SVQC #include -#endif -#ifdef SVQC #include -#endif -#ifdef SVQC #include #endif diff --git a/qcsrc/common/mutators/mutator/overkill/_mod.inc b/qcsrc/common/mutators/mutator/overkill/_mod.inc index 36dba0385..c42e2054d 100644 --- a/qcsrc/common/mutators/mutator/overkill/_mod.inc +++ b/qcsrc/common/mutators/mutator/overkill/_mod.inc @@ -10,7 +10,5 @@ #endif #ifdef SVQC #include -#endif -#ifdef SVQC #include #endif diff --git a/qcsrc/tools/genmod.sh b/qcsrc/tools/genmod.sh index 2c6c3226f..fa2fc438f 100755 --- a/qcsrc/tools/genmod.sh +++ b/qcsrc/tools/genmod.sh @@ -17,6 +17,44 @@ function hash() { git hash-object "$1" } +function print_ifdef() { + local filename="$1" + local pathtofile="$2" + local filetype="$3" + local outputfile="${MOD}.$filetype" + local currentqc="$4" # Current ifdef QC VM type + local nextqc="${5-NONE}" # Next ifdef QC VM type + + # End previous ifdef if required + if [ "$currentqc" != "$nextqc" ] \ + && [ "$currentqc" != "NONE" ] + then + printf "#endif\n" >> "$outputfile" + fi + + # Start new ifdef if required + if [ "${nextqc-}" != "NONE" ] \ + && [ "$currentqc" != "$nextqc" ] + then + printf "#ifdef %s\n" "$nextqc" >> "$outputfile" + fi + + # Update + currentqc="$nextqc" + + # Include the file + if [ "$currentqc" != "NONE" ] + then + # Indent if inside an ifdef block + printf "\t#include <%s>\n" "$pathtofile$filename" >> "$outputfile" + else + printf "#include <%s>\n" "$pathtofile$filename" >> "$outputfile" + fi + + # "Return" the current (potentially changed) ifdef QC VM type + echo "$currentqc" +} + function genmod() { # use context to work around cmake issue #12619 CTX="${PWD#"$ROOT"}/" @@ -35,6 +73,9 @@ function genmod() { echo "// $(basename "$0") autogenerated file; do not modify" > "${MOD}.inc" echo "// $(basename "$0") autogenerated file; do not modify" > "${MOD}.qh" + ifdef_inc="NONE" # Can be either CSQC, SVQC, MENUQC, or NONE + ifdef_qh="NONE" + # Dr. Jaska: TODO: replace ls with something else # LSP note: "Use find instead of ls to better handle non-alphanumeric filenames." # Dr. Jaska: find without some configuration would prefix everything with ./ which is likely unwanted @@ -49,8 +90,8 @@ function genmod() { # # file.qc into _mod.inc # file.qh into _mod.qh - if [[ -f "$f" ]]; then printf "#include <%s>\n" "${CTX}$f" >> ${MOD}.inc; fi - if [[ -f "${f%.qc}.qh" ]]; then printf "#include <%s>\n" "${CTX}${f%.qc}.qh" >> ${MOD}.qh; fi + if [[ -f "$f" ]]; then ifdef_inc=$(print_ifdef "$f" "$CTX" "inc" "$ifdef_inc"); fi + if [[ -f "${f%.qc}.qh" ]]; then ifdef_qh=$( print_ifdef "${f%.qc}.qh" "$CTX" "qh" "$ifdef_qh" ); fi # Print the following template: # @@ -61,19 +102,21 @@ function genmod() { # CSQC # cl_file.qc into _mod.inc # cl_file.qh into _mod.qh - if [[ -f "cl_$f" ]]; then printf "#ifdef %s\n\t#include <%s>\n#endif\n" CSQC "${CTX}cl_$f" >> ${MOD}.inc; fi - if [[ -f "cl_${f%.qc}.qh" ]]; then printf "#ifdef %s\n\t#include <%s>\n#endif\n" CSQC "${CTX}cl_${f%.qc}.qh" >> ${MOD}.qh; fi + if [[ -f "cl_$f" ]]; then ifdef_inc=$(print_ifdef "cl_$f" "$CTX" "inc" "$ifdef_inc" "CSQC"); fi + if [[ -f "cl_${f%.qc}.qh" ]]; then ifdef_qh=$( print_ifdef "cl_${f%.qc}.qh" "$CTX" "qh" "$ifdef_qh" "CSQC"); fi # SVQC # sv_file.qc into _mod.inc # sv_file.qh into _mod.qh - if [[ -f "sv_$f" ]]; then printf "#ifdef %s\n\t#include <%s>\n#endif\n" SVQC "${CTX}sv_$f" >> ${MOD}.inc; fi - if [[ -f "sv_${f%.qc}.qh" ]]; then printf "#ifdef %s\n\t#include <%s>\n#endif\n" SVQC "${CTX}sv_${f%.qc}.qh" >> ${MOD}.qh; fi + if [[ -f "sv_$f" ]]; then ifdef_inc=$(print_ifdef "sv_$f" "$CTX" "inc" "$ifdef_inc" "SVQC"); fi + if [[ -f "sv_${f%.qc}.qh" ]]; then ifdef_qh=$( print_ifdef "sv_${f%.qc}.qh" "$CTX" "qh" "$ifdef_qh" "SVQC"); fi # MENUQC # ui_file.qc into _mod.inc # ui_file.qh into _mod.qh - if [[ -f "ui_$f" ]]; then printf "#ifdef %s\n\t#include <%s>\n#endif\n" MENUQC "${CTX}ui_$f" >> ${MOD}.inc; fi - if [[ -f "ui_${f%.qc}.qh" ]]; then printf "#ifdef %s\n\t#include <%s>\n#endif\n" MENUQC "${CTX}ui_${f%.qc}.qh" >> ${MOD}.qh; fi + if [[ -f "ui_$f" ]]; then ifdef_inc=$(print_ifdef "ui_$f" "$CTX" "inc" "$ifdef_inc" "MENUQC"); fi + if [[ -f "ui_${f%.qc}.qh" ]]; then ifdef_qh=$( print_ifdef "ui_${f%.qc}.qh" "$CTX" "qh" "$ifdef_qh" "MENUQC"); fi done + if [ "$ifdef_inc" != "NONE" ]; then printf "#endif\n" >> "${MOD}.inc"; fi + if [ "$ifdef_qh" != "NONE" ]; then printf "#endif\n" >> "${MOD}.qh"; fi declare -l rec=1 -- 2.39.5