-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathgenerate-build-identifier.py
More file actions
executable file
·106 lines (86 loc) · 2.58 KB
/
generate-build-identifier.py
File metadata and controls
executable file
·106 lines (86 loc) · 2.58 KB
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
# Copyright 2024 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.
"""Generates an identifier from the files in the XNNPack project.
This generates a fingerprint of the XNNPack library sources.
"""
import argparse
import hashlib
import os
import sys
import textwrap
parser = argparse.ArgumentParser(
prog="XNNPackFingerprint",
description=(
"Generates a C source file that defines a function that returns a"
" fingerprint of the given XNNPack source files and writes it to the"
" output."
),
)
parser.add_argument(
"--output", required=True, action="store", help="Set the output"
)
parser.add_argument(
"--input_file_list",
required=False,
action="store",
help="Set an input file list to use instead of the arguments.",
)
parser.add_argument(
"inputs",
nargs="*",
help="The source files to use to generate the fingerprint.",
)
FILE_TEMPLATE = """
// clang-format off
// Copyright 2024 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.
// Auto-generated file. Do not edit!
// Generator: scripts/generate-build-identifier.py
//
// The following inputs were used to generate this file.
{genlist}
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
static const uint8_t xnn_build_identifier[] = {{
{id_data}
}};
size_t xnn_experimental_get_build_identifier_size() {{
return sizeof(xnn_build_identifier);
}}
const void* xnn_experimental_get_build_identifier_data() {{
return xnn_build_identifier;
}}
bool xnn_experimental_check_build_identifier(const void* data, const size_t size) {{
if(size != xnn_experimental_get_build_identifier_size()) {{
return false;
}}
return !memcmp(data, xnn_build_identifier, size);
}}
"""
def main(args) -> None:
m = hashlib.sha256()
inputs = args.inputs
if args.input_file_list:
with open(args.input_file_list, "r") as f:
inputs += f.read().splitlines()
inputs = sorted(inputs)
for path in inputs:
with open(path, "rb") as f:
m.update(f.read())
byte_list = ", ".join(str(b).rjust(3, "x") for b in m.digest())
byte_list = textwrap.indent(textwrap.fill(byte_list, width=40), " ").replace(
"x", " "
)
formatted_input_list = "\n".join("// - " + p for p in inputs)
with open(args.output, "w") as out:
out.write(
FILE_TEMPLATE.format(id_data=byte_list, genlist=formatted_input_list)
)
if __name__ == "__main__":
main(parser.parse_args())