blob: 0a8d335090e223af025e71d398081562697178df [file] [log] [blame]
Tamir Duberstein9d9d0b72015-04-11 20:23:45 -07001#! /usr/bin/env python
temporal40ee5512008-07-10 02:12:20 +00002#
3# See README for usage instructions.
Paul Yang704037f2018-11-28 16:45:16 -08004from distutils import util
Tamir Duberstein5018c432015-05-08 08:48:40 -04005import glob
temporal40ee5512008-07-10 02:12:20 +00006import os
Paul Yang704037f2018-11-28 16:45:16 -08007import pkg_resources
8import re
kenton@google.coma6de64a2009-04-18 02:28:15 +00009import subprocess
Tamir Duberstein5018c432015-05-08 08:48:40 -040010import sys
Paul Yang704037f2018-11-28 16:45:16 -080011import sysconfig
Paul Yang7f3e2372017-01-31 09:17:32 -080012import platform
temporal40ee5512008-07-10 02:12:20 +000013
liujisi@google.com9ced30c2012-08-01 06:22:19 +000014# We must use setuptools, not distutils, because we need to use the
15# namespace_packages option for the "google" package.
Dan O'Reilly3bdfb4b2015-08-20 13:51:26 -040016from setuptools import setup, Extension, find_packages
liujisi@google.com9ced30c2012-08-01 06:22:19 +000017
Benjamin Peterson188c44b2018-09-10 13:35:38 -070018from distutils.command.build_py import build_py as _build_py
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070019from distutils.command.clean import clean as _clean
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070020from distutils.spawn import find_executable
temporal40ee5512008-07-10 02:12:20 +000021
22# Find the Protocol Compiler.
liujisi@google.come34f1f62012-12-05 01:25:12 +000023if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
24 protoc = os.environ['PROTOC']
Joshua Habermanb99994d2020-03-31 16:25:37 -070025elif os.path.exists("../src/protoc"):
26 protoc = "../src/protoc"
27elif os.path.exists("../src/protoc.exe"):
28 protoc = "../src/protoc.exe"
29elif os.path.exists("../vsprojects/Debug/protoc.exe"):
30 protoc = "../vsprojects/Debug/protoc.exe"
31elif os.path.exists("../vsprojects/Release/protoc.exe"):
32 protoc = "../vsprojects/Release/protoc.exe"
temporal40ee5512008-07-10 02:12:20 +000033else:
34 protoc = find_executable("protoc")
35
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070036
Jisi Liu4573e112015-03-04 16:45:13 -080037def GetVersion():
38 """Gets the version from google/protobuf/__init__.py
39
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070040 Do not import google.protobuf.__init__ directly, because an installed
41 protobuf library may be loaded instead."""
Jisi Liu4573e112015-03-04 16:45:13 -080042
Joshua Habermanb99994d2020-03-31 16:25:37 -070043 with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file:
Behzad Tabibian2bf92b32015-05-07 19:04:56 +020044 exec(version_file.read(), globals())
cclauss35567c12018-06-25 19:50:40 +020045 global __version__
Jisi Liu4573e112015-03-04 16:45:13 -080046 return __version__
47
48
Feng Xiao8e142682015-05-26 00:11:09 -070049def generate_proto(source, require = True):
temporal40ee5512008-07-10 02:12:20 +000050 """Invokes the Protocol Compiler to generate a _pb2.py from the given
51 .proto file. Does nothing if the output already exists and is newer than
52 the input."""
53
Feng Xiao8e142682015-05-26 00:11:09 -070054 if not require and not os.path.exists(source):
55 return
56
Joshua Habermanb99994d2020-03-31 16:25:37 -070057 output = source.replace(".proto", "_pb2.py").replace("../src/", "")
temporal40ee5512008-07-10 02:12:20 +000058
temporal40ee5512008-07-10 02:12:20 +000059 if (not os.path.exists(output) or
60 (os.path.exists(source) and
61 os.path.getmtime(source) > os.path.getmtime(output))):
Joshua Habermanb99994d2020-03-31 16:25:37 -070062 print("Generating %s..." % output)
temporal40ee5512008-07-10 02:12:20 +000063
liujisi@google.com9ced30c2012-08-01 06:22:19 +000064 if not os.path.exists(source):
65 sys.stderr.write("Can't find required file: %s\n" % source)
66 sys.exit(-1)
67
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070068 if protoc is None:
temporal40ee5512008-07-10 02:12:20 +000069 sys.stderr.write(
70 "protoc is not installed nor found in ../src. Please compile it "
71 "or install the binary package.\n")
72 sys.exit(-1)
73
Joshua Habermanb99994d2020-03-31 16:25:37 -070074 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
kenton@google.coma6de64a2009-04-18 02:28:15 +000075 if subprocess.call(protoc_command) != 0:
temporal40ee5512008-07-10 02:12:20 +000076 sys.exit(-1)
77
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000078def GenerateUnittestProtos():
Adam Cozzetted64a2d92016-06-29 15:23:27 -070079 generate_proto("../src/google/protobuf/any_test.proto", False)
Adam Cozzette13fd0452017-09-12 10:32:01 -070080 generate_proto("../src/google/protobuf/map_proto2_unittest.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070081 generate_proto("../src/google/protobuf/map_unittest.proto", False)
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050082 generate_proto("../src/google/protobuf/test_messages_proto3.proto", False)
Yilun Chong18a0c2c2017-06-27 18:24:15 -070083 generate_proto("../src/google/protobuf/test_messages_proto2.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -070084 generate_proto("../src/google/protobuf/unittest_arena.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070085 generate_proto("../src/google/protobuf/unittest.proto", False)
86 generate_proto("../src/google/protobuf/unittest_custom_options.proto", False)
87 generate_proto("../src/google/protobuf/unittest_import.proto", False)
88 generate_proto("../src/google/protobuf/unittest_import_public.proto", False)
89 generate_proto("../src/google/protobuf/unittest_mset.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -070090 generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070091 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False)
92 generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False)
Hao Nguyen09cab822019-06-11 16:00:16 -070093 generate_proto("../src/google/protobuf/util/json_format.proto", False)
Jisi Liu46e8ff62015-10-05 11:59:43 -070094 generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False)
Feng Xiaoe841bac2015-12-11 17:09:20 -080095 generate_proto("google/protobuf/internal/any_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070096 generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False)
97 generate_proto("google/protobuf/internal/descriptor_pool_test2.proto", False)
98 generate_proto("google/protobuf/internal/factory_test1.proto", False)
99 generate_proto("google/protobuf/internal/factory_test2.proto", False)
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700100 generate_proto("google/protobuf/internal/file_options_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700101 generate_proto("google/protobuf/internal/import_test_package/inner.proto", False)
102 generate_proto("google/protobuf/internal/import_test_package/outer.proto", False)
103 generate_proto("google/protobuf/internal/missing_enum_values.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -0700104 generate_proto("google/protobuf/internal/message_set_extensions.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700105 generate_proto("google/protobuf/internal/more_extensions.proto", False)
106 generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False)
107 generate_proto("google/protobuf/internal/more_messages.proto", False)
Adam Cozzette837c94b2018-03-14 13:01:55 -0700108 generate_proto("google/protobuf/internal/no_package.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -0700109 generate_proto("google/protobuf/internal/packed_field_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700110 generate_proto("google/protobuf/internal/test_bad_identifiers.proto", False)
Joshua Habermanb7742c52020-04-08 10:30:17 -0700111 generate_proto("google/protobuf/internal/test_proto3_optional.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700112 generate_proto("google/protobuf/pyext/python.proto", False)
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000113
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700114
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000115class clean(_clean):
116 def run(self):
117 # Delete generated files in the code tree.
Joshua Habermanb99994d2020-03-31 16:25:37 -0700118 for (dirpath, dirnames, filenames) in os.walk("."):
temporal40ee5512008-07-10 02:12:20 +0000119 for filename in filenames:
120 filepath = os.path.join(dirpath, filename)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000121 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
Thomas Hisch451e0442018-04-09 21:43:10 +0200122 filepath.endswith(".so") or filepath.endswith(".o"):
temporal40ee5512008-07-10 02:12:20 +0000123 os.remove(filepath)
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000124 # _clean is an old-style class, so super() doesn't work.
125 _clean.run(self)
126
127class build_py(_build_py):
128 def run(self):
temporal40ee5512008-07-10 02:12:20 +0000129 # Generate necessary .proto file if it doesn't exist.
temporal40ee5512008-07-10 02:12:20 +0000130 generate_proto("../src/google/protobuf/descriptor.proto")
liujisi@google.com9b7f6c52010-12-08 03:45:27 +0000131 generate_proto("../src/google/protobuf/compiler/plugin.proto")
Jisi Liu7464f402015-10-06 14:20:26 -0700132 generate_proto("../src/google/protobuf/any.proto")
Jisi Liuf6fa5c72015-10-06 14:26:00 -0700133 generate_proto("../src/google/protobuf/api.proto")
134 generate_proto("../src/google/protobuf/duration.proto")
135 generate_proto("../src/google/protobuf/empty.proto")
Jisi Liu7464f402015-10-06 14:20:26 -0700136 generate_proto("../src/google/protobuf/field_mask.proto")
Jisi Liuf6fa5c72015-10-06 14:26:00 -0700137 generate_proto("../src/google/protobuf/source_context.proto")
138 generate_proto("../src/google/protobuf/struct.proto")
139 generate_proto("../src/google/protobuf/timestamp.proto")
140 generate_proto("../src/google/protobuf/type.proto")
141 generate_proto("../src/google/protobuf/wrappers.proto")
jieluo@google.combde4a322014-08-12 21:10:30 +0000142 GenerateUnittestProtos()
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000143
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000144 # _build_py is an old-style class, so super() doesn't work.
145 _build_py.run(self)
jieluo@google.combde4a322014-08-12 21:10:30 +0000146
Josh Haberman325392d2015-08-17 12:30:49 -0700147class test_conformance(_build_py):
148 target = 'test_python'
149 def run(self):
Yuchen Xie595231d2018-06-26 06:20:53 +0800150 # Python 2.6 dodges these extra failures.
151 os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = (
152 "--failure_list failure_list_python-post26.txt")
Josh Haberman4b31ffa2015-12-03 12:54:54 -0800153 cmd = 'cd ../conformance && make %s' % (test_conformance.target)
154 status = subprocess.check_call(cmd, shell=True)
Josh Haberman325392d2015-08-17 12:30:49 -0700155
temporal40ee5512008-07-10 02:12:20 +0000156
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700157def get_option_from_sys_argv(option_str):
158 if option_str in sys.argv:
159 sys.argv.remove(option_str)
160 return True
161 return False
162
163
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000164if __name__ == '__main__':
jieluo@google.com1eba9d92014-08-25 20:17:53 +0000165 ext_module_list = []
Josh Haberman00700b72015-10-06 14:13:09 -0700166 warnings_as_errors = '--warnings_as_errors'
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700167 if get_option_from_sys_argv('--cpp_implementation'):
168 # Link libprotobuf.a and libprotobuf-lite.a statically with the
169 # extension. Note that those libraries have to be compiled with
170 # -fPIC for this to work.
171 compile_static_ext = get_option_from_sys_argv('--compile_static_extension')
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700172 libraries = ['protobuf']
173 extra_objects = None
174 if compile_static_ext:
175 libraries = None
176 extra_objects = ['../src/.libs/libprotobuf.a',
177 '../src/.libs/libprotobuf-lite.a']
Josh Haberman325392d2015-08-17 12:30:49 -0700178 test_conformance.target = 'test_python_cpp'
Josh Habermand8814ed2015-10-07 11:46:23 -0700179
Paul Yangc9317432018-04-02 15:55:28 -0700180 extra_compile_args = []
181
182 if sys.platform != 'win32':
183 extra_compile_args.append('-Wno-write-strings')
184 extra_compile_args.append('-Wno-invalid-offsetof')
185 extra_compile_args.append('-Wno-sign-compare')
Feng Xiaoacd5b052018-08-09 21:21:01 -0700186 extra_compile_args.append('-Wno-unused-variable')
Arun Olappamanna Vasudevanc7e0e262018-07-23 13:35:15 -0700187 extra_compile_args.append('-std=c++11')
Paul Yangc9317432018-04-02 15:55:28 -0700188
Feng Xiaoacd5b052018-08-09 21:21:01 -0700189 if sys.platform == 'darwin':
190 extra_compile_args.append("-Wno-shorten-64-to-32");
Yilun Chong7bcb5222019-02-26 15:47:09 -0800191 extra_compile_args.append("-Wno-deprecated-register");
Feng Xiaoacd5b052018-08-09 21:21:01 -0700192
Paul Yang704037f2018-11-28 16:45:16 -0800193 # https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes
194 # C++ projects must now migrate to libc++ and are recommended to set a
195 # deployment target of macOS 10.9 or later, or iOS 7 or later.
196 if sys.platform == 'darwin':
197 mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
198 if mac_target and (pkg_resources.parse_version(mac_target) <
199 pkg_resources.parse_version('10.9.0')):
200 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9'
201 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub(
202 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1',
203 util.get_platform())
204
Paul Yangc9317432018-04-02 15:55:28 -0700205 # https://github.com/Theano/Theano/issues/4926
206 if sys.platform == 'win32':
207 extra_compile_args.append('-D_hypot=hypot')
208
209 # https://github.com/tpaviot/pythonocc-core/issues/48
210 if sys.platform == 'win32' and '64 bit' in sys.version:
211 extra_compile_args.append('-DMS_WIN64')
212
213 # MSVS default is dymanic
Paul Yang3b6d0272018-04-06 15:43:32 -0700214 if (sys.platform == 'win32'):
Paul Yangc9317432018-04-02 15:55:28 -0700215 extra_compile_args.append('/MT')
216
Josh Habermane1abdf22015-12-22 11:13:03 -0800217 if "clang" in os.popen('$CC --version 2> /dev/null').read():
Josh Habermand8814ed2015-10-07 11:46:23 -0700218 extra_compile_args.append('-Wno-shorten-64-to-32')
Josh Haberman00700b72015-10-06 14:13:09 -0700219
220 if warnings_as_errors in sys.argv:
221 extra_compile_args.append('-Werror')
222 sys.argv.remove(warnings_as_errors)
223
jieluo@google.com1eba9d92014-08-25 20:17:53 +0000224 # C++ implementation extension
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700225 ext_module_list.extend([
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700226 Extension(
227 "google.protobuf.pyext._message",
Tamir Duberstein5018c432015-05-08 08:48:40 -0400228 glob.glob('google/protobuf/pyext/*.cc'),
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700229 include_dirs=[".", "../src"],
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700230 libraries=libraries,
231 extra_objects=extra_objects,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700232 library_dirs=['../src/.libs'],
Josh Haberman00700b72015-10-06 14:13:09 -0700233 extra_compile_args=extra_compile_args,
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700234 ),
235 Extension(
236 "google.protobuf.internal._api_implementation",
237 glob.glob('google/protobuf/internal/api_implementation.cc'),
Paul Yangc9317432018-04-02 15:55:28 -0700238 extra_compile_args=extra_compile_args + ['-DPYTHON_PROTO2_CPP_IMPL_V2'],
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700239 ),
240 ])
Jisi Liuada65562015-02-25 16:39:11 -0800241 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
liujisi@google.com33165fe2010-11-02 13:14:58 +0000242
Dan O'Reilly3791c802015-08-20 20:49:45 -0400243 # Keep this list of dependencies in sync with tox.ini.
Feng Xiao283c40c2015-12-29 14:36:46 -0800244 install_requires = ['six>=1.9', 'setuptools']
Dan O'Reilly2621c8a2015-08-14 22:54:53 -0400245 if sys.version_info <= (2,7):
246 install_requires.append('ordereddict')
247 install_requires.append('unittest2')
248
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700249 setup(
250 name='protobuf',
251 version=GetVersion(),
252 description='Protocol Buffers',
Feng Xiaoafe98de2018-08-22 11:55:30 -0700253 download_url='https://github.com/protocolbuffers/protobuf/releases',
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700254 long_description="Protocol Buffers are Google's data interchange format",
255 url='https://developers.google.com/protocol-buffers/',
256 maintainer='protobuf@googlegroups.com',
257 maintainer_email='protobuf@googlegroups.com',
Sebastian Schuberth902af082017-02-28 09:58:24 +0100258 license='3-Clause BSD License',
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700259 classifiers=[
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400260 "Programming Language :: Python",
261 "Programming Language :: Python :: 2",
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400262 "Programming Language :: Python :: 2.7",
263 "Programming Language :: Python :: 3",
264 "Programming Language :: Python :: 3.3",
265 "Programming Language :: Python :: 3.4",
Paul Yang4dec4f92018-12-18 18:07:24 -0800266 "Programming Language :: Python :: 3.5",
267 "Programming Language :: Python :: 3.6",
268 "Programming Language :: Python :: 3.7",
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400269 ],
Craig Citro0e7c0c22016-03-04 20:40:24 -0800270 namespace_packages=['google'],
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700271 packages=find_packages(
272 exclude=[
273 'import_test_package',
274 ],
275 ),
276 test_suite='google.protobuf.internal',
277 cmdclass={
278 'clean': clean,
279 'build_py': build_py,
Josh Haberman325392d2015-08-17 12:30:49 -0700280 'test_conformance': test_conformance,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700281 },
Dan O'Reilly2621c8a2015-08-14 22:54:53 -0400282 install_requires=install_requires,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700283 ext_modules=ext_module_list,
284 )