File size: 4,674 Bytes
8b7c501 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import codecs
import math
import os
import re
import sys
import yaml
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
parser = argparse.ArgumentParser(
description='Amalgamation utility for microkernels')
parser.add_argument("-s", "--set", metavar="SET", required=True,
help="List of microkernel filenames in the BUILD.bazel file")
parser.add_argument("-i", "--include", metavar="INCLUDE",
help="Header file to include (e.g. immintrin.h, arm_neon.h)")
parser.add_argument("-o", "--output", metavar="FILE", required=True,
help='Output (C source) file')
def main(args):
options = parser.parse_args(args)
build_path = os.path.join(ROOT_DIR, "..", "BUILD.bazel")
with codecs.open(build_path, "r", encoding="utf-8") as build_file:
build_text = build_file.read()
pattern = r"\b" + options.set + r"\b\s*=\s*\["
match = re.search(pattern, build_text)
if not match:
raise ValueError(
"Failed to find file set %s (regex \"%s\") inside the BUILD.bazel file" %
(options.set, pattern))
start_pos = match.end()
end_pos = build_text.find("]", start_pos)
fileset = [filename.strip()[1:-1] for filename in
build_text[start_pos:end_pos].split(",")]
amalgam_lines = list()
amalgam_includes = set()
for filename in sorted(fileset):
if not filename:
continue
filepath = os.path.join(ROOT_DIR, "..", filename)
with codecs.open(filepath, "r", encoding="utf-8") as file:
filelines = file.read().splitlines()
consumed_license = False
consumed_includes = False
for line in filelines:
if line.startswith("//"):
if not consumed_license:
# Skip and generate a standard license header for amalgamated file
continue
elif line.lstrip().startswith("#"):
if not consumed_includes:
amalgam_includes.add(line)
continue
consumed_license = True
elif not line:
if not consumed_includes:
# Skip empty lines until end of headers
continue
else:
consumed_license = True
consumed_includes = True
amalgam_lines.append(line)
amalgam_lines.append("")
# Multi-line sequence for XOP intrinsics, which don't have a standardized header
amalgam_includes.discard("#ifdef _MSC_VER")
amalgam_includes.discard(" #include <intrin.h>")
amalgam_includes.discard("#else")
amalgam_includes.discard(" #include <x86intrin.h>")
amalgam_includes.discard("#endif")
# Single-line sequences for intrinsics with a standardized header
amalgam_includes.discard("#include <arm_acle.h>")
amalgam_includes.discard("#include <arm_fp16.h>")
amalgam_includes.discard("#include <arm_neon.h>")
amalgam_includes.discard("#include <emmintrin.h>")
amalgam_includes.discard("#include <immintrin.h>")
amalgam_includes.discard("#include <nmmintrin.h>")
amalgam_includes.discard("#include <smmintrin.h>")
amalgam_includes.discard("#include <tmmintrin.h>")
amalgam_includes.discard("#include <xmmintrin.h>")
amalgam_includes.discard("#include <wasm_simd128.h>")
amalgam_text = """\
// Copyright 2021 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
"""
amalgam_text += "\n".join(sorted(inc for inc in amalgam_includes if
not inc.startswith("#include <xnnpack/")))
if options.include:
if options.include == "xopintrin.h":
amalgam_text += "\n\n"
amalgam_text += "#ifdef _MSC_VER\n"
amalgam_text += " #include <intrin.h>\n"
amalgam_text += "#else\n"
amalgam_text += " #include <x86intrin.h>\n"
amalgam_text += "#endif\n\n"
else:
amalgam_text += "\n\n#include <%s>\n\n" % options.include
else:
amalgam_text += "\n\n"
amalgam_text += "\n".join(sorted(inc for inc in amalgam_includes if
inc.startswith("#include <xnnpack/")))
amalgam_text += "\n\n\n"
amalgam_text += "\n".join(amalgam_lines)
txt_changed = True
if os.path.exists(options.output):
with codecs.open(options.output, "r", encoding="utf-8") as amalgam_file:
txt_changed = amalgam_file.read() != amalgam_text
if txt_changed:
with codecs.open(options.output, "w", encoding="utf-8") as amalgam_file:
amalgam_file.write(amalgam_text)
if __name__ == "__main__":
main(sys.argv[1:])
|