setup.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #! /usr/bin/env python
  2. #
  3. # See README for usage instructions.
  4. import glob
  5. import os
  6. import subprocess
  7. import sys
  8. import platform
  9. # We must use setuptools, not distutils, because we need to use the
  10. # namespace_packages option for the "google" package.
  11. from setuptools import setup, Extension, find_packages
  12. from distutils.command.clean import clean as _clean
  13. if sys.version_info[0] == 3:
  14. # Python 3
  15. from distutils.command.build_py import build_py_2to3 as _build_py
  16. else:
  17. # Python 2
  18. from distutils.command.build_py import build_py as _build_py
  19. from distutils.spawn import find_executable
  20. # Find the Protocol Compiler.
  21. if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
  22. protoc = os.environ['PROTOC']
  23. elif os.path.exists("../src/protoc"):
  24. protoc = "../src/protoc"
  25. elif os.path.exists("../src/protoc.exe"):
  26. protoc = "../src/protoc.exe"
  27. elif os.path.exists("../vsprojects/Debug/protoc.exe"):
  28. protoc = "../vsprojects/Debug/protoc.exe"
  29. elif os.path.exists("../vsprojects/Release/protoc.exe"):
  30. protoc = "../vsprojects/Release/protoc.exe"
  31. else:
  32. protoc = find_executable("protoc")
  33. def GetVersion():
  34. """Gets the version from google/protobuf/__init__.py
  35. Do not import google.protobuf.__init__ directly, because an installed
  36. protobuf library may be loaded instead."""
  37. with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file:
  38. exec(version_file.read(), globals())
  39. global __version__
  40. return __version__
  41. def generate_proto(source, require = True):
  42. """Invokes the Protocol Compiler to generate a _pb2.py from the given
  43. .proto file. Does nothing if the output already exists and is newer than
  44. the input."""
  45. if not require and not os.path.exists(source):
  46. return
  47. output = source.replace(".proto", "_pb2.py").replace("../src/", "")
  48. if (not os.path.exists(output) or
  49. (os.path.exists(source) and
  50. os.path.getmtime(source) > os.path.getmtime(output))):
  51. print("Generating %s..." % output)
  52. if not os.path.exists(source):
  53. sys.stderr.write("Can't find required file: %s\n" % source)
  54. sys.exit(-1)
  55. if protoc is None:
  56. sys.stderr.write(
  57. "protoc is not installed nor found in ../src. Please compile it "
  58. "or install the binary package.\n")
  59. sys.exit(-1)
  60. protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
  61. if subprocess.call(protoc_command) != 0:
  62. sys.exit(-1)
  63. def GenerateUnittestProtos():
  64. generate_proto("../src/google/protobuf/any_test.proto", False)
  65. generate_proto("../src/google/protobuf/map_proto2_unittest.proto", False)
  66. generate_proto("../src/google/protobuf/map_unittest.proto", False)
  67. generate_proto("../src/google/protobuf/test_messages_proto3.proto", False)
  68. generate_proto("../src/google/protobuf/test_messages_proto2.proto", False)
  69. generate_proto("../src/google/protobuf/unittest_arena.proto", False)
  70. generate_proto("../src/google/protobuf/unittest_no_arena.proto", False)
  71. generate_proto("../src/google/protobuf/unittest_no_arena_import.proto", False)
  72. generate_proto("../src/google/protobuf/unittest.proto", False)
  73. generate_proto("../src/google/protobuf/unittest_custom_options.proto", False)
  74. generate_proto("../src/google/protobuf/unittest_import.proto", False)
  75. generate_proto("../src/google/protobuf/unittest_import_public.proto", False)
  76. generate_proto("../src/google/protobuf/unittest_mset.proto", False)
  77. generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False)
  78. generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False)
  79. generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False)
  80. generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False)
  81. generate_proto("google/protobuf/internal/any_test.proto", False)
  82. generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False)
  83. generate_proto("google/protobuf/internal/descriptor_pool_test2.proto", False)
  84. generate_proto("google/protobuf/internal/factory_test1.proto", False)
  85. generate_proto("google/protobuf/internal/factory_test2.proto", False)
  86. generate_proto("google/protobuf/internal/file_options_test.proto", False)
  87. generate_proto("google/protobuf/internal/import_test_package/inner.proto", False)
  88. generate_proto("google/protobuf/internal/import_test_package/outer.proto", False)
  89. generate_proto("google/protobuf/internal/missing_enum_values.proto", False)
  90. generate_proto("google/protobuf/internal/message_set_extensions.proto", False)
  91. generate_proto("google/protobuf/internal/more_extensions.proto", False)
  92. generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False)
  93. generate_proto("google/protobuf/internal/more_messages.proto", False)
  94. generate_proto("google/protobuf/internal/no_package.proto", False)
  95. generate_proto("google/protobuf/internal/packed_field_test.proto", False)
  96. generate_proto("google/protobuf/internal/test_bad_identifiers.proto", False)
  97. generate_proto("google/protobuf/pyext/python.proto", False)
  98. class clean(_clean):
  99. def run(self):
  100. # Delete generated files in the code tree.
  101. for (dirpath, dirnames, filenames) in os.walk("."):
  102. for filename in filenames:
  103. filepath = os.path.join(dirpath, filename)
  104. if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
  105. filepath.endswith(".so") or filepath.endswith(".o"):
  106. os.remove(filepath)
  107. # _clean is an old-style class, so super() doesn't work.
  108. _clean.run(self)
  109. class build_py(_build_py):
  110. def run(self):
  111. # Generate necessary .proto file if it doesn't exist.
  112. generate_proto("../src/google/protobuf/descriptor.proto")
  113. generate_proto("../src/google/protobuf/compiler/plugin.proto")
  114. generate_proto("../src/google/protobuf/any.proto")
  115. generate_proto("../src/google/protobuf/api.proto")
  116. generate_proto("../src/google/protobuf/duration.proto")
  117. generate_proto("../src/google/protobuf/empty.proto")
  118. generate_proto("../src/google/protobuf/field_mask.proto")
  119. generate_proto("../src/google/protobuf/source_context.proto")
  120. generate_proto("../src/google/protobuf/struct.proto")
  121. generate_proto("../src/google/protobuf/timestamp.proto")
  122. generate_proto("../src/google/protobuf/type.proto")
  123. generate_proto("../src/google/protobuf/wrappers.proto")
  124. GenerateUnittestProtos()
  125. # _build_py is an old-style class, so super() doesn't work.
  126. _build_py.run(self)
  127. class test_conformance(_build_py):
  128. target = 'test_python'
  129. def run(self):
  130. # Python 2.6 dodges these extra failures.
  131. os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = (
  132. "--failure_list failure_list_python-post26.txt")
  133. cmd = 'cd ../conformance && make %s' % (test_conformance.target)
  134. status = subprocess.check_call(cmd, shell=True)
  135. def get_option_from_sys_argv(option_str):
  136. if option_str in sys.argv:
  137. sys.argv.remove(option_str)
  138. return True
  139. return False
  140. if __name__ == '__main__':
  141. ext_module_list = []
  142. warnings_as_errors = '--warnings_as_errors'
  143. if get_option_from_sys_argv('--cpp_implementation'):
  144. # Link libprotobuf.a and libprotobuf-lite.a statically with the
  145. # extension. Note that those libraries have to be compiled with
  146. # -fPIC for this to work.
  147. compile_static_ext = get_option_from_sys_argv('--compile_static_extension')
  148. libraries = ['protobuf']
  149. extra_objects = None
  150. if compile_static_ext:
  151. libraries = None
  152. extra_objects = ['../src/.libs/libprotobuf.a',
  153. '../src/.libs/libprotobuf-lite.a']
  154. test_conformance.target = 'test_python_cpp'
  155. extra_compile_args = []
  156. if sys.platform != 'win32':
  157. extra_compile_args.append('-Wno-write-strings')
  158. extra_compile_args.append('-Wno-invalid-offsetof')
  159. extra_compile_args.append('-Wno-sign-compare')
  160. extra_compile_args.append('-std=c++11')
  161. # https://github.com/Theano/Theano/issues/4926
  162. if sys.platform == 'win32':
  163. extra_compile_args.append('-D_hypot=hypot')
  164. # https://github.com/tpaviot/pythonocc-core/issues/48
  165. if sys.platform == 'win32' and '64 bit' in sys.version:
  166. extra_compile_args.append('-DMS_WIN64')
  167. # MSVS default is dymanic
  168. if (sys.platform == 'win32'):
  169. extra_compile_args.append('/MT')
  170. if "clang" in os.popen('$CC --version 2> /dev/null').read():
  171. extra_compile_args.append('-Wno-shorten-64-to-32')
  172. if warnings_as_errors in sys.argv:
  173. extra_compile_args.append('-Werror')
  174. sys.argv.remove(warnings_as_errors)
  175. # C++ implementation extension
  176. ext_module_list.extend([
  177. Extension(
  178. "google.protobuf.pyext._message",
  179. glob.glob('google/protobuf/pyext/*.cc'),
  180. include_dirs=[".", "../src"],
  181. libraries=libraries,
  182. extra_objects=extra_objects,
  183. library_dirs=['../src/.libs'],
  184. extra_compile_args=extra_compile_args,
  185. ),
  186. Extension(
  187. "google.protobuf.internal._api_implementation",
  188. glob.glob('google/protobuf/internal/api_implementation.cc'),
  189. extra_compile_args=extra_compile_args + ['-DPYTHON_PROTO2_CPP_IMPL_V2'],
  190. ),
  191. ])
  192. os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
  193. # Keep this list of dependencies in sync with tox.ini.
  194. install_requires = ['six>=1.9', 'setuptools']
  195. if sys.version_info <= (2,7):
  196. install_requires.append('ordereddict')
  197. install_requires.append('unittest2')
  198. setup(
  199. name='protobuf',
  200. version=GetVersion(),
  201. description='Protocol Buffers',
  202. download_url='https://github.com/google/protobuf/releases',
  203. long_description="Protocol Buffers are Google's data interchange format",
  204. url='https://developers.google.com/protocol-buffers/',
  205. maintainer='protobuf@googlegroups.com',
  206. maintainer_email='protobuf@googlegroups.com',
  207. license='3-Clause BSD License',
  208. classifiers=[
  209. "Programming Language :: Python",
  210. "Programming Language :: Python :: 2",
  211. "Programming Language :: Python :: 2.7",
  212. "Programming Language :: Python :: 3",
  213. "Programming Language :: Python :: 3.3",
  214. "Programming Language :: Python :: 3.4",
  215. ],
  216. namespace_packages=['google'],
  217. packages=find_packages(
  218. exclude=[
  219. 'import_test_package',
  220. ],
  221. ),
  222. test_suite='google.protobuf.internal',
  223. cmdclass={
  224. 'clean': clean,
  225. 'build_py': build_py,
  226. 'test_conformance': test_conformance,
  227. },
  228. install_requires=install_requires,
  229. ext_modules=ext_module_list,
  230. )