build,include: rework compat-build-header.py
authorAnthony PERARD <anthony.perard@citrix.com>
Tue, 7 Sep 2021 07:29:33 +0000 (09:29 +0200)
committerJan Beulich <jbeulich@suse.com>
Tue, 7 Sep 2021 07:29:33 +0000 (09:29 +0200)
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 <anthony.perard@citrix.com>
Acked-by: Wei Liu <wl@xen.org>
xen/include/Makefile
xen/tools/compat-build-header.py

index 4fa10e68f9971b54a4e0c7b55c6149669480e11d..95daa8a289758f7d20d0593207ea50de0dc3eca5 100644 (file)
@@ -46,14 +46,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 <xen/compat.h>" >>$@.new; \
-       $(if $(filter-out compat/arch-%.h,$@),echo "#include <$(patsubst compat/%,public/%,$@)>" >>$@.new;) \
-       grep -v '^# [0-9]' $< | \
-       $(PYTHON) $(BASEDIR)/tools/compat-build-header.py | uniq >>$@.new; \
-       echo "#endif /* $$id */" >>$@.new
+       $(PYTHON) $(BASEDIR)/tools/compat-build-header.py <$< $@ >>$@.new; \
        mv -f $@.new $@
 
 compat/%.i: compat/%.c Makefile
index 065d3b1b6ee855f881e29661ea5566482d0c5309..5f5474fba051dfe7670885bc8b498ec93181ef54 100755 (executable)
@@ -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" ],
  [ r"__IfDeF__ (XEN_HAVE.*)", r"#ifdef \1" ],
@@ -23,7 +29,41 @@ 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 <xen/compat.h>""".format(header_id)
+
+print(header)
+
+if not re.match("compat/arch-.*.h$", output_filename):
+    x = output_filename.replace("compat/","public/")
+    print('#include <%s>' % x)
+
+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("#endif /* %s */" % header_id)