3 # This is simple script to detect libraries and configure standard features.
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
)
17 sys
.stderr
.write("Yes\n")
20 sys
.stderr
.write("No\n")
23 def c_try_compile(cfg
, code
, msg
):
26 ret
= os
.system("echo '%s' | %s -x c -o /dev/null - > /dev/null 2>&1" %
30 sys
.stderr
.write("No\n")
33 sys
.stderr
.write("Yes\n")
36 def c_compiler_exists(cfg
):
37 return c_try_compile(cfg
, "int main(void) { return 0; }",
38 "Checking for working compiler (%s) ... " %
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')
48 res
= str(check_output("%s --ldflags" % cfg
['PYTHON_CONFIG'][0], shell
=True))
49 res
= res
[res
.find('-lpython')+8:]
52 sys
.stderr
.write("%s\n" % 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]))
62 sys
.stderr
.write('No\n')
65 sys
.stderr
.write('Yes\n')
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])
74 sys
.stderr
.write('No\n')
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])
85 sys
.stderr
.write('No\n')
86 cfg
['PYTHON_CONFIG'][0] = ''
88 sys
.stderr
.write('Yes\n')
91 # Library checking api
94 def __init__(self
, libraries
, cfg
):
95 self
.libraries
= libraries
97 # Create dictionary for check results
102 def print_summary(self
):
103 sys
.stderr
.write("Settings and variables\n")
104 sys
.stderr
.write("----------------------\n")
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")
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
)
130 self
.results
[name
] = val
132 # Calls a function on arguments, all is stored in array if
134 # (I know this smells like a lisp, but I can't help myself)
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())
152 f
.write("//#define HAVE_%s\n" % i
[0].upper())
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]))
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
):
173 for i
in self
.libraries
:
174 if module
in i
[5] and self
.results
[i
[0]]:
178 # Returns list of cflags needed to build module
180 def get_cflags(self
, module
):
182 for i
in self
.libraries
:
183 if module
in i
[5] and self
.results
[i
[0]]:
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")
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")
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")
226 def write_config_mk(cfg
, libs
):
227 f
= open('config.gen.mk', 'w')
229 f
.write("# %s\n%s=%s\n" % (cfg
[i
][1], i
, cfg
[i
][0]))
230 libs
.write_config_mk(f
);
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'
248 'while test -n "$1"; do\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
262 ldflags
+= '-lGP_backends '
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')
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"]],
296 "Simple Direct Media Layer",
297 [header_exists
, "SDL/SDL.h"], "", "`sdl-config --libs`", ["backends"]],
299 "Library to load, handle and manipulate images in the JPEG format",
300 [header_exists
, "jpeglib.h"], "", "-ljpeg", ["loaders"]],
302 "Library to handle, display and manipulate GIF images",
303 [header_exists
, "gif_lib.h"], "", "-lgif", ["loaders"]],
305 "Tag Image File Format (TIFF) library",
306 [header_exists
, "tiffio.h"], "", "-ltiff", ["loaders"]],
309 [header_exists
, "X11/Xlib.h"], "", "-lX11", ["backends"]],
311 "MIT-SHM X Extension",
312 [header_exists
, "X11/extensions/XShm.h"], "", "-lXext", ["backends"]],
314 "A high-quality and portable font engine",
315 [header_exists
, "ft2build.h"], "", "`freetype-config --libs`", ["core"]],
318 [header_exists
, "dlfcn.h"], "", "-ldl", ["core"]],
321 [header_exists
, "linux/videodev2.h"], "", "", ["grabbers"]],
324 [header_exists
, "pthread.h"], "-pthread", "-pthread", ["core"]],
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
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
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
354 for i
in options
.enable
:
357 for i
in options
.disable
:
361 if getattr(options
, i
):
362 cfg
[i
][0] = getattr(options
, i
)
369 write_config_h(cfg
, l
)
370 write_config_mk(cfg
, l
)
371 write_gfxprim_config(cfg
, l
)