build: Update list of exported symbols.
[gfxprim/pasky.git] / configure
blob107db2db2a968b3f033e6d486b9c9dcee8172327
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 import subprocess
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 cmd = subprocess.Popen([cfg['PYTHON_CONFIG'][0], '--ldflags'],
49 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
50 res = str(cmd.communicate())
51 res = res[res.find('-lpython')+8:]
52 res = res[:3]
54 sys.stderr.write("%s\n" % res)
55 return res
57 def python_module_installed(cfg, module):
58 sys.stderr.write("Checking for python module %s ... " % module)
60 ret = os.system("echo 'import %s' | %s > /dev/null 2>&1" %
61 (module, cfg['PYTHON_BIN'][0]))
63 if ret:
64 sys.stderr.write('No\n')
65 return False
66 else:
67 sys.stderr.write('Yes\n')
68 return True
70 def check_for_swig(cfg):
71 sys.stderr.write("Checking for working swig ... ")
73 ret = os.system("%s -version > /dev/null 2>&1" % cfg['SWIG'][0])
75 if ret:
76 sys.stderr.write('No\n')
77 cfg['SWIG'][0] = ''
78 else:
79 sys.stderr.write('Yes\n')
81 def check_for_python_config(cfg):
82 sys.stderr.write("Checking for python-config ... ")
84 ret = os.system("%s --libs > /dev/null 2>&1" % cfg['PYTHON_CONFIG'][0])
86 if ret:
87 sys.stderr.write('No\n')
88 cfg['PYTHON_CONFIG'][0] = ''
89 else:
90 sys.stderr.write('Yes\n')
93 # Library checking api
95 class libraries:
96 def __init__(self, libraries, cfg):
97 self.libraries = libraries
98 self.cfg = cfg;
99 # Create dictionary for check results
100 self.results = dict()
102 # Print summary
104 def print_summary(self):
105 sys.stderr.write("Settings and variables\n")
106 sys.stderr.write("----------------------\n")
108 for i in cfg:
109 sys.stderr.write("%14s : %s\n" % (i, cfg[i][0]))
110 sys.stderr.write(" - %s\n\n" % cfg[i][1])
112 sys.stderr.write("Libraries to link against\n")
113 sys.stderr.write("-------------------------\n")
115 for i in self.libraries:
116 sys.stderr.write("%10s" % i[0])
118 if (self.results[i[0]]):
119 sys.stderr.write(" : Enabled\n")
120 else:
121 sys.stderr.write(" : Disabled\n")
123 sys.stderr.write(" - %s\n\n" % i[1])
125 # Enable/Disable library
127 def set(self, name, val):
128 if name not in map(lambda s: s[0], self.libraries):
129 sys.stderr.write("ERROR: Invalid library '%s'\n" % name)
130 exit(1)
131 else:
132 self.results[name] = val
134 # Calls a function on arguments, all is stored in array if
135 # not set previously
136 # (I know this smells like a lisp, but I can't help myself)
138 def check(self):
139 sys.stderr.write("Checking for libraries\n")
140 sys.stderr.write("----------------------\n")
141 for i in self.libraries:
142 if i[0] not in self.results:
143 self.results[i[0]] = i[2][0](self.cfg, *i[2][1:])
144 sys.stderr.write("\n")
146 # Writes '#define HAVE_XXX_H' into passed file
148 def write_config_h(self, f):
149 for i in self.libraries:
150 f.write("/*\n * %s\n */\n" % i[1])
151 if self.results[i[0]]:
152 f.write("#define HAVE_%s\n" % i[0].upper())
153 else:
154 f.write("//#define HAVE_%s\n" % i[0].upper())
155 f.write("\n")
158 # Writes LDLIBS and CFLAGS into passed file
160 def write_config_mk(self, f):
161 for i in self.libraries:
162 f.write("# %s - %s\n" % (i[0], i[1]))
163 if self.results[i[0]]:
164 f.write("HAVE_%s=yes\n" % i[0].upper())
165 # f.write("CFLAGS+=%s\nLDLIBS+=%s\n" % (i[3], i[4]))
166 else:
167 f.write("HAVE_%s=no\n" % i[0].upper())
170 # Return list of linker flags needed to build particular module
171 # (module may be core, loaders, backends, etc...
173 def get_linker_flags(self, module):
174 res = ''
175 for i in self.libraries:
176 if module in i[5] and self.results[i[0]]:
177 res += ' ' + i[4]
178 return res
180 # Returns list of cflags needed to build module
182 def get_cflags(self, module):
183 res = ''
184 for i in self.libraries:
185 if module in i[5] and self.results[i[0]]:
186 res += ' ' + i[3]
187 return res
189 def die_screaming(msg):
190 sys.stderr.write("\n************************************\n")
191 sys.stderr.write("ERROR: ")
192 sys.stderr.write(msg)
193 sys.stderr.write("\n************************************\n")
194 exit(1)
197 # Check for basic compiling tools
199 def basic_checks(cfg):
200 sys.stderr.write("Basic checks\n")
201 sys.stderr.write("------------\n")
203 if not c_compiler_exists(cfg):
204 die_screaming("No C compiler found")
206 if not python_module_installed(cfg, 'jinja2'):
207 die_screaming("No jinja2 python module found")
209 check_for_swig(cfg)
210 check_for_python_config(cfg)
212 cfg['PYTHON_VER'][0] = python_version(cfg)
214 if cfg['libdir'][0] == '':
215 sys.stderr.write("Checking for lib directory ... ")
217 if os.path.isdir(cfg['prefix'][0] + '/usr/lib64'):
218 cfg['libdir'][0] = '/usr/lib64'
219 else:
220 cfg['libdir'][0] = '/usr/lib'
222 sys.stderr.write(cfg['libdir'][0] + '\n');
224 sys.stderr.write('\n')
227 # Write configuration files
229 def write_config_h(cfg, libs):
230 f = open("config.h", "w")
231 f.write("/*\n * This file is genereated by configure script\n */\n");
232 f.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
233 libs.write_config_h(f);
234 f.write("#endif /* CONFIG_H */\n");
235 sys.stderr.write("Config 'config.h' written\n")
236 f.close()
238 def write_config_mk(cfg, libs):
239 f = open('config.gen.mk', 'w')
240 for i in cfg:
241 f.write("# %s\n%s=%s\n" % (cfg[i][1], i, cfg[i][0]))
242 libs.write_config_mk(f);
243 f.close()
244 sys.stderr.write("Config 'config.gen.mk' written\n")
247 # Generate app compilation helper
249 def write_gfxprim_config(cfg, libs):
250 modules = ['loaders', 'backends', 'grabbers']
252 f = open('gfxprim-config', 'w')
253 f.write('#!/bin/sh\n'
254 '#\n# Generated by configure, do not edit directly\n#\n\n'
255 'USAGE="Usage: $0 --list-modules --cflags --libs --libs-module_foo"\n'
256 '\nif test $# -eq 0; then\n'
257 '\techo "$USAGE"\n'
258 '\texit 1\n'
259 'fi\n\n'
260 'while test -n "$1"; do\n'
261 '\tcase "$1" in\n')
263 # General switches cflags and ldflags
264 f.write('\t--help) echo "$USAGE"; exit 0;;\n')
265 f.write('\t--list-modules) echo "%s"; exit 0;;\n' % ' '.join(modules))
266 f.write('\t--cflags) echo -n "-I/usr/include/GP/%s";;\n' %
267 libs.get_cflags('core'))
268 f.write('\t--libs) echo -n "-lGP -lrt -lm %s ";;\n' % libs.get_linker_flags('core'))
270 # ldflags for specific modules
271 for i in modules:
272 ldflags = ''
273 if i == 'backends':
274 ldflags += '-lGP_backends '
275 if i == 'grabbers':
276 ldflags += '-lGP_grabbers '
277 if i == 'loaders':
278 ldflags += '-lGP_loaders '
279 ldflags += libs.get_linker_flags(i)
280 f.write('\t--libs-%s) echo -n "%s ";;\n' % (i, ldflags))
282 f.write('\t*) echo "Invalid option \'$1\'"; echo $USAGE; exit 1;;\n')
284 f.write('\tesac\n\tshift\ndone\necho\n')
285 f.close()
286 os.system('chmod +x gfxprim-config')
288 if __name__ == '__main__':
290 # Dictionary for default configuration parameters
292 cfg = {'CC' : ['gcc', 'Path/name of the C compiler'],
293 'CFLAGS' : ['-pthread -W -Wall -Wextra -fPIC -O2 -ggdb -D_FORTIFY_SOURCE=2', 'C compiler flags'],
294 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
295 'SWIG' : ['swig', 'Simplified Wrapper and Interface Generator'],
296 'PYTHON_CONFIG' : ['python-config', 'Python config helper'],
297 'PYTHON_VER' : ['', 'Python version (derived from python config)'],
298 'include_path' : ['/usr/include', 'Path to the system headers'],
299 'prefix' : ['', 'Installation prefix'],
300 'bindir' : ['/usr/bin', 'Where to install binaries'],
301 'libdir' : ['', 'Where to install libraries'],
302 'includedir' : ['/usr/include', 'Where to install headers']}
305 # Library detection/enable disable
307 # name, description, [detection], cflags, ldflags, list of modules library is needed for
309 l = libraries([["libpng",
310 "Portable Network Graphics Library",
311 [header_exists, "png.h"], "", "-lpng", ["loaders"]],
312 ["libsdl",
313 "Simple Direct Media Layer",
314 [header_exists, "SDL/SDL.h"], "", "`sdl-config --libs`", ["backends"]],
315 ["jpeg",
316 "Library to load, handle and manipulate images in the JPEG format",
317 [header_exists, "jpeglib.h"], "", "-ljpeg", ["loaders"]],
318 ["giflib",
319 "Library to handle, display and manipulate GIF images",
320 [header_exists, "gif_lib.h"], "", "-lgif", ["loaders"]],
321 ["tiff",
322 "Tag Image File Format (TIFF) library",
323 [header_exists, "tiffio.h"], "", "-ltiff", ["loaders"]],
324 ["libX11",
325 "X11 library",
326 [header_exists, "X11/Xlib.h"], "", "-lX11", ["backends"]],
327 ["X_SHM",
328 "MIT-SHM X Extension",
329 [header_exists, "X11/extensions/XShm.h"], "", "-lXext", ["backends"]],
330 ["freetype",
331 "A high-quality and portable font engine",
332 [header_exists, "ft2build.h"], "", "`freetype-config --libs`", ["core"]],
333 ["dl",
334 "Dynamic linker",
335 [header_exists, "dlfcn.h"], "", "-ldl", ["core"]],
336 ["V4L2",
337 "Video for linux 2",
338 [header_exists, "linux/videodev2.h"], "", "", ["grabbers"]],
339 ["pthread",
340 "Posix Threads",
341 [header_exists, "pthread.h"], "-pthread", "-pthread", ["core"]],
342 ["backtrace",
343 "C stack trace writeout",
344 [c_try_compile, "#include <execinfo.h>\nint main(void) { backtrace(0, 0); }",
345 "Checking for backtrace() ... "], "", "", ["core"]]], cfg)
347 parser = OptionParser();
349 # Get configuration parameters from environment variables
350 for i in cfg:
351 if i in os.environ:
352 cfg[i][0] = os.environ[i]
354 # Enable disable libraries for linking
355 parser.add_option("-e", "--enable", dest="enable", action="append",
356 help="force enable library linking", metavar="libfoo")
357 parser.add_option("-d", "--disable", dest="disable", action="append",
358 help="disable library linking", metavar="libfoo")
360 # Add cfg config options
361 for i in cfg:
362 parser.add_option("", "--"+i, dest=i, metavar=cfg[i][0], help=cfg[i][1])
364 (options, args) = parser.parse_args();
367 # Enable/Disable libraries as user requested
368 # These are not checked later
370 if options.enable:
371 for i in options.enable:
372 l.set(i, True);
373 if options.disable:
374 for i in options.disable:
375 l.set(i, False);
377 for i in cfg:
378 if getattr(options, i):
379 cfg[i][0] = getattr(options, i)
381 basic_checks(cfg);
383 l.check()
384 l.print_summary()
386 write_config_h(cfg, l)
387 write_config_mk(cfg, l)
388 write_gfxprim_config(cfg, l)