demos: py_simple: New demo showimage.py.
[gfxprim.git] / configure
blobfed17b47e4ff9cdd5e28618c1a0b13c1ecb44bf8
1 #!/usr/bin/env python
3 # This is simple script to detect libraries and configure standard features.
5 import os
6 import sys
7 from optparse import OptionParser
9 def header_exists(cfg, filename):
10 fpath = cfg['include_path'][0] + '/' + filename
12 sys.stderr.write("Checking for '%s' ... " % fpath)
14 try:
15 st = os.stat(fpath)
16 sys.stderr.write("Yes\n")
17 return True
18 except os.error:
19 sys.stderr.write("No\n")
20 return False
22 def c_try_compile(cfg, code, msg):
23 sys.stderr.write(msg)
25 ret = os.system("echo '%s' | %s -x c -o /dev/null - > /dev/null 2>&1" %
26 (code, cfg["CC"][0]))
28 if ret:
29 sys.stderr.write("No\n")
30 return False
31 else:
32 sys.stderr.write("Yes\n")
33 return True
35 def c_compiler_exists(cfg):
36 return c_try_compile(cfg, "int main(void) { return 0; }",
37 "Checking for working compiler (%s) ... " %
38 cfg["CC"][0])
40 def python_module_installed(cfg, module):
41 sys.stderr.write("Checking for python module %s ... " % module)
43 ret = os.system("echo 'import %s' | %s > /dev/null 2>&1" %
44 (module, cfg['PYTHON_BIN'][0]))
46 if ret:
47 sys.stderr.write('No\n')
48 return False
49 else:
50 sys.stderr.write('Yes\n')
51 return True
53 def check_for_swig(cfg):
54 sys.stderr.write("Checking for working swig ... ")
56 ret = os.system("%s -version > /dev/null 2>&1" % cfg['SWIG'][0])
58 if ret:
59 sys.stderr.write('No\n')
60 cfg['SWIG'][0] = ''
61 else:
62 sys.stderr.write('Yes\n')
64 def check_for_python_config(cfg):
65 sys.stderr.write("Checking for python-config ... ")
67 ret = os.system("%s --libs > /dev/null 2>&1" % cfg['PYTHON_CONFIG'][0])
69 if ret:
70 sys.stderr.write('No\n')
71 cfg['PYTHON_CONFIG'][0] = ''
72 else:
73 sys.stderr.write('Yes\n')
76 # Library checking api
78 class libraries:
79 def __init__(self, libraries, cfg):
80 self.libraries = libraries
81 self.cfg = cfg;
82 # Create dictionary for check results
83 self.results = dict()
85 # Print summary
87 def print_summary(self):
88 sys.stderr.write("Libraries to link against\n")
89 sys.stderr.write("-------------------------\n")
91 for i in self.libraries:
92 sys.stderr.write("%10s" % i[0])
94 if (self.results[i[0]]):
95 sys.stderr.write(" : Enabled\n")
96 else:
97 sys.stderr.write(" : Disabled\n")
99 sys.stderr.write(" - %s\n\n" % i[1])
101 # Enable/Disable library
103 def set(self, name, val):
104 if name not in map(lambda s: s[0], self.libraries):
105 sys.stderr.write("ERROR: Invalid library '%s'\n" % name)
106 exit(1)
107 else:
108 self.results[name] = val
110 # Calls a function on arguments, all is stored in array if
111 # not set previously
112 # (I know this smells like a lisp, but I can't help myself)
114 def check(self):
115 sys.stderr.write("Checking for libraries\n")
116 sys.stderr.write("----------------------\n")
117 for i in self.libraries:
118 if i[0] not in self.results:
119 self.results[i[0]] = i[2][0](self.cfg, *i[2][1:])
120 sys.stderr.write("\n")
122 # Writes '#define HAVE_XXX_H' into passed file
124 def write_config_h(self, f):
125 for i in self.libraries:
126 f.write("/*\n * %s\n */\n" % i[1])
127 if self.results[i[0]]:
128 f.write("#define HAVE_%s\n" % i[0].upper())
129 else:
130 f.write("//#define HAVE_%s\n" % i[0].upper())
131 f.write("\n")
134 # Writes LDLIBS and CFLAGS into passed file
136 def write_config_mk(self, f):
137 for i in self.libraries:
138 f.write("# %s - %s\n" % (i[0], i[1]))
139 if self.results[i[0]]:
140 f.write("HAVE_%s=yes\n" % i[0].upper())
141 f.write("CFLAGS+=%s\nLDLIBS+=%s\n" % (i[3], i[4]))
142 else:
143 f.write("HAVE_%s=no\n" % i[0].upper())
146 # Return list of linker flags needed to build particular module
147 # (module may be core, loaders, backends, etc...
149 def get_linker_flags(self, module):
150 res = ''
151 for i in self.libraries:
152 if module in i[5] and self.results[i[0]]:
153 res += ' ' + i[4]
154 return res
156 # Returns list of cflags needed to build module
158 def get_cflags(self, module):
159 res = ''
160 for i in self.libraries:
161 if module in i[5] and self.results[i[0]]:
162 res += ' ' + i[3]
163 return res
165 def die_screaming(msg):
166 sys.stderr.write("\n************************************\n")
167 sys.stderr.write("ERROR: ")
168 sys.stderr.write(msg)
169 sys.stderr.write("\n************************************\n")
170 exit(1)
173 # Check for basic compiling tools
175 def basic_checks(cfg):
176 sys.stderr.write("Basic checks\n")
177 sys.stderr.write("------------\n")
179 if not c_compiler_exists(cfg):
180 die_screaming("No C compiler found")
182 if not python_module_installed(cfg, 'jinja2'):
183 die_screaming("No jinja2 python module found")
185 check_for_swig(cfg)
186 check_for_python_config(cfg)
187 sys.stderr.write("\n")
190 # Write configuration files
192 def write_config_h(cfg, libs):
193 f = open("config.h", "w")
194 f.write("/*\n * This file is genereated by configure script\n */\n");
195 f.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
196 libs.write_config_h(f);
197 f.write("#endif /* CONFIG_H */\n");
198 sys.stderr.write("Config 'config.h' written\n")
199 f.close()
201 def write_config_mk(cfg, libs):
202 f = open('config.gen.mk', 'w')
203 for i in cfg:
204 f.write("# %s\n%s=%s\n" % (cfg[i][1], i, cfg[i][0]))
205 libs.write_config_mk(f);
206 f.close()
207 sys.stderr.write("Config 'config.gen.mk' written\n")
210 # Generate app compilation helper
212 def write_gfxprim_config(cfg, libs):
213 modules = ['loaders', 'backends', 'grabbers']
215 f = open('gfxprim-config', 'w')
216 f.write('#!/bin/sh\n'
217 '#\n# Generated by configure, do not edit directly\n#\n\n'
218 'USAGE="Usage: $0 --list-modules --cflags --libs --libs-module_foo"\n'
219 '\nif test $# -eq 0; then\n'
220 '\techo "$USAGE"\n'
221 '\texit 1\n'
222 'fi\n\n'
223 'while test -n "$1"; do\n'
224 '\tcase "$1" in\n')
226 # General switches cflags and ldflags
227 f.write('\t--help) echo "$USAGE"; exit 0;;\n')
228 f.write('\t--list-modules) echo "%s"; exit 0;;\n' % ' '.join(modules))
229 f.write('\t--cflags) echo -n "-I/usr/include/GP/%s";;\n' %
230 libs.get_cflags('core'))
231 f.write('\t--libs) echo -n "-lGP %s ";;\n' % libs.get_linker_flags('core'))
233 # ldflags for specific modules
234 for i in modules:
235 ldflags = ''
236 if i == 'backends':
237 ldflags += '-lGP_backends '
238 if i == 'grabbers':
239 ldflags += '-lGP_grabbers '
240 ldflags += libs.get_linker_flags(i)
241 f.write('\t--libs-%s) echo -n "%s ";;\n' % (i, ldflags))
243 f.write('\t*) echo "Invalid option \'$1\'"; echo $USAGE; exit 1;;\n')
245 f.write('\tesac\n\tshift\ndone\necho\n')
246 f.close()
247 os.system('chmod +x gfxprim-config')
249 if __name__ == '__main__':
251 # Dictionary for default configuration parameters
253 cfg = {'CC' : ['gcc', 'Path/name of the C compiler'],
254 'CFLAGS' : ['-pthread -W -Wall -Wextra -fPIC -O2 -ggdb -D_FORTIFY_SOURCE=2', 'C compiler flags'],
255 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
256 'SWIG' : ['swig', 'Simplified Wrapper and Interface Generator'],
257 'PYTHON_CONFIG' : ['python-config', 'Python config helper'],
258 'include_path' : ['/usr/include', 'Path to the system headers']}
261 # Library detection/enable disable
263 # name, description, [detection], cflags, ldflags, list of modules library is needed for
265 l = libraries([["libpng",
266 "Portable Network Graphics Library",
267 [header_exists, "png.h"], "", "-lpng", ["loaders"]],
268 ["libsdl",
269 "Simple Direct Media Layer",
270 [header_exists, "SDL/SDL.h"], "", "`sdl-config --libs`", ["backends"]],
271 ["jpeg",
272 "Library to load, handle and manipulate images in the JPEG format",
273 [header_exists, "jpeglib.h"], "", "-ljpeg", ["loaders"]],
274 ["giflib",
275 "Library to handle, display and manipulate GIF images",
276 [header_exists, "gif_lib.h"], "", "-lgif", ["loaders"]],
277 ["tiff",
278 "Tag Image File Format (TIFF) library",
279 [header_exists, "tiffio.h"], "", "-ltiff", ["loaders"]],
280 ["libX11",
281 "X11 library",
282 [header_exists, "X11/Xlib.h"], "", "-lX11", ["backends"]],
283 ["X_SHM",
284 "MIT-SHM X Extension",
285 [header_exists, "X11/extensions/XShm.h"], "", "-lXext", ["backends"]],
286 ["freetype",
287 "A high-quality and portable font engine",
288 [header_exists, "ft2build.h"], "", "`freetype-config --libs`", ["core"]],
289 ["dl",
290 "Dynamic linker",
291 [header_exists, "dlfcn.h"], "", "-ldl", ["core"]],
292 ["V4L2",
293 "Video for linux 2",
294 [header_exists, "linux/videodev2.h"], "", "", ["grabbers"]],
295 ["pthread",
296 "Posix Threads",
297 [header_exists, "pthread.h"], "-pthread", "-pthread", ["core"]],
298 ["backtrace",
299 "C stack trace writeout",
300 [c_try_compile, "#include <execinfo.h>\nint main(void) { backtrace(0, 0); }",
301 "Checking for backtrace() ... "], "", "", ["core"]]], cfg)
303 parser = OptionParser();
305 # Get configuration parameters from environment variables
306 for i in cfg:
307 if i in os.environ:
308 cfg[i][0] = os.environ[i]
310 # Enable disable libraries for linking
311 parser.add_option("-e", "--enable", dest="enable", action="append",
312 help="force enable library linking", metavar="libfoo")
313 parser.add_option("-d", "--disable", dest="disable", action="append",
314 help="disable library linking", metavar="libfoo")
316 # Add cfg config options
317 for i in cfg:
318 parser.add_option("", "--"+i, dest=i, metavar=cfg[i][0], help=cfg[i][1])
320 (options, args) = parser.parse_args();
323 # Enable/Disable libraries as user requested
324 # These are not checked later
326 if options.enable:
327 for i in options.enable:
328 l.set(i, True);
329 if options.disable:
330 for i in options.disable:
331 l.set(i, False);
333 for i in cfg:
334 if getattr(options, i):
335 cfg[i][0] = getattr(options, i)
337 basic_checks(cfg);
339 l.check()
340 l.print_summary()
342 write_config_h(cfg, l)
343 write_config_mk(cfg, l)
344 write_gfxprim_config(cfg, l)