[v4,1/3] buildtools: add helper to convert text file to header

Message ID 20240801173029.65710-2-stephen@networkplumber.org (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers
Series restore lost cfgfile tests |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger Aug. 1, 2024, 5:29 p.m. UTC
Simple script to read a file and make it into a initialized
C string in a header file.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 buildtools/gen-header.py | 36 ++++++++++++++++++++++++++++++++++++
 buildtools/meson.build   |  2 +-
 2 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 buildtools/gen-header.py
  

Comments

Bruce Richardson Aug. 2, 2024, 3:15 p.m. UTC | #1
On Thu, Aug 01, 2024 at 10:29:08AM -0700, Stephen Hemminger wrote:
> Simple script to read a file and make it into a initialized
> C string in a header file.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  buildtools/gen-header.py | 36 ++++++++++++++++++++++++++++++++++++
>  buildtools/meson.build   |  2 +-
>  2 files changed, 37 insertions(+), 1 deletion(-)
>  create mode 100644 buildtools/gen-header.py
> 
Resulting header output LGTM

Acked-by: Bruce Richardson <bruce.richardson@intel.com>
  

Patch

diff --git a/buildtools/gen-header.py b/buildtools/gen-header.py
new file mode 100644
index 0000000000..06e645863c
--- /dev/null
+++ b/buildtools/gen-header.py
@@ -0,0 +1,36 @@ 
+#! /usr/bin/env python3
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright (c) 2023 Stephen Hemminger <stephen@networkplumber.org>
+
+"""
+Script to read a text file and convert it into a header file.
+"""
+import sys
+import os
+
+
+def main():
+    '''program main function'''
+    print(f'/* File autogenerated by {sys.argv[0]} */')
+    for path in sys.argv[1:]:
+        name = os.path.basename(path)
+        print()
+        print(f'/* generated from {name} */')
+        with open(path, "r") as f:
+            array = name.replace(".", "_")
+            print(f'static const char {array}[] = ' + '{')
+            line = f.readline()
+
+            # make sure empty string is null terminated
+            if not line:
+                print('    ""')
+
+            while line:
+                s = repr(line)
+                print('    {}'.format(s.replace("'", '"')))
+                line = f.readline()
+            print('};')
+
+
+if __name__ == "__main__":
+    main()
diff --git a/buildtools/meson.build b/buildtools/meson.build
index 3adf34e1a8..bc818a71d5 100644
--- a/buildtools/meson.build
+++ b/buildtools/meson.build
@@ -24,6 +24,7 @@  get_numa_count_cmd = py3 + files('get-numa-count.py')
 get_test_suites_cmd = py3 + files('get-test-suites.py')
 has_hugepages_cmd = py3 + files('has-hugepages.py')
 cmdline_gen_cmd = py3 + files('dpdk-cmdline-gen.py')
+header_gen_cmd = py3 + files('gen-header.py')
 
 # install any build tools that end-users might want also
 install_data([
@@ -48,4 +49,3 @@  else
     pmdinfo += 'ar'
     pmdinfogen += 'elf'
 endif
-