From e3af529e511ca492284cdd9f4584666b88a9e00f Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Sat, 23 May 2020 22:13:32 +0430 Subject: [PATCH] [meson] update generated headers only when the result is different This way it won't ruin incremental builds. We need a better way to declare source altering tasks https://github.com/mesonbuild/meson/issues/7156 yet this is good enough despite being not idiomatic. It is however not that smooth yet as the change may is detected on the next meson run. One of course can issue ./gen-ragel-artifacts.py manually for better experience before running meson. --- src/gen-hb-version.py | 12 ++++++++++-- src/gen-ragel-artifacts.py | 26 ++++++++++++++++++++++---- src/meson.build | 15 ++++++--------- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/gen-hb-version.py b/src/gen-hb-version.py index 70e69d8b6..e29642425 100755 --- a/src/gen-hb-version.py +++ b/src/gen-hb-version.py @@ -10,10 +10,18 @@ major, minor, micro = version.split (".") input = sys.argv[2] output = sys.argv[3] -with open (output, "wb") as output_file, open (input, "r", encoding='utf-8') as input_file: - output_file.write (input_file.read () +with open (input, "r", encoding='utf-8') as input_file: + generated = (input_file.read () .replace ("@HB_VERSION_MAJOR@", major) .replace ("@HB_VERSION_MINOR@", minor) .replace ("@HB_VERSION_MICRO@", micro) .replace ("@HB_VERSION@", version) .encode ()) + + with open (output, "rb") as current_file: + current = current_file.read() + + # write only if is changed + if generated != current: + with open (output, "wb") as output_file: + output_file.write () diff --git a/src/gen-ragel-artifacts.py b/src/gen-ragel-artifacts.py index 2f176bbaf..53a303849 100755 --- a/src/gen-ragel-artifacts.py +++ b/src/gen-ragel-artifacts.py @@ -1,11 +1,12 @@ #!/usr/bin/env python3 -import io, os, re, sys, subprocess, shutil +import io, os, re, sys, subprocess, shutil, tempfile -if len (sys.argv) < 3: +os.chdir(os.path.dirname(__file__)) + +if len (sys.argv) < 2: ragel_sources = [x for x in os.listdir ('.') if x.endswith ('.rl')] else: - os.chdir(sys.argv[1]) ragel_sources = sys.argv[2:] ragel = shutil.which ('ragel') @@ -17,6 +18,23 @@ if not ragel: if not len (ragel_sources): exit (77) +tempdir = tempfile.mkdtemp () + for rl in ragel_sources: hh = rl.replace ('.rl', '.hh') - subprocess.Popen ([ragel, '-e', '-F1', '-o', hh, rl]).wait () + shutil.copy (rl, tempdir) + # writing to stdout has some complication on Windows + subprocess.Popen ([ragel, '-e', '-F1', '-o', hh, rl], cwd=tempdir).wait () + + generated_path = os.path.join (tempdir, hh) + with open (generated_path, "rb") as temp_file: + generated = temp_file.read() + + with open (hh, "rb") as current_file: + current = current_file.read() + + # overwrite only if is changed + if generated != current: + shutil.copyfile (generated_path, hh) + +shutil.rmtree(tempdir) diff --git a/src/meson.build b/src/meson.build index c17513beb..698f00647 100644 --- a/src/meson.build +++ b/src/meson.build @@ -289,16 +289,13 @@ custom_target('hb-version.h', ragel = find_program('ragel', required: false) if not ragel.found() - message('You have to install ragel if you are going to develop HarfBuzz itself') + warning('You have to install ragel if you are going to develop HarfBuzz itself') else - foreach rl : hb_base_ragel_sources - hh = rl.split('.rl')[0] + '.hh' - custom_target(hh, - build_by_default: true, - depfile: rl, - output: hh, - command: [find_program('gen-ragel-artifacts.py'), meson.current_source_dir(), rl]) - endforeach + custom_target('ragel header files', + build_by_default: true, + input: hb_base_ragel_sources, + output: '.ragel-artifacts', + command: [find_program('gen-ragel-artifacts.py')] + hb_base_ragel_sources) endif custom_target('harfbuzz.cc',