The Assimilation Project  based on Assimilation version 1.1.7.1474836767
genpybindings.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # vim: smartindent tabstop=4 shiftwidth=4 expandtab number
3 #
4 # Genpybindings - generate python bindings for Assimilation C classes
5 #
6 # usage: genpybindings.py outfile sourceroot buildddir libdir libfile...
7 #
8 # outfile name of file to put output into
9 # sourceroot root of source directory tree
10 # buildroot root of build (binary) directory tree
11 # libdir where the runtime libraries are located
12 # libfiles list of library files that we might want to bind to (just one today)
13 #
14 # This file is part of the Assimilation Project.
15 #
16 # Copyright (C) 2011, 2012 - Alan Robertson <alanr@unix.sh>
17 #
18 # The Assimilation software is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU General Public License as published by
20 # the Free Software Foundation, either version 3 of the License, or
21 # (at your option) any later version.
22 #
23 # The Assimilation software is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
27 #
28 # You should have received a copy of the GNU General Public License
29 # along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/
30 #
31 #
32 import os, sys
33 glibpkgname='glib-2.0'
34 
35 def readcmdline(cmd):
36  'Read the first line of output from running a command'
37  fd = os.popen(cmd, 'r')
38  line=fd.readline()
39  fd.close()
40  return line.strip()
41 
42 glibheaderfile='glib.h'
43 
44 # Ask pkg-config for the -I flags for glib2
45 glibincflags=readcmdline('pkg-config --cflags-only-I %s' % glibpkgname)
46 # Ask pkg-config for the loader library flags for glib2
47 gliblibflags=readcmdline('pkg-config --libs %s' % glibpkgname).split(' ')
48 
49 # Compute the list of include directories for glib (without -I prefixes)
50 glibincdirs=[]
51 for iflag in glibincflags.split():
52  if not iflag.startswith('-I'):
53  continue
54  glibincdirs.append(iflag[2:])
55 
56 def findincfile(incdirs, filename):
57  'Find a(n include) file somewhere under this list of directories'
58  for dir in incdirs:
59  pathname=os.path.join(dir, filename)
60  if os.path.exists(pathname):
61  return pathname
62 
63 def find_cpp():
64  'Return a string saying how to find the C preprocessor - along with any necessary arguments'
65  # See http://code.google.com/p/ctypesgen/wiki/GettingStarted for Windows details...
66  return '--cpp=gcc -E -DCTYPESGEN -D__signed__=signed'
67 
68 def build_cmdargs(outfile, sourceroot, buildroot, libdir, libfiles):
69  'Build the ctypesgen command line to execute - and run it'
70  args=[ '--no-macro-warnings',
71  find_cpp(),
72  '-o', outfile,
73  '--runtime-libdir', libdir,
74  '--compile-libdir', os.path.join(buildroot, 'clientlib'),
75  '-I' + os.path.join(sourceroot, 'include')]
76  args.append('-L')
77 
78  # Glib library flags - typically -lglib-2.0
79  for flag in gliblibflags:
80  args.append(flag)
81 
82  # Our local libraries
83  for lib in libfiles:
84  args.append('-l')
85  args.append(lib + '.so') # Obviously needs to change for windows - or so I think ;-)
86  # -I flags for glib
87  for iflag in glibincflags.split():
88  args.append(iflag)
89  includedir=os.path.join(sourceroot, 'include')
90  # -I directive for our include directory
91  args.append('-I' + includedir)
92  # -I directive for our build include directory
93  buildincludedir=os.path.join(buildroot, 'include')
94  args.append('-I' + buildincludedir)
95 
96  # All the flags are set - now list out the headers to parse
97  # Starting with the full pathname to glib.h
98  args.append(findincfile(glibincdirs, glibheaderfile))
99  # Add on the pathnames of all our header files
100  hdrfiles=os.listdir(includedir)
101  hfileset={}
102  for hfile in hdrfiles:
103  if not hfile.endswith('.h'):
104  continue
105  hfileset[hfile] = True
106  args.append(os.path.join(includedir, hfile))
107  # Add on the pathnames of all our generated header files
108  hdrfiles=os.listdir(buildincludedir)
109  for hfile in hdrfiles:
110  # Sometimes people do an in-place build...
111  if not hfile.endswith('.h') or hfile in hfileset:
112  continue
113  args.append(os.path.join(buildincludedir, hfile))
114 
115  # Now build the quoted command line from the arguments
116  cmdline='ctypesgen.py'
117  for arg in args:
118  cmdline += ' "%s"' % arg
119 
120  #print ('Running', cmdline)
121  return cmdline
122 
123 if len(sys.argv) < 6:
124  sys.stderr.write('Usage: %s outfile sourceroot buildroot libdir libfile ...\n' % sys.argv[0])
125  raise SystemExit(1)
126 
127 outfile=sys.argv[1]
128 sourceroot=sys.argv[2]
129 buildroot=sys.argv[3]
130 libdir=sys.argv[4]
131 libfiles = sys.argv[5:]
132 raise SystemExit(os.system(build_cmdargs(outfile, sourceroot, buildroot, libdir, libfiles)))
def readcmdline(cmd)
def findincfile(incdirs, filename)
def build_cmdargs(outfile, sourceroot, buildroot, libdir, libfiles)