Path: blob/main_old/scripts/generate_new_renderer.py
1693 views
#!/usr/bin/python21#2# Copyright 2015 The ANGLE Project Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5#6# generate_new_renderer.py:7# Utility script to generate stubs for a new Renderer class.8# Usage: generate_new_renderer.py <renderer name> <renderer suffix>9# Renderer name is the folder for the renderer subdirectory10# Renderer suffix is the abbreviation to append after the class names.11#12# The script is fairly robust but may not work for all new methods or13# other unexpected features. It expects that abstract methods are all14# grouped after the public destructor or after the private15# DISALLOW_COPY_AND_ASSIGN macro.1617import os, sys, re, string1819if len(sys.argv) < 3:20print('Usage: ' + sys.argv[0] + ' <renderer dir name> <renderer class suffix>')21sys.exit(1)2223renderer_name = sys.argv[1]24renderer_suffix = sys.argv[2]2526# change to the renderer directory27os.chdir(os.path.join(os.path.dirname(sys.argv[0]), "..", "src", "libANGLE", "renderer"))2829# ensure subdir exists30if not os.path.isdir(renderer_name):31os.mkdir(renderer_name)3233impl_classes = [34'Buffer',35'Compiler',36'Context',37'Device',38'Display',39'FenceNV',40'FenceSync',41'Framebuffer',42'Image',43'Path',44'Program',45'Query',46'Renderbuffer',47'Sampler',48'Shader',49'Surface',50'Texture',51'TransformFeedback',52'VertexArray',53]5455h_file_template = """//56// Copyright 2016 The ANGLE Project Authors. All rights reserved.57// Use of this source code is governed by a BSD-style license that can be58// found in the LICENSE file.59//60// $TypedImpl.h:61// Defines the class interface for $TypedImpl, implementing $BaseImpl.62//6364#ifndef LIBANGLE_RENDERER_${RendererNameCaps}_${TypedImplCaps}_H_65#define LIBANGLE_RENDERER_${RendererNameCaps}_${TypedImplCaps}_H_6667#include "libANGLE/renderer/$BaseImpl.h"6869namespace rx70{7172class $TypedImpl : public $BaseImpl73{74public:75$TypedImpl($ConstructorParams);76~$TypedImpl() override;77$ImplMethodDeclarations$PrivateImplMethodDeclarations};7879} // namespace rx8081#endif // LIBANGLE_RENDERER_${RendererNameCaps}_${TypedImplCaps}_H_82"""8384cpp_file_template = """//85// Copyright 2016 The ANGLE Project Authors. All rights reserved.86// Use of this source code is governed by a BSD-style license that can be87// found in the LICENSE file.88//89// $TypedImpl.cpp:90// Implements the class methods for $TypedImpl.91//9293#include "libANGLE/renderer/$RendererName/$TypedImpl.h"9495#include "common/debug.h"9697namespace rx98{99100$TypedImpl::$TypedImpl($ConstructorParams) : $BaseImpl($BaseContructorArgs)101{102}103104$TypedImpl::~$TypedImpl()105{106}107$ImplMethodDefinitions108} // namespace rx109"""110111112def generate_impl_declaration(impl_stub):113# ensure the wrapped lines are aligned vertically114temp = re.sub(r'\n ', '\n', impl_stub)115return temp + ' override;\n'116117118def generate_impl_definition(impl_stub, typed_impl):119function_signature = impl_stub.strip()120121# strip comments122function_signature = re.sub(r'\/\/[^\n]*\n', '', function_signature).strip()123124prog = re.compile(r'^(.+[ \*\&])([^ \(\*\&]+\()')125return_value = prog.match(function_signature).group(1)126127# ensure the wrapped lines are aligned vertically128spaces = ' ' * len(typed_impl)129function_signature = re.sub(r'\n ', '\n' + spaces, function_signature)130131# add class scoping132function_signature = prog.sub(r'\1' + typed_impl + r'::\2', function_signature)133function_signature += '\n'134135return_statement = ''136return_type = return_value.strip()137138if return_type != 'void':139# specialized return values for Errors, pointers, etc140if return_type == 'gl::Error':141return_statement = ' return gl::InvalidOperation();\n'142elif return_type == 'egl::Error':143return_statement = ' return egl::EglBadAccess();\n'144elif return_type == 'LinkResult':145return_statement = ' return gl::InvalidOperation();\n'146elif re.search(r'\*$', return_type):147return_statement = ' return static_cast<' + return_type + '>(0);\n'148elif re.search(r'const ([^ \&]+) \&$', return_type):149obj_type = re.search(r'const ([^ \&]+) \&$', return_type).group(1)150return_statement = ' static ' + obj_type + ' local;\n' + ' return local;\n'151else:152return_statement = ' return ' + return_type + '();\n'153154body = '{\n' + ' UNIMPLEMENTED();\n' + return_statement + '}\n'155156return '\n' + function_signature + body157158159def get_constructor_args(constructor):160params = re.search(r'\((.*)\)', constructor).group(1)161args = ', '.join(re.findall(r'[^\w]?(\w+)(?:\,|$)', params))162return params, args163164165def parse_impl_header(base_impl):166impl_h_file_path = base_impl + '.h'167impl_h_file = open(impl_h_file_path, 'r')168169# extract impl stubs170copy = False171copy_private = False172impl_stubs = ''173private_impl_stubs = ''174constructor = base_impl + '() {}'175for line in impl_h_file:176clean_line = line.strip()177178match = re.search(r'^(?:explicit )?(' + base_impl + r'\([^\)]*\))', clean_line)179if match:180constructor = match.group(1)181182# begin capture when reading the destructor.183# begin capture also in the private scope (a few special cases)184# end capture when we reach a non-virtual function, or different scope.185if '~' + base_impl in clean_line:186copy = True187copy_private = False188elif 'private:' in clean_line:189copy = False190copy_private = True191elif ';' in clean_line and ' = 0' not in clean_line:192copy = False193copy_private = False194elif '}' in clean_line or 'protected:' in clean_line or 'private:' in clean_line:195copy = False196copy_private = False197elif copy:198impl_stubs += line199elif copy_private:200private_impl_stubs += line201202impl_h_file.close()203204return impl_stubs, private_impl_stubs, constructor205206207def get_base_class(base_impl):208impl_h_file_path = base_impl + '.h'209with open(impl_h_file_path, 'r') as impl_h_file:210for line in impl_h_file:211match = re.search(r'^class ' + base_impl + r' : public (\w+)', line)212if match:213return match.group(1)214return False215216217for impl_class in impl_classes:218219base_impl = impl_class + 'Impl'220typed_impl = impl_class + renderer_suffix221222h_file_path = os.path.join(renderer_name, typed_impl + '.h')223cpp_file_path = os.path.join(renderer_name, typed_impl + '.cpp')224225h_file = open(h_file_path, 'w')226cpp_file = open(cpp_file_path, 'w')227228# extract impl stubs229impl_stubs, private_impl_stubs, constructor = parse_impl_header(base_impl)230231# Handle base classes, skipping angle::NonCopyable.232base_class = get_base_class(base_impl)233if base_class and base_class != 'angle':234base_impl_stubs, base_private_impl_stubs, base_constructor = parse_impl_header(base_class)235impl_stubs += base_impl_stubs236private_impl_stubs += base_private_impl_stubs237238impl_method_declarations = ''239impl_method_definitions = ''240private_impl_method_declarations = ''241242for impl_stub in impl_stubs.split(' = 0;\n'):243# use 'virtual' to identify the strings with functions244if 'virtual' in impl_stub:245temp = re.sub(r'virtual ', '', impl_stub)246impl_method_declarations += generate_impl_declaration(temp)247impl_method_definitions += generate_impl_definition(temp, typed_impl)248249for impl_stub in private_impl_stubs.split(' = 0;\n'):250# use 'virtual' to identify the strings with functions251if 'virtual' in impl_stub:252temp = re.sub(r'virtual ', '', impl_stub)253private_impl_method_declarations += generate_impl_declaration(temp)254impl_method_definitions += generate_impl_definition(temp, typed_impl)255256constructor_params, base_constructor_args = get_constructor_args(constructor)257258if private_impl_method_declarations:259private_impl_method_declarations = "\n private:\n" + private_impl_method_declarations260261substitutions = {262'BaseImpl': base_impl,263'TypedImpl': typed_impl,264'TypedImplCaps': typed_impl.upper(),265'RendererName': renderer_name,266'RendererNameCaps': renderer_name.upper(),267'ImplMethodDeclarations': impl_method_declarations,268'ImplMethodDefinitions': impl_method_definitions,269'ConstructorParams': constructor_params,270'BaseContructorArgs': base_constructor_args,271'PrivateImplMethodDeclarations': private_impl_method_declarations272}273274h_file.write(string.Template(h_file_template).substitute(substitutions))275cpp_file.write(string.Template(cpp_file_template).substitute(substitutions))276277h_file.close()278cpp_file.close()279280# Print a block of source files to add to the build281print("Generated files:")282for impl_class in impl_classes:283path = "libANGLE/renderer/" + renderer_name + "/" + impl_class + renderer_suffix284print('\'' + path + ".cpp\',")285print('\'' + path + ".h\',")286287288