loaders: Make use of edhanced debug messages.
[gfxprim.git] / configure
blobbd08e4b078b7543757560ec7ef767f5526948e58
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")
65 # Library checking api
67 class libraries:
68 def __init__(self, libraries, cfg):
69 self.libraries = libraries
70 self.cfg = cfg;
71 # Create dictionary for check results
72 self.results = dict()
74 # Print summary
76 def print_summary(self):
77 sys.stderr.write("Libraries to link against\n")
78 sys.stderr.write("-------------------------\n")
80 for i in self.libraries:
81 sys.stderr.write("%10s" % i[0])
83 if (self.results[i[0]]):
84 sys.stderr.write(" : Enabled\n")
85 else:
86 sys.stderr.write(" : Disabled\n")
88 sys.stderr.write(" - %s\n\n" % i[1])
90 # Enable/Disable library
92 def set(self, name, val):
93 if name not in map(lambda s: s[0], self.libraries):
94 sys.stderr.write("ERROR: Invalid library '%s'\n" % name)
95 exit(1)
96 else:
97 self.results[name] = val
99 # Calls a function on arguments, all is stored in array if
100 # not set previously
101 # (I know this smells like a lisp, but I can't help myself)
103 def check(self):
104 sys.stderr.write("Checking for libraries\n")
105 sys.stderr.write("----------------------\n")
106 for i in self.libraries:
107 if i[0] not in self.results:
108 self.results[i[0]] = i[2][0](self.cfg, *i[2][1:])
109 sys.stderr.write("\n")
111 # Writes '#define HAVE_XXX_H' into passed file
113 def write_config_h(self, f):
114 for i in self.libraries:
115 f.write("/*\n * %s\n */\n" % i[1])
116 if self.results[i[0]]:
117 f.write("#define HAVE_%s\n" % i[0].upper())
118 else:
119 f.write("//#define HAVE_%s\n" % i[0].upper())
120 f.write("\n")
123 # Writes LDFLAGS and CFLAGS into passed file
125 def write_config_mk(self, f):
126 for i in self.libraries:
127 f.write("# %s - %s\n" % (i[0], i[1]))
128 if self.results[i[0]]:
129 f.write("HAVE_%s=yes\n" % i[0].upper())
130 f.write("CFLAGS+=%s\nLDFLAGS+=%s\n" % (i[3], i[4]))
131 else:
132 f.write("HAVE_%s=no\n" % i[0].upper())
135 # Return list of linker flags needed to build particular module
136 # (module may be core, loaders, backends, etc...
138 def get_linker_flags(self, module):
139 res = ""
140 for i in self.libraries:
141 if module in i[5] and self.results[i[0]]:
142 res += " " + i[4]
143 return res
145 def die_screaming(msg):
146 sys.stderr.write("\n************************************\n")
147 sys.stderr.write("ERROR: ")
148 sys.stderr.write(msg)
149 sys.stderr.write("\n************************************\n")
150 exit(1)
153 # Check for basic compiling tools
155 def basic_checks(cfg):
156 sys.stderr.write("Basic checks\n")
157 sys.stderr.write("------------\n")
159 if not c_compiler_exists(cfg):
160 die_screaming("No C compiler found")
162 if not python_module_installed(cfg, 'jinja2'):
163 die_screaming("No jinja2 python module found")
165 check_for_swig(cfg)
166 sys.stderr.write("\n")
169 # Write configuration files
171 def write_config_h(cfg, libs):
172 f = open("config.h", "w")
173 f.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
174 libs.write_config_h(f);
175 f.write("#endif /* CONFIG_H */\n");
176 sys.stderr.write("Config 'config.h' written\n")
177 f.close()
179 def write_config_mk(cfg, libs):
180 f = open('config.gen.mk', 'w')
181 for i in cfg:
182 f.write("# %s\n%s=%s\n" % (cfg[i][1], i, cfg[i][0]))
183 libs.write_config_mk(f);
184 f.close()
185 sys.stderr.write("Config 'config.gen.mk' written\n")
188 # Generate app compilation helper
190 def write_gfxprim_config(cfg, libs):
191 modules = ['loaders', 'backends']
193 f = open('gfxprim-config', 'w')
194 f.write('#!/bin/sh\n'
195 '#\n# Generated by configure, do not edit directly\n#\n\n'
196 'USAGE="Usage: $0 --list-modules --cflags --libs --libs-module_foo"\n'
197 '\nif test $# -eq 0; then\n'
198 '\techo "$USAGE"\n'
199 '\texit 1\n'
200 'fi\n\n'
201 'while test -n "$1"; do\n'
202 '\tcase "$1" in\n')
204 # General switches cflags and ldflags
205 f.write('\t--help) echo "$USAGE"; exit 0;;\n')
206 f.write('\t--list-modules) echo "%s"; exit 0;;\n' % ' '.join(modules))
207 f.write('\t--cflags) echo -n "-I/usr/include/GP/ ";;\n')
208 f.write('\t--libs) echo -n "-lGP %s ";;\n' % libs.get_linker_flags('core'))
210 # ldflags for specific modules
211 for i in modules:
212 ldflags = ''
213 if i == 'backends':
214 ldflags += '-lGP_backends '
215 ldflags += libs.get_linker_flags(i)
216 f.write('\t--libs-%s) echo -n "%s ";;\n' % (i, ldflags))
218 f.write('\t*) echo "Invalid option \'$1\'"; echo $USAGE; exit 1;;\n')
220 f.write('\tesac\n\tshift\ndone\necho\n')
221 f.close()
222 os.system('chmod +x gfxprim-config')
224 if __name__ == '__main__':
226 # Dictionary for default configuration parameters
228 cfg = {'CC' : ['gcc', 'Path/name of the C compiler'],
229 'CFLAGS' : ['-W -Wall -Wextra -fPIC -O2 -ggdb', 'C compiler flags'],
230 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
231 'SWIG' : ['swig', 'Simplified Wrapper and Interface Generator'],
232 'include_path': ['/usr/include', 'Path to the system headers']}
235 # Library detection/enable disable
237 # name, description, [detection], cflags, ldflags, list of modules library is needed for
239 l = libraries([["libpng",
240 "Portable Network Graphics Library",
241 [header_exists, "png.h"], "", "-lpng", ["loaders"]],
242 ["libsdl",
243 "Simple Direct Media Layer",
244 [header_exists, "SDL/SDL.h"], "", "`sdl-config --libs`", ["backends"]],
245 ["jpeg",
246 "Library to load, handle and manipulate images in the JPEG format",
247 [header_exists, "jpeglib.h"], "", "-ljpeg", ["loaders"]],
248 ["giflib",
249 "Library to handle, display and manipulate GIF images",
250 [header_exists, "gif_lib.h"], "", "-lgif", ["loaders"]],
251 ["libX11",
252 "X11 library",
253 [header_exists, "X11/Xlib.h"], "", "-lX11", ["backends"]],
254 ["freetype",
255 "A high-quality and portable font engine",
256 [header_exists, "ft2build.h"], "", "`freetype-config --libs`", ["core"]]], cfg)
258 parser = OptionParser();
260 # Enable disable libraries for linking
261 parser.add_option("-e", "--enable", dest="enable", action="append",
262 help="force enable library linking", metavar="libfoo")
263 parser.add_option("-d", "--disable", dest="disable", action="append",
264 help="disable library linking", metavar="libfoo")
266 # Add cfg config options
267 for i in cfg:
268 parser.add_option("", "--"+i, dest=i, metavar=cfg[i][0], help=cfg[i][1])
270 (options, args) = parser.parse_args();
273 # Enable/Disable libraries as user requested
274 # These are not checked later
276 if options.enable:
277 for i in options.enable:
278 l.set(i, True);
279 if options.disable:
280 for i in options.disable:
281 l.set(i, False);
283 for i in cfg:
284 if getattr(options, i):
285 cfg[i][0] = getattr(options, i)
287 basic_checks(cfg);
289 l.check()
290 l.print_summary()
292 write_config_h(cfg, l)
293 write_config_mk(cfg, l)
294 write_gfxprim_config(cfg, l)