Re: [XEN PATCH v5 16/16] build, include: rework compat-build-header.py

2020-04-28 Thread Wei Liu




On 21/04/2020 17:12, Anthony PERARD wrote:

Replace a mix of shell script and python script by all python script.

No change to the final generated headers.

Signed-off-by: Anthony PERARD 


To the best of my knowledge this patch is doing the right transformation.

Acked-by: Wei Liu 




Re: [XEN PATCH v5 16/16] build, include: rework compat-build-header.py

2020-04-28 Thread Jan Beulich
On 21.04.2020 18:12, Anthony PERARD wrote:
> Replace a mix of shell script and python script by all python script.
> 
> No change to the final generated headers.
> 
> Signed-off-by: Anthony PERARD 
> ---
> 
> Notes:
> v5:
> - Removed -P from CPP when generating compat/%.i
>   -> keep removing linemarkers and keep de-duplicating empty lines.
>   So that all the blank line that currently exist in the generated
>   headers stays in place.

Thanks for doing these adjustments. However, my Python isn't good
enough to actually ack this patch, I'm afraid.

Jan



[XEN PATCH v5 16/16] build,include: rework compat-build-header.py

2020-04-21 Thread Anthony PERARD
Replace a mix of shell script and python script by all python script.

No change to the final generated headers.

Signed-off-by: Anthony PERARD 
---

Notes:
v5:
- Removed -P from CPP when generating compat/%.i
  -> keep removing linemarkers and keep de-duplicating empty lines.
  So that all the blank line that currently exist in the generated
  headers stays in place.

v4:
- new patch

 xen/include/Makefile | 11 +--
 xen/tools/compat-build-header.py | 52 ++--
 2 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/xen/include/Makefile b/xen/include/Makefile
index 537085b82793..12660625119e 100644
--- a/xen/include/Makefile
+++ b/xen/include/Makefile
@@ -51,16 +51,7 @@ public-$(CONFIG_ARM) := $(wildcard public/arch-arm/*.h 
public/arch-arm/*/*.h)
 all: $(headers-y)
 
 compat/%.h: compat/%.i Makefile $(BASEDIR)/tools/compat-build-header.py
-   set -e; id=_$$(echo $@ | tr '[:lower:]-/.' '[:upper:]___'); \
-   echo "#ifndef $$id" >$@.new; \
-   echo "#define $$id" >>$@.new; \
-   echo "#include " >>$@.new; \
-   $(if $(filter-out compat/arch-%.h,$@),echo "#include <$(patsubst 
compat/%,public/%,$@)>" >>$@.new;) \
-   $(if $(prefix-y),echo "$(prefix-y)" >>$@.new;) \
-   grep -v '^# [0-9]' $< | \
-   $(PYTHON) $(BASEDIR)/tools/compat-build-header.py | uniq >>$@.new; \
-   $(if $(suffix-y),echo "$(suffix-y)" >>$@.new;) \
-   echo "#endif /* $$id */" >>$@.new
+   $(PYTHON) $(BASEDIR)/tools/compat-build-header.py <$< $@ "$(prefix-y)" 
"$(suffix-y)" >>$@.new; \
mv -f $@.new $@
 
 compat/%.i: compat/%.c Makefile
diff --git a/xen/tools/compat-build-header.py b/xen/tools/compat-build-header.py
index b85c43f13faf..34d5343c3dd9 100755
--- a/xen/tools/compat-build-header.py
+++ b/xen/tools/compat-build-header.py
@@ -2,6 +2,12 @@
 
 import re,sys
 
+try:
+maketrans = str.maketrans
+except AttributeError:
+# For python2
+from string import maketrans
+
 pats = [
  [ r"__InClUdE__(.*)", r"#include\1\n#pragma pack(4)" ],
  [ r"__IfDeF__ (XEN_HAVE.*)", r"#ifdef \1" ],
@@ -20,7 +26,49 @@ pats = [
  [ r"(^|[^\w])long([^\w]|$$)", r"\1int\2" ]
 ];
 
+output_filename = sys.argv[1]
+
+# tr '[:lower:]-/.' '[:upper:]___'
+header_id = '_' + \
+output_filename.upper().translate(maketrans('-/.','___'))
+
+header = """#ifndef {0}
+#define {0}
+#include """.format(header_id)
+
+print(header)
+
+if not re.match("compat/arch-.*.h$", output_filename):
+x = output_filename.replace("compat/","public/")
+print('#include <%s>' % x)
+
+def print_if_nonempty(s):
+if len(s):
+print(s)
+
+print_if_nonempty(sys.argv[2])
+
+last_line_empty = False
 for line in sys.stdin.readlines():
+line = line.rstrip()
+
+# Remove linemarkers generated by the preprocessor.
+if re.match(r"^# \d", line):
+continue
+
+# De-duplicate empty lines.
+if len(line) == 0:
+if not last_line_empty:
+print(line)
+last_line_empty = True
+continue
+else:
+last_line_empty = False
+
 for pat in pats:
-line = re.subn(pat[0], pat[1], line)[0]
-print(line.rstrip())
+line = re.sub(pat[0], pat[1], line)
+print(line)
+
+print_if_nonempty(sys.argv[3])
+
+print("#endif /* %s */" % header_id)
-- 
Anthony PERARD