summaryrefslogtreecommitdiffstats
path: root/ppapi/generators/idl_c_header.py
blob: feef088423d59835ff05de90164690b3f6583e26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/python
#
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

""" Generator for C style prototypes and definitions """

import glob
import os
import sys
import subprocess

from idl_log import ErrOut, InfoOut, WarnOut
from idl_node import IDLAttribute, IDLNode
from idl_ast import IDLAst
from idl_option import GetOption, Option, ParseOptions
from idl_outfile import IDLOutFile
from idl_parser import ParseFiles
from idl_c_proto import CGen

Option('dstroot', 'Base directory of output', default='../c')
Option('guard', 'Include guard prefix', default='ppapi/c')
Option('out', 'List of output files', default='')

cgen = CGen()

def IDLToHeader(filenode, relpath=None, prefix=None):
  path, name = os.path.split(filenode.GetProperty('NAME'))
  name = os.path.splitext(name)[0] + '.h'
  if prefix: name = '%s%s' % (prefix, name)
  if path: name = os.path.join(path, name)
  if relpath: name = os.path.join(relpath, name)
  return name


def GenerateHeader(filenode, pref, inline=True):
  name = filenode.GetProperty('NAME')
  if name == 'pp_stdint.idl': return

  # If we have an 'out' filter list, then check if we should output this file.
  outlist = GetOption('out')
  if outlist:
    outlist = outlist.split(',')
    if name not in outlist:
      return

  savename = IDLToHeader(filenode, relpath=GetOption('dstroot'), prefix=pref)
  out = IDLOutFile(savename)

  gpath = GetOption('guard')
  def_guard = IDLToHeader(filenode, relpath=gpath, prefix=pref)
  def_guard = def_guard.replace('/','_').replace('.','_').upper() + '_'
  copyright = filenode.GetChildren()[0]
  assert(copyright.IsA('Copyright'))

  fileinfo = filenode.GetChildren()[1]
  assert(fileinfo.IsA('Comment'))

  out.Write('%s\n' % cgen.Copyright(copyright))
  out.Write('/* From %s modified %s. */\n\n'% (
      filenode.GetProperty('NAME'), filenode.GetProperty('DATETIME')))
  out.Write('#ifndef %s\n#define %s\n\n' % (def_guard, def_guard))

  for label in filenode.GetListOf('Label'):
    if label.GetName() == GetOption('label'):
      cgen.SetVersionMap(label)

  deps = filenode.GetDeps('M14')
  # Generate set of includes
  includes = set([])
  for dep in deps:
    depfile = dep.GetProperty('FILE')
    if depfile:
      includes.add(depfile)
  includes = [IDLToHeader(include, relpath=gpath) for include in includes]
  includes.append('ppapi/c/pp_macros.h')

  # Assume we need stdint if we "include" C or C++ code
  if filenode.GetListOf('Include'):
    includes.append('ppapi/c/pp_stdint.h')

  includes = sorted(set(includes))
  cur_include = IDLToHeader(filenode, relpath=gpath)
  for include in includes:
    if include == cur_include: continue
    out.Write('#include "%s"\n' % include)

  # Generate the @file comment
  out.Write('\n%s\n' % cgen.Comment(fileinfo, prefix='*\n @file'))

  # Generate definitions.
  last_group = None
  for node in filenode.GetChildren()[2:]:
    # If we are part of a group comment marker...
    if last_group and last_group != node.cls:
      pre = cgen.CommentLines(['*',' @}', '']) + '\n'
    else:
      pre = '\n'

    if node.cls in ['Typedef', 'Interface', 'Struct', 'Enum']:
      if last_group != node.cls:
        pre += cgen.CommentLines(['*',' @addtogroup %ss' % node.cls, ' @{', ''])
      last_group = node.cls
    else:
      last_group = None

    if node.IsA('Comment'):
      item = '%s\n\n' % cgen.Comment(node)
    elif node.IsA('Inline'):
      if not inline: continue
      if node.GetName() == 'cc':
        item = cgen.Define(node, prefix=pref, comment=True)
        item = '#ifdef __cplusplus\n%s\n#endif  // __cplusplus\n\n' % item
      elif node.GetName() == 'c':
        item = cgen.Define(node, prefix=pref, comment=True)
      else:
        continue
      if not item: continue
    else:
      #
      # Otherwise we are defining a file level object, so generate the
      # correct document notation.
      #
      item = cgen.Define(node, prefix=pref, comment=True)
      if not item: continue
      asize = node.GetProperty('assert_size()')
      if asize:
        name = '%s%s' % (pref, node.GetName())
        if node.IsA('Struct'):
          form = 'PP_COMPILE_ASSERT_STRUCT_SIZE_IN_BYTES(%s, %s);\n'
        else:
          form = 'PP_COMPILE_ASSERT_SIZE_IN_BYTES(%s, %s);\n'
        item += form % (name, asize[0])

    if item: out.Write('%s%s' % (pre, item))
  if last_group:
    out.Write(cgen.CommentLines(['*',' @}', '']) + '\n')

  out.Write('#endif  /* %s */\n\n' % def_guard)
  out.Close()


