3 # This is simple script to detect libraries and configure standard features.
7 from optparse
import OptionParser
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("Checking for python-config Python version ... ")
44 if (cfg
['PYTHON_CONFIG'][0] is ''):
45 sys
.stderr
.write('NA\n')
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:]
54 sys
.stderr
.write("%s\n" % 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]))
64 sys
.stderr
.write('No\n')
67 sys
.stderr
.write('Yes\n')
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])
76 sys
.stderr
.write('No\n')
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])
87 sys
.stderr
.write('No\n')
88 cfg
['PYTHON_CONFIG'][0] = ''
90 sys
.stderr
.write('Yes\n')
93 # Library checking api
96 def __init__(self
, libraries
, cfg
):
97 self
.libraries
= libraries
99 # Create dictionary for check results
100 self
.results
= dict()
104 def print_summary(self
):
105 sys
.stderr
.write("Settings and variables\n")
106 sys
.stderr
.write("----------------------\n")
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")
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
)
132 self
.results
[name
] = val
134 # Calls a function on arguments, all is stored in array if
136 # (I know this smells like a lisp, but I can't help myself)
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())
154 f
.write("//#define HAVE_%s\n" % i
[0].upper())
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]))
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
):
175 for i
in self
.libraries
:
176 if module
in i
[5] and self
.results
[i
[0]]:
180 # Returns list of cflags needed to build module
182 def get_cflags(self
, module
):
184 for i
in self
.libraries
:
185 if module
in i
[5] and self
.results
[i
[0]]:
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")
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")
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'
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")
238 def write_config_mk(cfg
, libs
):
239 f
= open('config.gen.mk', 'w')
241 f
.write("# %s\n%s=%s\n" % (cfg
[i
][1], i
, cfg
[i
][0]))
242 libs
.write_config_mk(f
);
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'
260 'while test -n "$1"; do\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
274 ldflags
+= '-lGP_backends '
276 ldflags
+= '-lGP_grabbers '
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')
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"]],
313 "Simple Direct Media Layer",
314 [header_exists
, "SDL/SDL.h"], "", "`sdl-config --libs`", ["backends"]],
316 "Library to load, handle and manipulate images in the JPEG format",
317 [header_exists
, "jpeglib.h"], "", "-ljpeg", ["loaders"]],
319 "Library to handle, display and manipulate GIF images",
320 [header_exists
, "gif_lib.h"], "", "-lgif", ["loaders"]],
322 "Tag Image File Format (TIFF) library",
323 [header_exists
, "tiffio.h"], "", "-ltiff", ["loaders"]],
325 "Standard (de)compression library",
326 [header_exists
, "zlib.h"], "", "-lz", ["loaders"]],
329 [header_exists
, "X11/Xlib.h"], "", "-lX11", ["backends"]],
331 "MIT-SHM X Extension",
332 [header_exists
, "X11/extensions/XShm.h"], "", "-lXext", ["backends"]],
334 "Portable ascii art GFX library",
335 [header_exists
, "aalib.h"], "", "-laa", ["backends"]],
337 "A high-quality and portable font engine",
338 [header_exists
, "ft2build.h"], "", "`freetype-config --libs`", ["core"]],
341 [header_exists
, "dlfcn.h"], "", "-ldl", ["core"]],
344 [header_exists
, "linux/videodev2.h"], "", "", ["grabbers"]],
347 [header_exists
, "pthread.h"], "-pthread", "-pthread", ["core"]],
349 "C stack trace writeout",
350 [c_try_compile
, "#include <execinfo.h>\nint main(void) { backtrace(0, 0); }",
351 "Checking for backtrace() ... "], "", "", ["core"]]], cfg
)
353 parser
= OptionParser();
355 # Get configuration parameters from environment variables
358 cfg
[i
][0] = os
.environ
[i
]
360 # Enable disable libraries for linking
361 parser
.add_option("-e", "--enable", dest
="enable", action
="append",
362 help="force enable library linking", metavar
="libfoo")
363 parser
.add_option("-d", "--disable", dest
="disable", action
="append",
364 help="disable library linking", metavar
="libfoo")
366 # Add cfg config options
368 parser
.add_option("", "--"+i
, dest
=i
, metavar
=cfg
[i
][0], help=cfg
[i
][1])
370 (options
, args
) = parser
.parse_args();
373 # Enable/Disable libraries as user requested
374 # These are not checked later
377 for i
in options
.enable
:
380 for i
in options
.disable
:
384 if getattr(options
, i
):
385 cfg
[i
][0] = getattr(options
, i
)
392 write_config_h(cfg
, l
)
393 write_config_mk(cfg
, l
)
394 write_gfxprim_config(cfg
, l
)