A first pass at a rehuff tool for Theora.
[xiph/unicode.git] / vorbis-python / config_unix.py
blobe3e8b36785478e0469be66d93e4768c6f0ed53d0
1 #!/usr/bin/env python
3 import string
4 import os
5 import sys
6 import exceptions
8 log_name = 'config.log'
9 if os.path.isfile(log_name):
10 os.unlink(log_name)
12 def write_log(msg):
13 log_file = open(log_name, 'a')
14 log_file.write(msg)
15 log_file.write('\n')
16 log_file.close()
18 def exit(code=0):
19 sys.exit(code)
21 def msg_checking(msg):
22 print "Checking", msg, "...",
24 def execute(cmd):
25 write_log("Execute: %s" % cmd)
26 full_cmd = '%s 1>>%s 2>&1' % (cmd, log_name)
27 return os.system(full_cmd)
29 def run_test(input, flags = ''):
30 try:
31 f = open('_temp.c', 'w')
32 f.write(input)
33 f.close()
34 compile_cmd = '%s -o _temp _temp.c %s' % (os.environ.get('CC', 'cc'),
35 flags)
36 write_log("executing test: %s" % compile_cmd)
37 if not execute(compile_cmd):
38 execute('./_temp')
40 finally:
41 execute('rm -f _temp.c _temp')
43 ogg_test_program = '''
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ogg/ogg.h>
49 int main ()
51 system("touch conf.oggtest");
52 return 0;
54 '''
56 def find_ogg(ogg_prefix = '/usr/local', enable_oggtest = 1):
57 """A rough translation of ogg.m4"""
59 ogg_include_dir = ogg_prefix + '/include'
60 ogg_lib_dir = ogg_prefix + '/lib'
61 ogg_libs = 'ogg'
63 msg_checking('for Ogg')
65 if enable_oggtest:
66 execute('rm -f conf.oggtest')
68 try:
69 run_test(ogg_test_program, flags="-I" + ogg_include_dir)
70 if not os.path.isfile('conf.oggtest'):
71 raise RuntimeError, "Did not produce output"
72 execute('rm conf.oggtest')
74 except:
75 print "test program failed"
76 return None
78 print "success"
80 return {'ogg_libs' : ogg_libs,
81 'ogg_lib_dir' : ogg_lib_dir,
82 'ogg_include_dir' : ogg_include_dir}
85 vorbis_test_program = '''
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <vorbis/codec.h>
91 int main ()
93 system("touch conf.vorbistest");
94 return 0;
96 '''
98 def find_vorbis(ogg_data,
99 vorbis_prefix = '/usr/local',
100 enable_vorbistest = 1):
101 """A rough translation of vorbis.m4"""
103 ogg_libs = ogg_data['ogg_libs']
104 ogg_lib_dir = ogg_data['ogg_lib_dir']
105 ogg_include_dir = ogg_data['ogg_include_dir']
107 vorbis_include_dir = vorbis_prefix + '/include'
108 vorbis_lib_dir = vorbis_prefix + '/lib'
109 vorbis_libs = 'vorbis vorbisfile vorbisenc'
111 msg_checking('for Vorbis')
113 if enable_vorbistest:
114 execute('rm -f conf.vorbistest')
116 try:
117 run_test(vorbis_test_program,
118 flags = "-I%s -I%s" % (vorbis_include_dir,
119 ogg_include_dir))
120 if not os.path.isfile('conf.vorbistest'):
121 raise RuntimeError, "Did not produce output"
122 execute('rm conf.vorbistest')
124 except:
125 print "test program failed"
126 return None
128 print "success"
130 return {'vorbis_libs' : vorbis_libs,
131 'vorbis_lib_dir' : vorbis_lib_dir,
132 'vorbis_include_dir' : vorbis_include_dir}
134 def write_data(data):
135 f = open('Setup', 'w')
136 for item in data.items():
137 f.write('%s = %s\n' % item)
138 f.close()
139 print "Wrote Setup file"
141 def print_help():
142 print '''%s
143 --prefix Give the prefix in which vorbis was installed.
144 --with-ogg-dir [dir] Give the directory for ogg files
145 (separated by a space)
146 --with-vorbis-dir [dir] Give the directory for vorbis files''' % sys.argv[0]
147 exit()
149 def parse_args():
150 def arg_check(data, argv, pos, arg_type, key):
151 "Register an command line arg which takes an argument"
152 if len(argv) == pos:
153 print arg_type, "needs an argument"
154 exit(1)
155 data[key] = argv[pos]
157 data = {}
158 argv = sys.argv
159 for pos in range(len(argv)):
160 if argv[pos] == '--help':
161 print_help()
162 if argv[pos] == '--with-ogg-dir':
163 pos = pos + 1
164 arg_check(data, argv, pos, "Ogg dir", 'ogg_prefix')
165 if argv[pos] == '--with-vorbis-dir':
166 pos = pos + 1
167 arg_check(data, argv, pos, "Vorbis dir", 'vorbis_prefix')
168 if argv[pos] == '--prefix':
169 pos = pos + 1
170 arg_check(data, argv, pos, "Prefix", 'prefix')
172 return data
174 def main():
175 args = parse_args()
176 prefix = args.get('prefix', '/usr/local')
177 vorbis_prefix = args.get('vorbis_prefix', prefix)
178 ogg_prefix = args.get('ogg_prefix', prefix)
180 data = find_ogg(ogg_prefix = ogg_prefix)
181 if not data:
182 print "Config failure"
183 exit(1)
185 vorbis_data = find_vorbis(ogg_data = data,
186 vorbis_prefix = vorbis_prefix)
187 if not vorbis_data:
188 print "Config failure"
189 exit(1)
190 data.update(vorbis_data)
192 write_data(data)
194 if __name__ == '__main__':
195 main()