filters: Add support for arithmetic filters.
[gfxprim.git] / configure
blob12c3a89cfc6f6503396861fd43234a73262c2b17
1 #!/usr/bin/env python
3 # This is simple script to detect libraries and configure
4 # standard features.
6 import os
7 import sys
8 from optparse import OptionParser
10 def header_exists(cfg, filename):
11 fpath = cfg['include_path'][0] + '/' + filename
13 sys.stderr.write("Checking for '{0}' ... ".format(fpath))
15 try:
16 st = os.stat(fpath)
17 sys.stderr.write("Yes\n")
18 return True
19 except os.error:
20 sys.stderr.write("No\n")
21 return False
23 def c_try_compile(cfg, code, msg):
24 sys.stderr.write(msg)
26 ret = os.system("echo '{0}' | {1} -x c -o /dev/null - > /dev/null 2>&1".format(code, cfg["CC"][0]))
28 if ret:
29 sys.stderr.write("No\n")
30 return False
31 else:
32 sys.stderr.write("Yes\n")
33 return True
35 def c_compiler_exists(cfg):
36 return c_try_compile(cfg, "int main(void) { return 0; }",
37 "Checking for working compiler ({0}) ... ".format(cfg["CC"][0]))
39 def python_module_installed(cfg, module):
40 sys.stderr.write("Checking for python module {0} ... ".format(module))
42 ret = os.system("echo 'import {0}' | {1} > /dev/null 2>&1".format(module, cfg['PYTHON_BIN'][0]))
44 if ret:
45 sys.stderr.write('No\n')
46 return False
47 else:
48 sys.stderr.write('Yes\n')
49 return True
52 # Library checking api
54 class libraries:
55 def __init__(self, libraries, cfg):
56 self.libraries = libraries
57 self.cfg = cfg;
58 # Create dictionary for check results
59 self.results = dict()
61 # Print summary
63 def print_summary(self):
64 sys.stderr.write("Libraries to link against\n")
65 sys.stderr.write("-------------------------\n")
67 for i in self.libraries:
68 sys.stderr.write("{0:10}".format(i[0]))
70 if (self.results[i[0]]):
71 sys.stderr.write(" : Enabled\n")
72 else:
73 sys.stderr.write(" : Disabled\n")
75 sys.stderr.write(" - {0}\n\n".format(i[1]))
77 # Enable/Disable library
79 def set(self, name, val):
80 if name not in map(lambda s: s[0], self.libraries):
81 sys.stderr.write("ERROR: Invalid library '{0}'\n".format(name))
82 exit(1)
83 else:
84 self.results[name] = val
86 # Calls a function on arguments, all is stored in array if
87 # not set previously
88 # (I know this smells like a lisp, but I can't help myself)
90 def check(self):
91 sys.stderr.write("Checking for libraries\n")
92 sys.stderr.write("----------------------\n")
93 for i in self.libraries:
94 if i[0] not in self.results:
95 self.results[i[0]] = i[2][0](self.cfg, *i[2][1:])
96 sys.stderr.write("\n")
98 # Writes '#define HAVE_XXX_H' into passed file
100 def write(self, f):
101 for i in self.libraries:
102 f.write("/*\n * {0}\n */\n".format(i[1]))
103 if self.results[i[0]]:
104 f.write("#define HAVE_{0}\n".format(i[0].upper()))
105 else:
106 f.write("//#define HAVE_{0}\n".format(i[0].upper()))
107 f.write("\n")
109 def basic_checks(cfg):
110 sys.stderr.write("Basic checks\n")
111 sys.stderr.write("------------\n")
112 if not c_compiler_exists(cfg):
113 exit(1)
114 if not python_module_installed(cfg, 'jinja2'):
115 exit(1)
116 sys.stderr.write("\n")
119 # Write configuration files
121 def write_config_h(cfg, libs):
122 f = open("config.h", "w")
123 f.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
124 libs.write(f);
125 f.write("#endif /* CONFIG_H */\n");
126 sys.stderr.write("Config 'config.h' written\n")
127 f.close()
129 def write_config_mk(cfg, libs):
130 f = open('config.gen.mk', 'w')
131 for i in cfg:
132 f.write("#{0}\n{1}={2}\n".format(cfg[i][1], i, cfg[i][0]))
133 f.close()
134 sys.stderr.write("Config 'config.gen.mk' written\n")
136 if __name__ == '__main__':
138 # Dictionary for default configuration parameters
140 cfg = {'CC' : ['gcc', 'Path/name of the C compiler'],
141 'CFLAGS' : ['-W -Wall -Wextra -fPIC -O2', 'C compiler flags'],
142 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
143 'include_path': ['/usr/include', 'Path to the system headers']}
146 # Library detection/enable disable
148 l = libraries([["libpng",
149 "Portable Network Graphics Library",
150 [header_exists, "png.h"]],
151 ["libsdl",
152 "Simple Direct Media Layer",
153 [header_exists, "SDL/SDL.h"]],
154 ["jpeg",
155 "Library to load, handle and manipulate images in the JPEG format",
156 [header_exists, "jpeglib.h"]]], cfg)
158 parser = OptionParser();
160 # Enable disable libraries for linking
161 parser.add_option("-e", "--enable", dest="enable", action="append",
162 help="force enable library linking", metavar="libfoo")
163 parser.add_option("-d", "--disable", dest="disable", action="append",
164 help="disable library linking", metavar="libfoo")
166 # Add cfg config options
167 for i in cfg:
168 parser.add_option("", "--"+i, dest=i, metavar=cfg[i][0], help=cfg[i][1])
170 (options, args) = parser.parse_args();
173 # Enable/Disable libraries as user requested
174 # These are not checked later
176 if options.enable:
177 for i in options.enable:
178 l.set(i, True);
179 if options.disable:
180 for i in options.disable:
181 l.set(i, False);
183 for i in cfg:
184 if getattr(options, i):
185 cfg[i][0] = getattr(options, i)
187 basic_checks(cfg);
189 l.check()
190 l.print_summary()
192 write_config_h(cfg, l);
193 write_config_mk(cfg, l);