build: fix libclang-based parsing on macOS
On macOS, libclang (used via the cindex Python module) does not
automatically locate system headers like <stdint.h>. This patch
adds the `-isysroot` flag during parsing and points it to the
macOS SDK path obtained via `xcrun`. This ensures standard headers
are resolved correctly when parsing C source files using libclang.
Signed-off-by: Antonio de Angelis <antonio.deangelis@arm.com>
Change-Id: I4253228647360974e5f666be587a6c68efc5bdd9
diff --git a/tools/modules/c_struct.py b/tools/modules/c_struct.py
index a019fce..2598c54 100644
--- a/tools/modules/c_struct.py
+++ b/tools/modules/c_struct.py
@@ -10,6 +10,8 @@
import struct
from collections import Counter
import os
+import subprocess
+import platform
import logging
logger = logging.getLogger("TF-M.{}".format(__name__))
@@ -36,6 +38,21 @@
'long double' : 'd',
}
+
+def get_macos_sdk_path():
+ """Get the path to the macOS SDK via xcrun."""
+ try:
+ result = subprocess.run(
+ ['xcrun', '--sdk', 'macosx', '--show-sdk-path'],
+ capture_output=True,
+ text=True,
+ check=True
+ )
+ return result.stdout.strip()
+ except subprocess.CalledProcessError as e:
+ raise RuntimeError(f"Failed to get macOS SDK path: {e.stderr}")
+
+
def _c_struct_or_union_from_cl_cursor(cursor, name, f):
def is_field(x):
if x.kind == cl.CursorKind.FIELD_DECL:
@@ -613,6 +630,9 @@
args = ["-I{}".format(i) for i in includes if os.path.isdir(i)]
args += ["-D{}".format(d) for d in defines]
+ if platform.system() == 'Darwin':
+ args.extend(['-isysroot', get_macos_sdk_path()])
+
if not os.path.isfile(h_file):
return FileNotFoundError