3 # This is simple script to detect libraries and configure
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
))
17 sys
.stderr
.write("Yes\n")
20 sys
.stderr
.write("No\n")
23 def c_try_compile(cfg
, code
, msg
):
26 ret
= os
.system("echo '{0}' | {1} -x c -o /dev/null - > /dev/null 2>&1".format(code
, cfg
["CC"][0]))
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 ({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]))
45 sys
.stderr
.write('No\n')
48 sys
.stderr
.write('Yes\n')
52 # Library checking api
55 def __init__(self
, libraries
, cfg
):
56 self
.libraries
= libraries
58 # Create dictionary for check results
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")
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
))
84 self
.results
[name
] = val
86 # Calls a function on arguments, all is stored in array if
88 # (I know this smells like a lisp, but I can't help myself)
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()))
106 f
.write("//#define HAVE_{0}\n".format(i
[0].upper()))
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
):
122 if not python_module_installed(cfg
, 'jinja2'):
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")
137 def write_config_mk(cfg
, libs
):
138 f
= open('config.gen.mk', 'w')
140 f
.write("# {0}\n{1}={2}\n".format(cfg
[i
][1], i
, cfg
[i
][0]))
141 libs
.write_config_mk(f
);
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"],
163 "Simple Direct Media Layer",
164 [header_exists
, "SDL/SDL.h"], "", ""],
166 "Library to load, handle and manipulate images in the JPEG format",
167 [header_exists
, "jpeglib.h"], "", "-ljpeg"],
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
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
191 for i
in options
.enable
:
194 for i
in options
.disable
:
198 if getattr(options
, i
):
199 cfg
[i
][0] = getattr(options
, i
)
206 write_config_h(cfg
, l
);
207 write_config_mk(cfg
, l
);