From 42049493cb287e0427518cccbed2ef14788d1575 Mon Sep 17 00:00:00 2001 From: Bruno Lopes Date: Wed, 16 Apr 2025 22:16:03 -0300 Subject: [PATCH] tools: Port meson-mkenums.sh to Python --- meson.build | 2 +- pdb/meson-enumcode.py | 3 +- pdb/meson-enumgen.py | 3 +- pdb/meson-pdbgen.py | 3 +- tools/meson-mkenums.py | 67 ++++++++++++++++++++++++++++++++++++++++++ tools/meson-mkenums.sh | 53 --------------------------------- 6 files changed, 71 insertions(+), 60 deletions(-) create mode 100644 tools/meson-mkenums.py delete mode 100755 tools/meson-mkenums.sh diff --git a/meson.build b/meson.build index 0223808918..f8be5e58c2 100644 --- a/meson.build +++ b/meson.build @@ -1426,7 +1426,7 @@ module_dependencies = find_program('tools'/'module-dependencies.py') meson_install_subdir= find_program('tools'/'meson_install_subdir.py') gimp_mkenums = find_program('tools' / 'gimp-mkenums') -mkenums_wrap = find_program(meson.current_source_dir() / 'tools' / 'meson-mkenums.sh') +mkenums_wrap = find_program(meson.current_source_dir() / 'tools' / 'meson-mkenums.py') libgimp_mkenums_dtails = \ ' { 0, NULL, NULL }\n' + \ diff --git a/pdb/meson-enumcode.py b/pdb/meson-enumcode.py index e00be20790..5fc8148a9a 100644 --- a/pdb/meson-enumcode.py +++ b/pdb/meson-enumcode.py @@ -22,7 +22,6 @@ result = subprocess.run( stderr=subprocess.PIPE ) if result.returncode == 0: - timestamp = datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y") with open(os.path.join(top_builddir, 'pdb', 'stamp-enumcode.h'), 'w') as f: - f.write(f"/* Generated on {timestamp}. */\n") + f.write(f"/* Generated on {datetime.now().strftime('%a %b %d %H:%M:%S %Z %Y')}. */\n") sys.exit(result.returncode) diff --git a/pdb/meson-enumgen.py b/pdb/meson-enumgen.py index c76b0c44a3..73502cc98b 100644 --- a/pdb/meson-enumgen.py +++ b/pdb/meson-enumgen.py @@ -19,7 +19,6 @@ result = subprocess.run( stderr=subprocess.PIPE ) if result.returncode == 0: - timestamp = datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y") with open(os.path.join(top_builddir, 'pdb', 'stamp-enumgen.h'), 'w') as f: - f.write(f"/* Generated on {timestamp}. */\n") + f.write(f"/* Generated on {datetime.now().strftime('%a %b %d %H:%M:%S %Z %Y')}. */\n") sys.exit(result.returncode) diff --git a/pdb/meson-pdbgen.py b/pdb/meson-pdbgen.py index e35b0b55a5..b8ad8fb9b4 100644 --- a/pdb/meson-pdbgen.py +++ b/pdb/meson-pdbgen.py @@ -19,7 +19,6 @@ result = subprocess.run( stderr=subprocess.PIPE ) if result.returncode == 0: - timestamp = datetime.now().strftime("%a %b %d %H:%M:%S %Z %Y") with open(os.path.join(top_builddir, 'pdb', 'stamp-pdbgen.h'), 'w') as f: - f.write(f"/* Generated on {timestamp}. */\n") + f.write(f"/* Generated on {datetime.now().strftime('%a %b %d %H:%M:%S %Z %Y')}. */\n") sys.exit(result.returncode) diff --git a/tools/meson-mkenums.py b/tools/meson-mkenums.py new file mode 100644 index 0000000000..efdf9b3855 --- /dev/null +++ b/tools/meson-mkenums.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +import sys +import os +import subprocess +import filecmp +import shutil +from datetime import datetime + +# This is a wrapper to the tools/gimp-mkenums perl script which: +# * sets a few common values; +# * updates the ${filebase}enums.c directly in the source directory in +# order for it to be versioned. +# * Create a no-op stamp-header file to be included by the resulting +# enums.c. The goal is to make sure that meson will trigger a rebuild +# of the enums.c generation before compiling, if the enums.h changed. +# See the explanation here: +# https://github.com/mesonbuild/meson/issues/10196#issuecomment-1080742592 +# This is also the trick used for pdbgen. + +# Arguments to this script: +# The perl binary to use. +PERL = sys.argv[1] +# Root of the source directory. +top_srcdir = sys.argv[2] +# Current source folder. +srcdir = sys.argv[3] +# Current build folder. +builddir = sys.argv[4] +# Base of the generated enums.c file name. +filebase = sys.argv[5] +# Includes before #include "${filebase}enums.h" +preincludes = sys.argv[6] +# Includes after #include "${filebase}enums.h" +postincludes = sys.argv[7] +# Value for --dtail option if the default doesn't fit. +dtail = sys.argv[8] if len(sys.argv) >= 9 else None + +if not dtail: + dtail = " { 0, NULL, NULL }\n };\n\n static GType type = 0;\n\n if (G_UNLIKELY (! type))\n {\n type = g_@type@_register_static (\"@EnumName@\", values);\n gimp_type_set_translation_context (type, \"@enumnick@\");\n gimp_@type@_set_value_descriptions (type, descs);\n }\n\n return type;\n}\n" + +args = [ + PERL, os.path.join(top_srcdir, 'tools', 'gimp-mkenums'), + '--fhead', f'#include "stamp-{filebase}enums.h"\n' f'#include "config.h"\n' f'{preincludes}' f'#include "{filebase}enums.h"\n' f'{postincludes}', + '--fprod', '\n/* enumerations from "@basename@" */', + '--vhead', 'GType\n' '@enum_name@_get_type (void)\n' '{\n' ' static const G@Type@Value values[] =\n' ' {', + '--vprod', ' { @VALUENAME@, "@VALUENAME@", "@valuenick@" },', + '--vtail', ' { 0, NULL, NULL }\n };\n', + '--dhead', ' static const Gimp@Type@Desc descs[] =\n {', + '--dprod', ' { @VALUENAME@, @valuedesc@, @valuehelp@ },' '@if (\'@valueabbrev@\' ne \'NULL\')@\n' ' /* Translators: this is an abbreviated version of @valueudesc@.\n' ' Keep it short. */\n' ' { @VALUENAME@, @valueabbrev@, NULL },@endif@', + '--dtail', dtail, + os.path.join(f"{filebase}enums.h") +] +tmp_enums_c = os.path.join(builddir, f"{filebase}enums-tmp.c") +with open(tmp_enums_c, 'w') as f: + subprocess.run(args, check=True, stdout=f, text=True, cwd=srcdir) + +target_enums_c = os.path.join(srcdir, f"{filebase}enums.c") +if not filecmp.cmp(tmp_enums_c, target_enums_c, shallow=False): + shutil.copy(tmp_enums_c, target_enums_c) +else: + try: + os.utime(target_enums_c, None) + except OSError: + pass + +with open(os.path.join(builddir, f"stamp-{filebase}enums.h"), 'w') as f: + f.write(f"/* Generated on {datetime.now().strftime('%a %b %d %H:%M:%S %Z %Y')}. */\n") diff --git a/tools/meson-mkenums.sh b/tools/meson-mkenums.sh deleted file mode 100755 index ee8b938894..0000000000 --- a/tools/meson-mkenums.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/sh - -# This is a wrapper to the tools/gimp-mkenums perl script which: -# * sets a few common values; -# * updates the ${filebase}enums.c directly in the source directory in -# order for it to be versioned. -# * Create a no-op stamp-header file to be included by the resulting -# enums.c. The goal is to make sure that meson will trigger a rebuild -# of the enums.c generation before compiling, if the enums.h changed. -# See the explanation here: -# https://github.com/mesonbuild/meson/issues/10196#issuecomment-1080742592 -# This is also the trick used for pdbgen. - -# Arguments to this script: -# The perl binary to use. -PERL="$1" -# Root of the source directory. -top_srcdir="$2" -# Current source folder. -srcdir="$3" -# Current build folder. -builddir="$4" -# Base of the generated enums.c file name. -filebase="$5" -# Includes before #include "${filebase}enums.h" -preincludes="$6" -# Includes after #include "${filebase}enums.h" -postincludes="$7" -# Value for --dtail option if the default doesn't fit. -dtail="$8" - -if [ -z "$dtail" ]; then - dtail=" { 0, NULL, NULL }\n };\n\n static GType type = 0;\n\n if (G_UNLIKELY (! type))\n {\n type = g_@type@_register_static (\"@EnumName@\", values);\n gimp_type_set_translation_context (type, \"@enumnick@\");\n gimp_@type@_set_value_descriptions (type, descs);\n }\n\n return type;\n}\n" -fi - -$PERL $top_srcdir/tools/gimp-mkenums \ - --fhead "#include \"stamp-${filebase}enums.h\"\n#include \"config.h\"\n$preincludes#include \"${filebase}enums.h\"\n$postincludes" \ - --fprod "\n/* enumerations from \"@basename@\" */" \ - --vhead "GType\n@enum_name@_get_type (void)\n{\n static const G@Type@Value values[] =\n {" \ - --vprod " { @VALUENAME@, \"@VALUENAME@\", \"@valuenick@\" }," \ - --vtail " { 0, NULL, NULL }\n };\n" \ - --dhead " static const Gimp@Type@Desc descs[] =\n {" \ - --dprod " { @VALUENAME@, @valuedesc@, @valuehelp@ },@if ('@valueabbrev@' ne 'NULL')@\n /* Translators: this is an abbreviated version of @valueudesc@.\n Keep it short. */\n { @VALUENAME@, @valueabbrev@, NULL },@endif@" \ - --dtail "$dtail" \ - "$srcdir/${filebase}enums.h" > "$builddir/${filebase}enums-tmp.c" - -if ! cmp -s "$builddir/${filebase}enums-tmp.c" "$srcdir/${filebase}enums.c"; then - cp "$builddir/${filebase}enums-tmp.c" "$srcdir/${filebase}enums.c"; -else - touch "$srcdir/${filebase}enums.c"; 2> /dev/null || true; -fi - -echo "/* Generated on `date`. */" > $builddir/stamp-${filebase}enums.h