Hash :
400d9fe4
Author :
Date :
2022-04-23T01:08:19
Rename feature files to *_autogen.h To clarify further that they are not to be edited by hand. Bug: angleproject:6435 Change-Id: Iaf79706d2b688a43b3ebb65700cfbdd71a49a742 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3603842 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@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
#! /usr/bin/python3
# Copyright 2022 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_features.py:
# Code generation for ANGLE features.
# NOTE: don't run this script directly. Run scripts/run_code_generation.py.
from collections import namedtuple
import json
import os
import re
import sys
feature_files = {
'd3d_features.json': ('D3D', 'FeaturesD3D'),
'frontend_features.json': ('Frontend', 'FrontendFeatures'),
'gl_features.json': ('OpenGL', 'FeaturesGL'),
'mtl_features.json': ('Metal', 'FeaturesMtl'),
'vk_features.json': ('Vulkan', 'FeaturesVk'),
}
feature_list_header_file = '../../util/angle_features_autogen.h'
feature_list_source_file = '../../util/angle_features_autogen.cpp'
template_header = u"""// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {input_file_name}.
//
{description}
#ifndef ANGLE_PLATFORM_{NAME}_H_
#define ANGLE_PLATFORM_{NAME}_H_
#include "platform/Feature.h"
namespace angle
{{
struct {name} : FeatureSetBase
{{
{name}();
~{name}();
{features}
}};
inline {name}::{name}() = default;
inline {name}::~{name}() = default;
}} // namespace angle
#endif // ANGLE_PLATFORM_{NAME}_H_
"""
template_feature = u"""FeatureInfo {var_name} = {{
"{display_name}", FeatureCategory::{category},
{description},
&members, {issue}
}};
"""
template_feature_list_header = u"""// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {input_file_name}.
//
// Copyright 2022 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.
//
// angle_features_autogen.h: List of ANGLE features to help enable/disable them in tests.
#ifndef ANGLE_SRC_TESTS_TEST_UTILS_ANGLE_FEATURES_AUTOGEN_H_
#define ANGLE_SRC_TESTS_TEST_UTILS_ANGLE_FEATURES_AUTOGEN_H_
#include "util_export.h"
namespace angle
{{
enum class Feature
{{
{features}
InvalidEnum,
EnumCount = InvalidEnum,
}};
ANGLE_UTIL_EXPORT extern const char *GetFeatureName(Feature feature);
}} // namespace angle
#endif // ANGLE_SRC_TESTS_TEST_UTILS_ANGLE_FEATURES_AUTOGEN_H_
"""
template_feature_enum = u"""{VarName},"""
template_feature_list_source = u"""// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {input_file_name}.
//
// Copyright 2022 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.
//
// angle_features_autogen.cpp: List of ANGLE features to help enable/disable them in tests.
#include "angle_features_autogen.h"
#include "common/PackedEnums.h"
namespace angle
{{
namespace
{{
constexpr PackedEnumMap<Feature, const char *> kFeatureNames = {{ {{
{features}
}} }};
}} // anonymous namespace
const char *GetFeatureName(Feature feature)
{{
return kFeatureNames[feature];
}}
}} // namespace angle
"""
template_feature_string = u"""{{Feature::{VarName}, "{display_name}"}},"""
def make_camel_case(json_name):
assert '_' in json_name, 'feature names in the json file are expected to be in snake_case'
return re.sub('_(.)', lambda m: m.group(1).upper(), json_name)
def make_header_name(class_name):
return class_name + '_autogen.h'
def main():
if len(sys.argv) == 2 and sys.argv[1] == 'inputs':
print(','.join(list(feature_files.keys())))
return
if len(sys.argv) == 2 and sys.argv[1] == 'outputs':
print(','.join(
[make_header_name(class_name) for (_, class_name) in feature_files.values()]) + ',' +
feature_list_header_file + ',' + feature_list_source_file)
return
name_map = {}
for src_file, (category_prefix, class_name) in feature_files.items():
with open(src_file) as fin:
src = json.loads(fin.read())
features_json = src['features']
features = []
# Go over the list of features and write the header file that declares the features struct
for feature_json in features_json:
json_name = feature_json['name']
var_name = make_camel_case(json_name)
# Use the same (camelCase) name for display as well
display_name = var_name
issue = feature_json.get('issue', None)
feature = template_feature.format(
var_name=var_name,
display_name=display_name,
category=category_prefix + feature_json['category'],
description='\n'.join('"' + line + '"' for line in feature_json['description']),
issue='' if issue is None else '"' + issue + '"')
features.append(feature)
# Keep track of the feature names. Sometimes the same feature name is present in
# multiple backends. That's ok for the purposes of feature overriding.
name_map[var_name] = display_name
description = '\n'.join(['// ' + line for line in src['description']])
header_file = make_header_name(class_name)
header = template_header.format(
script_name=os.path.basename(__file__),
input_file_name=src_file,
description=description.replace(src_file, header_file),
name=class_name,
NAME=class_name.upper(),
features='\n'.join(features))
with open(header_file, 'w') as fout:
fout.write(header)
fout.close()
# Generate helpers for use by tests to override a feature or not.
feature_enums = []
feature_strings = []
for var_name, display_name in sorted(name_map.items(), key=lambda item: item[0].lower()):
VarName = var_name[0].upper() + var_name[1:]
feature_enums.append(template_feature_enum.format(VarName=VarName))
feature_strings.append(
template_feature_string.format(VarName=VarName, display_name=display_name))
with open(feature_list_header_file, 'w') as fout:
fout.write(
template_feature_list_header.format(
script_name=os.path.basename(__file__),
input_file_name='*_features.json',
features='\n'.join(feature_enums)))
fout.close()
with open(feature_list_source_file, 'w') as fout:
fout.write(
template_feature_list_source.format(
script_name=os.path.basename(__file__),
input_file_name='*_features.json',
features='\n'.join(feature_strings)))
fout.close()
if __name__ == '__main__':
sys.exit(main())