Robin Jarry, Oct 06, 2024 at 14:25:
I think you need to change run_command() to custom_target(). I was thinking of this patch to be much simpler as follows:

diff --git a/buildtools/chkincs/check_extern_c.sh 
b/buildtools/chkincs/check_extern_c.sh

Sorry, I didn't think about windows compatibility which probably requires python. Please disregard my previous message. Here is a simplified python version:

diff --git a/buildtools/chkincs/check_extern_c.py 
b/buildtools/chkincs/check_extern_c.py
new file mode 100755
index 000000000000..9534ef10ba4e
--- /dev/null
+++ b/buildtools/chkincs/check_extern_c.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: BSD-3-Clause
+
+import argparse
+import re
+import sys
+
+SYMBOL_EXPR = [
+    # external variable definitions
+    re.compile(r"^extern\s+[a-z0-9_]+\s", re.MULTILINE),
+    # exported functions
+    re.compile(r"rte_[a-z0-9_]+\("),
+    re.compile(r"cmdline_[a-z0-9_]+\("),
+    re.compile(r"vt100_[a-z0-9_]+\("),
+    re.compile(r"rdline_[a-z0-9_]+\("),
+    re.compile(r"cirbuf_[a-z0-9_]+\("),
+    # windows compatibility
+    re.compile(r"pthread_[a-z0-9_]+\("),
+    re.compile(r"regcomp\("),
+    re.compile(r"count_cpu\("),
+]
+
+
+def has_symbols(buf):
+    for expr in SYMBOL_EXPR:
+        if expr.search(buf):
+            return True
+    return False
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("mode", choices=("redundant", "missing"))
+    parser.add_argument("headers", nargs="+")
+    args = parser.parse_args()
+
+    ret = 0
+
+    for h in args.headers:
+        with open(h) as f:
+            buf = f.read()
+        if args.mode == "missing":
+            if has_symbols(buf) and 'extern "C"' not in buf:
+                print('error: missing extern "C":', h)
+                ret = 1
+        elif args.mode == "redundant":
+            if not has_symbols(buf) and 'extern "C"' in buf:
+                print('error: redundant extern "C":', h)
+                ret = 1
+
+    return ret
+
+
+if __name__ == "__main__":
+    sys.exit(main())
diff --git a/buildtools/chkincs/meson.build b/buildtools/chkincs/meson.build
index f2dadcae18ef..381f30f42431 100644
--- a/buildtools/chkincs/meson.build
+++ b/buildtools/chkincs/meson.build
@@ -38,14 +38,11 @@ if not add_languages('cpp', required: false)
endif

# check for extern C in files, since this is not detected as an error by the 
compiler
-grep = find_program('grep', required: false)
-if grep.found()
-    errlist = run_command([grep, '--files-without-match', '^extern "C"', 
dpdk_chkinc_headers],
-            check: false, capture: true).stdout().split()
-    if errlist != []
-        error('Files missing C++ \'extern "C"\' guards:\n- ' + '\n- 
'.join(errlist))
-    endif
-endif
+custom_target('check-extern-c',
+        command: files('check_extern_c.py') + ['missing', '@INPUT@'],
+        input: dpdk_chkinc_headers,
+        output: 'check-extern-c',
+        build_by_default: true)

gen_cpp_files = generator(gen_c_file_for_header,
        output: '@BASENAME@.cpp',






Reply via email to