def Generate(self): """Generates a Code object with the .h for a single namespace. """ c = Code() (c.Append(cpp_util.CHROMIUM_LICENSE).Append().Append( cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file).Append()) # 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) # Hack: tabs and windows have circular references, so only generate hard # references for them (i.e. anything that can't be forward declared). In # other cases, generate soft dependencies so that they can include # non-optional types from other namespaces. include_soft = self._namespace.name not in ('tabs', 'windows') (c.Append('#ifndef %s' % ifndef_name).Append( '#define %s' % ifndef_name).Append().Append('#include <stdint.h>'). Append().Append('#include <map>').Append('#include <memory>').Append( '#include <string>').Append('#include <vector>').Append().Append( '#include "base/logging.h"').Append( '#include "base/values.h"').Cblock( self._type_helper.GenerateIncludes( include_soft=include_soft)).Append()) # Hack: we're not generating soft includes for tabs and windows, so we need # to generate forward declarations for them. if not include_soft: c.Cblock(self._type_helper.GenerateForwardDeclarations()) cpp_namespace = cpp_util.GetCppNamespace( self._namespace.environment.namespace_pattern, self._namespace.unix_name) c.Concat(cpp_util.OpenNamespace(cpp_namespace)) c.Append() if self._namespace.properties: (c.Append('//').Append('// Properties').Append('//').Append()) for prop in self._namespace.properties.values(): property_code = self._type_helper.GeneratePropertyValues( prop, 'extern const %(type)s %(name)s;') if property_code: c.Cblock(property_code) if self._namespace.types: (c.Append('//').Append('// Types').Append('//').Append().Cblock( self._GenerateTypes(self._FieldDependencyOrder(), is_toplevel=True, generate_typedefs=True))) if self._namespace.functions: (c.Append('//').Append('// Functions').Append('//').Append()) for function in self._namespace.functions.values(): c.Cblock(self._GenerateFunction(function)) if self._namespace.events: (c.Append('//').Append('// Events').Append('//').Append()) for event in self._namespace.events.values(): c.Cblock(self._GenerateEvent(event)) (c.Concat(cpp_util.CloseNamespace(cpp_namespace)).Append( '#endif // %s' % ifndef_name).Append()) return c
def Generate(self, _): # namespace not relevant, this is a bundle c = code.Code() c.Append(cpp_util.CHROMIUM_LICENSE) c.Append() c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, 'generated_schemas.h'))) c.Append() c.Append('#include "base/lazy_instance.h"') c.Append() c.Append('namespace {') for api in self._bundle._api_defs: namespace = self._bundle._model.namespaces[api.get('namespace')] json_content = json.dumps(_PrefixSchemaWithNamespace( _RemoveUnneededFields(api)), separators=(',', ':')) # Escape all double-quotes and backslashes. For this to output a valid # JSON C string, we need to escape \ and ". Note that some schemas are # too large to compile on windows. Split the JSON up into several # strings, since apparently that helps. max_length = 8192 segments = [ json_content[i:i + max_length].replace('\\', '\\\\').replace( '"', '\\"') for i in xrange(0, len(json_content), max_length) ] c.Append( 'const char %s[] = "%s";' % (_FormatNameAsConstant(namespace.name), '" "'.join(segments))) c.Append() c.Sblock('struct Static {') c.Sblock('Static() {') for api in self._bundle._api_defs: namespace = self._bundle._model.namespaces[api.get('namespace')] c.Append('schemas["%s"] = %s;' % (namespace.name, _FormatNameAsConstant(namespace.name))) c.Eblock('}') c.Append() c.Append('std::map<std::string, const char*> schemas;') c.Eblock('};') c.Append() c.Append('base::LazyInstance<Static> g_lazy_instance;') c.Append() c.Append('} // namespace') c.Append() c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) c.Append() c.Append('// static') c.Sblock('base::StringPiece %s::Get(const std::string& name) {' % self._bundle._GenerateBundleClass('GeneratedSchemas')) c.Append('return IsGenerated(name) ? ' 'g_lazy_instance.Get().schemas[name] : "";') c.Eblock('}') c.Append() c.Append('// static') c.Sblock('bool %s::IsGenerated(std::string name) {' % self._bundle._GenerateBundleClass('GeneratedSchemas')) c.Append('return g_lazy_instance.Get().schemas.count(name) > 0;') c.Eblock('}') c.Append() c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) c.Append() return c
def Generate(self): """Generates a Code object with the .h for a single namespace. """ c = Code() (c.Append(cpp_util.CHROMIUM_LICENSE) .Append() .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) .Append() ) ifndef_name = cpp_util.GenerateIfndefName(self._namespace.source_file_dir, self._target_namespace) (c.Append('#ifndef %s' % ifndef_name) .Append('#define %s' % ifndef_name) .Append() .Append('#include <map>') .Append('#include <string>') .Append('#include <vector>') .Append() .Append('#include "base/basictypes.h"') .Append('#include "base/logging.h"') .Append('#include "base/memory/linked_ptr.h"') .Append('#include "base/memory/scoped_ptr.h"') .Append('#include "base/values.h"') .Cblock(self._type_helper.GenerateIncludes()) .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.Concat(self._type_helper.GetNamespaceStart()) c.Append() if self._namespace.properties: (c.Append('//') .Append('// Properties') .Append('//') .Append() ) for property in self._namespace.properties.values(): property_code = self._type_helper.GeneratePropertyValues( property, 'extern const %(type)s %(name)s;') if property_code: c.Cblock(property_code) if self._namespace.types: (c.Append('//') .Append('// Types') .Append('//') .Append() .Cblock(self._GenerateTypes(self._FieldDependencyOrder(), is_toplevel=True, generate_typedefs=True)) ) if self._namespace.functions: (c.Append('//') .Append('// Functions') .Append('//') .Append() ) for function in self._namespace.functions.values(): c.Cblock(self._GenerateFunction(function)) if self._namespace.events: (c.Append('//') .Append('// Events') .Append('//') .Append() ) 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)) .Append('#endif // %s' % ifndef_name) .Append() ) return c
def Generate(self): """Generates a Code object with the .cc for a single namespace. """ cpp_namespace = cpp_util.GetCppNamespace( self._namespace.environment.namespace_pattern, self._namespace.unix_name) c = Code() (c.Append(cpp_util.CHROMIUM_LICENSE) .Append() .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) .Append() .Append(self._util_cc_helper.GetIncludePath()) .Append('#include "base/logging.h"') .Append('#include "base/strings/string_number_conversions.h"') .Append('#include "base/strings/utf_string_conversions.h"') .Append('#include "%s/%s.h"' % (self._namespace.source_file_dir, self._namespace.short_filename)) .Append('#include <set>') .Append('#include <utility>') .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) .Append() .Append('using base::UTF8ToUTF16;') .Append() .Concat(cpp_util.OpenNamespace(cpp_namespace)) ) if self._namespace.properties: (c.Append('//') .Append('// Properties') .Append('//') .Append() ) for prop in self._namespace.properties.values(): property_code = self._type_helper.GeneratePropertyValues( prop, 'const %(type)s %(name)s = %(value)s;', nodoc=True) if property_code: c.Cblock(property_code) if self._namespace.types: (c.Append('//') .Append('// Types') .Append('//') .Append() .Cblock(self._GenerateTypes(None, self._namespace.types.values())) ) if self._namespace.functions: (c.Append('//') .Append('// Functions') .Append('//') .Append() ) for function in self._namespace.functions.values(): c.Cblock(self._GenerateFunction(function)) if self._namespace.events: (c.Append('//') .Append('// Events') .Append('//') .Append() ) for event in self._namespace.events.values(): c.Cblock(self._GenerateEvent(event)) c.Cblock(cpp_util.CloseNamespace(cpp_namespace)) c.Append() return c
def Generate(self, _): # namespace not relevant, this is a bundle c = code.Code() c.Append(cpp_util.CHROMIUM_LICENSE) c.Append() c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, 'generated_schemas.h'))) c.Append() c.Append('#include <algorithm>') c.Append('#include <iterator>') c.Append() c.Append('#include "base/stl_util.h"') c.Append() c.Append('namespace {') for api in self._bundle._api_defs: namespace = self._bundle._model.namespaces[api.get('namespace')] json_content = json.dumps(_PrefixSchemaWithNamespace( _RemoveUnneededFields(api)), separators=(',', ':')) # This will output a valid JSON C string. Note that some schemas are # too large to compile on windows. Split the JSON up into several # strings, since apparently that helps. max_length = 8192 segments = [ json_content[i:i + max_length] for i in range(0, len(json_content), max_length) ] c.Append('constexpr char %s[] = R"R(%s)R";' % (_FormatNameAsConstant( namespace.name), ')R" R"R('.join(segments))) c.Append('} // namespace') c.Append() c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) c.Append() c.Append('// static') c.Sblock('bool %s::IsGenerated(base::StringPiece name) {' % self._bundle._GenerateBundleClass('GeneratedSchemas')) c.Append('return !Get(name).empty();') c.Eblock('}') c.Append() c.Append('// static') c.Sblock('base::StringPiece %s::Get(base::StringPiece name) {' % self._bundle._GenerateBundleClass('GeneratedSchemas')) c.Sblock('static constexpr struct kSchemaMapping {') c.Append('const base::StringPiece name;') c.Append('const base::StringPiece schema;') c.Sblock( 'constexpr bool operator<(const kSchemaMapping& that) const {') c.Append('return name < that.name;') c.Eblock('}') c.Eblock() c.Sblock('} kSchemas[] = {') namespaces = [ self._bundle._model.namespaces[api.get('namespace')].name for api in self._bundle._api_defs ] for namespace in sorted(namespaces): schema_constant_name = _FormatNameAsConstant(namespace) c.Append('{"%s", %s},' % (namespace, schema_constant_name)) c.Eblock('};') c.Append( 'static_assert(base::STLIsSorted(kSchemas), "|kSchemas| should be ' 'sorted.");') c.Sblock('auto it = std::lower_bound(std::begin(kSchemas), ' 'std::end(kSchemas),') c.Append('kSchemaMapping{name, base::StringPiece()});') c.Eblock() c.Sblock('if (it != std::end(kSchemas) && it->name == name)') c.Append('return it->schema;') c.Eblock() c.Append('return base::StringPiece();') c.Eblock('}') c.Append() c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) c.Append() return c
def Generate(self, _): # namespace not relevant, this is a bundle c = code.Code() c.Append(cpp_util.CHROMIUM_LICENSE) c.Append() c.Append('#include "%s"' % (os.path.join(self._bundle._source_file_dir, 'generated_schemas.h'))) c.Append() c.Append('#include <algorithm>') c.Append('#include <iterator>') c.Append() c.Append('#include "base/containers/fixed_flat_map.h"') c.Append('#include "base/strings/string_piece.h"') c.Append() c.Append('namespace {') for api in self._bundle._api_defs: namespace = self._bundle._model.namespaces[api.get('namespace')] json_content = json.dumps(_PrefixSchemaWithNamespace( _RemoveUnneededFields(api)), separators=(',', ':')) # This will output a valid JSON C string. Note that some schemas are # too large to compile on windows. Split the JSON up into several # strings, since apparently that helps. max_length = 8192 segments = [ json_content[i:i + max_length] for i in range(0, len(json_content), max_length) ] c.Append('constexpr char %s[] = R"R(%s)R";' % (_FormatNameAsConstant( namespace.name), ')R" R"R('.join(segments))) c.Append('} // namespace') c.Append() c.Concat(cpp_util.OpenNamespace(self._bundle._cpp_namespace)) c.Append() c.Append('// static') c.Sblock('bool %s::IsGenerated(base::StringPiece name) {' % self._bundle._GenerateBundleClass('GeneratedSchemas')) c.Append('return !Get(name).empty();') c.Eblock('}') c.Append() c.Append('// static') c.Sblock('base::StringPiece %s::Get(base::StringPiece name) {' % self._bundle._GenerateBundleClass('GeneratedSchemas')) c.Append( 'static constexpr auto kSchemas = ' 'base::MakeFixedFlatMap<base::StringPiece, base::StringPiece>({') c.Sblock() namespaces = [ self._bundle._model.namespaces[api.get('namespace')].name for api in self._bundle._api_defs ] for namespace in sorted(namespaces): schema_constant_name = _FormatNameAsConstant(namespace) c.Append('{"%s", %s},' % (namespace, schema_constant_name)) c.Eblock('});') c.Append('auto it = kSchemas.find(name);') c.Append( 'return it != kSchemas.end() ? it->second : base::StringPiece();') c.Eblock('}') c.Append() c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) c.Append() return c