make_lite.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #!/usr/bin/python
  2. import commands, os, re, string, sys, time
  3. def count_enclosed_functions (source):
  4. func_count = 0
  5. open_brace = 0
  6. close_brace = 0
  7. for ch in source:
  8. if ch == '{':
  9. open_brace += 1
  10. elif ch == '}':
  11. close_brace += 1
  12. if open_brace == close_brace:
  13. func_count += 1
  14. if open_brace < close_brace:
  15. print "count_enclosed_functions : open_brace < close_brace"
  16. return -1
  17. return func_count
  18. def find_function_prototype (source, proto_name):
  19. proto_re = "(^[a-zA-Z_ \t]+\s+%s[^a-zA-Z0-9_]\s*\([^\)]+\)\s+;\n)" % (proto_name)
  20. proto_result = re.search (proto_re, source, re.MULTILINE | re.DOTALL)
  21. if not proto_result:
  22. return None
  23. proto_text = proto_result.groups ()[0]
  24. return proto_text
  25. def find_function_definition (source, func_name):
  26. func_re = "(\n[a-zA-Z_ \t]+\n%s[^a-zA-Z0-9_].* /\* %s \*/\n)" % (func_name, func_name)
  27. func_result = re.search (func_re, source, re.MULTILINE | re.DOTALL)
  28. if not func_result:
  29. sys.exit (1)
  30. return None
  31. func_text = func_result.groups ()[0]
  32. # Now to check that we only have one enclosing function.
  33. func_count = count_enclosed_functions (func_text)
  34. if func_count != 1:
  35. return None
  36. return func_text
  37. def find_include (source, inc_name):
  38. inc_re = "(^#include\s+[\<\"]%s[\"\>]\s*)" % inc_name
  39. inc_result = re.search (inc_re, source, re.MULTILINE | re.DOTALL)
  40. if not inc_result:
  41. return None
  42. inc_text = inc_result.groups ()[0]
  43. return inc_text
  44. def find_assign_statement (source, var_name):
  45. var_re = "(^\s+%s\s*=[^;]+;)" % var_name
  46. var_result = re.search (var_re, source, re.MULTILINE | re.DOTALL)
  47. if not var_result:
  48. return None
  49. assign_text = var_result.groups ()[0]
  50. return assign_text
  51. #--------------------------------------------------------------------------------
  52. def remove_include (source, inc_name):
  53. inc_text = find_include (source, inc_name)
  54. if not inc_text:
  55. print "remove_include : include '%s' not found. Exiting." % inc_name
  56. sys.exit (1)
  57. source = string.replace (source, inc_text, "")
  58. return source
  59. def remove_assign (source, assign_name):
  60. assign_text = find_assign (source, inc_name)
  61. if not inc_text:
  62. print "remove_include : include '%s' not found. Exiting." % inc_name
  63. sys.exit (1)
  64. source = string.replace (source, inc_text, "")
  65. return source
  66. def remove_prototype (source, proto_name):
  67. proto_text = find_function_prototype (source, proto_name)
  68. if not proto_text:
  69. print "remove_prototype : prototype '%s' not found. Exiting." % proto_name
  70. sys.exit (1)
  71. source = string.replace (source, proto_text, "")
  72. return source
  73. def remove_function (source, func_name):
  74. func_text = find_function_definition (source, func_name)
  75. if not func_text:
  76. print "remove_function : function '%s' not found. Exiting." % func_name
  77. sys.exit (1)
  78. source = string.replace (source, func_text, "/* Function %s() removed here. */\n" % func_name)
  79. return source
  80. def remove_all_assignments (source, var):
  81. count = 0
  82. while 1:
  83. assign_text = find_assign_statement (source, var)
  84. if not assign_text:
  85. if count != 0:
  86. break
  87. print "remove_all_assignments : variable '%s' not found. Exiting." % var
  88. sys.exit (1)
  89. source = string.replace (source, assign_text, "")
  90. count += 1
  91. return source
  92. #----------------------------------------------------------------
  93. def remove_funcs_and_protos_from_file (filename, func_list):
  94. source_code = open (filename, 'r').read ()
  95. for func in func_list:
  96. source_code = remove_prototype (source_code, func) ;
  97. source_code = remove_function (source_code, func) ;
  98. open (filename, 'w').write (source_code)
  99. def remove_funcs_from_file (filename, func_list):
  100. source_code = open (filename, 'r').read ()
  101. for func in func_list:
  102. source_code = remove_function (source_code, func) ;
  103. open (filename, 'w').write (source_code)
  104. def remove_protos_from_file (filename, func_list):
  105. source_code = open (filename, 'r').read ()
  106. for func in func_list:
  107. source_code = remove_prototype (source_code, func) ;
  108. open (filename, 'w').write (source_code)
  109. def remove_includes_from_file (filename, inc_list):
  110. source_code = open (filename, 'r').read ()
  111. for inc in inc_list:
  112. source_code = remove_include (source_code, inc) ;
  113. open (filename, 'w').write (source_code)
  114. def remove_all_assignments_from_file (filename, var_list):
  115. source_code = open (filename, 'r').read ()
  116. for var in var_list:
  117. source_code = remove_all_assignments (source_code, var) ;
  118. open (filename, 'w').write (source_code)
  119. def remove_comment_start_end (filename, start_comment, end_comment):
  120. source_code = open (filename, 'r').read ()
  121. while 1:
  122. start_index = string.find (source_code, start_comment)
  123. end_index = string.find (source_code, end_comment)
  124. if start_index < 0 or end_index < start_index:
  125. break
  126. end_index += len (end_comment)
  127. source_code = source_code [:start_index-1] + source_code [end_index:] ;
  128. open (filename, 'w').write (source_code)
  129. def remove_strings_from_file (filename, str_list):
  130. file_text = open (filename, 'r').read ()
  131. for current_str in str_list:
  132. file_text = string.replace (file_text, current_str, '')
  133. open (filename, 'w').write (file_text)
  134. def string_replace_in_file (filename, from_str, to_str):
  135. file_text = open (filename, 'r').read ()
  136. file_text = string.replace (file_text, from_str, to_str)
  137. open (filename, 'w').write (file_text)
  138. def remove_regex_from_file (filename, regex_list):
  139. file_text = open (filename, 'r').read ()
  140. for regex in regex_list:
  141. file_text = re.sub (regex, '', file_text, re.MULTILINE | re.DOTALL)
  142. open (filename, 'w').write (file_text)
  143. #==========================================================================
  144. def find_configure_version (filename):
  145. # AM_INIT_AUTOMAKE(libsndfile,0.0.21pre6)
  146. file = open (filename)
  147. while 1:
  148. line = file.readline ()
  149. if re.search ("AC_INIT", line):
  150. x = re.sub ("[^\(]+\(", "", line)
  151. x = re.sub ("\).*\n", "", x)
  152. x = string.split (x, ",")
  153. package = x [0]
  154. version = x [1]
  155. break
  156. file.close ()
  157. # version = re.escape (version)
  158. return package, version
  159. def fix_configure_ac_file (filename):
  160. data = open (filename, 'r').read ()
  161. data = string.replace (data, "AM_INIT_AUTOMAKE(libsndfile,", "AM_INIT_AUTOMAKE(libsndfile_lite,", 1)
  162. file = open (filename, 'w')
  163. file.write (data)
  164. file.close ()
  165. def make_dist_file (package, version):
  166. print "Making dist file."
  167. tar_gz_file = "%s-%s.tar.gz" % (package, version)
  168. if os.path.exists (tar_gz_file):
  169. return
  170. if os.system ("make dist"):
  171. sys.exit (1)
  172. return
  173. def delete_files (file_list):
  174. for file_name in file_list:
  175. os.remove (file_name)
  176. #=======================================================================
  177. source_dir = os.getcwd ()
  178. conf_package, conf_version = find_configure_version ('configure.ac')
  179. package_version = "%s-%s" % (conf_package, conf_version)
  180. lite_version = "%s_lite-%s" % (conf_package, conf_version)
  181. os.system ("rm -rf %s%s.tar.gz" % (source_dir, package_version))
  182. os.system ("make dist")
  183. make_dist_file (conf_package, conf_version)
  184. os.chdir ("/tmp")
  185. print "Uncompressing .tar.gz file."
  186. os.system ("rm -rf %s" % package_version)
  187. if os.system ("tar zxf %s/%s.tar.gz" % (source_dir, package_version)):
  188. sys.exit (1)
  189. print "Renaming to libsndfile_lite."
  190. os.system ("rm -rf %s" % lite_version)
  191. os.rename (package_version, lite_version)
  192. print "Changing into libsndfile_lite directory."
  193. os.chdir (lite_version)
  194. print "Removing un-neeed directories."
  195. delete_dirs = [ 'src/G72x' ]
  196. for dir_name in delete_dirs:
  197. os.system ("rm -rf %s" % dir_name)
  198. print "Removing un-needed files."
  199. delete_files ([ 'src/ircam.c', 'src/nist.c',
  200. 'src/ima_adpcm.c', 'src/ms_adpcm.c', 'src/au_g72x.c',
  201. 'src/mat4.c', 'src/mat5.c', 'src/dwvw.c', 'src/paf.c',
  202. 'src/ogg.c', 'src/pvf.c', 'src/xi.c', 'src/htk.c',
  203. 'src/sd2.c', 'src/rx2.c', 'src/txw.c', 'src/wve.c',
  204. 'src/dwd.c', 'src/svx.c', 'src/voc.c', 'src/vox_adpcm.c',
  205. 'src/sds.c'
  206. ])
  207. print "Hacking 'configure.ac' and 'src/Makefile.am'."
  208. remove_strings_from_file ('configure.ac', [ 'src/G72x/Makefile' ])
  209. remove_strings_from_file ('src/Makefile.am', [ 'G72x/libg72x.la', 'G72x',
  210. 'ircam.c', 'nist.c', 'ima_adpcm.c', 'ms_adpcm.c', 'au_g72x.c', 'mat4.c',
  211. 'mat5.c', 'dwvw.c', 'paf.c', 'ogg.c', 'pvf.c', 'xi.c', 'htk.c',
  212. 'sd2.c', 'rx2.c', 'txw.c', 'wve.c', 'dwd.c', 'svx.c', 'voc.c',
  213. 'vox_adpcm.c', 'sds.c'
  214. ])
  215. #----------------------------------------------------------------------------
  216. print "Hacking header files."
  217. remove_protos_from_file ('src/common.h', [ 'xi_open', 'sd2_open', 'ogg_open',
  218. 'dwvw_init', 'paf_open', 'svx_open', 'nist_open', 'rx2_open', 'mat4_open',
  219. 'voc_open', 'txw_open', 'dwd_open', 'htk_open', 'wve_open', 'mat5_open',
  220. 'pvf_open', 'ircam_open', 'sds_open',
  221. 'float32_init', 'double64_init', 'aiff_ima_init', 'vox_adpcm_init',
  222. 'wav_w64_ima_init', 'wav_w64_msadpcm_init'
  223. ])
  224. remove_protos_from_file ('src/au.h',
  225. [ 'au_g72x_reader_init', 'au_g72x_writer_init' ])
  226. remove_protos_from_file ('src/wav_w64.h', [ 'msadpcm_write_adapt_coeffs' ])
  227. #----------------------------------------------------------------------------
  228. print "Hacking case statements."
  229. remove_comment_start_end ('src/sndfile.c', '/* Lite remove start */' , '/* Lite remove end */')
  230. remove_comment_start_end ('src/aiff.c', '/* Lite remove start */' , '/* Lite remove end */')
  231. remove_comment_start_end ('src/au.c', '/* Lite remove start */' , '/* Lite remove end */')
  232. remove_comment_start_end ('src/raw.c', '/* Lite remove start */' , '/* Lite remove end */')
  233. remove_comment_start_end ('src/w64.c', '/* Lite remove start */' , '/* Lite remove end */')
  234. remove_comment_start_end ('src/wav.c', '/* Lite remove start */' , '/* Lite remove end */')
  235. remove_comment_start_end ('src/double64.c', '/* Lite remove start */' , '/* Lite remove end */')
  236. remove_comment_start_end ('src/float32.c', '/* Lite remove start */' , '/* Lite remove end */')
  237. #----------------------------------------------------------------------------
  238. print "Hacking src/pcm.c."
  239. remove_funcs_from_file ('src/pcm.c', [
  240. 'f2sc_array', 'f2sc_clip_array', 'f2uc_array', 'f2uc_clip_array',
  241. 'f2bes_array', 'f2bes_clip_array', 'f2les_array', 'f2les_clip_array',
  242. 'f2let_array', 'f2let_clip_array', 'f2bet_array', 'f2bet_clip_array',
  243. 'f2bei_array', 'f2bei_clip_array', 'f2lei_array', 'f2lei_clip_array',
  244. 'd2sc_array', 'd2sc_clip_array', 'd2uc_array', 'd2uc_clip_array',
  245. 'd2bes_array', 'd2bes_clip_array', 'd2les_array', 'd2les_clip_array',
  246. 'd2let_array', 'd2let_clip_array', 'd2bet_array', 'd2bet_clip_array',
  247. 'd2bei_array', 'd2bei_clip_array', 'd2lei_array', 'd2lei_clip_array',
  248. ])
  249. remove_funcs_and_protos_from_file ('src/pcm.c', [
  250. 'pcm_read_sc2f', 'pcm_read_uc2f', 'pcm_read_les2f', 'pcm_read_bes2f',
  251. 'pcm_read_let2f', 'pcm_read_bet2f', 'pcm_read_lei2f', 'pcm_read_bei2f',
  252. 'pcm_read_sc2d', 'pcm_read_uc2d', 'pcm_read_les2d', 'pcm_read_bes2d',
  253. 'pcm_read_let2d', 'pcm_read_bet2d', 'pcm_read_lei2d', 'pcm_read_bei2d',
  254. 'pcm_write_f2sc', 'pcm_write_f2uc', 'pcm_write_f2bes', 'pcm_write_f2les',
  255. 'pcm_write_f2bet', 'pcm_write_f2let', 'pcm_write_f2bei', 'pcm_write_f2lei',
  256. 'pcm_write_d2sc', 'pcm_write_d2uc', 'pcm_write_d2bes', 'pcm_write_d2les',
  257. 'pcm_write_d2bet', 'pcm_write_d2let', 'pcm_write_d2bei', 'pcm_write_d2lei',
  258. 'sc2f_array', 'uc2f_array', 'bes2f_array', 'les2f_array',
  259. 'bet2f_array', 'let2f_array', 'bei2f_array', 'lei2f_array',
  260. 'sc2d_array', 'uc2d_array', 'bes2d_array', 'les2d_array',
  261. 'bet2d_array', 'let2d_array', 'bei2d_array', 'lei2d_array'
  262. ])
  263. remove_includes_from_file ('src/pcm.c', [ 'float_cast.h' ])
  264. remove_all_assignments_from_file ('src/pcm.c', [
  265. 'psf-\>write_float', 'psf\-\>write_double',
  266. 'psf-\>read_float', 'psf\-\>read_double' ])
  267. #----------------------------------------------------------------------------
  268. print "Hacking src/ulaw.c."
  269. remove_funcs_and_protos_from_file ('src/ulaw.c', [
  270. 'ulaw_read_ulaw2f', 'ulaw_read_ulaw2d',
  271. 'ulaw_write_f2ulaw', 'ulaw_write_d2ulaw',
  272. 'ulaw2f_array', 'ulaw2d_array', 'f2ulaw_array', 'd2ulaw_array'
  273. ])
  274. remove_includes_from_file ('src/ulaw.c', [ 'float_cast.h' ])
  275. remove_all_assignments_from_file ('src/ulaw.c', [
  276. 'psf-\>write_float', 'psf\-\>write_double',
  277. 'psf-\>read_float', 'psf\-\>read_double' ])
  278. #----------------------------------------------------------------------------
  279. print "Hacking src/alaw.c."
  280. remove_funcs_and_protos_from_file ('src/alaw.c', [
  281. 'alaw_read_alaw2f', 'alaw_read_alaw2d',
  282. 'alaw_write_f2alaw', 'alaw_write_d2alaw',
  283. 'alaw2f_array', 'alaw2d_array', 'f2alaw_array', 'd2alaw_array'
  284. ])
  285. remove_includes_from_file ('src/alaw.c', [ 'float_cast.h' ])
  286. remove_all_assignments_from_file ('src/alaw.c', [
  287. 'psf-\>write_float', 'psf\-\>write_double',
  288. 'psf-\>read_float', 'psf\-\>read_double' ])
  289. #----------------------------------------------------------------------------
  290. print "Hacking src/gsm610.c."
  291. remove_funcs_and_protos_from_file ('src/gsm610.c', [
  292. 'gsm610_read_f', 'gsm610_read_d', 'gsm610_write_f', 'gsm610_write_d'
  293. ])
  294. remove_includes_from_file ('src/gsm610.c', [ 'float_cast.h' ])
  295. remove_all_assignments_from_file ('src/gsm610.c', [
  296. 'psf-\>write_float', 'psf\-\>write_double',
  297. 'psf-\>read_float', 'psf\-\>read_double' ])
  298. #----------------------------------------------------------------------------
  299. print "Hacking src/float32.c."
  300. # string_replace_in_file ('src/float32.c', '"float_cast.h"', '<math.h>')
  301. remove_funcs_from_file ('src/float32.c', [ 'float32_init' ])
  302. remove_funcs_and_protos_from_file ('src/float32.c', [
  303. 'host_read_f2s', 'host_read_f2i', 'host_read_f', 'host_read_f2d',
  304. 'host_write_s2f', 'host_write_i2f', 'host_write_f', 'host_write_d2f',
  305. 'f2s_array', 'f2i_array', 'f2d_array', 's2f_array', 'i2f_array', 'd2f_array',
  306. 'float32_peak_update',
  307. 'replace_read_f2s', 'replace_read_f2i', 'replace_read_f', 'replace_read_f2d',
  308. 'replace_write_s2f', 'replace_write_i2f', 'replace_write_f', 'replace_write_d2f',
  309. 'bf2f_array', 'f2bf_array',
  310. 'float32_get_capability',
  311. ])
  312. #----------------------------------------------------------------------------
  313. print "Hacking src/double64.c."
  314. remove_funcs_from_file ('src/double64.c', [ 'double64_init' ])
  315. remove_funcs_and_protos_from_file ('src/double64.c', [
  316. 'host_read_d2s', 'host_read_d2i', 'host_read_d2f', 'host_read_d',
  317. 'host_write_s2d', 'host_write_i2d', 'host_write_f2d', 'host_write_d',
  318. 'd2s_array', 'd2i_array', 'd2f_array',
  319. 's2d_array', 'i2d_array', 'f2d_array',
  320. 'double64_peak_update', 'double64_get_capability',
  321. 'replace_read_d2s', 'replace_read_d2i', 'replace_read_d2f', 'replace_read_d',
  322. 'replace_write_s2d', 'replace_write_i2d', 'replace_write_f2d', 'replace_write_d',
  323. 'd2bd_read', 'bd2d_write'
  324. ])
  325. #----------------------------------------------------------------------------
  326. print "Hacking test programs."
  327. delete_files ([ 'tests/dwvw_test.c', 'tests/floating_point_test.c',
  328. 'tests/dft_cmp.c', 'tests/peak_chunk_test.c',
  329. 'tests/scale_clip_test.tpl', 'tests/scale_clip_test.def'
  330. ])
  331. remove_comment_start_end ('tests/write_read_test.def', '/* Lite remove start */', '/* Lite remove end */')
  332. remove_comment_start_end ('tests/write_read_test.tpl', '/* Lite remove start */', '/* Lite remove end */')
  333. remove_comment_start_end ('tests/Makefile.am', '# Lite remove start', '# Lite remove end')
  334. remove_strings_from_file ('tests/Makefile.am', [
  335. 'scale_clip_test.tpl', 'scale_clip_test.def',
  336. '\n\t./dwvw_test',
  337. '\n\t./floating_point_test', '\n\t./scale_clip_test',
  338. '\n\t./peak_chunk_test aiff', '\n\t./peak_chunk_test wav',
  339. '\n\t./command_test norm', '\n\t./command_test peak',
  340. '\n\t./lossy_comp_test wav_ima', '\n\t./lossy_comp_test wav_msadpcm',
  341. '\n\t./lossy_comp_test au_g721', '\n\t./lossy_comp_test au_g723',
  342. '\n\t./lossy_comp_test vox_adpcm',
  343. '\n\t./lossy_comp_test w64_ima', '\n\t./lossy_comp_test w64_msadpcm',
  344. 'peak_chunk_test', 'dwvw_test', 'floating_point_test', 'scale_clip_test',
  345. 'paf-tests', 'svx-tests', 'nist-tests', 'ircam-tests', 'voc-tests',
  346. 'mat4-tests', 'mat5-tests', 'pvf-tests', 'xi-tests', 'htk-tests',
  347. 'sds-tests'
  348. ])
  349. remove_comment_start_end ('tests/pcm_test.c', '/* Lite remove start */', '/* Lite remove end */')
  350. remove_funcs_and_protos_from_file ('tests/pcm_test.c', [
  351. 'pcm_test_float', 'pcm_test_double'
  352. ])
  353. remove_comment_start_end ('tests/lossy_comp_test.c', '/* Lite remove start */', '/* Lite remove end */')
  354. remove_funcs_and_protos_from_file ('tests/lossy_comp_test.c', [
  355. 'lcomp_test_float', 'lcomp_test_double', 'sdlcomp_test_float', 'sdlcomp_test_double',
  356. 'smoothed_diff_float', 'smoothed_diff_double'
  357. ])
  358. remove_comment_start_end ('tests/multi_file_test.c', '/* Lite remove start */', '/* Lite remove end */')
  359. remove_strings_from_file ('tests/stdio_test.c', [
  360. '"paf",', '"svx",', '"nist",', '"ircam",', '"voc",', '"mat4",', '"mat5",', '"pvf",'
  361. ])
  362. remove_comment_start_end ('tests/pipe_test.c', '/* Lite remove start */', '/* Lite remove end */')
  363. #----------------------------------------------------------------------------
  364. print "Fixing configure.ac file."
  365. fix_configure_ac_file ('configure.ac')
  366. print "Building and testing source."
  367. # Try --disable-shared --disable-gcc-opt
  368. if os.system ("./reconfigure.mk && ./configure --disable-shared --disable-gcc-opt && make check"):
  369. os.system ('PS1="FIX > " bash --norc')
  370. sys.exit (1)
  371. print "Making distcheck"
  372. if os.system ("make distcheck"):
  373. os.system ('PS1="FIX > " bash --norc')
  374. sys.exit (1)
  375. print "Copying tarball"
  376. if os.system ("cp %s.tar.gz %s" % (lite_version, source_dir)):
  377. print "??? %s.tar.gz ???" % lite_version
  378. os.system ('PS1="FIX > " bash --norc')
  379. sys.exit (1)
  380. os.chdir (source_dir)
  381. os.system ("rm -rf /tmp/%s" % lite_version)
  382. print "Done."