diff options
author | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-05 08:02:06 +0000 |
---|---|---|
committer | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-05 08:02:06 +0000 |
commit | 8d368d625b14b85907c1fb08c4e67e3aa5bc064c (patch) | |
tree | 64c64f8ff50fa3e8ed3b02774d49d7ec84ae564e | |
parent | e6477f130202747afd404c16f9a86e8333e3337a (diff) | |
download | chromium_src-8d368d625b14b85907c1fb08c4e67e3aa5bc064c.zip chromium_src-8d368d625b14b85907c1fb08c4e67e3aa5bc064c.tar.gz chromium_src-8d368d625b14b85907c1fb08c4e67e3aa5bc064c.tar.bz2 |
Make the root_namespace argument to json_schema_compiler.gypi a string
pattern rather than a prefix.
BUG=370952
R=yoz@chromium.org
TBR=dmazzoni@chromium.org
Review URL: https://codereview.chromium.org/437883002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@287488 0039d316-1c4b-4281-b951-d872f2087c98
23 files changed, 209 insertions, 155 deletions
diff --git a/build/json_schema_bundle_compile.gypi b/build/json_schema_bundle_compile.gypi index 23dcff8..7e712d7 100644 --- a/build/json_schema_bundle_compile.gypi +++ b/build/json_schema_bundle_compile.gypi @@ -5,9 +5,15 @@ { 'variables': { # When including this gypi, the following variables must be set: - # schema_files: an array of json or idl files that comprise the api model. - # cc_dir: path to generated files - # root_namespace: the C++ namespace that all generated files go under + # schema_files: + # An array of json or idl files that comprise the api model. + # cc_dir: + # The directory to put the generated code in. + # root_namespace: + # A Python string substituion pattern used to generate the C++ + # namespace for each API. Use %(namespace)s to replace with the API + # namespace, like "toplevel::%(namespace)s_api". + # # Functions and namespaces can be excluded by setting "nocompile" to true. # The default root path of API implementation sources is # chrome/browser/extensions/api and can be overridden by setting "impl_dir". diff --git a/build/json_schema_compile.gypi b/build/json_schema_compile.gypi index 1f94b46..d164748 100644 --- a/build/json_schema_compile.gypi +++ b/build/json_schema_compile.gypi @@ -5,9 +5,15 @@ { 'variables': { # When including this gypi, the following variables must be set: - # schema_files: a list of json or IDL files that comprise the api model. - # cc_dir: path to generated files - # root_namespace: the C++ namespace that all generated files go under + # schema_files: + # An array of json or idl files that comprise the api model. + # cc_dir: + # The directory to put the generated code in. + # root_namespace: + # A Python string substituion pattern used to generate the C++ + # namespace for each API. Use %(namespace)s to replace with the API + # namespace, like "toplevel::%(namespace)s_api". + # # Functions and namespaces can be excluded by setting "nocompile" to true. # The default root path of API implementation sources is # chrome/browser/extensions/api and can be overridden by setting "impl_dir". diff --git a/chrome/browser/performance_monitor/BUILD.gn b/chrome/browser/performance_monitor/BUILD.gn index 0439566..d3250bc 100644 --- a/chrome/browser/performance_monitor/BUILD.gn +++ b/chrome/browser/performance_monitor/BUILD.gn @@ -6,5 +6,5 @@ import("//extensions/generated_extensions_api.gni") generated_extensions_api("performance_monitor") { sources = [ "events.json" ] - root_namespace = "performance_monitor" + root_namespace = "performance_monitor::%(namespace)s" } diff --git a/chrome/browser/performance_monitor/performance_monitor.gyp b/chrome/browser/performance_monitor/performance_monitor.gyp index e3587f4..4739109 100644 --- a/chrome/browser/performance_monitor/performance_monitor.gyp +++ b/chrome/browser/performance_monitor/performance_monitor.gyp @@ -19,7 +19,7 @@ 'events.json', ], 'cc_dir': 'chrome/browser/performance_monitor', - 'root_namespace': 'performance_monitor', + 'root_namespace': 'performance_monitor::%(namespace)s', }, }, ], diff --git a/chrome/common/extensions/api/BUILD.gn b/chrome/common/extensions/api/BUILD.gn index 2dcc3ed..d4362fb 100644 --- a/chrome/common/extensions/api/BUILD.gn +++ b/chrome/common/extensions/api/BUILD.gn @@ -35,7 +35,7 @@ generated_extensions_api("api") { } impl_dir = "//chrome/browser/extensions/api" - root_namespace = "extensions::api" + root_namespace = "extensions::api::%(namespace)s" bundle = true deps = [ diff --git a/chrome/common/extensions/api/api.gyp b/chrome/common/extensions/api/api.gyp index 5965415..d995e1a 100644 --- a/chrome/common/extensions/api/api.gyp +++ b/chrome/common/extensions/api/api.gyp @@ -206,7 +206,7 @@ }], ], 'cc_dir': 'chrome/common/extensions/api', - 'root_namespace': 'extensions::api', + 'root_namespace': 'extensions::api::%(namespace)s', }, 'dependencies': [ # Different APIs include some headers from chrome/common that in turn diff --git a/extensions/common/api/BUILD.gn b/extensions/common/api/BUILD.gn index 95320f9..c2d0225 100644 --- a/extensions/common/api/BUILD.gn +++ b/extensions/common/api/BUILD.gn @@ -24,7 +24,7 @@ generated_extensions_api("api") { "test.json", "usb.idl", ] - root_namespace = "extensions::core_api" + root_namespace = "extensions::core_api::%(namespace)s" impl_dir = "//extensions/browser/api" bundle = true deps = [ diff --git a/extensions/common/api/api.gyp b/extensions/common/api/api.gyp index 49725aa5..229a5bc 100644 --- a/extensions/common/api/api.gyp +++ b/extensions/common/api/api.gyp @@ -50,7 +50,7 @@ }], ], 'cc_dir': 'extensions/common/api', - 'root_namespace': 'extensions::core_api', + 'root_namespace': 'extensions::core_api::%(namespace)s', 'impl_dir': 'extensions/browser/api', }, 'conditions': [ diff --git a/extensions/generated_extensions_api.gni b/extensions/generated_extensions_api.gni index bd66a5f..5360711e 100644 --- a/extensions/generated_extensions_api.gni +++ b/extensions/generated_extensions_api.gni @@ -12,9 +12,9 @@ # sources [required] A list of schema files to be compiled. # # root_namespace [required] -# The namespace in which generated API code is to be -# wrapped. C++ namespace syntax is accepted for nested namespace -# (e.g. "foo::bar::api"). +# A Python string substituion pattern used to generate the C++ +# namespace for each API. Use %(namespace)s to replace with the API +# namespace, like "toplevel::%(namespace)s_api". # # bundle [optional, default = false] # Boolean indicating if the schema files should be bundled or not. diff --git a/extensions/shell/common/api/api.gyp b/extensions/shell/common/api/api.gyp index 2a4d126..0353410 100644 --- a/extensions/shell/common/api/api.gyp +++ b/extensions/shell/common/api/api.gyp @@ -25,7 +25,7 @@ 'shell.idl', ], 'cc_dir': 'extensions/shell/common/api', - 'root_namespace': 'extensions::shell_api', + 'root_namespace': 'extensions::shell_api::%(namespace)s', 'impl_dir': 'extensions/shell/browser/api', }, 'dependencies': [ diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py index 61f5bf1..43434b8 100644 --- a/tools/json_schema_compiler/cc_generator.py +++ b/tools/json_schema_compiler/cc_generator.py @@ -9,25 +9,23 @@ import schema_util import util_cc_helper class CCGenerator(object): - def __init__(self, type_generator, cpp_namespace): + def __init__(self, type_generator, cpp_namespace_pattern): self._type_generator = type_generator - self._cpp_namespace = cpp_namespace + self._cpp_namespace_pattern = cpp_namespace_pattern def Generate(self, namespace): return _Generator(namespace, self._type_generator, - self._cpp_namespace).Generate() + self._cpp_namespace_pattern).Generate() class _Generator(object): """A .cc generator for a namespace. """ - def __init__(self, namespace, cpp_type_generator, cpp_namespace): + def __init__(self, namespace, cpp_type_generator, cpp_namespace_pattern): self._namespace = namespace self._type_helper = cpp_type_generator - self._cpp_namespace = cpp_namespace - self._target_namespace = ( - self._type_helper.GetCppNamespaceName(self._namespace)) + self._cpp_namespace_pattern = cpp_namespace_pattern self._util_cc_helper = ( util_cc_helper.UtilCCHelper(self._type_helper)) self._generate_error_messages = namespace.compiler_options.get( @@ -36,6 +34,9 @@ class _Generator(object): def Generate(self): """Generates a Code object with the .cc for a single namespace. """ + cpp_namespace = cpp_util.GetCppNamespace(self._cpp_namespace_pattern, + self._namespace.unix_name) + c = Code() (c.Append(cpp_util.CHROMIUM_LICENSE) .Append() @@ -52,8 +53,7 @@ class _Generator(object): .Append() .Append('using base::UTF8ToUTF16;') .Append() - .Concat(cpp_util.OpenNamespace(self._cpp_namespace)) - .Cblock(self._type_helper.GetNamespaceStart()) + .Concat(cpp_util.OpenNamespace(cpp_namespace)) ) if self._namespace.properties: (c.Append('//') @@ -91,9 +91,7 @@ class _Generator(object): ) for event in self._namespace.events.values(): c.Cblock(self._GenerateEvent(event)) - (c.Concat(self._type_helper.GetNamespaceEnd()) - .Cblock(cpp_util.CloseNamespace(self._cpp_namespace)) - ) + c.Cblock(cpp_util.CloseNamespace(cpp_namespace)) c.Append() return c diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py index 25d9012..ff91ccf 100755 --- a/tools/json_schema_compiler/compiler.py +++ b/tools/json_schema_compiler/compiler.py @@ -33,19 +33,19 @@ from schema_loader import SchemaLoader GENERATORS = ['cpp', 'cpp-bundle', 'dart'] def GenerateSchema(generator, - filenames, + file_paths, root, destdir, - root_namespace, + cpp_namespace_pattern, dart_overrides_dir, impl_dir): # Merge the source files into a single list of schemas. api_defs = [] - for filename in filenames: - schema = os.path.normpath(filename) + for file_path in file_paths: + schema = os.path.normpath(file_path) schema_loader = SchemaLoader( - os.path.dirname(os.path.relpath(os.path.normpath(filename), root)), - os.path.dirname(filename)) + os.path.dirname(os.path.relpath(schema, root)), + os.path.dirname(file_path)) api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) # If compiling the C++ model code, delete 'nocompile' nodes. @@ -64,8 +64,8 @@ def GenerateSchema(generator, src_path = None # Load the actual namespaces into the model. - for target_namespace, schema_filename in zip(api_defs, filenames): - relpath = os.path.relpath(os.path.normpath(schema_filename), root) + for target_namespace, file_path in zip(api_defs, file_paths): + relpath = os.path.relpath(os.path.normpath(file_path), root) namespace = api_model.AddNamespace(target_namespace, relpath, include_compiler_options=True) @@ -78,19 +78,19 @@ def GenerateSchema(generator, else: src_path = os.path.commonprefix((src_path, namespace.source_file_dir)) - path, filename = os.path.split(schema_filename) - short_filename, extension = os.path.splitext(filename) + path, filename = os.path.split(file_path) + filename_base, _ = os.path.splitext(filename) # Construct the type generator with all the namespaces in this model. type_generator = CppTypeGenerator(api_model, schema_loader, - default_namespace=default_namespace) + default_namespace) if generator == 'cpp-bundle': cpp_bundle_generator = CppBundleGenerator(root, api_model, api_defs, type_generator, - root_namespace, + cpp_namespace_pattern, src_path, impl_dir) generators = [ @@ -100,10 +100,10 @@ def GenerateSchema(generator, ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) ] elif generator == 'cpp': - cpp_generator = CppGenerator(type_generator, root_namespace) + cpp_generator = CppGenerator(type_generator, cpp_namespace_pattern) generators = [ - ('%s.h' % short_filename, cpp_generator.h_generator), - ('%s.cc' % short_filename, cpp_generator.cc_generator) + ('%s.h' % filename_base, cpp_generator.h_generator), + ('%s.cc' % filename_base, cpp_generator.cc_generator) ] elif generator == 'dart': generators = [ @@ -147,18 +147,18 @@ if __name__ == '__main__': parser.add_option('-i', '--impl-dir', dest='impl_dir', help='The root path of all API implementations') - (opts, filenames) = parser.parse_args() + (opts, file_paths) = parser.parse_args() - if not filenames: + if not file_paths: sys.exit(0) # This is OK as a no-op # Unless in bundle mode, only one file should be specified. - if opts.generator != 'cpp-bundle' and len(filenames) > 1: - # TODO(sashab): Could also just use filenames[0] here and not complain. + if opts.generator != 'cpp-bundle' and len(file_paths) > 1: + # TODO(sashab): Could also just use file_paths[0] here and not complain. raise Exception( "Unless in bundle mode, only one file can be specified at a time.") - result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, + result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, opts.namespace, opts.dart_overrides_dir, opts.impl_dir) if not opts.destdir: diff --git a/tools/json_schema_compiler/cpp_bundle_generator.py b/tools/json_schema_compiler/cpp_bundle_generator.py index 91ae920..62c754b 100644 --- a/tools/json_schema_compiler/cpp_bundle_generator.py +++ b/tools/json_schema_compiler/cpp_bundle_generator.py @@ -39,17 +39,21 @@ class CppBundleGenerator(object): model, api_defs, cpp_type_generator, - cpp_namespace, + cpp_namespace_pattern, source_file_dir, impl_dir): self._root = root self._model = model self._api_defs = api_defs self._cpp_type_generator = cpp_type_generator - self._cpp_namespace = cpp_namespace self._source_file_dir = source_file_dir self._impl_dir = impl_dir + # Hack: assume that the C++ namespace for the bundle is the namespace of the + # files without the last component of the namespace. A cleaner way to do + # this would be to make it a separate variable in the gyp file. + self._cpp_namespace = cpp_namespace_pattern.rsplit('::', 1)[0] + self.api_cc_generator = _APICCGenerator(self) self.api_h_generator = _APIHGenerator(self) self.schemas_cc_generator = _SchemasCCGenerator(self) @@ -65,7 +69,8 @@ class CppBundleGenerator(object): c.Append(cpp_util.CHROMIUM_LICENSE) c.Append() c.Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % self._source_file_dir) - ifndef_name = cpp_util.GenerateIfndefName(self._source_file_dir, file_base) + ifndef_name = cpp_util.GenerateIfndefName( + '%s/%s.h' % (self._source_file_dir, file_base)) c.Append() c.Append('#ifndef %s' % ifndef_name) c.Append('#define %s' % ifndef_name) @@ -147,7 +152,7 @@ class _APIHGenerator(object): def __init__(self, cpp_bundle): self._bundle = cpp_bundle - def Generate(self, namespace): + def Generate(self, _): # namespace not relevant, this is a bundle c = code.Code() c.Append('#include <string>') @@ -174,7 +179,7 @@ class _APICCGenerator(object): def __init__(self, cpp_bundle): self._bundle = cpp_bundle - def Generate(self, namespace): + def Generate(self, _): # namespace not relevant, this is a bundle c = code.Code() c.Append(cpp_util.CHROMIUM_LICENSE) c.Append() @@ -222,7 +227,7 @@ class _SchemasHGenerator(object): def __init__(self, cpp_bundle): self._bundle = cpp_bundle - def Generate(self, namespace): + def Generate(self, _): # namespace not relevant, this is a bundle c = code.Code() c.Append('#include <map>') c.Append('#include <string>') @@ -258,7 +263,7 @@ class _SchemasCCGenerator(object): def __init__(self, cpp_bundle): self._bundle = cpp_bundle - def Generate(self, namespace): + def Generate(self, _): # namespace not relevant, this is a bundle c = code.Code() c.Append(cpp_util.CHROMIUM_LICENSE) c.Append() diff --git a/tools/json_schema_compiler/cpp_generator.py b/tools/json_schema_compiler/cpp_generator.py index b765229..7441ac5 100644 --- a/tools/json_schema_compiler/cpp_generator.py +++ b/tools/json_schema_compiler/cpp_generator.py @@ -6,6 +6,6 @@ from cc_generator import CCGenerator from h_generator import HGenerator class CppGenerator(object): - def __init__(self, type_generator, cpp_namespace): - self.h_generator = HGenerator(type_generator, cpp_namespace) - self.cc_generator = CCGenerator(type_generator, cpp_namespace) + def __init__(self, type_generator, cpp_namespace_pattern): + self.h_generator = HGenerator(type_generator, cpp_namespace_pattern) + self.cc_generator = CCGenerator(type_generator, cpp_namespace_pattern) diff --git a/tools/json_schema_compiler/cpp_type_generator.py b/tools/json_schema_compiler/cpp_type_generator.py index 75972fa..de6d130 100644 --- a/tools/json_schema_compiler/cpp_type_generator.py +++ b/tools/json_schema_compiler/cpp_type_generator.py @@ -35,24 +35,6 @@ class CppTypeGenerator(object): self._default_namespace = model.namespaces.values()[0] self._schema_loader = schema_loader - def GetCppNamespaceName(self, namespace): - """Gets the mapped C++ namespace name for the given namespace relative to - the root namespace. - """ - return namespace.unix_name - - def GetNamespaceStart(self): - """Get opening self._default_namespace namespace declaration. - """ - return Code().Append('namespace %s {' % - self.GetCppNamespaceName(self._default_namespace)) - - def GetNamespaceEnd(self): - """Get closing self._default_namespace namespace declaration. - """ - return Code().Append('} // %s' % - self.GetCppNamespaceName(self._default_namespace)) - def GetEnumNoneValue(self, type_): """Gets the enum value in the given model.Property indicating no value has been set. @@ -148,23 +130,26 @@ class CppTypeGenerator(object): PropertyType.OBJECT, PropertyType.CHOICES)) - def GenerateForwardDeclarations(self): + def GenerateForwardDeclarations(self, cpp_namespace_pattern): """Returns the forward declarations for self._default_namespace. """ c = Code() - - for namespace, dependencies in self._NamespaceTypeDependencies().items(): - c.Append('namespace %s {' % namespace.unix_name) - for dependency in dependencies: - # No point forward-declaring hard dependencies. - if dependency.hard: - continue + for namespace, deps in self._NamespaceTypeDependencies().iteritems(): + filtered_deps = [ + dep for dep in deps # Add more ways to forward declare things as necessary. - if dependency.type_.property_type in (PropertyType.CHOICES, - PropertyType.OBJECT): - c.Append('struct %s;' % dependency.type_.name) - c.Append('}') + if (not dep.hard and + dep.type_.property_type in (PropertyType.CHOICES, + PropertyType.OBJECT))] + if not filtered_deps: + continue + cpp_namespace = cpp_util.GetCppNamespace(cpp_namespace_pattern, + namespace.unix_name) + c.Concat(cpp_util.OpenNamespace(cpp_namespace)) + for dep in filtered_deps: + c.Append('struct %s;' % dep.type_.name) + c.Concat(cpp_util.CloseNamespace(cpp_namespace)) return c def GenerateIncludes(self, include_soft=False): diff --git a/tools/json_schema_compiler/cpp_type_generator_test.py b/tools/json_schema_compiler/cpp_type_generator_test.py index 7782b23..f3a760a 100755 --- a/tools/json_schema_compiler/cpp_type_generator_test.py +++ b/tools/json_schema_compiler/cpp_type_generator_test.py @@ -58,15 +58,26 @@ class CppTypeGeneratorTest(unittest.TestCase): self.assertEquals('', manager.GenerateIncludes().Render()) self.assertEquals('#include "path/to/tabs.h"', manager.GenerateIncludes(include_soft=True).Render()) - self.assertEquals('namespace tabs {\n' - 'struct Tab;\n' - '}', - manager.GenerateForwardDeclarations().Render()) + self.assertEquals( + 'namespace tabs {\n' + 'struct Tab;\n' + '} // namespace tabs', + manager.GenerateForwardDeclarations('%(namespace)s').Render()) + self.assertEquals( + 'namespace foo {\n' + 'namespace bar {\n' + 'namespace tabs {\n' + 'struct Tab;\n' + '} // namespace tabs\n' + '} // namespace bar\n' + '} // namespace foo', + manager.GenerateForwardDeclarations('foo::bar::%(namespace)s').Render()) manager = CppTypeGenerator(self.models.get('permissions'), _FakeSchemaLoader(m)) self.assertEquals('', manager.GenerateIncludes().Render()) self.assertEquals('', manager.GenerateIncludes().Render()) - self.assertEquals('', manager.GenerateForwardDeclarations().Render()) + self.assertEquals( + '', manager.GenerateForwardDeclarations('%(namespace)s').Render()) manager = CppTypeGenerator(self.models.get('content_settings'), _FakeSchemaLoader(m)) self.assertEquals('', manager.GenerateIncludes().Render()) @@ -85,11 +96,8 @@ class CppTypeGeneratorTest(unittest.TestCase): self.assertEquals('#include "path/to/browser_action.h"\n' '#include "path/to/font_settings.h"', manager.GenerateIncludes().Render()) - self.assertEquals('namespace browser_action {\n' - '}\n' - 'namespace font_settings {\n' - '}', - manager.GenerateForwardDeclarations().Render()) + self.assertEquals( + '', manager.GenerateForwardDeclarations('%(namespace)s').Render()) def testGetCppTypeSimple(self): manager = CppTypeGenerator(self.models.get('tabs'), _FakeSchemaLoader(None)) diff --git a/tools/json_schema_compiler/cpp_util.py b/tools/json_schema_compiler/cpp_util.py index e7c29ac..187d99f 100644 --- a/tools/json_schema_compiler/cpp_util.py +++ b/tools/json_schema_compiler/cpp_util.py @@ -90,13 +90,16 @@ def GetParameterDeclaration(param, type_): } -def GenerateIfndefName(path, filename): - """Formats a path and filename as a #define name. +def GenerateIfndefName(file_path): + """Formats |file_path| as a #define name. Presumably |file_path| is a header + file, or there's little point in generating a #define for it. - e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. + e.g chrome/extensions/gen/file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. """ - return (('%s_%s_H__' % (path, filename)) - .upper().replace(os.sep, '_').replace('/', '_')) + return (('%s__' % file_path).upper() + .replace('\\', '_') + .replace('/', '_') + .replace('.', '_')) def PadForGenerics(var): @@ -106,28 +109,21 @@ def PadForGenerics(var): return ('%s ' % var) if var.endswith('>') else var -def OpenNamespace(namespace): + +def OpenNamespace(cpp_namespace): """Get opening root namespace declarations. """ c = Code() - # In lieu of GYP supporting None for the namespace variable the '' namespace - # implies there is no root namespace. - if namespace == '': - return c - for component in namespace.split('::'): + for component in cpp_namespace.split('::'): c.Append('namespace %s {' % component) return c -def CloseNamespace(namespace): +def CloseNamespace(cpp_namespace): """Get closing root namespace declarations. """ c = Code() - # In lieu of GYP supporting None for the namespace variable the '' namespace - # implies there is no root namespace. - if namespace == '': - return c - for component in reversed(namespace.split('::')): + for component in reversed(cpp_namespace.split('::')): c.Append('} // namespace %s' % component) return c @@ -145,3 +141,18 @@ def CamelCase(unix_name): def ClassName(filepath): return CamelCase(os.path.split(filepath)[1]) + + +def GetCppNamespace(pattern, namespace): + '''Returns the C++ namespace given |pattern| which includes a %(namespace)s + substitution, and the |namespace| to substitute. It is expected that |pattern| + has been passed as a flag to compiler.py from GYP/GN. + ''' + # For some reason Windows builds escape the % characters, so unescape them. + # This means that %% can never appear legitimately within a pattern, but + # that's ok. It should never happen. + cpp_namespace = pattern.replace('%%', '%') % { 'namespace': namespace } + assert '%' not in cpp_namespace, \ + ('Did not manage to fully substitute namespace "%s" into pattern "%s"' + % (namespace, pattern)) + return cpp_namespace diff --git a/tools/json_schema_compiler/cpp_util_test.py b/tools/json_schema_compiler/cpp_util_test.py index ede3095..eef4c554 100755 --- a/tools/json_schema_compiler/cpp_util_test.py +++ b/tools/json_schema_compiler/cpp_util_test.py @@ -3,29 +3,61 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -import cpp_util import unittest +from cpp_util import ( + Classname, CloseNamespace, GenerateIfndefName, OpenNamespace) + class CppUtilTest(unittest.TestCase): def testClassname(self): - self.assertEquals('Permissions', cpp_util.Classname('permissions')) + self.assertEquals('Permissions', Classname('permissions')) self.assertEquals('UpdateAllTheThings', - cpp_util.Classname('updateAllTheThings')) - self.assertEquals('Aa_Bb_Cc', cpp_util.Classname('aa.bb.cc')) + Classname('updateAllTheThings')) + self.assertEquals('Aa_Bb_Cc', Classname('aa.bb.cc')) def testNamespaceDeclaration(self): - self.assertEquals('namespace extensions {', - cpp_util.OpenNamespace('extensions').Render()) - self.assertEquals('} // namespace extensions', - cpp_util.CloseNamespace('extensions').Render()) - self.assertEquals('namespace extensions {\n' - 'namespace gen {\n' - 'namespace api {', - cpp_util.OpenNamespace('extensions::gen::api').Render()) - self.assertEquals('} // namespace api\n' - '} // namespace gen\n' - '} // namespace extensions', - cpp_util.CloseNamespace('extensions::gen::api').Render()) + self.assertEquals('namespace foo {', + OpenNamespace('foo').Render()) + self.assertEquals('} // namespace foo', + CloseNamespace('foo').Render()) + + self.assertEquals( + 'namespace extensions {\n' + 'namespace foo {', + OpenNamespace('extensions::foo').Render()) + self.assertEquals( + '} // namespace foo\n' + '} // namespace extensions', + CloseNamespace('extensions::foo').Render()) + + self.assertEquals( + 'namespace extensions {\n' + 'namespace gen {\n' + 'namespace api {', + OpenNamespace('extensions::gen::api').Render()) + self.assertEquals( + '} // namespace api\n' + '} // namespace gen\n' + '} // namespace extensions', + CloseNamespace('extensions::gen::api').Render()) + + self.assertEquals( + 'namespace extensions {\n' + 'namespace gen {\n' + 'namespace api {\n' + 'namespace foo {', + OpenNamespace('extensions::gen::api::foo').Render()) + self.assertEquals( + '} // namespace foo\n' + '} // namespace api\n' + '} // namespace gen\n' + '} // namespace extensions', + CloseNamespace('extensions::gen::api::foo').Render()) + + def testGenerateIfndefName(self): + self.assertEquals('FOO_BAR_BAZ_H__', GenerateIfndefName('foo\\bar\\baz.h')) + self.assertEquals('FOO_BAR_BAZ_H__', GenerateIfndefName('foo/bar/baz.h')) + if __name__ == '__main__': unittest.main() diff --git a/tools/json_schema_compiler/features_h_generator.py b/tools/json_schema_compiler/features_h_generator.py index 025f734..4198bb4 100644 --- a/tools/json_schema_compiler/features_h_generator.py +++ b/tools/json_schema_compiler/features_h_generator.py @@ -32,8 +32,12 @@ class _Generator(object): .Append(cpp_util.GENERATED_FEATURE_MESSAGE % self._source_file) .Append() ) - ifndef_name = cpp_util.GenerateIfndefName(self._source_file_filename, - self._class_name) + + # Hack: for the purpose of gyp the header file will always be the source + # file with its file extension replaced by '.h'. Assume so. + output_file = os.path.splitext(self._namespace.source_file)[0] + '.h' + ifndef_name = cpp_util.GenerateIfndefName(output_file) + (c.Append('#ifndef %s' % ifndef_name) .Append('#define %s' % ifndef_name) .Append() diff --git a/tools/json_schema_compiler/h_generator.py b/tools/json_schema_compiler/h_generator.py index fbb79e3..95c8056 100644 --- a/tools/json_schema_compiler/h_generator.py +++ b/tools/json_schema_compiler/h_generator.py @@ -2,31 +2,31 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import os + from code import Code from model import PropertyType import cpp_util import schema_util class HGenerator(object): - def __init__(self, type_generator, cpp_namespace): + def __init__(self, type_generator, cpp_namespace_pattern): self._type_generator = type_generator - self._cpp_namespace = cpp_namespace + self._cpp_namespace_pattern = cpp_namespace_pattern def Generate(self, namespace): return _Generator(namespace, self._type_generator, - self._cpp_namespace).Generate() + self._cpp_namespace_pattern).Generate() class _Generator(object): """A .h generator for a namespace. """ - def __init__(self, namespace, cpp_type_generator, cpp_namespace): + def __init__(self, namespace, cpp_type_generator, cpp_namespace_pattern): self._namespace = namespace self._type_helper = cpp_type_generator - self._cpp_namespace = cpp_namespace - self._target_namespace = ( - self._type_helper.GetCppNamespaceName(self._namespace)) + self._cpp_namespace_pattern = cpp_namespace_pattern self._generate_error_messages = namespace.compiler_options.get( 'generate_error_messages', False) @@ -40,8 +40,11 @@ class _Generator(object): .Append() ) - ifndef_name = cpp_util.GenerateIfndefName(self._namespace.source_file_dir, - self._target_namespace) + # Hack: for the purpose of gyp the header file will always be the source + # file with its file extension replaced by '.h'. Assume so. + output_file = os.path.splitext(self._namespace.source_file)[0] + '.h' + ifndef_name = cpp_util.GenerateIfndefName(output_file) + (c.Append('#ifndef %s' % ifndef_name) .Append('#define %s' % ifndef_name) .Append() @@ -58,19 +61,16 @@ class _Generator(object): .Append() ) - c.Concat(cpp_util.OpenNamespace(self._cpp_namespace)) # TODO(calamity): These forward declarations should be #includes to allow # $ref types from other files to be used as required params. This requires # some detangling of windows and tabs which will currently lead to circular # #includes. - forward_declarations = ( - self._type_helper.GenerateForwardDeclarations()) - if not forward_declarations.IsEmpty(): - (c.Append() - .Cblock(forward_declarations) - ) + c.Cblock(self._type_helper.GenerateForwardDeclarations( + self._cpp_namespace_pattern)) - c.Concat(self._type_helper.GetNamespaceStart()) + cpp_namespace = cpp_util.GetCppNamespace(self._cpp_namespace_pattern, + self._namespace.unix_name) + c.Concat(cpp_util.OpenNamespace(cpp_namespace)) c.Append() if self._namespace.properties: (c.Append('//') @@ -109,8 +109,7 @@ class _Generator(object): ) for event in self._namespace.events.values(): c.Cblock(self._GenerateEvent(event)) - (c.Concat(self._type_helper.GetNamespaceEnd()) - .Concat(cpp_util.CloseNamespace(self._cpp_namespace)) + (c.Concat(cpp_util.CloseNamespace(cpp_namespace)) .Append('#endif // %s' % ifndef_name) .Append() ) diff --git a/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp b/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp index 9eb3250..7921f87 100644 --- a/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp +++ b/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp @@ -28,7 +28,7 @@ 'error_generation.json' ], 'cc_dir': 'tools/json_schema_compiler/test', - 'root_namespace': 'test::api', + 'root_namespace': 'test::api::%(namespace)s', }, 'inputs': [ '<@(schema_files)', diff --git a/ui/accessibility/BUILD.gn b/ui/accessibility/BUILD.gn index 1eac576..32a4f71 100644 --- a/ui/accessibility/BUILD.gn +++ b/ui/accessibility/BUILD.gn @@ -76,7 +76,7 @@ test("accessibility_unittests") { generated_extensions_api("ax_gen") { sources = [ "ax_enums.idl" ] deps = [ "//base/third_party/dynamic_annotations" ] - root_namespace = "" + root_namespace = "ui" impl_dir = "." bundle = true } diff --git a/ui/accessibility/accessibility.gyp b/ui/accessibility/accessibility.gyp index 1fa6828..33032d7a 100644 --- a/ui/accessibility/accessibility.gyp +++ b/ui/accessibility/accessibility.gyp @@ -109,7 +109,7 @@ 'cc_dir': 'ui/accessibility', # TODO(dtseng): Change this once all files under ui/accessibility # namespaced under ui::ax. - 'root_namespace': '', + 'root_namespace': 'ui', }, }, ], |