On Tue, Jul 26, 2016 at 7:28 AM, Ján Tomko <[email protected]> wrote:
> Instead of spawning three processes per file, rewrite the check
> in perl and run it once for all the files.
> ---
> Alternatively, grep --max-count 1 could help with getting rid of
> the per-file commands and extra sed, but I don't know if it's
> portable enough.
>
>  ChangeLog    |  7 +++++++
>  top/maint.mk | 25 ++++++++++++++++++-------
>  2 files changed, 25 insertions(+), 7 deletions(-)
>
> diff --git a/ChangeLog b/ChangeLog
> index b698a6c..64e2026 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,5 +1,12 @@
>  2016-07-26  Ján Tomko  <[email protected]>
>
> +       maint.mk: speed up require_config_h_first
> +
> +       Instead of spawning three processes per file, rewrite the check
> +       in perl and run it once for all the files.

Thank you. Nice patch.
I've made some small changes and am ready to push this, but will wait
to hear from you.
From dce9706ee7661229bde91ee2e2357b68ffe68c7b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A1n=20Tomko?= <[email protected]>
Date: Tue, 26 Jul 2016 16:28:16 +0200
Subject: [PATCH] maint.mk: speed up require_config_h_first

Instead of spawning three processes per file,
rewrite the check in perl and run it once for all the files.
* top/maint.mk (perl_config_h_first_): Define.
(sc_require_config_h_first): Use it in place of shell code.
---
 ChangeLog    |  8 ++++++++
 top/maint.mk | 23 ++++++++++++++++-------
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2414e8e..e70d6fc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2016-07-30  Ján Tomko  <[email protected]>
+
+       maint.mk: speed up require_config_h_first
+       Instead of spawning three processes per file,
+       rewrite the check in perl and run it once for all the files.
+       * top/maint.mk (perl_config_h_first_): Define.
+       (sc_require_config_h_first): Use it in place of shell code.
+
 2016-07-15  Paul Eggert  <[email protected]>

        obstack: pacify GCC 6 with -Wnull-dereference
diff --git a/top/maint.mk b/top/maint.mk
index aa23364..f3d55c1 100644
--- a/top/maint.mk
+++ b/top/maint.mk
@@ -437,17 +437,26 @@ sc_require_config_h:
        halt='the above files do not include <config.h>'                \
          $(_sc_search_regexp)

+# Print each file name for which the first #include does not match
+# $(config_h_header).  Like grep -m 1, this only looks at the first match.
+perl_config_h_first_ =                                                 \
+  -e 'BEGIN {$$ret = 0}'                                               \
+  -e 'if (/^\# *include\b/) {'                                         \
+  -e '  if (not m{^\# *include $(config_h_header)}) {'                 \
+  -e '    print "$$ARGV\n";'                                           \
+  -e '    $$ret = 1;'                                                  \
+  -e '  }'                                                             \
+  -e '  \# Move on to next file after first include'                   \
+  -e '  close ARGV;'                                                   \
+  -e '}'                                                               \
+  -e 'END {exit $$ret}'
+
 # You must include <config.h> before including any other header file.
 # This can possibly be via a package-specific header, if given by cfg.mk.
 sc_require_config_h_first:
        @if $(VC_LIST_EXCEPT) | grep '\.c$$' > /dev/null; then          \
-         fail=0;                                                       \
-         for i in $$($(VC_LIST_EXCEPT) | grep '\.c$$'); do             \
-           grep '^# *include\>' $$i | $(SED) 1q                        \
-               | grep -E '^# *include $(config_h_header)' > /dev/null  \
-             || { echo $$i; fail=1; };                                 \
-         done;                                                         \
-         test $$fail = 1 &&                                            \
+         files=$$($(VC_LIST_EXCEPT) | grep '\.c$$') &&                 \
+         perl -n $(perl_config_h_first_) $$files ||                    \
            { echo '$(ME): the above files include some other header'   \
                'before <config.h>' 1>&2; exit 1; } || :;               \
        else :;                                                         \
-- 
2.8.0-rc2

Reply via email to