[v4,1/3] buildtools: add helper to convert text file to header
Checks
Commit Message
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
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>
new file mode 100644
@@ -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()
@@ -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
-