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")
65 # Library checking api
68 def __init__(self
, libraries
, cfg
):
69 self
.libraries
= libraries
71 # Create dictionary for check results
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")
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
)
97 self
.results
[name
] = val
99 # Calls a function on arguments, all is stored in array if
101 # (I know this smells like a lisp, but I can't help myself)
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())
119 f
.write("//#define HAVE_%s\n" % i
[0].upper())
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]))
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
):
140 for i
in self
.libraries
:
141 if module
in i
[5] and self
.results
[i
[0]]:
145 def basic_checks(cfg
):
146 sys
.stderr
.write("Basic checks\n")
147 sys
.stderr
.write("------------\n")
148 if not c_compiler_exists(cfg
):
150 if not python_module_installed(cfg
, 'jinja2'):
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")
166 def write_config_mk(cfg
, libs
):
167 f
= open('config.gen.mk', 'w')
169 f
.write("# %s\n%s=%s\n" % (cfg
[i
][1], i
, cfg
[i
][0]))
170 libs
.write_config_mk(f
);
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'
188 'while test -n "$1"; do\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
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')
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"}],
230 "Simple Direct Media Layer",
231 [header_exists
, "SDL/SDL.h"], "", "`sdl-config --libs`", {"backends"}],
233 "Library to load, handle and manipulate images in the JPEG format",
234 [header_exists
, "jpeglib.h"], "", "-ljpeg", {"loaders"}],
236 "Library to handle, display and manipulate GIF images",
237 [header_exists
, "gif_lib.h"], "", "-lgif", {"loaders"}],
240 [header_exists
, "X11/Xlib.h"], "", "-lX11", {"backends"}],
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
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
264 for i
in options
.enable
:
267 for i
in options
.disable
:
271 if getattr(options
, i
):
272 cfg
[i
][0] = getattr(options
, i
)
279 write_config_h(cfg
, l
)
280 write_config_mk(cfg
, l
)
281 write_gfxprim_config(cfg
, l
)