Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

540 lines
20KB

  1. #!/usr/bin/env python
  2. import glob
  3. import os
  4. import shutil
  5. import subprocess
  6. import waflib.Logs as Logs
  7. import waflib.Options as Options
  8. import waflib.extras.autowaf as autowaf
  9. # Library and package version (UNIX style major, minor, micro)
  10. # major increment <=> incompatible changes
  11. # minor increment <=> compatible changes (additions)
  12. # micro increment <=> no interface changes
  13. SERD_VERSION = '0.22.0'
  14. SERD_MAJOR_VERSION = '0'
  15. # Mandatory waf variables
  16. APPNAME = 'serd' # Package name for waf dist
  17. VERSION = SERD_VERSION # Package version for waf dist
  18. top = '.' # Source directory
  19. out = 'build' # Build directory
  20. def options(opt):
  21. opt.load('compiler_c')
  22. autowaf.set_options(opt)
  23. opt.add_option('--no-utils', action='store_true', dest='no_utils',
  24. help='Do not build command line utilities')
  25. opt.add_option('--test', action='store_true', dest='build_tests',
  26. help='Build unit tests')
  27. opt.add_option('--no-coverage', action='store_true', dest='no_coverage',
  28. help='Do not use gcov for code coverage')
  29. opt.add_option('--stack-check', action='store_true', dest='stack_check',
  30. help='Include runtime stack sanity checks')
  31. opt.add_option('--static', action='store_true', dest='static',
  32. help='Build static library')
  33. opt.add_option('--no-shared', action='store_true', dest='no_shared',
  34. help='Do not build shared library')
  35. opt.add_option('--static-progs', action='store_true', dest='static_progs',
  36. help='Build programs as static binaries')
  37. opt.add_option('--largefile', action='store_true', dest='largefile',
  38. help='Build with large file support on 32-bit systems')
  39. opt.add_option('--no-posix', action='store_true', dest='no_posix',
  40. help='Do not use posix_memalign, posix_fadvise, and fileno, even if present')
  41. def configure(conf):
  42. conf.load('compiler_c')
  43. autowaf.configure(conf)
  44. autowaf.display_header('Serd Configuration')
  45. autowaf.set_c99_mode(conf)
  46. conf.env.BUILD_TESTS = Options.options.build_tests
  47. conf.env.BUILD_UTILS = not Options.options.no_utils
  48. conf.env.BUILD_SHARED = not Options.options.no_shared
  49. conf.env.STATIC_PROGS = Options.options.static_progs
  50. conf.env.BUILD_STATIC = (Options.options.static or
  51. Options.options.static_progs)
  52. if not conf.env.BUILD_SHARED and not conf.env.BUILD_STATIC:
  53. conf.fatal('Neither a shared nor a static build requested')
  54. if Options.options.stack_check:
  55. autowaf.define(conf, 'SERD_STACK_CHECK', SERD_VERSION)
  56. if Options.options.largefile:
  57. conf.env.append_unique('DEFINES', ['_FILE_OFFSET_BITS=64'])
  58. if conf.env.BUILD_TESTS and not Options.options.no_coverage:
  59. conf.check_cc(lib='gcov', define_name='HAVE_GCOV', mandatory=False)
  60. if not Options.options.no_posix:
  61. conf.check(function_name = 'posix_memalign',
  62. header_name = 'stdlib.h',
  63. define_name = 'HAVE_POSIX_MEMALIGN',
  64. defines = ['_POSIX_C_SOURCE=201112L'],
  65. mandatory = False)
  66. conf.check(function_name = 'posix_fadvise',
  67. header_name = 'fcntl.h',
  68. define_name = 'HAVE_POSIX_FADVISE',
  69. defines = ['_POSIX_C_SOURCE=201112L'],
  70. mandatory = False)
  71. conf.check(function_name = 'fileno',
  72. header_name = 'stdio.h',
  73. define_name = 'HAVE_FILENO',
  74. defines = ['_POSIX_C_SOURCE=201112L'],
  75. mandatory = False)
  76. autowaf.define(conf, 'SERD_VERSION', SERD_VERSION)
  77. autowaf.set_lib_env(conf, 'serd', SERD_VERSION)
  78. conf.write_config_header('serd_config.h', remove=False)
  79. autowaf.display_msg(conf, 'Utilities', bool(conf.env.BUILD_UTILS))
  80. autowaf.display_msg(conf, 'Unit tests', bool(conf.env.BUILD_TESTS))
  81. print('')
  82. lib_source = [
  83. 'src/env.c',
  84. 'src/node.c',
  85. 'src/reader.c',
  86. 'src/string.c',
  87. 'src/uri.c',
  88. 'src/writer.c',
  89. ]
  90. def build(bld):
  91. # C Headers
  92. includedir = '${INCLUDEDIR}/serd-%s/serd' % SERD_MAJOR_VERSION
  93. bld.install_files(includedir, bld.path.ant_glob('serd/*.h'))
  94. # Pkgconfig file
  95. autowaf.build_pc(bld, 'SERD', SERD_VERSION, SERD_MAJOR_VERSION, [],
  96. {'SERD_MAJOR_VERSION' : SERD_MAJOR_VERSION})
  97. libflags = ['-fvisibility=hidden']
  98. libs = ['m']
  99. defines = []
  100. if bld.env.MSVC_COMPILER:
  101. libflags = []
  102. libs = []
  103. defines = ['snprintf=_snprintf']
  104. # Shared Library
  105. if bld.env.BUILD_SHARED:
  106. bld(features = 'c cshlib',
  107. export_includes = ['.'],
  108. source = lib_source,
  109. includes = ['.', './src'],
  110. lib = libs,
  111. name = 'libserd',
  112. target = 'serd-%s' % SERD_MAJOR_VERSION,
  113. vnum = SERD_VERSION,
  114. install_path = '${LIBDIR}',
  115. defines = defines + ['SERD_SHARED', 'SERD_INTERNAL'],
  116. cflags = libflags)
  117. # Static library
  118. if bld.env.BUILD_STATIC:
  119. bld(features = 'c cstlib',
  120. export_includes = ['.'],
  121. source = lib_source,
  122. includes = ['.', './src'],
  123. lib = libs,
  124. name = 'libserd_static',
  125. target = 'serd-%s' % SERD_MAJOR_VERSION,
  126. vnum = SERD_VERSION,
  127. install_path = '${LIBDIR}',
  128. defines = defines + ['SERD_INTERNAL'])
  129. if bld.env.BUILD_TESTS:
  130. test_libs = libs
  131. test_cflags = ['']
  132. if bld.is_defined('HAVE_GCOV'):
  133. test_libs += ['gcov']
  134. test_cflags += ['-fprofile-arcs', '-ftest-coverage']
  135. # Profiled static library for test coverage
  136. bld(features = 'c cstlib',
  137. source = lib_source,
  138. includes = ['.', './src'],
  139. lib = test_libs,
  140. name = 'libserd_profiled',
  141. target = 'serd_profiled',
  142. install_path = '',
  143. defines = defines + ['SERD_INTERNAL'],
  144. cflags = test_cflags)
  145. # Static profiled serdi for tests
  146. bld(features = 'c cprogram',
  147. source = 'src/serdi.c',
  148. includes = ['.', './src'],
  149. use = 'libserd_profiled',
  150. lib = test_libs,
  151. target = 'serdi_static',
  152. install_path = '',
  153. defines = defines,
  154. cflags = test_cflags)
  155. # Unit test program
  156. bld(features = 'c cprogram',
  157. source = 'tests/serd_test.c',
  158. includes = ['.', './src'],
  159. use = 'libserd_profiled',
  160. lib = test_libs,
  161. target = 'serd_test',
  162. install_path = '',
  163. defines = defines,
  164. cflags = test_cflags)
  165. # Utilities
  166. if bld.env.BUILD_UTILS:
  167. obj = bld(features = 'c cprogram',
  168. source = 'src/serdi.c',
  169. target = 'serdi',
  170. includes = ['.', './src'],
  171. use = 'libserd',
  172. lib = libs,
  173. install_path = '${BINDIR}')
  174. if not bld.env.BUILD_SHARED or bld.env.STATIC_PROGS:
  175. obj.use = 'libserd_static'
  176. if bld.env.STATIC_PROGS:
  177. obj.env.SHLIB_MARKER = obj.env.STLIB_MARKER
  178. obj.linkflags = ['-static']
  179. # Documentation
  180. autowaf.build_dox(bld, 'SERD', SERD_VERSION, top, out)
  181. # Man page
  182. bld.install_files('${MANDIR}/man1', 'doc/serdi.1')
  183. bld.add_post_fun(autowaf.run_ldconfig)
  184. if bld.env.DOCS:
  185. bld.add_post_fun(fix_docs)
  186. def lint(ctx):
  187. subprocess.call('cpplint.py --filter=+whitespace/comments,-whitespace/tab,-whitespace/braces,-whitespace/labels,-build/header_guard,-readability/casting,-readability/todo,-build/include src/* serd/*', shell=True)
  188. def amalgamate(ctx):
  189. shutil.copy('serd/serd.h', 'build/serd.h')
  190. amalgamation = open('build/serd.c', 'w')
  191. serd_internal_h = open('src/serd_internal.h')
  192. for l in serd_internal_h:
  193. if l == '#include "serd/serd.h"\n':
  194. amalgamation.write('#include "serd.h"\n')
  195. else:
  196. amalgamation.write(l)
  197. serd_internal_h.close()
  198. for f in lib_source:
  199. fd = open(f)
  200. amalgamation.write('\n/**\n @file %s\n*/' % f)
  201. header = True
  202. for l in fd:
  203. if header:
  204. if l == '*/\n':
  205. header = False
  206. else:
  207. if l != '#include "serd_internal.h"\n':
  208. amalgamation.write(l)
  209. fd.close()
  210. amalgamation.close()
  211. for i in ['c', 'h']:
  212. Logs.info('Wrote build/serd.%s' % i)
  213. def fix_docs(ctx):
  214. if ctx.cmd == 'build':
  215. autowaf.make_simple_dox(APPNAME)
  216. def upload_docs(ctx):
  217. os.system('rsync -ravz --delete -e ssh build/doc/html/ drobilla@drobilla.net:~/drobilla.net/docs/serd/')
  218. for page in glob.glob('doc/*.[1-8]'):
  219. os.system('soelim %s | pre-grohtml troff -man -wall -Thtml | post-grohtml > build/%s.html' % (page, page))
  220. os.system('rsync -avz --delete -e ssh build/%s.html drobilla@drobilla.net:~/drobilla.net/man/' % page)
  221. def file_equals(patha, pathb, subst_from='', subst_to=''):
  222. fa = open(patha, 'rU')
  223. fb = open(pathb, 'rU')
  224. for line in fa:
  225. if line.replace(subst_from, subst_to) != fb.readline().replace(subst_from, subst_to):
  226. return False
  227. fa.close()
  228. fb.close()
  229. return True
  230. def earl_assertion(test, passed, asserter):
  231. import datetime
  232. asserter_str = ''
  233. if asserter is not None:
  234. asserter_str = '\n\tearl:assertedBy <%s> ;' % asserter
  235. passed_str = 'earl:failed'
  236. if passed:
  237. passed_str = 'earl:passed'
  238. return '''
  239. []
  240. a earl:Assertion ;%s
  241. earl:subject <http://drobilla.net/sw/serd> ;
  242. earl:test <%s> ;
  243. earl:result [
  244. a earl:TestResult ;
  245. earl:outcome %s ;
  246. dc:date "%s"^^xsd:dateTime
  247. ] .
  248. ''' % (asserter_str,
  249. test,
  250. passed_str,
  251. datetime.datetime.now().replace(microsecond=0).isoformat())
  252. def test_thru(ctx, base, path, check_filename, flags):
  253. in_filename = os.path.join(ctx.path.abspath(), path);
  254. out_filename = path + '.thru'
  255. command = ('%s %s -i turtle -o turtle -p foo "%s" "%s" | '
  256. '%s -i turtle -o ntriples -c foo - "%s" > %s') % (
  257. 'serdi_static', flags.ljust(5),
  258. in_filename, base,
  259. 'serdi_static', base, out_filename)
  260. passed = autowaf.run_test(ctx, APPNAME, command, 0, name=out_filename)
  261. if not passed:
  262. Logs.pprint('RED', '** Failed command: %s' % command)
  263. return False
  264. if not os.access(out_filename, os.F_OK):
  265. Logs.pprint('RED', 'FAIL: %s is missing' % out_filename)
  266. elif not file_equals(check_filename, out_filename, '_:docid', '_:genid'):
  267. Logs.pprint('RED', 'FAIL: %s != %s' % (out_filename, check_filename))
  268. else:
  269. #Logs.pprint('GREEN', '** Pass %s == %s' % (out_filename, check_filename))
  270. return True
  271. return False
  272. def test_manifest(ctx, srcdir, testdir, report, base_uri):
  273. import rdflib
  274. import urlparse
  275. rdf = rdflib.Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
  276. rdfs = rdflib.Namespace('http://www.w3.org/2000/01/rdf-schema#')
  277. mf = rdflib.Namespace('http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#')
  278. rdft = rdflib.Namespace('http://www.w3.org/ns/rdftest#')
  279. earl = rdflib.Namespace('http://www.w3.org/ns/earl#')
  280. model = rdflib.ConjunctiveGraph()
  281. model.parse(os.path.join(srcdir, 'tests', testdir, 'manifest.ttl'),
  282. rdflib.URIRef(base_uri + 'manifest.ttl'),
  283. format='n3')
  284. asserter = ''
  285. if os.getenv('USER') == 'drobilla':
  286. asserter = 'http://drobilla.net/drobilla#me'
  287. def run_test(action_node, expected_return):
  288. output = os.path.join('tests', testdir, action_node + '.out')
  289. action = os.path.join(srcdir, 'tests', testdir, action_node)
  290. rel = os.path.relpath(action, os.path.join(srcdir, 'tests', testdir))
  291. command = 'serdi_static -f "%s" "%s" > %s' % (action, base_uri + rel, output)
  292. return autowaf.run_test(ctx, APPNAME, command, expected_return, name=name)
  293. for i in sorted(model.triples([None, rdf.type, rdft.TestTurtlePositiveSyntax])):
  294. test = i[0]
  295. name = model.value(test, mf.name, None)
  296. action_node = model.value(test, mf.action, None)[len(base_uri):]
  297. passed = run_test(action_node, 0)
  298. report.write(earl_assertion(test, passed, asserter))
  299. for i in sorted(model.triples([None, rdf.type, rdft.TestTurtleNegativeSyntax])):
  300. test = i[0]
  301. name = model.value(test, mf.name, None)
  302. action_node = model.value(test, mf.action, None)[len(base_uri):]
  303. passed = run_test(action_node, 1)
  304. report.write(earl_assertion(test, passed, asserter))
  305. for i in sorted(model.triples([None, rdf.type, rdft.TestTurtleNegativeEval])):
  306. test = i[0]
  307. name = model.value(test, mf.name, None)
  308. action_node = model.value(test, mf.action, None)[len(base_uri):]
  309. passed = run_test(action_node, 1)
  310. report.write(earl_assertion(test, passed, asserter))
  311. for i in sorted(model.triples([None, rdf.type, rdft.TestTurtleEval])):
  312. test = i[0]
  313. name = model.value(test, mf.name, None)
  314. action_node = model.value(test, mf.action, None)[len(base_uri):]
  315. result_node = model.value(test, mf.result, None)[len(base_uri):]
  316. passed = run_test(action_node, 0)
  317. if passed:
  318. action = os.path.join('tests', testdir, action_node)
  319. output = action + '.out'
  320. result = os.path.join(srcdir, 'tests', testdir, result_node)
  321. if not os.access(output, os.F_OK):
  322. passed = False
  323. Logs.pprint('RED', 'FAIL: %s output %s is missing' % (name, output))
  324. elif not file_equals(result, output):
  325. passed = False
  326. Logs.pprint('RED', 'FAIL: %s != %s' % (os.path.abspath(output), result))
  327. else:
  328. Logs.pprint('GREEN', '** Pass %s' % output)
  329. test_thru(ctx, base_uri + action_node, action, result, "")
  330. report.write(earl_assertion(test, passed, asserter))
  331. def test(ctx):
  332. blddir = autowaf.build_dir(APPNAME, 'tests')
  333. for i in ['', 'bad', 'good', 'new', 'TurtleTests', 'extra']:
  334. try:
  335. os.makedirs(os.path.join(blddir, i))
  336. except:
  337. pass
  338. for i in glob.glob(blddir + '/*.*'):
  339. os.remove(i)
  340. srcdir = ctx.path.abspath()
  341. orig_dir = os.path.abspath(os.curdir)
  342. os.chdir(os.path.join(srcdir, 'tests', 'good'))
  343. old_good_tests = glob.glob('*.ttl')
  344. old_good_tests.sort()
  345. old_good_tests.remove('manifest.ttl')
  346. good_tests = { 'good': old_good_tests }
  347. os.chdir(orig_dir)
  348. os.chdir(srcdir)
  349. bad_tests = glob.glob('tests/bad/*.ttl')
  350. bad_tests.sort()
  351. os.chdir(orig_dir)
  352. autowaf.pre_test(ctx, APPNAME)
  353. os.environ['PATH'] = '.' + os.pathsep + os.getenv('PATH')
  354. autowaf.run_tests(ctx, APPNAME, ['serd_test'], dirs=['.'])
  355. autowaf.run_tests(ctx, APPNAME, [
  356. 'serdi_static -q -o turtle "%s/tests/good/base.ttl" "base.ttl" > tests/good/base.ttl.out' % srcdir],
  357. 0, name='base')
  358. if not file_equals('%s/tests/good/base.ttl' % srcdir, 'tests/good/base.ttl.out'):
  359. Logs.pprint('RED', 'FAIL: build/tests/base.ttl.out is incorrect')
  360. nul = os.devnull
  361. autowaf.run_tests(ctx, APPNAME, [
  362. 'serdi_static "file://%s/tests/good/manifest.ttl" > %s' % (srcdir, nul),
  363. # 'serdi_static %s/tests/good/UTF-8.ttl > %s' % (srcdir, nul),
  364. 'serdi_static -v > %s' % nul,
  365. 'serdi_static -h > %s' % nul,
  366. 'serdi_static -s "<foo> a <#Thingie> ." > %s' % nul,
  367. 'serdi_static %s > %s' % (nul, nul)],
  368. 0, name='serdi-cmd-good')
  369. autowaf.run_tests(ctx, APPNAME, [
  370. 'serdi_static -q "file://%s/tests/bad-id-clash.ttl" > %s' % (srcdir, nul),
  371. 'serdi_static > %s' % nul,
  372. 'serdi_static ftp://example.org/unsupported.ttl > %s' % nul,
  373. 'serdi_static -i > %s' % nul,
  374. 'serdi_static -o > %s' % nul,
  375. 'serdi_static -z > %s' % nul,
  376. 'serdi_static -p > %s' % nul,
  377. 'serdi_static -c > %s' % nul,
  378. 'serdi_static -r > %s' % nul,
  379. 'serdi_static -i illegal > %s' % nul,
  380. 'serdi_static -o illegal > %s' % nul,
  381. 'serdi_static -i turtle > %s' % nul,
  382. 'serdi_static /no/such/file > %s' % nul],
  383. 1, name='serdi-cmd-bad')
  384. def test_base(test):
  385. return ('http://www.w3.org/2001/sw/DataAccess/df1/tests/'
  386. + test.replace('\\', '/'))
  387. # Good tests
  388. for tdir, tests in good_tests.items():
  389. commands = []
  390. for test in tests:
  391. for lax in ['', '-l']:
  392. path = os.path.join('tests', tdir, test)
  393. commands += [ 'serdi_static %s -f "%s" "%s" > %s.out' % (
  394. lax, os.path.join(srcdir, path), test_base(test), path) ]
  395. autowaf.run_tests(ctx, APPNAME, commands, 0, name=tdir)
  396. Logs.pprint('BOLD', '\nVerifying turtle => ntriples')
  397. for test in tests:
  398. check_filename = os.path.join(
  399. srcdir, 'tests', tdir, test.replace('.ttl', '.nt'))
  400. out_filename = os.path.join('tests', tdir, test + '.out')
  401. if not os.access(out_filename, os.F_OK):
  402. Logs.pprint('RED', 'FAIL: %s output is missing' % test)
  403. elif not file_equals(check_filename, out_filename):
  404. Logs.pprint('RED', 'FAIL: %s is incorrect' % out_filename)
  405. else:
  406. Logs.pprint('GREEN', 'Pass: %s' % test)
  407. # Bad tests
  408. commands = []
  409. for test in bad_tests:
  410. for lax in ['', '-l']:
  411. commands += [ 'serdi_static %s -q "%s" "%s" > %s.out' % (
  412. lax, os.path.join(srcdir, test), test_base(test), test) ]
  413. autowaf.run_tests(ctx, APPNAME, commands, 1, name='bad')
  414. # Don't do a round-trip test for test-id.ttl, IDs have changed
  415. good_tests['good'].remove('test-id.ttl')
  416. # Round-trip good tests
  417. for tdir, tests in good_tests.items():
  418. thru_tests = tests;
  419. commands = []
  420. num = 0
  421. for test in thru_tests:
  422. num += 1
  423. flags = ''
  424. if (num % 2 == 0):
  425. flags += '-b'
  426. if (num % 5 == 0):
  427. flags += ' -f'
  428. if (num % 3 == 0):
  429. flags += ' -r http://www.w3.org/'
  430. if (num % 7 == 0):
  431. flags += ' -e'
  432. path = os.path.join('tests', tdir, test)
  433. check = os.path.join(srcdir, path.replace('.ttl', '.nt'))
  434. test_thru(ctx, test_base(test), path, check, flags)
  435. # New manifest-driven tests
  436. try:
  437. report = open('earl.ttl', 'w')
  438. report.write('''@prefix earl: <http://www.w3.org/ns/earl#> .
  439. @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n''')
  440. serd_ttl = open(os.path.join(srcdir, 'serd.ttl'))
  441. for line in serd_ttl:
  442. report.write(line)
  443. serd_ttl.close()
  444. turtle_tests = 'http://www.w3.org/2013/TurtleTests/'
  445. test_manifest(ctx, srcdir, 'TurtleTests', report, turtle_tests)
  446. report.close()
  447. except:
  448. pass
  449. autowaf.post_test(ctx, APPNAME)