blob: d003c969ab99a722cbb3dce05548dfef1f9e1764 [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
Misha Seltzer223e8912020-05-14 21:09:09 -04005import fnmatch
Tamir Duberstein5018c432015-05-08 08:48:40 -04006import glob
temporal40ee5512008-07-10 02:12:20 +00007import os
Paul Yang704037f2018-11-28 16:45:16 -08008import pkg_resources
9import re
kenton@google.coma6de64a2009-04-18 02:28:15 +000010import subprocess
Tamir Duberstein5018c432015-05-08 08:48:40 -040011import sys
Paul Yang704037f2018-11-28 16:45:16 -080012import sysconfig
Paul Yang7f3e2372017-01-31 09:17:32 -080013import platform
temporal40ee5512008-07-10 02:12:20 +000014
liujisi@google.com9ced30c2012-08-01 06:22:19 +000015# We must use setuptools, not distutils, because we need to use the
16# namespace_packages option for the "google" package.
Dan O'Reilly3bdfb4b2015-08-20 13:51:26 -040017from setuptools import setup, Extension, find_packages
liujisi@google.com9ced30c2012-08-01 06:22:19 +000018
Benjamin Peterson188c44b2018-09-10 13:35:38 -070019from distutils.command.build_py import build_py as _build_py
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070020from distutils.command.clean import clean as _clean
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070021from distutils.spawn import find_executable
temporal40ee5512008-07-10 02:12:20 +000022
23# Find the Protocol Compiler.
liujisi@google.come34f1f62012-12-05 01:25:12 +000024if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
25 protoc = os.environ['PROTOC']
Joshua Habermanb99994d2020-03-31 16:25:37 -070026elif os.path.exists("../src/protoc"):
27 protoc = "../src/protoc"
28elif os.path.exists("../src/protoc.exe"):
29 protoc = "../src/protoc.exe"
30elif os.path.exists("../vsprojects/Debug/protoc.exe"):
31 protoc = "../vsprojects/Debug/protoc.exe"
32elif os.path.exists("../vsprojects/Release/protoc.exe"):
33 protoc = "../vsprojects/Release/protoc.exe"
temporal40ee5512008-07-10 02:12:20 +000034else:
35 protoc = find_executable("protoc")
36
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070037
Jisi Liu4573e112015-03-04 16:45:13 -080038def GetVersion():
39 """Gets the version from google/protobuf/__init__.py
40
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070041 Do not import google.protobuf.__init__ directly, because an installed
42 protobuf library may be loaded instead."""
Jisi Liu4573e112015-03-04 16:45:13 -080043
Joshua Habermanb99994d2020-03-31 16:25:37 -070044 with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file:
Behzad Tabibian2bf92b32015-05-07 19:04:56 +020045 exec(version_file.read(), globals())
cclauss35567c12018-06-25 19:50:40 +020046 global __version__
Jisi Liu4573e112015-03-04 16:45:13 -080047 return __version__
48
49
Feng Xiao8e142682015-05-26 00:11:09 -070050def generate_proto(source, require = True):
temporal40ee5512008-07-10 02:12:20 +000051 """Invokes the Protocol Compiler to generate a _pb2.py from the given
52 .proto file. Does nothing if the output already exists and is newer than
53 the input."""
54
Feng Xiao8e142682015-05-26 00:11:09 -070055 if not require and not os.path.exists(source):
56 return
57
Joshua Habermanb99994d2020-03-31 16:25:37 -070058 output = source.replace(".proto", "_pb2.py").replace("../src/", "")
temporal40ee5512008-07-10 02:12:20 +000059
temporal40ee5512008-07-10 02:12:20 +000060 if (not os.path.exists(output) or
61 (os.path.exists(source) and
62 os.path.getmtime(source) > os.path.getmtime(output))):
Joshua Habermanb99994d2020-03-31 16:25:37 -070063 print("Generating %s..." % output)
temporal40ee5512008-07-10 02:12:20 +000064
liujisi@google.com9ced30c2012-08-01 06:22:19 +000065 if not os.path.exists(source):
66 sys.stderr.write("Can't find required file: %s\n" % source)
67 sys.exit(-1)
68
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070069 if protoc is None:
temporal40ee5512008-07-10 02:12:20 +000070 sys.stderr.write(
71 "protoc is not installed nor found in ../src. Please compile it "
72 "or install the binary package.\n")
73 sys.exit(-1)
74
Joshua Habermanb99994d2020-03-31 16:25:37 -070075 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
kenton@google.coma6de64a2009-04-18 02:28:15 +000076 if subprocess.call(protoc_command) != 0:
temporal40ee5512008-07-10 02:12:20 +000077 sys.exit(-1)
78
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000079def GenerateUnittestProtos():
Adam Cozzetted64a2d92016-06-29 15:23:27 -070080 generate_proto("../src/google/protobuf/any_test.proto", False)
Adam Cozzette13fd0452017-09-12 10:32:01 -070081 generate_proto("../src/google/protobuf/map_proto2_unittest.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070082 generate_proto("../src/google/protobuf/map_unittest.proto", False)
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050083 generate_proto("../src/google/protobuf/test_messages_proto3.proto", False)
Yilun Chong18a0c2c2017-06-27 18:24:15 -070084 generate_proto("../src/google/protobuf/test_messages_proto2.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -070085 generate_proto("../src/google/protobuf/unittest_arena.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070086 generate_proto("../src/google/protobuf/unittest.proto", False)
87 generate_proto("../src/google/protobuf/unittest_custom_options.proto", False)
88 generate_proto("../src/google/protobuf/unittest_import.proto", False)
89 generate_proto("../src/google/protobuf/unittest_import_public.proto", False)
90 generate_proto("../src/google/protobuf/unittest_mset.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -070091 generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070092 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False)
93 generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False)
Hao Nguyen09cab822019-06-11 16:00:16 -070094 generate_proto("../src/google/protobuf/util/json_format.proto", False)
Jisi Liu46e8ff62015-10-05 11:59:43 -070095 generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False)
Feng Xiaoe841bac2015-12-11 17:09:20 -080096 generate_proto("google/protobuf/internal/any_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070097 generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False)
98 generate_proto("google/protobuf/internal/descriptor_pool_test2.proto", False)
99 generate_proto("google/protobuf/internal/factory_test1.proto", False)
100 generate_proto("google/protobuf/internal/factory_test2.proto", False)
Adam Cozzetted64a2d92016-06-29 15:23:27 -0700101 generate_proto("google/protobuf/internal/file_options_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700102 generate_proto("google/protobuf/internal/import_test_package/inner.proto", False)
103 generate_proto("google/protobuf/internal/import_test_package/outer.proto", False)
104 generate_proto("google/protobuf/internal/missing_enum_values.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -0700105 generate_proto("google/protobuf/internal/message_set_extensions.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700106 generate_proto("google/protobuf/internal/more_extensions.proto", False)
107 generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False)
108 generate_proto("google/protobuf/internal/more_messages.proto", False)
Adam Cozzette837c94b2018-03-14 13:01:55 -0700109 generate_proto("google/protobuf/internal/no_package.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -0700110 generate_proto("google/protobuf/internal/packed_field_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700111 generate_proto("google/protobuf/internal/test_bad_identifiers.proto", False)
Joshua Habermanb7742c52020-04-08 10:30:17 -0700112 generate_proto("google/protobuf/internal/test_proto3_optional.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700113 generate_proto("google/protobuf/pyext/python.proto", False)
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000114
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700115
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000116class clean(_clean):
117 def run(self):
118 # Delete generated files in the code tree.
Joshua Habermanb99994d2020-03-31 16:25:37 -0700119 for (dirpath, dirnames, filenames) in os.walk("."):
temporal40ee5512008-07-10 02:12:20 +0000120 for filename in filenames:
121 filepath = os.path.join(dirpath, filename)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000122 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
Thomas Hisch451e0442018-04-09 21:43:10 +0200123 filepath.endswith(".so") or filepath.endswith(".o"):
temporal40ee5512008-07-10 02:12:20 +0000124 os.remove(filepath)
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000125 # _clean is an old-style class, so super() doesn't work.
126 _clean.run(self)
127
128class build_py(_build_py):
129 def run(self):
temporal40ee5512008-07-10 02:12:20 +0000130 # Generate necessary .proto file if it doesn't exist.
temporal40ee5512008-07-10 02:12:20 +0000131 generate_proto("../src/google/protobuf/descriptor.proto")
liujisi@google.com9b7f6c52010-12-08 03:45:27 +0000132 generate_proto("../src/google/protobuf/compiler/plugin.proto")
Jisi Liu7464f402015-10-06 14:20:26 -0700133 generate_proto("../src/google/protobuf/any.proto")
Jisi Liuf6fa5c72015-10-06 14:26:00 -0700134 generate_proto("../src/google/protobuf/api.proto")
135 generate_proto("../src/google/protobuf/duration.proto")
136 generate_proto("../src/google/protobuf/empty.proto")
Jisi Liu7464f402015-10-06 14:20:26 -0700137 generate_proto("../src/google/protobuf/field_mask.proto")
Jisi Liuf6fa5c72015-10-06 14:26:00 -0700138 generate_proto("../src/google/protobuf/source_context.proto")
139 generate_proto("../src/google/protobuf/struct.proto")
140 generate_proto("../src/google/protobuf/timestamp.proto")
141 generate_proto("../src/google/protobuf/type.proto")
142 generate_proto("../src/google/protobuf/wrappers.proto")
jieluo@google.combde4a322014-08-12 21:10:30 +0000143 GenerateUnittestProtos()
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000144
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000145 # _build_py is an old-style class, so super() doesn't work.
146 _build_py.run(self)
jieluo@google.combde4a322014-08-12 21:10:30 +0000147
Misha Seltzer223e8912020-05-14 21:09:09 -0400148 def find_package_modules(self, package, package_dir):
149 exclude = (
150 "*test*",
151 "google/protobuf/internal/*_pb2.py",
152 "google/protobuf/internal/_parameterized.py",
153 "google/protobuf/pyext/python_pb2.py",
154 )
155 modules = _build_py.find_package_modules(self, package, package_dir)
156 return [(pkg, mod, fil) for (pkg, mod, fil) in modules
157 if not any(fnmatch.fnmatchcase(fil, pat=pat) for pat in exclude)]
158
159
Josh Haberman325392d2015-08-17 12:30:49 -0700160class test_conformance(_build_py):
161 target = 'test_python'
162 def run(self):
Yuchen Xie595231d2018-06-26 06:20:53 +0800163 # Python 2.6 dodges these extra failures.
164 os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = (
165 "--failure_list failure_list_python-post26.txt")
Josh Haberman4b31ffa2015-12-03 12:54:54 -0800166 cmd = 'cd ../conformance && make %s' % (test_conformance.target)
167 status = subprocess.check_call(cmd, shell=True)
Josh Haberman325392d2015-08-17 12:30:49 -0700168
temporal40ee5512008-07-10 02:12:20 +0000169
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700170def get_option_from_sys_argv(option_str):
171 if option_str in sys.argv:
172 sys.argv.remove(option_str)
173 return True
174 return False
175
176
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000177if __name__ == '__main__':
jieluo@google.com1eba9d92014-08-25 20:17:53 +0000178 ext_module_list = []
Josh Haberman00700b72015-10-06 14:13:09 -0700179 warnings_as_errors = '--warnings_as_errors'
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700180 if get_option_from_sys_argv('--cpp_implementation'):
181 # Link libprotobuf.a and libprotobuf-lite.a statically with the
182 # extension. Note that those libraries have to be compiled with
183 # -fPIC for this to work.
184 compile_static_ext = get_option_from_sys_argv('--compile_static_extension')
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700185 libraries = ['protobuf']
186 extra_objects = None
187 if compile_static_ext:
188 libraries = None
189 extra_objects = ['../src/.libs/libprotobuf.a',
190 '../src/.libs/libprotobuf-lite.a']
Josh Haberman325392d2015-08-17 12:30:49 -0700191 test_conformance.target = 'test_python_cpp'
Josh Habermand8814ed2015-10-07 11:46:23 -0700192
Paul Yangc9317432018-04-02 15:55:28 -0700193 extra_compile_args = []
194
195 if sys.platform != 'win32':
196 extra_compile_args.append('-Wno-write-strings')
197 extra_compile_args.append('-Wno-invalid-offsetof')
198 extra_compile_args.append('-Wno-sign-compare')
Feng Xiaoacd5b052018-08-09 21:21:01 -0700199 extra_compile_args.append('-Wno-unused-variable')
Arun Olappamanna Vasudevanc7e0e262018-07-23 13:35:15 -0700200 extra_compile_args.append('-std=c++11')
Paul Yangc9317432018-04-02 15:55:28 -0700201
Feng Xiaoacd5b052018-08-09 21:21:01 -0700202 if sys.platform == 'darwin':
203 extra_compile_args.append("-Wno-shorten-64-to-32");
Yilun Chong7bcb5222019-02-26 15:47:09 -0800204 extra_compile_args.append("-Wno-deprecated-register");
Feng Xiaoacd5b052018-08-09 21:21:01 -0700205
Paul Yang704037f2018-11-28 16:45:16 -0800206 # https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes
207 # C++ projects must now migrate to libc++ and are recommended to set a
208 # deployment target of macOS 10.9 or later, or iOS 7 or later.
209 if sys.platform == 'darwin':
210 mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
211 if mac_target and (pkg_resources.parse_version(mac_target) <
212 pkg_resources.parse_version('10.9.0')):
213 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9'
214 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub(
215 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1',
216 util.get_platform())
217
Paul Yangc9317432018-04-02 15:55:28 -0700218 # https://github.com/Theano/Theano/issues/4926
219 if sys.platform == 'win32':
220 extra_compile_args.append('-D_hypot=hypot')
221
222 # https://github.com/tpaviot/pythonocc-core/issues/48
223 if sys.platform == 'win32' and '64 bit' in sys.version:
224 extra_compile_args.append('-DMS_WIN64')
225
226 # MSVS default is dymanic
Paul Yang3b6d0272018-04-06 15:43:32 -0700227 if (sys.platform == 'win32'):
Paul Yangc9317432018-04-02 15:55:28 -0700228 extra_compile_args.append('/MT')
229
Josh Habermane1abdf22015-12-22 11:13:03 -0800230 if "clang" in os.popen('$CC --version 2> /dev/null').read():
Josh Habermand8814ed2015-10-07 11:46:23 -0700231 extra_compile_args.append('-Wno-shorten-64-to-32')
Josh Haberman00700b72015-10-06 14:13:09 -0700232
233 if warnings_as_errors in sys.argv:
234 extra_compile_args.append('-Werror')
235 sys.argv.remove(warnings_as_errors)
236
jieluo@google.com1eba9d92014-08-25 20:17:53 +0000237 # C++ implementation extension
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700238 ext_module_list.extend([
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700239 Extension(
240 "google.protobuf.pyext._message",
Tamir Duberstein5018c432015-05-08 08:48:40 -0400241 glob.glob('google/protobuf/pyext/*.cc'),
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700242 include_dirs=[".", "../src"],
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700243 libraries=libraries,
244 extra_objects=extra_objects,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700245 library_dirs=['../src/.libs'],
Josh Haberman00700b72015-10-06 14:13:09 -0700246 extra_compile_args=extra_compile_args,
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700247 ),
248 Extension(
249 "google.protobuf.internal._api_implementation",
250 glob.glob('google/protobuf/internal/api_implementation.cc'),
Paul Yangc9317432018-04-02 15:55:28 -0700251 extra_compile_args=extra_compile_args + ['-DPYTHON_PROTO2_CPP_IMPL_V2'],
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700252 ),
253 ])
Jisi Liuada65562015-02-25 16:39:11 -0800254 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
liujisi@google.com33165fe2010-11-02 13:14:58 +0000255
Dan O'Reilly3791c802015-08-20 20:49:45 -0400256 # Keep this list of dependencies in sync with tox.ini.
Misha Seltzer7daf0aa2020-05-14 21:53:32 -0400257 install_requires = ['six>=1.9']
Dan O'Reilly2621c8a2015-08-14 22:54:53 -0400258 if sys.version_info <= (2,7):
259 install_requires.append('ordereddict')
260 install_requires.append('unittest2')
261
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700262 setup(
263 name='protobuf',
264 version=GetVersion(),
265 description='Protocol Buffers',
Feng Xiaoafe98de2018-08-22 11:55:30 -0700266 download_url='https://github.com/protocolbuffers/protobuf/releases',
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700267 long_description="Protocol Buffers are Google's data interchange format",
268 url='https://developers.google.com/protocol-buffers/',
269 maintainer='protobuf@googlegroups.com',
270 maintainer_email='protobuf@googlegroups.com',
Sebastian Schuberth902af082017-02-28 09:58:24 +0100271 license='3-Clause BSD License',
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700272 classifiers=[
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400273 "Programming Language :: Python",
274 "Programming Language :: Python :: 2",
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400275 "Programming Language :: Python :: 2.7",
276 "Programming Language :: Python :: 3",
277 "Programming Language :: Python :: 3.3",
278 "Programming Language :: Python :: 3.4",
Paul Yang4dec4f92018-12-18 18:07:24 -0800279 "Programming Language :: Python :: 3.5",
280 "Programming Language :: Python :: 3.6",
281 "Programming Language :: Python :: 3.7",
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400282 ],
Craig Citro0e7c0c22016-03-04 20:40:24 -0800283 namespace_packages=['google'],
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700284 packages=find_packages(
285 exclude=[
286 'import_test_package',
David L. Jonesff06e232020-08-31 10:40:57 -0700287 'protobuf_distutils',
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700288 ],
289 ),
290 test_suite='google.protobuf.internal',
291 cmdclass={
292 'clean': clean,
293 'build_py': build_py,
Josh Haberman325392d2015-08-17 12:30:49 -0700294 'test_conformance': test_conformance,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700295 },
Dan O'Reilly2621c8a2015-08-14 22:54:53 -0400296 install_requires=install_requires,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700297 ext_modules=ext_module_list,
298 )