From 9e57b5c3f48c15aa8fb0ced408ec745eb6ad8874 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Sun, 23 Aug 2020 11:24:52 +1000 Subject: Generate an appropriate shebang line for MakeHeader script Use configure.ac to handle platform differences where some build hosts have only a python3, or only python, binary. Related to https://github.com/htop-dev/htop/pull/6 --- .gitignore | 1 + configure.ac | 5 ++- scripts/MakeHeader.py | 104 ----------------------------------------------- scripts/MakeHeader.py.in | 104 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 105 deletions(-) delete mode 100755 scripts/MakeHeader.py create mode 100755 scripts/MakeHeader.py.in diff --git a/.gitignore b/.gitignore index f94f3f52..b642d7cb 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,5 @@ libtool ltmain.sh m4/ missing +scripts/MakeHeader.py stamp-h1 diff --git a/configure.ac b/configure.ac index 323a94e9..a2eb53ec 100644 --- a/configure.ac +++ b/configure.ac @@ -299,6 +299,9 @@ AC_ARG_ENABLE([werror], [AS_HELP_STRING([--enable-werror], [Treat warnings as er AS_IF([test "x$enable_werror" = "xyes"], [AM_CFLAGS="$AM_CFLAGS -Werror"]) AC_SUBST([AM_CFLAGS]) +AC_CHECK_PROGS(PYTHON, [python python3 python2]) +AC_SUBST(PYTHON) + # Bail out on errors. # ---------------------------------------------------------------------- if test ! -z "$missing_libraries"; then @@ -320,7 +323,7 @@ AM_CONDITIONAL([HTOP_DARWIN], [test "$my_htop_platform" = darwin]) AM_CONDITIONAL([HTOP_SOLARIS], [test "$my_htop_platform" = solaris]) AM_CONDITIONAL([HTOP_UNSUPPORTED], [test "$my_htop_platform" = unsupported]) AC_SUBST(my_htop_platform) -AC_CONFIG_FILES([Makefile htop.1]) +AC_CONFIG_FILES([Makefile htop.1 scripts/MakeHeader.py], [chmod +x scripts/MakeHeader.py]) AC_OUTPUT if test "$my_htop_platform" = "unsupported" diff --git a/scripts/MakeHeader.py b/scripts/MakeHeader.py deleted file mode 100755 index 3ef34b88..00000000 --- a/scripts/MakeHeader.py +++ /dev/null @@ -1,104 +0,0 @@ -#!/usr/bin/env python -import os, sys, string, io -try: - from StringIO import StringIO -except ImportError: - StringIO = io.StringIO - -ANY=1 -COPY=2 -SKIP=3 -SKIPONE=4 - -state = ANY -static = 0 - -file = io.open(sys.argv[1], "r", encoding="utf-8") -name = sys.argv[1][:-2] - -out = StringIO() - -selfheader = '#include "' + name + '.h"' - -out.write( "/* Do not edit this file. It was automatically generated. */\n" ) -out.write( "\n" ) - -out.write( "#ifndef HEADER_" + os.path.basename(name) + "\n") -out.write( "#define HEADER_" + os.path.basename(name) + "\n") -is_blank = False -for line in file.readlines(): - line = line[:-1] - if state == ANY: - if line == '/*{': - state = COPY - elif line == selfheader: - pass - elif line.find("#include") == 0: - pass - elif line.find("htop - ") == 0 and line[-2:] == ".c": - out.write(line[:-2] + ".h\n") - elif line.find("static ") != -1: - if line[-1] == "{": - state = SKIP - static = 1 - else: - state = SKIPONE - elif len(line) > 1: - static = 0 - equals = line.find(" = ") - if line[-3:] == "= {": - out.write( "extern " + line[:-4] + ";\n" ) - state = SKIP - elif equals != -1: - out.write("extern " + line[:equals] + ";\n" ) - elif line.startswith("typedef struct"): - state = SKIP - elif line[-1] == "{": - out.write("extern " + line[:-2].replace("inline ", "") + ";\n") - state = SKIP - elif line[-1] == ";": - out.write("extern " + line + "\n") - else: - out.write( line + "\n") - is_blank = False - elif line == "": - if not is_blank: - out.write( line + "\n") - is_blank = True - else: - out.write( line + "\n") - is_blank = False - elif state == COPY: - is_blank = False - if line == "}*/": - state = ANY - else: - out.write( line + "\n") - elif state == SKIP: - is_blank = False - if len(line) >= 1 and line[0] == "}": - if static == 1: - state = SKIPONE - else: - state = ANY - static = 0 - elif state == SKIPONE: - is_blank = False - state = ANY - -out.write( "\n" ) -out.write( "#endif\n" ) - -# only write a new .h file if something changed. -# This prevents a lot of recompilation during development -out.seek(0) -try: - with io.open(name + ".h", "r", encoding="utf-8") as orig: - origcontents = orig.readlines() -except: - origcontents = "" -if origcontents != out.readlines(): - with io.open(name + ".h", "w", encoding="utf-8") as new: - print("Writing "+name+".h") - new.write(out.getvalue()) -out.close() diff --git a/scripts/MakeHeader.py.in b/scripts/MakeHeader.py.in new file mode 100755 index 00000000..9da6685a --- /dev/null +++ b/scripts/MakeHeader.py.in @@ -0,0 +1,104 @@ +#!/usr/bin/env @PYTHON@ +import os, sys, string, io +try: + from StringIO import StringIO +except ImportError: + StringIO = io.StringIO + +ANY=1 +COPY=2 +SKIP=3 +SKIPONE=4 + +state = ANY +static = 0 + +file = io.open(sys.argv[1], "r", encoding="utf-8") +name = sys.argv[1][:-2] + +out = StringIO() + +selfheader = '#include "' + name + '.h"' + +out.write( "/* Do not edit this file. It was automatically generated. */\n" ) +out.write( "\n" ) + +out.write( "#ifndef HEADER_" + os.path.basename(name) + "\n") +out.write( "#define HEADER_" + os.path.basename(name) + "\n") +is_blank = False +for line in file.readlines(): + line = line[:-1] + if state == ANY: + if line == '/*{': + state = COPY + elif line == selfheader: + pass + elif line.find("#include") == 0: + pass + elif line.find("htop - ") == 0 and line[-2:] == ".c": + out.write(line[:-2] + ".h\n") + elif line.find("static ") != -1: + if line[-1] == "{": + state = SKIP + static = 1 + else: + state = SKIPONE + elif len(line) > 1: + static = 0 + equals = line.find(" = ") + if line[-3:] == "= {": + out.write( "extern " + line[:-4] + ";\n" ) + state = SKIP + elif equals != -1: + out.write("extern " + line[:equals] + ";\n" ) + elif line.startswith("typedef struct"): + state = SKIP + elif line[-1] == "{": + out.write("extern " + line[:-2].replace("inline ", "") + ";\n") + state = SKIP + elif line[-1] == ";": + out.write("extern " + line + "\n") + else: + out.write( line + "\n") + is_blank = False + elif line == "": + if not is_blank: + out.write( line + "\n") + is_blank = True + else: + out.write( line + "\n") + is_blank = False + elif state == COPY: + is_blank = False + if line == "}*/": + state = ANY + else: + out.write( line + "\n") + elif state == SKIP: + is_blank = False + if len(line) >= 1 and line[0] == "}": + if static == 1: + state = SKIPONE + else: + state = ANY + static = 0 + elif state == SKIPONE: + is_blank = False + state = ANY + +out.write( "\n" ) +out.write( "#endif\n" ) + +# only write a new .h file if something changed. +# This prevents a lot of recompilation during development +out.seek(0) +try: + with io.open(name + ".h", "r", encoding="utf-8") as orig: + origcontents = orig.readlines() +except: + origcontents = "" +if origcontents != out.readlines(): + with io.open(name + ".h", "w", encoding="utf-8") as new: + print("Writing "+name+".h") + new.write(out.getvalue()) +out.close() -- cgit v1.2.3