2012-04-03 03:34:16 +02:00
|
|
|
# Copyright (c) 2009 The Chromium Embedded Framework Authors. All rights
|
|
|
|
# reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
# can be found in the LICENSE file.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
from cef_parser import *
|
2017-05-18 10:41:47 +02:00
|
|
|
from clang_util import clang_format
|
|
|
|
from file_util import *
|
|
|
|
import hashlib
|
2012-04-03 03:34:16 +02:00
|
|
|
from make_capi_header import *
|
|
|
|
from make_cpptoc_header import *
|
|
|
|
from make_cpptoc_impl import *
|
|
|
|
from make_ctocpp_header import *
|
|
|
|
from make_ctocpp_impl import *
|
|
|
|
from make_gypi_file import *
|
2018-07-27 17:39:53 +02:00
|
|
|
from make_libcef_dll_dylib_impl import *
|
2016-01-19 21:09:01 +01:00
|
|
|
from make_views_stub_impl import *
|
2015-04-26 20:40:01 +02:00
|
|
|
from make_wrapper_types_header import *
|
2012-04-03 03:34:16 +02:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
# cannot be loaded as a module
|
|
|
|
if __name__ != "__main__":
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stderr.write('This file cannot be loaded as a module!')
|
|
|
|
sys.exit()
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
# parse command-line options
|
|
|
|
disc = """
|
|
|
|
This utility generates files for the CEF C++ to C API translation layer.
|
|
|
|
"""
|
|
|
|
|
|
|
|
parser = OptionParser(description=disc)
|
2017-05-28 15:04:18 +02:00
|
|
|
parser.add_option(
|
|
|
|
'--root-dir',
|
|
|
|
dest='rootdir',
|
|
|
|
metavar='DIR',
|
|
|
|
help='CEF root directory [required]')
|
|
|
|
parser.add_option(
|
|
|
|
'--backup',
|
|
|
|
action='store_true',
|
|
|
|
dest='backup',
|
|
|
|
default=False,
|
|
|
|
help='create a backup of modified files')
|
|
|
|
parser.add_option(
|
|
|
|
'--force',
|
|
|
|
action='store_true',
|
|
|
|
dest='force',
|
|
|
|
default=False,
|
|
|
|
help='force rewrite of the file')
|
|
|
|
parser.add_option(
|
|
|
|
'-c',
|
|
|
|
'--classes',
|
|
|
|
dest='classes',
|
|
|
|
action='append',
|
|
|
|
help='only translate the specified classes')
|
|
|
|
parser.add_option(
|
|
|
|
'-q',
|
|
|
|
'--quiet',
|
|
|
|
action='store_true',
|
|
|
|
dest='quiet',
|
|
|
|
default=False,
|
|
|
|
help='do not output detailed status information')
|
2012-04-03 03:34:16 +02:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
# the rootdir option is required
|
|
|
|
if options.rootdir is None:
|
2017-05-28 15:04:18 +02:00
|
|
|
parser.print_help(sys.stdout)
|
|
|
|
sys.exit()
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
# determine the paths
|
|
|
|
root_dir = os.path.abspath(options.rootdir)
|
|
|
|
cpp_header_dir = os.path.join(root_dir, 'include')
|
|
|
|
cpp_header_test_dir = os.path.join(cpp_header_dir, 'test')
|
2016-01-19 21:09:01 +01:00
|
|
|
cpp_header_views_dir = os.path.join(cpp_header_dir, 'views')
|
2015-04-26 20:40:01 +02:00
|
|
|
capi_header_dir = os.path.join(cpp_header_dir, 'capi')
|
|
|
|
libcef_dll_dir = os.path.join(root_dir, 'libcef_dll')
|
|
|
|
cpptoc_global_impl = os.path.join(libcef_dll_dir, 'libcef_dll.cc')
|
2017-05-28 15:04:18 +02:00
|
|
|
ctocpp_global_impl = os.path.join(libcef_dll_dir, 'wrapper',
|
|
|
|
'libcef_dll_wrapper.cc')
|
2015-04-26 20:40:01 +02:00
|
|
|
wrapper_types_header = os.path.join(libcef_dll_dir, 'wrapper_types.h')
|
|
|
|
cpptoc_dir = os.path.join(libcef_dll_dir, 'cpptoc')
|
|
|
|
ctocpp_dir = os.path.join(libcef_dll_dir, 'ctocpp')
|
|
|
|
gypi_file = os.path.join(root_dir, 'cef_paths.gypi')
|
2016-01-19 21:09:01 +01:00
|
|
|
views_stub_impl = os.path.join(libcef_dll_dir, 'views_stub.cc')
|
2018-07-27 17:39:53 +02:00
|
|
|
libcef_dll_dylib_impl = os.path.join(libcef_dll_dir, 'wrapper',
|
|
|
|
'libcef_dll_dylib.cc')
|
2015-04-26 20:40:01 +02:00
|
|
|
|
|
|
|
# make sure the header directory exists
|
|
|
|
if not path_exists(cpp_header_dir):
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stderr.write('Directory ' + cpp_header_dir + ' does not exist.')
|
|
|
|
sys.exit()
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
# create the header object
|
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('Parsing C++ headers from ' + cpp_header_dir + '...\n')
|
2012-04-03 03:34:16 +02:00
|
|
|
header = obj_header()
|
2015-04-26 20:40:01 +02:00
|
|
|
|
|
|
|
# add include files to be processed
|
|
|
|
header.set_root_directory(cpp_header_dir)
|
2013-08-22 18:59:52 +02:00
|
|
|
excluded_files = ['cef_application_mac.h', 'cef_version.h']
|
2015-04-26 20:40:01 +02:00
|
|
|
header.add_directory(cpp_header_dir, excluded_files)
|
|
|
|
header.add_directory(cpp_header_test_dir)
|
2016-01-19 21:09:01 +01:00
|
|
|
header.add_directory(cpp_header_views_dir)
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-05-18 10:41:47 +02:00
|
|
|
# Track the number of files that were written.
|
2012-04-03 03:34:16 +02:00
|
|
|
writect = 0
|
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
|
2017-05-18 10:41:47 +02:00
|
|
|
def update_file(file, newcontents):
|
2017-05-28 15:04:18 +02:00
|
|
|
""" Replaces the contents of |file| with |newcontents| if necessary. """
|
|
|
|
oldcontents = ''
|
|
|
|
oldhash = ''
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
if newcontents[-1:] != "\n":
|
|
|
|
# Add newline at end of file.
|
|
|
|
newcontents += "\n"
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
# clang-format is slow so we don't want to apply it if the pre-formatted
|
|
|
|
# content hasn't changed. To check for changes we embed a hash of the pre-
|
|
|
|
# formatted content in the resulting file.
|
|
|
|
hash_start = "$hash="
|
|
|
|
hash_end = "$"
|
|
|
|
hash_token = "$$HASH$$"
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
if not options.force and path_exists(file):
|
|
|
|
oldcontents = read_file(file)
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
# Extract the existing hash.
|
|
|
|
start = oldcontents.find(hash_start)
|
|
|
|
if start > 0:
|
|
|
|
end = oldcontents.find(hash_end, start + len(hash_start))
|
|
|
|
if end > 0:
|
|
|
|
oldhash = oldcontents[start + len(hash_start):end]
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
# Compute the new hash.
|
|
|
|
rev = hashlib.sha1(newcontents).digest()
|
|
|
|
newhash = ''.join(format(ord(i), '0>2x') for i in rev)
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
if oldhash == newhash:
|
|
|
|
# Pre-formatted contents have not changed.
|
|
|
|
return
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
newcontents = newcontents.replace(hash_token, newhash, 1)
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
# Apply clang-format for C/C++ files.
|
2018-07-27 17:39:53 +02:00
|
|
|
if os.path.splitext(file)[1][1:] in ('c', 'cc', 'cpp', 'h'):
|
2017-05-28 15:04:18 +02:00
|
|
|
result = clang_format(file, newcontents)
|
|
|
|
if result != None:
|
|
|
|
newcontents = result
|
|
|
|
else:
|
|
|
|
raise Exception("Call to clang-format failed")
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
if options.backup and oldcontents != '':
|
|
|
|
backup_file(file)
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
filedir = os.path.split(file)[0]
|
|
|
|
if not os.path.isdir(filedir):
|
|
|
|
make_dir(filedir)
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
write_file(file, newcontents)
|
2017-05-18 10:41:47 +02:00
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
global writect
|
|
|
|
writect += 1
|
2017-05-18 10:41:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
# output the C API header
|
2015-04-26 20:40:01 +02:00
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('In C API header directory ' + capi_header_dir + '...\n')
|
2015-04-26 20:40:01 +02:00
|
|
|
filenames = sorted(header.get_file_names())
|
|
|
|
for filename in filenames:
|
2017-05-28 15:04:18 +02:00
|
|
|
if not options.quiet:
|
|
|
|
sys.stdout.write('Generating ' + filename + ' C API header...\n')
|
|
|
|
update_file(*write_capi_header(header, capi_header_dir, filename))
|
2015-04-26 20:40:01 +02:00
|
|
|
|
|
|
|
# output the wrapper types header
|
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('Generating wrapper types header...\n')
|
2017-05-18 10:41:47 +02:00
|
|
|
update_file(*write_wrapper_types_header(header, wrapper_types_header))
|
2015-04-26 20:40:01 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
# build the list of classes to parse
|
|
|
|
allclasses = header.get_class_names()
|
|
|
|
if not options.classes is None:
|
2017-05-28 15:04:18 +02:00
|
|
|
for cls in options.classes:
|
|
|
|
if not cls in allclasses:
|
|
|
|
sys.stderr.write('ERROR: Unknown class: ' + cls)
|
|
|
|
sys.exit()
|
|
|
|
classes = options.classes
|
2012-04-03 03:34:16 +02:00
|
|
|
else:
|
2017-05-28 15:04:18 +02:00
|
|
|
classes = allclasses
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
classes = sorted(classes)
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
# output CppToC global file
|
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('Generating CppToC global implementation...\n')
|
2017-05-18 10:41:47 +02:00
|
|
|
update_file(*write_cpptoc_impl(header, None, cpptoc_global_impl))
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
# output CToCpp global file
|
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('Generating CToCpp global implementation...\n')
|
2017-05-18 10:41:47 +02:00
|
|
|
update_file(*write_ctocpp_impl(header, None, ctocpp_global_impl))
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
# output CppToC class files
|
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('In CppToC directory ' + cpptoc_dir + '...\n')
|
2015-04-26 20:40:01 +02:00
|
|
|
for cls in classes:
|
2017-05-28 15:04:18 +02:00
|
|
|
if not options.quiet:
|
|
|
|
sys.stdout.write('Generating ' + cls + 'CppToC class header...\n')
|
|
|
|
update_file(*write_cpptoc_header(header, cls, cpptoc_dir))
|
|
|
|
if not options.quiet:
|
|
|
|
sys.stdout.write('Generating ' + cls + 'CppToC class implementation...\n')
|
|
|
|
update_file(*write_cpptoc_impl(header, cls, cpptoc_dir))
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
# output CppToC class files
|
2012-04-03 03:34:16 +02:00
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('In CToCpp directory ' + ctocpp_dir + '...\n')
|
2015-04-26 20:40:01 +02:00
|
|
|
for cls in classes:
|
2017-05-28 15:04:18 +02:00
|
|
|
if not options.quiet:
|
|
|
|
sys.stdout.write('Generating ' + cls + 'CToCpp class header...\n')
|
|
|
|
update_file(*write_ctocpp_header(header, cls, ctocpp_dir))
|
|
|
|
if not options.quiet:
|
|
|
|
sys.stdout.write('Generating ' + cls + 'CToCpp class implementation...\n')
|
|
|
|
update_file(*write_ctocpp_impl(header, cls, ctocpp_dir))
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
# output the gypi file
|
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('Generating ' + gypi_file + ' file...\n')
|
2017-05-18 10:41:47 +02:00
|
|
|
update_file(*write_gypi_file(header, gypi_file))
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
# output the views stub file
|
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('Generating ' + views_stub_impl + ' file...\n')
|
2017-05-18 10:41:47 +02:00
|
|
|
update_file(*write_views_stub_impl(header, views_stub_impl))
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2018-07-27 17:39:53 +02:00
|
|
|
# output the libcef dll dylib file
|
|
|
|
if not options.quiet:
|
|
|
|
sys.stdout.write('Generating ' + libcef_dll_dylib_impl + ' file...\n')
|
|
|
|
update_file(*write_libcef_dll_dylib_impl(header, libcef_dll_dylib_impl))
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
if not options.quiet:
|
2017-05-28 15:04:18 +02:00
|
|
|
sys.stdout.write('Done - Wrote ' + str(writect) + ' files.\n')
|