build: The backends module needs to add -lGP_backends.
[gfxprim.git] / configure
blob5d2540953b8f1b14631966ec83fba97a900ce8ec
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 basic_checks(cfg):
146 sys.stderr.write("Basic checks\n")
147 sys.stderr.write("------------\n")
148 if not c_compiler_exists(cfg):
149 exit(1)
150 if not python_module_installed(cfg, 'jinja2'):
151 exit(1)
152 check_for_swig(cfg)
153 sys.stderr.write("\n")
156 # Write configuration files
158 def write_config_h(cfg, libs):
159 f = open("config.h", "w")
160 f.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
161 libs.write_config_h(f);
162 f.write("#endif /* CONFIG_H */\n");
163 sys.stderr.write("Config 'config.h' written\n")
164 f.close()
166 def write_config_mk(cfg, libs):
167 f = open('config.gen.mk', 'w')
168 for i in cfg:
169 f.write("# %s\n%s=%s\n" % (cfg[i][1], i, cfg[i][0]))
170 libs.write_config_mk(f);
171 f.close()
172 sys.stderr.write("Config 'config.gen.mk' written\n")
175 # Generate app compilation helper
177 def write_gfxprim_config(cfg, libs):
178 modules = ['loaders', 'backends']
180 f = open('gfxprim-config', 'w')
181 f.write('#!/bin/sh\n'
182 '#\n# Generated by configure, do not edit directly\n#\n\n'
183 'USAGE="Usage: $0 --list-modules --cflags --libs --libs-module_foo"\n'
184 '\nif test $# -eq 0; then\n'
185 '\techo "$USAGE"\n'
186 '\texit 1\n'
187 'fi\n\n'
188 'while test -n "$1"; do\n'
189 '\tcase "$1" in\n')
191 # General switches cflags and ldflags
192 f.write('\t--help) echo "$USAGE"; exit 0;;\n')
193 f.write('\t--list-modules) echo "%s"; exit 0;;\n' % ' '.join(modules))
194 f.write('\t--cflags) echo -n "-I/usr/include/GP/ ";;\n')
195 f.write('\t--libs) echo -n "-lGP ";;\n')
198 # ldflags for specific modules
199 for i in modules:
200 ldflags = ''
201 if i == 'backends':
202 ldflags += '-lGP_backends '
203 ldflags += libs.get_linker_flags(i)
204 f.write('\t--libs-%s) echo -n "%s ";;\n' % (i, ldflags))
207 f.write('\tesac\n\tshift\ndone\necho\n')
208 f.close()
209 os.system('chmod +x gfxprim-config')
211 if __name__ == '__main__':
213 # Dictionary for default configuration parameters
215 cfg = {'CC' : ['gcc', 'Path/name of the C compiler'],
216 'CFLAGS' : ['-W -Wall -Wextra -fPIC -O2 -ggdb', 'C compiler flags'],
217 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
218 'SWIG' : ['swig', 'Simplified Wrapper and Interface Generator'],
219 'include_path': ['/usr/include', 'Path to the system headers']}
222 # Library detection/enable disable
224 # name, description, [detection], cflags, ldflags, dict of modules library is needed for
226 l = libraries([["libpng",
227 "Portable Network Graphics Library",
228 [header_exists, "png.h"], "", "-lpng", {"loaders"}],
229 ["libsdl",
230 "Simple Direct Media Layer",
231 [header_exists, "SDL/SDL.h"], "", "`sdl-config --libs`", {"backends"}],
232 ["jpeg",
233 "Library to load, handle and manipulate images in the JPEG format",
234 [header_exists, "jpeglib.h"], "", "-ljpeg", {"loaders"}],
235 ["giflib",
236 "Library to handle, display and manipulate GIF images",
237 [header_exists, "gif_lib.h"], "", "-lgif", {"loaders"}],
238 ["libX11",
239 "X11 library",
240 [header_exists, "X11/Xlib.h"], "", "-lX11", {"backends"}],
241 ["freetype",
242 "A high-quality and portable font engine",
243 [header_exists, "ft2build.h"], "", "`freetype-config --libs`", {"text"}]], cfg)
245 parser = OptionParser();
247 # Enable disable libraries for linking
248 parser.add_option("-e", "--enable", dest="enable", action="append",
249 help="force enable library linking", metavar="libfoo")
250 parser.add_option("-d", "--disable", dest="disable", action="append",
251 help="disable library linking", metavar="libfoo")
253 # Add cfg config options
254 for i in cfg:
255 parser.add_option("", "--"+i, dest=i, metavar=cfg[i][0], help=cfg[i][1])
257 (options, args) = parser.parse_args();
260 # Enable/Disable libraries as user requested
261 # These are not checked later
263 if options.enable:
264 for i in options.enable:
265 l.set(i, True);
266 if options.disable:
267 for i in options.disable:
268 l.set(i, False);
270 for i in cfg:
271 if getattr(options, i):
272 cfg[i][0] = getattr(options, i)
274 basic_checks(cfg);
276 l.check()
277 l.print_summary()
279 write_config_h(cfg, l)
280 write_config_mk(cfg, l)
281 write_gfxprim_config(cfg, l)