1#!/usr/bin/env python
2#
3# Copyright 2021 Nikita Melekhin. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# The tool checks the compatability of linkern and libc bits/ structs
8
9import sys
10import os
11
12
13def list_of_files_relative(opath):
14 res = []
15
16 def is_file_type(name, ending):
17 if len(name) <= len(ending):
18 return False
19 return (name[-len(ending)-1::] == '.'+ending)
20
21 for path, subdirs, files in os.walk(opath):
22 for name in files:
23 # It runs from out dir, at least it should
24 file = path + "/" + name
25 if is_file_type(file, 'h'):
26 res.append(file[len(opath):])
27 return res
28
29
30def process_file(file, res_map):
31 def accept_line(line):
32 if line.endswith("\n"):
33 line = line[:-1]
34
35 if (len(line) == 0):
36 return False
37
38 block = [
39 "#include",
40 "#ifndef",
41 "#endif",
42 "#define _KERNEL_LIBKERN",
43 "#define _LIBC",
44 ]
45
46 for b in block:
47 if line.startswith(b):
48 return False
49
50 return True
51
52 with open(file) as ofile:
53 for line in ofile:
54 if line.endswith("\n"):
55 line = line[:-1]
56 if accept_line(line):
57 if line in res_map:
58 res_map[line] += 1
59 else:
60 res_map[line] = 1
61
62
63def create_map_of_lines(files):
64 res_map = {}
65 for f in files:
66 process_file(f, res_map)
67 return res_map
68
69
70def check_files(pbase, pslave, files):
71 filesbase = [pbase+x for x in files]
72 filesslave = [pslave+x for x in files]
73 libkern_map = create_map_of_lines(filesbase)
74 libc_map = create_map_of_lines(filesslave)
75
76 for i, x in libkern_map.items():
77 if i in libc_map:
78 if x != libc_map[i]:
79 return False
80 else:
81 print("Can't find {0} in LibC".format(i))
82 return False
83
84 return True
85
86
87libkern_files = list_of_files_relative("kernel/include/libkern/bits")
88libc_files = list_of_files_relative("libs/libc/include/bits")
89
90if len(libkern_files) != len(libc_files):
91 print("Note: LibC and LibKern might not be compatible, taking LibKern as base")
92
93if check_files("kernel/include/libkern/bits", "libs/libc/include/bits", libkern_files):
94 print("OK")
95else:
96 print("Failed")
97 exit(1)