3 # This is simple script to detect libraries and configure standard features.
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
)
16 sys
.stderr
.write("Yes\n")
19 sys
.stderr
.write("No\n")
22 def c_try_compile(cfg
, code
, msg
):
25 ret
= os
.system("echo '%s' | %s -x c -o /dev/null - > /dev/null 2>&1" %
29 sys
.stderr
.write("No\n")
32 sys
.stderr
.write("Yes\n")
35 def c_compiler_exists(cfg
):
36 return c_try_compile(cfg
, "int main(void) { return 0; }",
37 "Checking for working compiler (%s) ... " %
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]))
47 sys
.stderr
.write('No\n')
50 sys
.stderr
.write('Yes\n')
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])
59 sys
.stderr
.write('No\n')
62 sys
.stderr
.write('Yes\n')
64 def check_for_python_config(cfg
):
65 sys
.stderr
.write("Checking for python-config ... ")
67 ret
= os
.system("%s --libs > /dev/null 2>&1" % cfg
['PYTHON_CONFIG'][0])
70 sys
.stderr
.write('No\n')
71 cfg
['PYTHON_CONFIG'][0] = ''
73 sys
.stderr
.write('Yes\n')
76 # Library checking api
79 def __init__(self
, libraries
, cfg
):
80 self
.libraries
= libraries
82 # Create dictionary for check results
87 def print_summary(self
):
88 sys
.stderr
.write("Libraries to link against\n")
89 sys
.stderr
.write("-------------------------\n")
91 for i
in self
.libraries
:
92 sys
.stderr
.write("%10s" % i
[0])
94 if (self
.results
[i
[0]]):
95 sys
.stderr
.write(" : Enabled\n")
97 sys
.stderr
.write(" : Disabled\n")
99 sys
.stderr
.write(" - %s\n\n" % i
[1])
101 # Enable/Disable library
103 def set(self
, name
, val
):
104 if name
not in map(lambda s
: s
[0], self
.libraries
):
105 sys
.stderr
.write("ERROR: Invalid library '%s'\n" % name
)
108 self
.results
[name
] = val
110 # Calls a function on arguments, all is stored in array if
112 # (I know this smells like a lisp, but I can't help myself)
115 sys
.stderr
.write("Checking for libraries\n")
116 sys
.stderr
.write("----------------------\n")
117 for i
in self
.libraries
:
118 if i
[0] not in self
.results
:
119 self
.results
[i
[0]] = i
[2][0](self
.cfg
, *i
[2][1:])
120 sys
.stderr
.write("\n")
122 # Writes '#define HAVE_XXX_H' into passed file
124 def write_config_h(self
, f
):
125 for i
in self
.libraries
:
126 f
.write("/*\n * %s\n */\n" % i
[1])
127 if self
.results
[i
[0]]:
128 f
.write("#define HAVE_%s\n" % i
[0].upper())
130 f
.write("//#define HAVE_%s\n" % i
[0].upper())
134 # Writes LDLIBS and CFLAGS into passed file
136 def write_config_mk(self
, f
):
137 for i
in self
.libraries
:
138 f
.write("# %s - %s\n" % (i
[0], i
[1]))
139 if self
.results
[i
[0]]:
140 f
.write("HAVE_%s=yes\n" % i
[0].upper())
141 f
.write("CFLAGS+=%s\nLDLIBS+=%s\n" % (i
[3], i
[4]))
143 f
.write("HAVE_%s=no\n" % i
[0].upper())
146 # Return list of linker flags needed to build particular module
147 # (module may be core, loaders, backends, etc...
149 def get_linker_flags(self
, module
):
151 for i
in self
.libraries
:
152 if module
in i
[5] and self
.results
[i
[0]]:
156 # Returns list of cflags needed to build module
158 def get_cflags(self
, module
):
160 for i
in self
.libraries
:
161 if module
in i
[5] and self
.results
[i
[0]]:
165 def die_screaming(msg
):
166 sys
.stderr
.write("\n************************************\n")
167 sys
.stderr
.write("ERROR: ")
168 sys
.stderr
.write(msg
)
169 sys
.stderr
.write("\n************************************\n")
173 # Check for basic compiling tools
175 def basic_checks(cfg
):
176 sys
.stderr
.write("Basic checks\n")
177 sys
.stderr
.write("------------\n")
179 if not c_compiler_exists(cfg
):
180 die_screaming("No C compiler found")
182 if not python_module_installed(cfg
, 'jinja2'):
183 die_screaming("No jinja2 python module found")
186 check_for_python_config(cfg
)
187 sys
.stderr
.write("\n")
190 # Write configuration files
192 def write_config_h(cfg
, libs
):
193 f
= open("config.h", "w")
194 f
.write("/*\n * This file is genereated by configure script\n */\n");
195 f
.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
196 libs
.write_config_h(f
);
197 f
.write("#endif /* CONFIG_H */\n");
198 sys
.stderr
.write("Config 'config.h' written\n")
201 def write_config_mk(cfg
, libs
):
202 f
= open('config.gen.mk', 'w')
204 f
.write("# %s\n%s=%s\n" % (cfg
[i
][1], i
, cfg
[i
][0]))
205 libs
.write_config_mk(f
);
207 sys
.stderr
.write("Config 'config.gen.mk' written\n")
210 # Generate app compilation helper
212 def write_gfxprim_config(cfg
, libs
):
213 modules
= ['loaders', 'backends', 'grabbers']
215 f
= open('gfxprim-config', 'w')
216 f
.write('#!/bin/sh\n'
217 '#\n# Generated by configure, do not edit directly\n#\n\n'
218 'USAGE="Usage: $0 --list-modules --cflags --libs --libs-module_foo"\n'
219 '\nif test $# -eq 0; then\n'
223 'while test -n "$1"; do\n'
226 # General switches cflags and ldflags
227 f
.write('\t--help) echo "$USAGE"; exit 0;;\n')
228 f
.write('\t--list-modules) echo "%s"; exit 0;;\n' % ' '.join(modules
))
229 f
.write('\t--cflags) echo -n "-I/usr/include/GP/%s";;\n' %
230 libs
.get_cflags('core'))
231 f
.write('\t--libs) echo -n "-lGP %s ";;\n' % libs
.get_linker_flags('core'))
233 # ldflags for specific modules
237 ldflags
+= '-lGP_backends '
239 ldflags
+= '-lGP_grabbers '
240 ldflags
+= libs
.get_linker_flags(i
)
241 f
.write('\t--libs-%s) echo -n "%s ";;\n' % (i
, ldflags
))
243 f
.write('\t*) echo "Invalid option \'$1\'"; echo $USAGE; exit 1;;\n')
245 f
.write('\tesac\n\tshift\ndone\necho\n')
247 os
.system('chmod +x gfxprim-config')
249 if __name__
== '__main__':
251 # Dictionary for default configuration parameters
253 cfg
= {'CC' : ['gcc', 'Path/name of the C compiler'],
254 'CFLAGS' : ['-pthread -W -Wall -Wextra -fPIC -O2 -ggdb -D_FORTIFY_SOURCE=2', 'C compiler flags'],
255 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
256 'SWIG' : ['swig', 'Simplified Wrapper and Interface Generator'],
257 'PYTHON_CONFIG' : ['python-config', 'Python config helper'],
258 'include_path' : ['/usr/include', 'Path to the system headers']}
261 # Library detection/enable disable
263 # name, description, [detection], cflags, ldflags, list of modules library is needed for
265 l
= libraries([["libpng",
266 "Portable Network Graphics Library",
267 [header_exists
, "png.h"], "", "-lpng", ["loaders"]],
269 "Simple Direct Media Layer",
270 [header_exists
, "SDL/SDL.h"], "", "`sdl-config --libs`", ["backends"]],
272 "Library to load, handle and manipulate images in the JPEG format",
273 [header_exists
, "jpeglib.h"], "", "-ljpeg", ["loaders"]],
275 "Library to handle, display and manipulate GIF images",
276 [header_exists
, "gif_lib.h"], "", "-lgif", ["loaders"]],
278 "Tag Image File Format (TIFF) library",
279 [header_exists
, "tiffio.h"], "", "-ltiff", ["loaders"]],
282 [header_exists
, "X11/Xlib.h"], "", "-lX11", ["backends"]],
284 "MIT-SHM X Extension",
285 [header_exists
, "X11/extensions/XShm.h"], "", "-lXext", ["backends"]],
287 "A high-quality and portable font engine",
288 [header_exists
, "ft2build.h"], "", "`freetype-config --libs`", ["core"]],
291 [header_exists
, "dlfcn.h"], "", "-ldl", ["core"]],
294 [header_exists
, "linux/videodev2.h"], "", "", ["grabbers"]],
297 [header_exists
, "pthread.h"], "-pthread", "-pthread", ["core"]],
299 "C stack trace writeout",
300 [c_try_compile
, "#include <execinfo.h>\nint main(void) { backtrace(0, 0); }",
301 "Checking for backtrace() ... "], "", "", ["core"]]], cfg
)
303 parser
= OptionParser();
305 # Get configuration parameters from environment variables
308 cfg
[i
][0] = os
.environ
[i
]
310 # Enable disable libraries for linking
311 parser
.add_option("-e", "--enable", dest
="enable", action
="append",
312 help="force enable library linking", metavar
="libfoo")
313 parser
.add_option("-d", "--disable", dest
="disable", action
="append",
314 help="disable library linking", metavar
="libfoo")
316 # Add cfg config options
318 parser
.add_option("", "--"+i
, dest
=i
, metavar
=cfg
[i
][0], help=cfg
[i
][1])
320 (options
, args
) = parser
.parse_args();
323 # Enable/Disable libraries as user requested
324 # These are not checked later
327 for i
in options
.enable
:
330 for i
in options
.disable
:
334 if getattr(options
, i
):
335 cfg
[i
][0] = getattr(options
, i
)
342 write_config_h(cfg
, l
)
343 write_config_mk(cfg
, l
)
344 write_gfxprim_config(cfg
, l
)