setup.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from glob import glob
  2. from os import path
  3. from setuptools import Extension, setup
  4. from setuptools.command.install import install
  5. from subprocess import check_output, CalledProcessError
  6. ROOT_DIR = path.dirname(path.realpath(__file__))
  7. MOD_DIR = path.join(ROOT_DIR, 'cpython')
  8. SRC_DIR = path.join(ROOT_DIR, 'src')
  9. CODEC_DIR = path.join(SRC_DIR, 'codec')
  10. INCL_DIR = path.join(ROOT_DIR, 'include')
  11. EXT_DIR = path.join(ROOT_DIR, 'ext')
  12. LEXER = 're2c'
  13. PARSER = 'lemon'
  14. sources = (
  15. glob(path.join(SRC_DIR, '*.c')) +
  16. glob(path.join(CODEC_DIR, '*_grammar.c')) +
  17. glob(path.join(SRC_DIR, 'codec', '*_parser.c')) +
  18. glob(path.join(MOD_DIR, '*.c')) +
  19. [
  20. path.join(EXT_DIR, 'openldap', 'libraries', 'liblmdb', 'mdb.c'),
  21. path.join(EXT_DIR, 'openldap', 'libraries', 'liblmdb', 'midl.c'),
  22. path.join(EXT_DIR, 'xxHash', 'xxhash.c'),
  23. path.join(EXT_DIR, 'hashmap', 'hashmap.c'),
  24. path.join(EXT_DIR, 'tpl', 'src', 'tpl.c'),
  25. path.join(EXT_DIR, 'log', 'src', 'log.c'),
  26. ]
  27. )
  28. debug = True
  29. compile_args = [
  30. '-DLOG_USE_COLOR',
  31. # '-std=c99',
  32. ]
  33. if debug:
  34. compile_args.extend(['-DDEBUG', '-g3', '-O0'])
  35. else:
  36. compile_args.extend(['-g0', '-O3'])
  37. class LSUPInstallCmd(install):
  38. """
  39. Run LSUP-specific hooks in extension build phase.
  40. TODO Extending the Extension class may be best to narrow the scope to the
  41. C module.
  42. """
  43. def run(self):
  44. # Run grammar and parser generators.
  45. try:
  46. lexer_ex_path = check_output(['which', LEXER])
  47. except CalledProcessError:
  48. raise SystemError(f'Lexer program `{LEXER}` is not installed.')
  49. try:
  50. parser_ex_path = check_output(['which', PARSER])
  51. except CalledProcessError:
  52. raise SystemError(f'Lexer program `{PARSER}` is not installed.')
  53. print("Generating grammar.")
  54. for fpath in glob(path.join(CODEC_DIR, '*_grammar.y')):
  55. check_output([
  56. parser_ex_path, fpath, 'q', '-m',
  57. '-T' + fpath.join(CODEC_DIR, 'lempar.c'), f'-d{CODEC_DIR}'
  58. ])
  59. print("Generating parser.")
  60. for fpath in glob(path.join(CODEC_DIR, '*_lexer.re')):
  61. check_output([
  62. lexer_ex_path, fpath, '-o',
  63. fpath.replace('_lexer.re', '_parser.c'), '-T', '--case-ranges',
  64. ])
  65. install.run(self)
  66. setup(
  67. name="lsup_rdf",
  68. version="1.0a2",
  69. description='Ultra-compact RDF library.',
  70. author='Stefano Cossu <https://notabug.org/scossu>',
  71. url='https://notabug.org/scossu/lsup_rdf',
  72. license='https://notabug.org/scossu/lsup_rdf/src/master/LICENSE',
  73. package_dir={'lsup_rdf': path.join(MOD_DIR, 'lsup_rdf')},
  74. packages=['lsup_rdf'],
  75. cmdclasss={'install': LSUPInstallCmd},
  76. ext_modules=[
  77. Extension(
  78. "_lsup_rdf",
  79. sources,
  80. include_dirs=[
  81. ROOT_DIR,
  82. INCL_DIR,
  83. path.join(EXT_DIR, 'hashmap'),
  84. path.join(EXT_DIR, 'tpl', 'src'),
  85. path.join(EXT_DIR, 'log', 'src'),
  86. ],
  87. libraries=['uuid'],
  88. extra_compile_args=compile_args,
  89. ),
  90. ],
  91. )