1#!/usr/bin/env python3 2# 3# Copyright (c) 2021 Nordic Semiconductor NA 4# 5# SPDX-License-Identifier: Apache-2.0 6 7"""Check minimal libc error numbers against newlib. 8 9This script loads the errno.h included in Zephyr's minimal libc and checks its 10contents against the SDK's newlib errno.h. This is done to ensure that both C 11libraries are aligned at all times. 12""" 13 14import os 15import re 16import sys 17from pathlib import Path 18 19 20def parse_errno(path): 21 with open(path) as f: 22 r = re.compile(r'^\s*#define\s+([A-Z]+)\s+([0-9]+)') 23 errnos = [] 24 for line in f: 25 m = r.match(line) 26 if m: 27 errnos.append(m.groups()) 28 29 return errnos 30 31 32def main(): 33 minimal = Path("lib/libc/minimal/include/errno.h") 34 newlib = Path("arm-zephyr-eabi/arm-zephyr-eabi/include/sys/errno.h") 35 36 try: 37 minimal = os.environ['ZEPHYR_BASE'] / minimal 38 newlib = os.environ['ZEPHYR_SDK_INSTALL_DIR'] / newlib 39 except KeyError as e: 40 print(f'Environment variable missing: {e}', file=sys.stderr) 41 sys.exit(1) 42 43 minimal = parse_errno(minimal) 44 newlib = parse_errno(newlib) 45 46 for e in minimal: 47 if ( 48 e[0] not in [x[0] for x in newlib] 49 or e[1] != next(filter(lambda _e: _e[0] == e[0], newlib))[1] 50 ): 51 print('Invalid entry in errno.h:', file=sys.stderr) 52 print(f'{e[0]} (with value {e[1]})', file=sys.stderr) 53 sys.exit(1) 54 55 print('errno.h validated correctly') 56 57 58if __name__ == "__main__": 59 main() 60