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 die_screaming(msg
):
146 sys
.stderr
.write("\n************************************\n")
147 sys
.stderr
.write("ERROR: ")
148 sys
.stderr
.write(msg
)
149 sys
.stderr
.write("\n************************************\n")
153 # Check for basic compiling tools
155 def basic_checks(cfg
):
156 sys
.stderr
.write("Basic checks\n")
157 sys
.stderr
.write("------------\n")
159 if not c_compiler_exists(cfg
):
160 die_screaming("No C compiler found")
162 if not python_module_installed(cfg
, 'jinja2'):
163 die_screaming("No jinja2 python module found")
166 sys
.stderr
.write("\n")
169 # Write configuration files
171 def write_config_h(cfg
, libs
):
172 f
= open("config.h", "w")
173 f
.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
174 libs
.write_config_h(f
);
175 f
.write("#endif /* CONFIG_H */\n");
176 sys
.stderr
.write("Config 'config.h' written\n")
179 def write_config_mk(cfg
, libs
):
180 f
= open('config.gen.mk', 'w')
182 f
.write("# %s\n%s=%s\n" % (cfg
[i
][1], i
, cfg
[i
][0]))
183 libs
.write_config_mk(f
);
185 sys
.stderr
.write("Config 'config.gen.mk' written\n")
188 # Generate app compilation helper
190 def write_gfxprim_config(cfg
, libs
):
191 modules
= ['loaders', 'backends']
193 f
= open('gfxprim-config', 'w')
194 f
.write('#!/bin/sh\n'
195 '#\n# Generated by configure, do not edit directly\n#\n\n'
196 'USAGE="Usage: $0 --list-modules --cflags --libs --libs-module_foo"\n'
197 '\nif test $# -eq 0; then\n'
201 'while test -n "$1"; do\n'
204 # General switches cflags and ldflags
205 f
.write('\t--help) echo "$USAGE"; exit 0;;\n')
206 f
.write('\t--list-modules) echo "%s"; exit 0;;\n' % ' '.join(modules
))
207 f
.write('\t--cflags) echo -n "-I/usr/include/GP/ ";;\n')
208 f
.write('\t--libs) echo -n "-lGP %s ";;\n' % libs
.get_linker_flags('core'))
210 # ldflags for specific modules
214 ldflags
+= '-lGP_backends '
215 ldflags
+= libs
.get_linker_flags(i
)
216 f
.write('\t--libs-%s) echo -n "%s ";;\n' % (i
, ldflags
))
218 f
.write('\t*) echo "Invalid option \'$1\'"; echo $USAGE; exit 1;;\n')
220 f
.write('\tesac\n\tshift\ndone\necho\n')
222 os
.system('chmod +x gfxprim-config')
224 if __name__
== '__main__':
226 # Dictionary for default configuration parameters
228 cfg
= {'CC' : ['gcc', 'Path/name of the C compiler'],
229 'CFLAGS' : ['-W -Wall -Wextra -fPIC -O2 -ggdb', 'C compiler flags'],
230 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
231 'SWIG' : ['swig', 'Simplified Wrapper and Interface Generator'],
232 'include_path': ['/usr/include', 'Path to the system headers']}
235 # Library detection/enable disable
237 # name, description, [detection], cflags, ldflags, list of modules library is needed for
239 l
= libraries([["libpng",
240 "Portable Network Graphics Library",
241 [header_exists
, "png.h"], "", "-lpng", ["loaders"]],
243 "Simple Direct Media Layer",
244 [header_exists
, "SDL/SDL.h"], "", "`sdl-config --libs`", ["backends"]],
246 "Library to load, handle and manipulate images in the JPEG format",
247 [header_exists
, "jpeglib.h"], "", "-ljpeg", ["loaders"]],
249 "Library to handle, display and manipulate GIF images",
250 [header_exists
, "gif_lib.h"], "", "-lgif", ["loaders"]],
253 [header_exists
, "X11/Xlib.h"], "", "-lX11", ["backends"]],
255 "A high-quality and portable font engine",
256 [header_exists
, "ft2build.h"], "", "`freetype-config --libs`", ["core"]]], cfg
)
258 parser
= OptionParser();
260 # Enable disable libraries for linking
261 parser
.add_option("-e", "--enable", dest
="enable", action
="append",
262 help="force enable library linking", metavar
="libfoo")
263 parser
.add_option("-d", "--disable", dest
="disable", action
="append",
264 help="disable library linking", metavar
="libfoo")
266 # Add cfg config options
268 parser
.add_option("", "--"+i
, dest
=i
, metavar
=cfg
[i
][0], help=cfg
[i
][1])
270 (options
, args
) = parser
.parse_args();
273 # Enable/Disable libraries as user requested
274 # These are not checked later
277 for i
in options
.enable
:
280 for i
in options
.disable
:
284 if getattr(options
, i
):
285 cfg
[i
][0] = getattr(options
, i
)
292 write_config_h(cfg
, l
)
293 write_config_mk(cfg
, l
)
294 write_gfxprim_config(cfg
, l
)