1# Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights 2# reserved. Use of this source code is governed by a BSD-style license that 3# can be found in the LICENSE file. 4 5from __future__ import absolute_import 6from cef_parser import * 7from date_util import * 8 9 10def make_capi_global_funcs(funcs, defined_names, translate_map, indent): 11 result = '' 12 first = True 13 for func in funcs: 14 comment = func.get_comment() 15 if first or len(comment) > 0: 16 result += '\n' + format_comment(comment, indent, translate_map) 17 if func.get_retval().get_type().is_result_string(): 18 result += indent + '// The resulting string must be freed by calling cef_string_userfree_free().\n' 19 result += indent + 'CEF_EXPORT ' + func.get_capi_proto(defined_names) + ';\n' 20 if first: 21 first = False 22 return result 23 24 25def make_capi_member_funcs(funcs, defined_names, translate_map, indent): 26 result = '' 27 first = True 28 for func in funcs: 29 comment = func.get_comment() 30 if first or len(comment) > 0: 31 result += '\n' + format_comment(comment, indent, translate_map) 32 if func.get_retval().get_type().is_result_string(): 33 result += indent + '// The resulting string must be freed by calling cef_string_userfree_free().\n' 34 parts = func.get_capi_parts() 35 result += indent+parts['retval']+' (CEF_CALLBACK *'+parts['name']+ \ 36 ')('+', '.join(parts['args'])+');\n' 37 if first: 38 first = False 39 return result 40 41 42def make_capi_header(header, filename): 43 # structure names that have already been defined 44 defined_names = header.get_defined_structs() 45 46 # map of strings that will be changed in C++ comments 47 translate_map = header.get_capi_translations() 48 49 # header string 50 result = \ 51"""// Copyright (c) $YEAR$ Marshall A. Greenblatt. All rights reserved. 52// 53// Redistribution and use in source and binary forms, with or without 54// modification, are permitted provided that the following conditions are 55// met: 56// 57// * Redistributions of source code must retain the above copyright 58// notice, this list of conditions and the following disclaimer. 59// * Redistributions in binary form must reproduce the above 60// copyright notice, this list of conditions and the following disclaimer 61// in the documentation and/or other materials provided with the 62// distribution. 63// * Neither the name of Google Inc. nor the name Chromium Embedded 64// Framework nor the names of its contributors may be used to endorse 65// or promote products derived from this software without specific prior 66// written permission. 67// 68// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 69// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 70// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 71// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 72// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 73// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 74// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 75// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 76// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 77// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 78// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 79// 80// --------------------------------------------------------------------------- 81// 82// This file was generated by the CEF translator tool and should not edited 83// by hand. See the translator.README.txt file in the tools directory for 84// more information. 85// 86// $hash=$$HASH$$$ 87// 88 89#ifndef $GUARD$ 90#define $GUARD$ 91#pragma once 92 93""" 94 95 # Protect against incorrect use of test headers. 96 if filename.startswith('test/'): 97 result += \ 98"""#if !defined(BUILDING_CEF_SHARED) && !defined(WRAPPING_CEF_SHARED) && \\ 99 !defined(UNIT_TEST) 100#error This file can be included for unit tests only 101#endif 102 103""" 104 105 classes = header.get_classes(filename) 106 107 # identify all includes and forward declarations 108 translated_includes = set([]) 109 internal_includes = set([]) 110 all_declares = set([]) 111 for cls in classes: 112 includes = cls.get_includes() 113 for include in includes: 114 if include.startswith('base/'): 115 # base/ headers are C++. They should not be included by 116 # translated CEF API headers. 117 raise Exception('Disallowed include of %s.h from %s' % (include, 118 filename)) 119 elif include.startswith('internal/'): 120 # internal/ headers may be C or C++. Include them as-is. 121 internal_includes.add(include) 122 else: 123 translated_includes.add(include) 124 declares = cls.get_forward_declares() 125 for declare in declares: 126 declare_cls = header.get_class(declare) 127 if declare_cls is None: 128 raise Exception('Unknown class: %s' % declare) 129 all_declares.add(declare_cls.get_capi_name()) 130 131 # output translated includes 132 if len(translated_includes) > 0: 133 sorted_includes = sorted(translated_includes) 134 for include in sorted_includes: 135 result += '#include "include/capi/' + include + '_capi.h"\n' 136 else: 137 result += '#include "include/capi/cef_base_capi.h"\n' 138 139 # output internal includes 140 if len(internal_includes) > 0: 141 sorted_includes = sorted(internal_includes) 142 for include in sorted_includes: 143 result += '#include "include/' + include + '.h"\n' 144 145 result += \ 146""" 147#ifdef __cplusplus 148extern "C" { 149#endif 150 151""" 152 153 # output forward declarations 154 if len(all_declares) > 0: 155 sorted_declares = sorted(all_declares) 156 for declare in sorted_declares: 157 result += 'struct _' + declare + ';\n' 158 159 # output classes 160 for cls in classes: 161 # virtual functions are inside the structure 162 classname = cls.get_capi_name() 163 result += '\n' + format_comment(cls.get_comment(), '', translate_map) 164 result += 'typedef struct _'+classname+' {\n'+\ 165 ' ///\n'+\ 166 ' // Base structure.\n'+\ 167 ' ///\n'+\ 168 ' '+cls.get_parent_capi_name()+' base;\n' 169 funcs = cls.get_virtual_funcs() 170 result += make_capi_member_funcs(funcs, defined_names, translate_map, ' ') 171 result += '} ' + classname + ';\n\n' 172 173 defined_names.append(cls.get_capi_name()) 174 175 # static functions become global 176 funcs = cls.get_static_funcs() 177 if len(funcs) > 0: 178 result += make_capi_global_funcs(funcs, defined_names, translate_map, 179 '') + '\n' 180 181 # output global functions 182 funcs = header.get_funcs(filename) 183 if len(funcs) > 0: 184 result += make_capi_global_funcs(funcs, defined_names, translate_map, '') 185 186 # footer string 187 result += \ 188""" 189#ifdef __cplusplus 190} 191#endif 192 193#endif // $GUARD$ 194""" 195 196 # add the copyright year 197 result = result.replace('$YEAR$', get_year()) 198 # add the guard string 199 guard = 'CEF_INCLUDE_CAPI_' + \ 200 filename.replace('/', '_').replace('.', '_capi_').upper() + '_' 201 result = result.replace('$GUARD$', guard) 202 203 return result 204 205 206def write_capi_header(header, header_dir, filename): 207 file = get_capi_file_name(os.path.join(header_dir, filename)) 208 newcontents = make_capi_header(header, filename) 209 return (file, newcontents) 210 211 212# test the module 213if __name__ == "__main__": 214 import sys 215 216 # verify that the correct number of command-line arguments are provided 217 if len(sys.argv) < 2: 218 sys.stderr.write('Usage: ' + sys.argv[0] + ' <infile>\n') 219 sys.exit() 220 221 # create the header object 222 header = obj_header() 223 header.add_file(sys.argv[1]) 224 225 # dump the result to stdout 226 filename = os.path.split(sys.argv[1])[1] 227 sys.stdout.write(make_capi_header(header, filename)) 228