main.py 933 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import os.path
  2. import sys
  3. from cffi.setuptools_ext import execfile
  4. usage = '''Usage: {} INPUT:VAR OUTPUT
  5. Generate CFFI C module source code.
  6. INPUT is a source .py file.
  7. VAR is a cffi.FFI() object defined in the source file.
  8. OUTPUT is a .c or .cpp output file.
  9. '''
  10. def main():
  11. if len(sys.argv) != 3 or ':' not in sys.argv[1]:
  12. sys.stdout.write(usage.format(sys.argv[0]))
  13. sys.exit(1)
  14. mod_spec, c_file = sys.argv[1:3]
  15. build_file_name, ffi_var_name = mod_spec.rsplit(':', 1)
  16. source_dir = os.path.dirname(os.path.abspath(build_file_name))
  17. sys._MEIPASS = source_dir # For pygit2.
  18. sys.dont_write_bytecode = True
  19. sys.path = [source_dir]
  20. mod_vars = {'__name__': '__cffi__', '__file__': build_file_name}
  21. execfile(build_file_name, mod_vars)
  22. ffi = mod_vars[ffi_var_name]
  23. if callable(ffi):
  24. ffi = ffi()
  25. ffi.emit_c_code(c_file)
  26. if __name__ == '__main__':
  27. main()