xref: /aosp_15_r20/external/executorch/backends/xnnpack/third-party/generate-cpuinfo-wrappers.py (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1#!/usr/bin/env python3
2
3from __future__ import print_function
4import os
5
6
7CPUINFO_SOURCES = {
8    None: [
9        "init.c",
10        "api.c",
11        "cache.c",
12        "log.c",
13    ],
14    "defined(__linux__)": [
15        "linux/multiline.c",
16        "linux/cpulist.c",
17        "linux/mockfile.c",
18        "linux/smallfile.c",
19        "linux/processors.c",
20    ],
21    "defined(__MACH__) && defined(__APPLE__)": [
22        "mach/topology.c",
23    ],
24    "defined(__i386__) || defined(__i686__) || defined(__x86_64__) || defined(_WIN32)": [
25        "x86/cache/init.c",
26        "x86/cache/deterministic.c",
27        "x86/cache/descriptor.c",
28        "x86/info.c",
29        "x86/mockcpuid.c",
30        "x86/isa.c",
31        "x86/topology.c",
32        "x86/name.c",
33        "x86/init.c",
34        "x86/uarch.c",
35        "x86/vendor.c",
36    ],
37    "(defined(__i386__) || defined(__i686__) || defined(__x86_64__)) && defined(__linux__)": [
38        "x86/linux/init.c",
39        "x86/linux/cpuinfo.c",
40    ],
41    "(defined(__i386__) || defined(__i686__) || defined(__x86_64__)) && defined(__MACH__) && defined(__APPLE__)": [
42        "x86/mach/init.c",
43    ],
44    "defined(_WIN32)": [
45        "x86/windows/init.c",
46    ],
47    "(defined(__arm__) || defined(__aarch64__)) && defined(__linux__)": [
48        "arm/linux/cpuinfo.c",
49        "arm/linux/hwcap.c",
50        "arm/linux/init.c",
51        "arm/linux/clusters.c",
52        "arm/linux/midr.c",
53        "arm/linux/chipset.c",
54        "arm/tlb.c",
55        "arm/uarch.c",
56        "arm/cache.c",
57    ],
58    "defined(__arm__) && defined(__linux__)": [
59        "arm/linux/aarch32-isa.c",
60    ],
61    "defined(__aarch64__) && defined(__linux__)": [
62        "arm/linux/aarch64-isa.c",
63    ],
64    "(defined(__arm__) || defined(__aarch64__)) && defined(__ANDROID__)": [
65        "arm/android/properties.c",
66    ],
67    "(defined(__arm__) || defined(__aarch64__)) && defined(TARGET_OS_MAC) && TARGET_OS_MAC": [
68        "arm/mach/init.c",
69    ],}
70
71
72if __name__ == "__main__":
73    for condition, filenames in CPUINFO_SOURCES.items():
74        for filename in filenames:
75            filepath = os.path.join("cpuinfo-wrappers", filename)
76            if not os.path.exists(os.path.dirname(filepath)):
77                print(filepath)
78                os.makedirs(os.path.dirname(filepath))
79            with open(filepath, "w") as wrapper:
80                print("/* Auto-generated by generate-wrappers.py script. Do not modify */", file=wrapper)
81                print(file=wrapper)
82                print("#ifdef __APPLE__", file=wrapper)
83                print("\t#include <TargetConditionals.h>", file=wrapper)
84                print("#endif /* __APPLE__ */", file=wrapper)
85                print(file=wrapper)
86
87                if not condition:
88                    print("#include <%s>" % filename, file=wrapper)
89                else:
90                    # Include source file only if condition is satisfied
91                    print("#if %s" % condition, file=wrapper)
92                    print("#include <%s>" % filename, file=wrapper)
93                    print("#endif /* %s */" % condition, file=wrapper)
94