loaders: Add support for OS/2 header type.
[gfxprim.git] / configure
blobbb7c7cb681edb710778511766045883ebd978593
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_config_h(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")
110 # Writes LDFLAGS and CFLAGS into passed file
112 def write_config_mk(self, f):
113 for i in self.libraries:
114 if self.results[i[0]]:
115 f.write("# {0}\nCFLAGS+={1}\nLDFLAGS+={2}\n".format(i[0], i[3], i[4]))
117 def basic_checks(cfg):
118 sys.stderr.write("Basic checks\n")
119 sys.stderr.write("------------\n")
120 if not c_compiler_exists(cfg):
121 exit(1)
122 if not python_module_installed(cfg, 'jinja2'):
123 exit(1)
124 sys.stderr.write("\n")
127 # Write configuration files
129 def write_config_h(cfg, libs):
130 f = open("config.h", "w")
131 f.write("#ifndef CONFIG_H\n#define CONFIG_H\n\n")
132 libs.write_config_h(f);
133 f.write("#endif /* CONFIG_H */\n");
134 sys.stderr.write("Config 'config.h' written\n")
135 f.close()
137 def write_config_mk(cfg, libs):
138 f = open('config.gen.mk', 'w')
139 for i in cfg:
140 f.write("# {0}\n{1}={2}\n".format(cfg[i][1], i, cfg[i][0]))
141 libs.write_config_mk(f);
142 f.close()
143 sys.stderr.write("Config 'config.gen.mk' written\n")
145 if __name__ == '__main__':
147 # Dictionary for default configuration parameters
149 cfg = {'CC' : ['gcc', 'Path/name of the C compiler'],
150 'CFLAGS' : ['-W -Wall -Wextra -fPIC -O2', 'C compiler flags'],
151 'PYTHON_BIN' : ['python', 'Path/name of python interpreter'],
152 'include_path': ['/usr/include', 'Path to the system headers']}
155 # Library detection/enable disable
157 # name, description, [detection], cflags, ldflags
159 l = libraries([["libpng",
160 "Portable Network Graphics Library",
161 [header_exists, "png.h"], "", "-lpng"],
162 ["libsdl",
163 "Simple Direct Media Layer",
164 [header_exists, "SDL/SDL.h"], "", ""],
165 ["jpeg",
166 "Library to load, handle and manipulate images in the JPEG format",
167 [header_exists, "jpeglib.h"], "", "-ljpeg"],
168 ["freetype",
169 "A high-quality and portable font engine",
170 [header_exists, "ft2build.h"], "", "`freetype-config --libs`"]], cfg)
172 parser = OptionParser();
174 # Enable disable libraries for linking
175 parser.add_option("-e", "--enable", dest="enable", action="append",
176 help="force enable library linking", metavar="libfoo")
177 parser.add_option("-d", "--disable", dest="disable", action="append",
178 help="disable library linking", metavar="libfoo")
180 # Add cfg config options
181 for i in cfg:
182 parser.add_option("", "--"+i, dest=i, metavar=cfg[i][0], help=cfg[i][1])
184 (options, args) = parser.parse_args();
187 # Enable/Disable libraries as user requested
188 # These are not checked later
190 if options.enable:
191 for i in options.enable:
192 l.set(i, True);
193 if options.disable:
194 for i in options.disable:
195 l.set(i, False);
197 for i in cfg:
198 if getattr(options, i):
199 cfg[i][0] = getattr(options, i)
201 basic_checks(cfg);
203 l.check()
204 l.print_summary()
206 write_config_h(cfg, l);
207 write_config_mk(cfg, l);