def GenerateFileTest(cfile, filenode, pref):
  tests = []
  original = IDLToHeader(filenode, relpath='')
  savename = IDLToHeader(filenode, relpath='', prefix=pref)
  cfile.Write('/* Test %s */\n' % filenode.GetProperty('NAME'))
  cfile.Write('#include "%s"\n' % original)
  cfile.Write('#include "%s"\n' % savename)

  # For all children (excluding copyright notice)
  for node in filenode.children[1:]:
    # Test enums by assigning all enum items to themselves. Unfortunately this
    # will not catch cases where the '.h' has enums that the IDL does not.
    if node.IsA('Enum'):
      tests.append('test_%s' % node.GetProperty('NAME'))
      cfile.Write('int test_%s() {\n' % node.GetProperty('NAME'))
      cfile.Write('  int errors = 0;\n')
      for enum in node.GetListOf('EnumItem'):
        cfile.Write('  errors += VERIFY_ENUM(%s, %s%s);\n' %
            (enum.GetProperty('NAME'), pref, enum.GetProperty('NAME')))
      cfile.Write('  if (errors) printf("Failure in %s:%s.\\n");\n' %
          (filenode.GetName(), node.GetName()))
      cfile.Write('  return errors;\n}\n\n')
      continue

    # Use a structure asignment to verify equivilency
    if node.IsA('Interface', 'Struct'):
      tests.append('test_%s' % node.GetName())
      rtype1 = cgen.GetTypeName(node)
      rtype2 = cgen.GetTypeName(node, prefix='tst_')
      cfile.Write('int test_%s() {\n' % node.GetName())
      cfile.Write('  int errors = 0;\n');
      cmp_structs = '  %s A;\n  %s B;\n' % (rtype1, rtype2)
      cfile.Write(cmp_structs)
      cfile.Write('  memset(&A, 0, sizeof(A));\n')
      cfile.Write('  memset(&B, 1, sizeof(B));\n')
      for member in node.GetListOf('Member', 'Function'):
        if member.GetOneOf('Array'):
          cfile.Write('  memcpy(A.%s, B.%s, sizeof(A.%s));\n' %
                      (member.GetName(), member.GetName(), member.GetName()))
        else:
          cfile.Write('  A.%s = B.%s;\n' % (member.GetName(), member.GetName()))
      cfile.Write('  errors += (sizeof(A) != sizeof(B)) ? 1 : 0;\n')
      cfile.Write('  errors += (memcmp(&A, &B, sizeof(A))) ? 1 : 0;\n')
      cfile.Write('  return (sizeof(A) == sizeof(B));\n}\n')
      continue

  cfile.Write('\n\n')
  return tests

def GenerateBaseTest(cfile):
  for inc in ['<stdio.h>','<string.h>','"pp_stdint.h"']:
    cfile.Write('#include %s\n' % inc)
  cfile.Write('\n')
  cfile.Write('#define VERIFY_ENUM(orig, idl) Check(#orig, orig, idl)\n\n')
  cfile.Write('int Check(const char *name, int v1, int v2) {\n')
  cfile.Write('  if (v1 == v2) return 0;\n')
  cfile.Write('  printf("Mismatch enum %s : %d vs %d\\n", name, v1, v2);\n')
  cfile.Write('  return 1;\n}\n\n')


def Main(args):
  filenames = ParseOptions(args)
  ast = ParseFiles(filenames)
  testFuncs = []
  skipList = []

  outdir = GetOption('dstroot')

  if GetOption('test'):
    prefix = 'tst_'
    cfile = IDLOutFile(os.path.join(outdir, 'test.c'))
    GenerateBaseTest(cfile)
  else:
    prefix = ''
    cfile = None

  for filenode in ast.GetListOf('File'):
    if GetOption('verbose'):
      print "Working on %s" % filenode

    # If this file has errors, skip it
    if filenode.GetProperty('ERRORS') > 0:
      skipList.append(filenode)
      continue

    GenerateHeader(filenode, prefix, inline=not cfile)
    if cfile: GenerateFileTest(cfile, filenode, prefix)

  if cfile:
    cfile.Write('int main(int argc, const char *args[]) {\n')
    cfile.Write('  int errors = 0;\n');
    for testname in testFuncs:
      cfile.Write('  errors += %s();\n' % testname)

    cfile.Write('  printf("Found %d error(s) in the IDL.\\n", errors);\n')
    cfile.Write('  return errors;\n}\n\n')
    cfile.Close()

  # TODO(noelallen) Add a standard test
    if not skipList:
      args = ['gcc', '-o', 'tester', '%s/test.c' % outdir, '-I%s' % outdir,
              '-I../c', '-I../..', '-DPPAPI_INSTANCE_REMOVE_SCRIPTING']
      InfoOut.Log('Running: %s' % ' '.join(args))
      ret = subprocess.call(args)
      if ret:
        ErrOut.Log('Failed to compile.')
        return -1
      InfoOut.Log('Running: ./tester')
      ret = subprocess.call('./tester')
      if ret > 0:
        ErrOut.Log('Found %d errors.' % ret)
        return ret
      InfoOut.Log('Success!')
      return 0

  for filenode in skipList:
    errcnt = filenode.GetProperty('ERRORS')
    ErrOut.Log('%s : Skipped because of %d errors.' % (
        filenode.GetName(), errcnt))

  if not skipList:
    InfoOut.Log('Processed %d files.' % len(ast.GetListOf('File')))
  return len(skipList)

if __name__ == '__main__':
  sys.exit(Main(sys.argv[1:]))