tests: core: WritePixel Testsuite
[gfxprim.git] / configure
blob9deeb6cf98c660fe041dd90f64ad813113f2fb01
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
8 from subprocess import check_output
10 def header_exists(cfg, filename):
11 fpath = cfg['include_path'][0] + '/' + filename
13 sys.stderr.write("Checking for '%s' ... " % fpath)
15 try:
16 st = os.stat(fpath)
17 sys.stderr.write("Yes\n")
18 return True
19 except os.error:
20 sys.stderr.write("No\n")
21 return False
23 def c_try_compile(cfg, code, msg):
24 sys.stderr.write(msg)
26 ret = os.system("echo '%s' | %s -x c -o /dev/null - > /dev/null 2>&1" %
27 (code, cfg["CC"][0]))
29 if ret:
30 sys.stderr.write("No\n")
31 return False
32 else:
33 sys.stderr.write("Yes\n")
34 return True
36 def c_compiler_exists(cfg):
37 return c_try_compile(cfg, "int main(void) { return 0; }",
38 "Checking for working compiler (%s) ... " %
39 cfg["CC"][0])
41 def python_version(cfg):
42 sys.stderr.write("Cddhecking for python-config Python version ... ")
44 if (cfg['PYTHON_CONFIG'][0] is ''):
45 sys.stderr.write('NA\n')
46 return ''
48 res = str(check_output("%s --ldflags" % cfg['PYTHON_CONFIG'][0], shell=True))
49 res = res[res.find('-lpython')+8:]
50 res = res[:3]
52 sys.stderr.write("%s\n" % res)
53 return res
55 def python_module_installed(cfg, module):
56 sys.stderr.write("Checking for python module %s ... " % module)
58 ret = os.system("echo 'import %s' | %s > /dev/null 2>&1" %
59 (module, cfg['PYTHON_BIN'][0]))
61 if ret:
62 sys.stderr.write('No\n')
63 return False
64 else:
65 sys.stderr.write('Yes\n')
66 return True
68 def check_for_swig(cfg):
69 sys.stderr.write("Checking for working swig ... ")
71 ret = os.system("%s -version > /dev/null 2>&1" % cfg['SWIG'][0])
73 if ret:
74 sys.stderr.write('No\n')
75 cfg['SWIG'][0] = ''
76 else:
77 sys.stderr.write('Yes\n')
79 def check_for_python_config(cfg):
80 sys.stderr.write("Checking for python-config ... ")
82 ret = os.system("%s --libs > /dev/null 2>&1" % cfg['PYTHON_CONFIG'][0])
84 if ret:
85 sys.stderr.write('No\n')
86 cfg['PYTHON_CONFIG'][0] = ''
87 else:
88 sys.stderr.write('Yes\n')
91 # Library checking api
93 class libraries:
94 def __init__(self, libraries, cfg):
95 self.libraries = libraries
96 self.cfg = cfg;
97 # Create dictionary for check results
98 self.results = dict()
100 # Print summary
102 def print_summary(self):
103 sys.stderr.write("Settings and variables\n")
104 sys.stderr.write("----------------------\n")
106 for i in cfg:
107 sys.stderr.write("%14s : %s\n" % (i, cfg[i][0]))
108 sys.stderr.write(" - %s\n\n" % cfg[i][1])
110 sys.stderr.write("Libraries to link against\n")
111 sys.stderr.write("-------------------------\n")
113 for i in self.libraries:
114 sys.stderr.write("%10s" % i[0])
116 if (self.results[i[0]]):
117 sys.stderr.write(" : Enabled\n")
118 else:
119 sys.stderr.write(" : Disabled\n")
121 sys.stderr.write(" - %s\n\n" % i[1])
123 # Enable/Disable library
125 def set(self, name, val):
126 if name not in map(lambda s: s[0], self.libraries):
127 sys.stderr.write("ERROR: Invalid library '%s'\n" % name)
128 exit(1)
129 else:
130 self.results[name] = val
132 # Calls a function on arguments, all is stored in array if
133 # not set previously
134 # (I know this smells like a lisp, but I can't help myself)
136 def check(self):
137 sys.stderr.write("Checking for libraries\n")
138 sys.stderr.write("----------------------\n")
139 for i in self.libraries:
140 if i[0] not in self.results:
141 self.results[i[0]] = i[2][0](self.cfg, *i[2][1:])
142 sys.stderr.write("\n")
144 # Writes '#define HAVE_XXX_H' into passed file
146 def write_config_h(self, f):
147 for i in self.libraries:
148 f.write("/*\n * %s\n */\n" % i[1])
149 if self.results[i[0]]:
150 f.write("#define HAVE_%s\n" % i[0].upper())
151 else:
152 f.write("//#define HAVE_%s\n" % i[0].upper())
153 f.write("\n")
156 # Writes LDLIBS and CFLAGS into passed file
158 def write_config_mk(self, f):
159 for i in self.libraries:
160 f.write("# %s - %s\n" % (i[0], i[1]))
161 if self.results[i[0]]:
162 f.write("HAVE_%s=yes\n" % i[0].upper())
163 f.write("CFLAGS+=%s\nLDLIBS+=%s\n" % (i[3], i[4]))
164 else:
165 f.write("HAVE_%s=no\n" % i[0].upper())
168 # Return list of linker flags needed to build particular module
169 # (module may be core, loaders, backends, etc...
171 def get_linker_flags(self, module):
172 res = ''
173 for i in self.libraries:
174 if module in i[5] and self.results[i[0]]:
175 res += ' ' + i[4]
176 return res
178 # Returns list of cflags needed to build module
180 def get_cflags(self, module):
181 res = ''
182 for i in self.libraries:
183 if module in i[5] and self.results[i[0]]:
184 res += ' ' + i[3]
185 return res
187 def die_screaming(msg):
188 sys.stderr.write("\n************************************\n")
189 sys.stderr.write("ERROR: ")
190 sys.stderr.write(msg)
191 sys.stderr.write("\n************************************\n")
192 exit(1)
195 # Check for basic compiling tools
197 def basic_checks(cfg):
198 sys.stderr.write("Basic checks\n")
199 sys.stderr.write("------------\n")
201 if not c_compiler_exists(cfg):
202 die_screaming("No C compiler found")
204 if not python_module_installed(cfg, 'jinja2'):
205 die_screaming("No jinja2 python module found")
207 check_for_swig(cfg)
208 check_for_python_config(cfg)
210 cfg['PYTHON_VER'][0] = python_version(cfg)
212 sys.stderr.write("\n")
215 # Write configuration files
217 def write_config_h(cfg, libs):
218 f = open("config.h", "w")
219 f.write("/*\n * This file is genereated by configure script\n */\n");
220 f.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
221 libs.write_config_h(f);
222 f.write("#endif /* CONFIG_H */\n");
223 sys.stderr.write("Config 'config.h' written\n")
224 f.close()
226 def write_config_mk(cfg, libs):
227 f = open('config.gen.mk', 'w')
228 for i in cfg:
229 f.write("# %s\n%s=%s\n" % (cfg[i][1], i, cfg[i][0]))
230 libs.write_config_mk(f);
231 f.close()
232 sys.stderr.write("Config 'config.gen.mk' written\n")
235 # Generate app compilation helper
237 def write_gfxprim_config(cfg, libs):
238 modules = ['loaders', 'backends', 'grabbers']
240 f = open('gfxprim-config', 'w')
241 f.write('#!/bin/sh\n'
242 '#\n# Generated by configure, do not edit directly\n#\n\n'
243 'USAGE="Usage: $0 --list-modules --cflags --libs --libs-module_foo"\n'
244 '\nif test $# -eq 0; then\n'
245 '\techo "$USAGE"\n'
246 '\texit 1\n'
247 'fi\n\n'
248 'while test -n "$1"; do\n'
249 '\tcase "$1" in\n')
251 # General switches cflags and ldflags
252 f.write('\t--help) echo "$USAGE"; exit 0;;\n')
253 f.write('\t--list-modules) echo "%s"; exit 0;;\n' % ' '.join(modules))
254 f.write('\t--cflags) echo -n "-I/usr/include/GP/%s";;\n' %
255 libs.get_cflags('core'))
256 f.write('\t--libs) echo -n "-lGP %s ";;\n' % libs.get_linker_flags('core'))
258 # ldflags for specific modules
259 for i in modules:
260 ldflags = ''
261 if i == 'backends':
262 ldflags += '-lGP_backends '
263 if i == 'grabbers':
264 ldflags += '-lGP_grabbers '
265 ldflags += libs.get_linker_flags(i)
266 f.write('\t--libs-%s) echo -n "%s ";;\n' % (i, ldflags))
268 f.write('\t*) echo "Invalid option \'$1\'"; echo $USAGE; exit 1;;\n')
270 f.write('\tesac\n\tshift\ndone\necho\n')
271 f.close()
272 os.system('chmod +x gfxprim-config')
274 if __name__ == '__main__':
276 # Dictionary for default configuration parameters
278 cfg = {'CC' : ['gcc', 'Path/name of the C compiler'],
279 'CFLAGS' : ['-pthread -W -Wall -Wextra -fPIC -O2 -ggdb -D_FORTIFY_SOURCE=2', 'C compiler flags'],
280 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
281 'SWIG' : ['swig', 'Simplified Wrapper and Interface Generator'],
282 'PYTHON_CONFIG' : ['python-config', 'Python config helper'],
283 'PYTHON_VER' : ['', 'Python version (derived from python config)'],
284 'include_path' : ['/usr/include', 'Path to the system headers'],
285 'prefix' : ['/usr', 'Installation prefix']}
288 # Library detection/enable disable
290 # name, description, [detection], cflags, ldflags, list of modules library is needed for
292 l = libraries([["libpng",
293 "Portable Network Graphics Library",
294 [header_exists, "png.h"], "", "-lpng", ["loaders"]],
295 ["libsdl",
296 "Simple Direct Media Layer",
297 [header_exists, "SDL/SDL.h"], "", "`sdl-config --libs`", ["backends"]],
298 ["jpeg",
299 "Library to load, handle and manipulate images in the JPEG format",
300 [header_exists, "jpeglib.h"], "", "-ljpeg", ["loaders"]],
301 ["giflib",
302 "Library to handle, display and manipulate GIF images",
303 [header_exists, "gif_lib.h"], "", "-lgif", ["loaders"]],
304 ["tiff",
305 "Tag Image File Format (TIFF) library",
306 [header_exists, "tiffio.h"], "", "-ltiff", ["loaders"]],
307 ["libX11",
308 "X11 library",
309 [header_exists, "X11/Xlib.h"], "", "-lX11", ["backends"]],
310 ["X_SHM",
311 "MIT-SHM X Extension",
312 [header_exists, "X11/extensions/XShm.h"], "", "-lXext", ["backends"]],
313 ["freetype",
314 "A high-quality and portable font engine",
315 [header_exists, "ft2build.h"], "", "`freetype-config --libs`", ["core"]],
316 ["dl",
317 "Dynamic linker",
318 [header_exists, "dlfcn.h"], "", "-ldl", ["core"]],
319 ["V4L2",
320 "Video for linux 2",
321 [header_exists, "linux/videodev2.h"], "", "", ["grabbers"]],
322 ["pthread",
323 "Posix Threads",
324 [header_exists, "pthread.h"], "-pthread", "-pthread", ["core"]],
325 ["backtrace",
326 "C stack trace writeout",
327 [c_try_compile, "#include <execinfo.h>\nint main(void) { backtrace(0, 0); }",
328 "Checking for backtrace() ... "], "", "", ["core"]]], cfg)
330 parser = OptionParser();
332 # Get configuration parameters from environment variables
333 for i in cfg:
334 if i in os.environ:
335 cfg[i][0] = os.environ[i]
337 # Enable disable libraries for linking
338 parser.add_option("-e", "--enable", dest="enable", action="append",
339 help="force enable library linking", metavar="libfoo")
340 parser.add_option("-d", "--disable", dest="disable", action="append",
341 help="disable library linking", metavar="libfoo")
343 # Add cfg config options
344 for i in cfg:
345 parser.add_option("", "--"+i, dest=i, metavar=cfg[i][0], help=cfg[i][1])
347 (options, args) = parser.parse_args();
350 # Enable/Disable libraries as user requested
351 # These are not checked later
353 if options.enable:
354 for i in options.enable:
355 l.set(i, True);
356 if options.disable:
357 for i in options.disable:
358 l.set(i, False);
360 for i in cfg:
361 if getattr(options, i):
362 cfg[i][0] = getattr(options, i)
364 basic_checks(cfg);
366 l.check()
367 l.print_summary()
369 write_config_h(cfg, l)
370 write_config_mk(cfg, l)
371 write_gfxprim_config(cfg, l)