Files
node/deps/libffi/generate-headers.py
cjihrigandPaolo Insogna d0fa608c07 lib,src,test,doc: add node:ffi module
PR-URL: https://github.com/nodejs/node/pull/62072
Co-authored-by: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Bryan English <bryan@bryanenglish.com>
2026-04-14 10:44:30 +01:00

245 lines
7.0 KiB
Python

#!/usr/bin/env python3
import argparse
import os
import platform
import sys
from pathlib import Path
LIBFFI_VERSION = '3.5.2'
LIBFFI_VERSION_NUMBER = '30502'
TARGETS = {
('freebsd', 'arm'): ('ARM', 'arm'),
('freebsd', 'arm64'): ('AARCH64', 'aarch64'),
('freebsd', 'x64'): ('X86_64', 'x86'),
('linux', 'arm'): ('ARM', 'arm'),
('linux', 'arm64'): ('AARCH64', 'aarch64'),
('linux', 'x64'): ('X86_64', 'x86'),
('mac', 'arm64'): ('AARCH64', 'aarch64'),
('mac', 'x64'): ('X86_64', 'x86'),
('win', 'arm'): ('ARM_WIN32', 'arm'),
('win', 'arm64'): ('ARM_WIN64', 'aarch64'),
('win', 'x64'): ('X86_WIN64', 'x86'),
}
def get_target(os_name, target_arch):
try:
return TARGETS[(os_name, target_arch)]
except KeyError as exc:
supported = ', '.join(
f'{os_name}/{arch}' for os_name, arch in sorted(TARGETS))
raise ValueError(
f'Unsupported libffi target {os_name}/{target_arch}. '
f'Supported targets: {supported}.') from exc
def has_long_double(os_name, target_arch):
if os_name == 'win':
return '0'
if os_name == 'mac' and target_arch == 'arm64':
return '0'
if target_arch == 'arm':
return '0'
return '1'
def uses_exec_trampoline_table(os_name, target_arch):
return os_name == 'mac' and target_arch in ('arm', 'arm64')
def render_ffi_header(base_dir, os_name, target_arch, target):
template = (base_dir / 'include' / 'ffi.h.in').read_text(encoding='utf-8')
replacements = {
'@FFI_EXEC_TRAMPOLINE_TABLE@':
'1' if uses_exec_trampoline_table(os_name, target_arch) else '0',
'@FFI_VERSION_NUMBER@': LIBFFI_VERSION_NUMBER,
'@FFI_VERSION_STRING@': LIBFFI_VERSION,
'@HAVE_LONG_DOUBLE@': has_long_double(os_name, target_arch),
'@TARGET@': target,
'@VERSION@': LIBFFI_VERSION,
}
for source, replacement in replacements.items():
template = template.replace(source, replacement)
return template
def render_fficonfig(os_name, target_arch):
lines = [
'/* Auto-generated by generate-headers.py. */',
'#ifndef LIBFFI_FFICONFIG_H_',
'#define LIBFFI_FFICONFIG_H_',
'',
'#define HAVE_ALLOCA 1',
'#define HAVE_MEMCPY 1',
'#define HAVE_MEMORY_H 1',
'#define HAVE_STDINT_H 1',
'#define HAVE_STRING_H 1',
'#define STDC_HEADERS 1',
]
if os_name != 'win':
lines.extend([
'#define HAVE_AS_CFI_PSEUDO_OP 1',
'#define HAVE_HIDDEN_VISIBILITY_ATTRIBUTE 1',
'#define HAVE_MKOSTEMP 1',
'#define HAVE_RO_EH_FRAME 1',
'#define EH_FRAME_FLAGS "a"',
])
if target_arch in ('x64', 'x86') and os_name != 'win':
lines.append('#define HAVE_AS_X86_PCREL 1')
if uses_exec_trampoline_table(os_name, target_arch):
lines.append('#define FFI_EXEC_TRAMPOLINE_TABLE 1')
elif os_name in ('freebsd', 'mac'):
lines.append('#define FFI_MMAP_EXEC_WRIT 1')
if os_name == 'linux':
lines.extend([
'#define HAVE_DLFCN_H 1',
'#define HAVE_MMAP 1',
'#define HAVE_MMAP_ANON 1',
'#define HAVE_MMAP_FILE 1',
'#define HAVE_SYS_MMAN_H 1',
'#define HAVE_UNISTD_H 1',
])
elif os_name in ('freebsd', 'mac'):
lines.extend([
'#define HAVE_DLFCN_H 1',
'#define HAVE_MMAP 1',
'#define HAVE_MMAP_ANON 1',
'#define HAVE_MMAP_DEV_ZERO 1',
'#define HAVE_MMAP_FILE 1',
'#define HAVE_SYS_MMAN_H 1',
'#define HAVE_UNISTD_H 1',
])
if os_name == 'mac' and target_arch == 'arm64':
lines.extend([
'#if defined(__arm64e__)',
'#define HAVE_ARM64E_PTRAUTH 1',
'#endif',
])
lines.extend([
'',
'#ifdef HAVE_HIDDEN_VISIBILITY_ATTRIBUTE',
'#ifdef LIBFFI_ASM',
'#ifdef __APPLE__',
'#define FFI_HIDDEN(name) .private_extern name',
'#else',
'#define FFI_HIDDEN(name) .hidden name',
'#endif',
'#else',
'#define FFI_HIDDEN __attribute__ ((visibility ("hidden")))',
'#endif',
'#else',
'#ifdef LIBFFI_ASM',
'#define FFI_HIDDEN(name)',
'#else',
'#define FFI_HIDDEN',
'#endif',
'#endif',
'',
'#endif /* LIBFFI_FFICONFIG_H_ */',
'',
])
return '\n'.join(lines)
def generate_headers(output_dir, target_arch, os_name):
base_dir = Path(__file__).resolve().parent
output_dir = Path(str(output_dir).strip().strip('"'))
output_dir.mkdir(parents=True, exist_ok=True)
target, target_dir = get_target(os_name, target_arch)
ffitarget_src = base_dir / 'src' / target_dir / 'ffitarget.h'
if not ffitarget_src.exists():
raise FileNotFoundError(f'Missing libffi target header: {ffitarget_src}')
(output_dir / 'ffi.h').write_text(
render_ffi_header(base_dir, os_name, target_arch, target),
encoding='utf-8')
(output_dir / 'fficonfig.h').write_text(
render_fficonfig(os_name, target_arch),
encoding='utf-8')
(output_dir / 'ffitarget.h').write_text(
ffitarget_src.read_text(encoding='utf-8'),
encoding='utf-8')
def detect_os_name():
if sys.platform.startswith('win'):
return 'win'
if sys.platform == 'darwin':
return 'mac'
if sys.platform.startswith('linux'):
return 'linux'
if sys.platform.startswith('freebsd'):
return 'freebsd'
raise ValueError(f'Unsupported host platform {sys.platform!r}')
def detect_target_arch():
candidates = [
os.environ.get('TARGET_ARCH'),
os.environ.get('npm_config_arch'),
os.environ.get('VSCMD_ARG_TGT_ARCH'),
os.environ.get('Platform'),
os.environ.get('PROCESSOR_ARCHITECTURE'),
platform.machine(),
]
aliases = {
'amd64': 'x64',
'x86_64': 'x64',
'x64': 'x64',
'win32': 'x64',
'i386': 'x86',
'i686': 'x86',
'x86': 'x86',
'arm64': 'arm64',
'aarch64': 'arm64',
'arm': 'arm',
}
for candidate in candidates:
if not candidate:
continue
normalized = aliases.get(candidate.lower())
if normalized is not None:
return normalized
raise ValueError('Unable to determine target architecture for libffi headers')
def main(argv=None):
parser = argparse.ArgumentParser(description='Generate libffi headers')
parser.add_argument('--output-dir', required=True)
parser.add_argument('--target-arch')
parser.add_argument('--os')
args = parser.parse_args(argv)
try:
generate_headers(args.output_dir,
args.target_arch or detect_target_arch(),
args.os or detect_os_name())
except Exception as exc: # pylint: disable=broad-except
print(exc, file=sys.stderr)
return 1
return 0
if __name__ == '__main__':
sys.exit(main())