setup.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, 'tpl', 'src', 'tpl.c'),
  24. path.join(EXT_DIR, 'log', 'src', 'log.c'),
  25. ]
  26. )
  27. debug = True
  28. compile_args = [
  29. '-DLOG_USE_COLOR',
  30. # '-std=c99',
  31. ]
  32. if debug:
  33. compile_args.extend(['-DDEBUG', '-g3', '-O0'])
  34. else:
  35. compile_args.extend(['-g0', '-O3'])
  36. class LSUPInstallCmd(install):
  37. """
  38. Run LSUP-specific hooks in extension build phase.
  39. TODO Extending the Extension class may be best to narrow the scope to the
  40. C module.
  41. """
  42. def run(self):
  43. # Run grammar and parser generators.
  44. try:
  45. lexer_ex_path = check_output(['which', LEXER])
  46. except CalledProcessError:
  47. raise SystemError(f'Lexer program `{LEXER}` is not installed.')
  48. try:
  49. parser_ex_path = check_output(['which', PARSER])
  50. except CalledProcessError:
  51. raise SystemError(f'Lexer program `{PARSER}` is not installed.')
  52. print("Generating grammar.")
  53. for fpath in glob(path.join(CODEC_DIR, '*_grammar.y')):
  54. check_output([
  55. parser_ex_path, fpath, 'q', '-m',
  56. '-T' + fpath.join(CODEC_DIR, 'lempar.c'), f'-d{CODEC_DIR}'
  57. ])
  58. print("Generating parser.")
  59. for fpath in glob(path.join(CODEC_DIR, '*_lexer.re')):
  60. check_output([
  61. lexer_ex_path, fpath, '-o',
  62. fpath.replace('_lexer.re', '_parser.c'), '-T', '--case-ranges',
  63. ])
  64. install.run(self)
  65. setup(
  66. name="lsup_rdf",
  67. version="1.0a1",
  68. description='Ultra-compact RDF library.',
  69. author='Stefano Cossu <https://notabug.org/scossu>',
  70. url='https://notabug.org/scossu/lsup_rdf',
  71. license='https://notabug.org/scossu/lsup_rdf/src/master/LICENSE',
  72. package_dir={'lsup_rdf': path.join(MOD_DIR, 'lsup_rdf')},
  73. packages=['lsup_rdf'],
  74. cmdclasss={'install': LSUPInstallCmd},
  75. ext_modules=[
  76. Extension(
  77. "_lsup_rdf",
  78. sources,
  79. include_dirs=[
  80. ROOT_DIR,
  81. INCL_DIR,
  82. path.join(EXT_DIR, 'uthash', 'src'),
  83. path.join(EXT_DIR, 'tpl', 'src'),
  84. path.join(EXT_DIR, 'log', 'src'),
  85. ],
  86. libraries=['uuid'],
  87. extra_compile_args=compile_args,
  88. ),
  89. ],
  90. )