Hash :
6937ea98
Author :
Date :
2022-09-08T14:15:10
Use base name of the generator script in various generated files Increases compiler cache hits especially in cases where the file is run during build. Bug: angleproject:7642 Change-Id: I769dae2d7cca2cf1e238531f4cb356bad41b06dd Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3880323 Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Kenneth Russell <kbr@chromium.org> Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com> Reviewed-by: Jamie Madill <jmadill@chromium.org>
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
#!/usr/bin/python3
# Copyright 2018 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# gen_packed_gl_enums.py:
# Code generation for the packed enums.
# NOTE: don't run this script directly. Run scripts/run_code_generation.py.
import json, os, sys
from collections import namedtuple
from collections import OrderedDict
Enum = namedtuple('Enum', ['name', 'values', 'max_value'])
EnumValue = namedtuple('EnumValue', ['name', 'gl_name', 'value'])
Generators = [
{
'json': 'packed_gl_enums.json',
'output': 'PackedGLEnums',
'includes': '#include <angle_gl.h>',
'namespace': 'gl',
'enum_type': 'GLenum',
},
{
'json': 'packed_egl_enums.json',
'output': 'PackedEGLEnums',
'includes': '#include <EGL/egl.h>\n#include <EGL/eglext.h>',
'namespace': 'egl',
'enum_type': 'EGLenum',
},
{
'json': 'packed_cl_enums.json',
'output': 'PackedCLEnums',
'includes': '#include <angle_cl.h>\ntypedef cl_uint CLenum;',
'namespace': 'cl',
'enum_type': 'CLenum',
},
]
def load_enums(path):
with open(path) as map_file:
enums_dict = json.loads(map_file.read(), object_pairs_hook=OrderedDict)
enums = []
for (enum_name, value_list) in enums_dict.items():
values = []
i = 0
for (value_name, value_gl_name) in value_list.items():
values.append(EnumValue(value_name, value_gl_name, i))
i += 1
assert (i < 255) # This makes sure enums fit in the uint8_t
enums.append(Enum(enum_name, values, i))
enums.sort(key=lambda enum: enum.name)
return enums
def generate_include_guard(path):
return path.replace(".", "_").upper()
def header_name_from_cpp_name(path):
return path.replace(".cpp", ".h")
header_template = """// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {data_source_name}.
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// {file_name}:
// Declares ANGLE-specific enums classes for {api_enum_name}s and functions operating
// on them.
#ifndef COMMON_{include_guard}_
#define COMMON_{include_guard}_
{includes}
#include <cstdint>
#include <ostream>
namespace {namespace}
{{
template<typename Enum>
Enum From{api_enum_name}({api_enum_name} from);
{content}
}} // namespace {namespace}
#endif // COMMON_{include_guard}_
"""
enum_declaration_template = """
enum class {enum_name} : uint8_t
{{
{value_declarations}
InvalidEnum = {max_value},
EnumCount = {max_value},
}};
template <>
{enum_name} From{api_enum_name}<{enum_name}>({api_enum_name} from);
{api_enum_name} To{api_enum_name}({enum_name} from);
std::ostream &operator<<(std::ostream &os, {enum_name} value);
"""
def write_header(enums, path_prefix, file_name, data_source_name, includes, namespace,
api_enum_name):
content = ['']
for enum in enums:
value_declarations = []
for value in enum.values:
value_declarations.append(' ' + value.name + ' = ' + str(value.value) + ',')
content.append(
enum_declaration_template.format(
enum_name=enum.name,
max_value=str(enum.max_value),
value_declarations='\n'.join(value_declarations),
api_enum_name=api_enum_name))
header = header_template.format(
content=''.join(content),
data_source_name=data_source_name,
script_name=os.path.basename(sys.argv[0]),
file_name=file_name,
include_guard=generate_include_guard(file_name),
includes=includes,
namespace=namespace,
api_enum_name=api_enum_name)
with (open(path_prefix + file_name, 'wt')) as f:
f.write(header)
cpp_template = """// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {data_source_name}.
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// {file_name}:
// Implements ANGLE-specific enums classes for {api_enum_name}s and functions operating
// on them.
#include "common/debug.h"
#include "common/{header_name}"
namespace {namespace}
{{
{content}
}} // namespace {namespace}
"""
enum_implementation_template = """
template <>
{enum_name} From{api_enum_name}<{enum_name}>({api_enum_name} from)
{{
switch (from)
{{
{from_glenum_cases}
default:
return {enum_name}::InvalidEnum;
}}
}}
{api_enum_name} To{api_enum_name}({enum_name} from)
{{
switch (from)
{{
{to_glenum_cases}
default:
UNREACHABLE();
return 0;
}}
}}
std::ostream &operator<<(std::ostream &os, {enum_name} value)
{{
switch (value)
{{
{ostream_cases}
default:
os << "GL_INVALID_ENUM";
break;
}}
return os;
}}
"""
def write_cpp(enums, path_prefix, file_name, data_source_name, namespace, api_enum_name):
content = ['']
for enum in enums:
from_glenum_cases = []
to_glenum_cases = []
ostream_cases = []
for value in enum.values:
qualified_name = enum.name + '::' + value.name
from_glenum_cases.append(' case ' + value.gl_name + ':\n return ' +
qualified_name + ';')
to_glenum_cases.append(' case ' + qualified_name + ':\n return ' +
value.gl_name + ';')
ostream_cases.append(' case ' + qualified_name + ':\n os << "' +
value.gl_name + '";\n break;')
content.append(
enum_implementation_template.format(
enum_name=enum.name,
from_glenum_cases='\n'.join(from_glenum_cases),
max_value=str(enum.max_value),
to_glenum_cases='\n'.join(to_glenum_cases),
api_enum_name=api_enum_name,
ostream_cases='\n'.join(ostream_cases)))
cpp = cpp_template.format(
content=''.join(content),
data_source_name=data_source_name,
script_name=os.path.basename(sys.argv[0]),
file_name=file_name,
header_name=header_name_from_cpp_name(file_name),
namespace=namespace,
api_enum_name=api_enum_name)
with (open(path_prefix + file_name, 'wt')) as f:
f.write(cpp)
def main():
# auto_script parameters.
if len(sys.argv) > 1:
inputs = []
outputs = []
for generator in Generators:
inputs += [generator['json']]
outputs += [
generator['output'] + '_autogen.cpp',
generator['output'] + '_autogen.h',
]
if sys.argv[1] == 'inputs':
print(','.join(inputs))
elif sys.argv[1] == 'outputs':
print(','.join(outputs))
else:
print('Invalid script parameters')
return 1
return 0
path_prefix = os.path.dirname(os.path.realpath(__file__)) + os.path.sep
for generator in Generators:
json_file = generator['json']
output_file = generator['output']
includes = generator['includes']
namespace = generator['namespace']
enum_type = generator['enum_type']
enums = load_enums(path_prefix + json_file)
write_header(enums, path_prefix, output_file + '_autogen.h', json_file, includes,
namespace, enum_type)
write_cpp(enums, path_prefix, output_file + '_autogen.cpp', json_file, namespace,
enum_type)
return 0
if __name__ == '__main__':
sys.exit(main())