Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 77d9cd1f6ce1353a803ec173a07e7f9e21a4fd46 [file] [log] [blame]
Tamir Duberstein9d9d0b72015-04-12 03:23:451#! /usr/bin/env python
temporal40ee5512008-07-10 02:12:202#
3# See README for usage instructions.
Paul Yang704037f2018-11-29 00:45:164from distutils import util
Misha Seltzer223e8912020-05-15 01:09:095import fnmatch
Tamir Duberstein5018c432015-05-08 12:48:406import glob
temporal40ee5512008-07-10 02:12:207import os
Paul Yang704037f2018-11-29 00:45:168import pkg_resources
9import re
[email protected]a6de64a2009-04-18 02:28:1510import subprocess
Tamir Duberstein5018c432015-05-08 12:48:4011import sys
Paul Yang704037f2018-11-29 00:45:1612import sysconfig
Paul Yang7f3e2372017-01-31 17:17:3213import platform
temporal40ee5512008-07-10 02:12:2014
[email protected]9ced30c2012-08-01 06:22:1915# 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 17:51:2617from setuptools import setup, Extension, find_packages
[email protected]9ced30c2012-08-01 06:22:1918
Sandy Zhang215dd132021-09-10 20:04:5219from distutils.command.build_ext import build_ext as _build_ext
Benjamin Peterson188c44b2018-09-10 20:35:3820from distutils.command.build_py import build_py as _build_py
Tamir Duberstein21a7cf92015-04-12 01:24:2421from distutils.command.clean import clean as _clean
Tamir Duberstein21a7cf92015-04-12 01:24:2422from distutils.spawn import find_executable
temporal40ee5512008-07-10 02:12:2023
24# Find the Protocol Compiler.
[email protected]e34f1f62012-12-05 01:25:1225if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
26 protoc = os.environ['PROTOC']
Joshua Habermanb99994d2020-03-31 23:25:3727elif os.path.exists("../src/protoc"):
28 protoc = "../src/protoc"
29elif os.path.exists("../src/protoc.exe"):
30 protoc = "../src/protoc.exe"
31elif os.path.exists("../vsprojects/Debug/protoc.exe"):
32 protoc = "../vsprojects/Debug/protoc.exe"
33elif os.path.exists("../vsprojects/Release/protoc.exe"):
34 protoc = "../vsprojects/Release/protoc.exe"
temporal40ee5512008-07-10 02:12:2035else:
36 protoc = find_executable("protoc")
37
Tamir Duberstein21a7cf92015-04-12 01:24:2438
Jisi Liu4573e112015-03-05 00:45:1339def GetVersion():
40 """Gets the version from google/protobuf/__init__.py
41
Tamir Duberstein21a7cf92015-04-12 01:24:2442 Do not import google.protobuf.__init__ directly, because an installed
43 protobuf library may be loaded instead."""
Jisi Liu4573e112015-03-05 00:45:1344
Joshua Habermanb99994d2020-03-31 23:25:3745 with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file:
Behzad Tabibian2bf92b32015-05-07 17:04:5646 exec(version_file.read(), globals())
cclauss35567c12018-06-25 17:50:4047 global __version__
Jisi Liu4573e112015-03-05 00:45:1348 return __version__
49
50
Feng Xiao8e142682015-05-26 07:11:0951def generate_proto(source, require = True):
temporal40ee5512008-07-10 02:12:2052 """Invokes the Protocol Compiler to generate a _pb2.py from the given
53 .proto file. Does nothing if the output already exists and is newer than
54 the input."""
55
Feng Xiao8e142682015-05-26 07:11:0956 if not require and not os.path.exists(source):
57 return
58
Joshua Habermanb99994d2020-03-31 23:25:3759 output = source.replace(".proto", "_pb2.py").replace("../src/", "")
temporal40ee5512008-07-10 02:12:2060
temporal40ee5512008-07-10 02:12:2061 if (not os.path.exists(output) or
62 (os.path.exists(source) and
63 os.path.getmtime(source) > os.path.getmtime(output))):
Joshua Habermanb99994d2020-03-31 23:25:3764 print("Generating %s..." % output)
temporal40ee5512008-07-10 02:12:2065
[email protected]9ced30c2012-08-01 06:22:1966 if not os.path.exists(source):
67 sys.stderr.write("Can't find required file: %s\n" % source)
68 sys.exit(-1)
69
Tamir Duberstein21a7cf92015-04-12 01:24:2470 if protoc is None:
temporal40ee5512008-07-10 02:12:2071 sys.stderr.write(
72 "protoc is not installed nor found in ../src. Please compile it "
73 "or install the binary package.\n")
74 sys.exit(-1)
75
Joshua Habermanb99994d2020-03-31 23:25:3776 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
[email protected]a6de64a2009-04-18 02:28:1577 if subprocess.call(protoc_command) != 0:
temporal40ee5512008-07-10 02:12:2078 sys.exit(-1)
79
[email protected]b55a20f2012-09-22 02:40:5080def GenerateUnittestProtos():
Adam Cozzetted64a2d92016-06-29 22:23:2781 generate_proto("../src/google/protobuf/any_test.proto", False)
Adam Cozzette13fd0452017-09-12 17:32:0182 generate_proto("../src/google/protobuf/map_proto2_unittest.proto", False)
Feng Xiao8e142682015-05-26 07:11:0983 generate_proto("../src/google/protobuf/map_unittest.proto", False)
Joshua Habermanf1ce60e2016-12-03 16:51:2584 generate_proto("../src/google/protobuf/test_messages_proto3.proto", False)
Yilun Chong18a0c2c2017-06-28 01:24:1585 generate_proto("../src/google/protobuf/test_messages_proto2.proto", False)
Jisi Liub0f66112015-08-21 18:18:4586 generate_proto("../src/google/protobuf/unittest_arena.proto", False)
Feng Xiao8e142682015-05-26 07:11:0987 generate_proto("../src/google/protobuf/unittest.proto", False)
88 generate_proto("../src/google/protobuf/unittest_custom_options.proto", False)
89 generate_proto("../src/google/protobuf/unittest_import.proto", False)
90 generate_proto("../src/google/protobuf/unittest_import_public.proto", False)
91 generate_proto("../src/google/protobuf/unittest_mset.proto", False)
Jisi Liub0f66112015-08-21 18:18:4592 generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False)
Feng Xiao8e142682015-05-26 07:11:0993 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False)
94 generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False)
Hao Nguyen09cab822019-06-11 23:00:1695 generate_proto("../src/google/protobuf/util/json_format.proto", False)
Jisi Liu46e8ff62015-10-05 18:59:4396 generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False)
Feng Xiaoe841bac2015-12-12 01:09:2097 generate_proto("google/protobuf/internal/any_test.proto", False)
Feng Xiao8e142682015-05-26 07:11:0998 generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False)
99 generate_proto("google/protobuf/internal/descriptor_pool_test2.proto", False)
100 generate_proto("google/protobuf/internal/factory_test1.proto", False)
101 generate_proto("google/protobuf/internal/factory_test2.proto", False)
Adam Cozzetted64a2d92016-06-29 22:23:27102 generate_proto("google/protobuf/internal/file_options_test.proto", False)
Feng Xiao8e142682015-05-26 07:11:09103 generate_proto("google/protobuf/internal/import_test_package/inner.proto", False)
104 generate_proto("google/protobuf/internal/import_test_package/outer.proto", False)
105 generate_proto("google/protobuf/internal/missing_enum_values.proto", False)
Jisi Liub0f66112015-08-21 18:18:45106 generate_proto("google/protobuf/internal/message_set_extensions.proto", False)
Feng Xiao8e142682015-05-26 07:11:09107 generate_proto("google/protobuf/internal/more_extensions.proto", False)
108 generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False)
109 generate_proto("google/protobuf/internal/more_messages.proto", False)
Adam Cozzette837c94b2018-03-14 20:01:55110 generate_proto("google/protobuf/internal/no_package.proto", False)
Jisi Liub0f66112015-08-21 18:18:45111 generate_proto("google/protobuf/internal/packed_field_test.proto", False)
Feng Xiao8e142682015-05-26 07:11:09112 generate_proto("google/protobuf/internal/test_bad_identifiers.proto", False)
Joshua Habermanb7742c52020-04-08 17:30:17113 generate_proto("google/protobuf/internal/test_proto3_optional.proto", False)
Feng Xiao8e142682015-05-26 07:11:09114 generate_proto("google/protobuf/pyext/python.proto", False)
[email protected]9ced30c2012-08-01 06:22:19115
Tamir Duberstein21a7cf92015-04-12 01:24:24116
[email protected]9ced30c2012-08-01 06:22:19117class clean(_clean):
118 def run(self):
119 # Delete generated files in the code tree.
Joshua Habermanb99994d2020-03-31 23:25:37120 for (dirpath, dirnames, filenames) in os.walk("."):
temporal40ee5512008-07-10 02:12:20121 for filename in filenames:
122 filepath = os.path.join(dirpath, filename)
[email protected]33165fe2010-11-02 13:14:58123 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
Thomas Hisch451e0442018-04-09 19:43:10124 filepath.endswith(".so") or filepath.endswith(".o"):
temporal40ee5512008-07-10 02:12:20125 os.remove(filepath)
[email protected]9ced30c2012-08-01 06:22:19126 # _clean is an old-style class, so super() doesn't work.
127 _clean.run(self)
128
129class build_py(_build_py):
130 def run(self):
temporal40ee5512008-07-10 02:12:20131 # Generate necessary .proto file if it doesn't exist.
temporal40ee5512008-07-10 02:12:20132 generate_proto("../src/google/protobuf/descriptor.proto")
[email protected]9b7f6c52010-12-08 03:45:27133 generate_proto("../src/google/protobuf/compiler/plugin.proto")
Jisi Liu7464f402015-10-06 21:20:26134 generate_proto("../src/google/protobuf/any.proto")
Jisi Liuf6fa5c72015-10-06 21:26:00135 generate_proto("../src/google/protobuf/api.proto")
136 generate_proto("../src/google/protobuf/duration.proto")
137 generate_proto("../src/google/protobuf/empty.proto")
Jisi Liu7464f402015-10-06 21:20:26138 generate_proto("../src/google/protobuf/field_mask.proto")
Jisi Liuf6fa5c72015-10-06 21:26:00139 generate_proto("../src/google/protobuf/source_context.proto")
140 generate_proto("../src/google/protobuf/struct.proto")
141 generate_proto("../src/google/protobuf/timestamp.proto")
142 generate_proto("../src/google/protobuf/type.proto")
143 generate_proto("../src/google/protobuf/wrappers.proto")
[email protected]bde4a322014-08-12 21:10:30144 GenerateUnittestProtos()
[email protected]b55a20f2012-09-22 02:40:50145
[email protected]9ced30c2012-08-01 06:22:19146 # _build_py is an old-style class, so super() doesn't work.
147 _build_py.run(self)
[email protected]bde4a322014-08-12 21:10:30148
Misha Seltzer223e8912020-05-15 01:09:09149 def find_package_modules(self, package, package_dir):
150 exclude = (
151 "*test*",
152 "google/protobuf/internal/*_pb2.py",
153 "google/protobuf/internal/_parameterized.py",
154 "google/protobuf/pyext/python_pb2.py",
155 )
156 modules = _build_py.find_package_modules(self, package, package_dir)
157 return [(pkg, mod, fil) for (pkg, mod, fil) in modules
158 if not any(fnmatch.fnmatchcase(fil, pat=pat) for pat in exclude)]
159
160
Sandy Zhang215dd132021-09-10 20:04:52161class build_ext(_build_ext):
162
163 def get_ext_filename(self, ext_name):
164 # since python3.5, python extensions' shared libraries use a suffix that
165 # corresponds to the value of sysconfig.get_config_var('EXT_SUFFIX') and
166 # contains info about the architecture the library targets. E.g. on x64
167 # linux the suffix is ".cpython-XYZ-x86_64-linux-gnu.so" When
168 # crosscompiling python wheels, we need to be able to override this
169 # suffix so that the resulting file name matches the target architecture
170 # and we end up with a well-formed wheel.
171 filename = _build_ext.get_ext_filename(self, ext_name)
172 orig_ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")
173 new_ext_suffix = os.getenv("PROTOCOL_BUFFERS_OVERRIDE_EXT_SUFFIX")
174 if new_ext_suffix and filename.endswith(orig_ext_suffix):
175 filename = filename[:-len(orig_ext_suffix)] + new_ext_suffix
176 return filename
177
Josh Haberman325392d2015-08-17 19:30:49178class test_conformance(_build_py):
179 target = 'test_python'
180 def run(self):
Yuchen Xie595231d2018-06-25 22:20:53181 # Python 2.6 dodges these extra failures.
182 os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = (
183 "--failure_list failure_list_python-post26.txt")
Josh Haberman4b31ffa2015-12-03 20:54:54184 cmd = 'cd ../conformance && make %s' % (test_conformance.target)
185 status = subprocess.check_call(cmd, shell=True)
Josh Haberman325392d2015-08-17 19:30:49186
temporal40ee5512008-07-10 02:12:20187
Manjunath Kudlurcf828de2016-03-25 17:58:46188def get_option_from_sys_argv(option_str):
189 if option_str in sys.argv:
190 sys.argv.remove(option_str)
191 return True
192 return False
193
194
[email protected]9ced30c2012-08-01 06:22:19195if __name__ == '__main__':
[email protected]1eba9d92014-08-25 20:17:53196 ext_module_list = []
Josh Haberman00700b72015-10-06 21:13:09197 warnings_as_errors = '--warnings_as_errors'
Manjunath Kudlurcf828de2016-03-25 17:58:46198 if get_option_from_sys_argv('--cpp_implementation'):
199 # Link libprotobuf.a and libprotobuf-lite.a statically with the
200 # extension. Note that those libraries have to be compiled with
201 # -fPIC for this to work.
202 compile_static_ext = get_option_from_sys_argv('--compile_static_extension')
Manjunath Kudlurcf828de2016-03-25 17:58:46203 libraries = ['protobuf']
204 extra_objects = None
205 if compile_static_ext:
206 libraries = None
207 extra_objects = ['../src/.libs/libprotobuf.a',
208 '../src/.libs/libprotobuf-lite.a']
Josh Haberman325392d2015-08-17 19:30:49209 test_conformance.target = 'test_python_cpp'
Josh Habermand8814ed2015-10-07 18:46:23210
Paul Yangc9317432018-04-02 22:55:28211 extra_compile_args = []
212
213 if sys.platform != 'win32':
Adam Cozzette3afc8282021-10-08 23:45:26214 extra_compile_args.append('-Wno-write-strings')
215 extra_compile_args.append('-Wno-invalid-offsetof')
216 extra_compile_args.append('-Wno-sign-compare')
217 extra_compile_args.append('-Wno-unused-variable')
218 extra_compile_args.append('-std=c++11')
Paul Yangc9317432018-04-02 22:55:28219
Feng Xiaoacd5b052018-08-10 04:21:01220 if sys.platform == 'darwin':
221 extra_compile_args.append("-Wno-shorten-64-to-32");
Yilun Chong7bcb5222019-02-26 23:47:09222 extra_compile_args.append("-Wno-deprecated-register");
Feng Xiaoacd5b052018-08-10 04:21:01223
Paul Yang704037f2018-11-29 00:45:16224 # https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes
225 # C++ projects must now migrate to libc++ and are recommended to set a
226 # deployment target of macOS 10.9 or later, or iOS 7 or later.
227 if sys.platform == 'darwin':
Thomas BACCELLI26c0fbc2020-12-06 14:40:04228 mac_target = str(sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET'))
Paul Yang704037f2018-11-29 00:45:16229 if mac_target and (pkg_resources.parse_version(mac_target) <
230 pkg_resources.parse_version('10.9.0')):
231 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9'
232 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub(
233 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1',
234 util.get_platform())
235
Paul Yangc9317432018-04-02 22:55:28236 # https://github.com/Theano/Theano/issues/4926
237 if sys.platform == 'win32':
238 extra_compile_args.append('-D_hypot=hypot')
239
240 # https://github.com/tpaviot/pythonocc-core/issues/48
241 if sys.platform == 'win32' and '64 bit' in sys.version:
242 extra_compile_args.append('-DMS_WIN64')
243
244 # MSVS default is dymanic
Paul Yang3b6d0272018-04-06 22:43:32245 if (sys.platform == 'win32'):
Paul Yangc9317432018-04-02 22:55:28246 extra_compile_args.append('/MT')
247
Josh Habermane1abdf22015-12-22 19:13:03248 if "clang" in os.popen('$CC --version 2> /dev/null').read():
Josh Habermand8814ed2015-10-07 18:46:23249 extra_compile_args.append('-Wno-shorten-64-to-32')
Josh Haberman00700b72015-10-06 21:13:09250
Josh Haberman00700b72015-10-06 21:13:09251 if warnings_as_errors in sys.argv:
252 extra_compile_args.append('-Werror')
253 sys.argv.remove(warnings_as_errors)
254
[email protected]1eba9d92014-08-25 20:17:53255 # C++ implementation extension
Manjunath Kudlurcf828de2016-03-25 17:58:46256 ext_module_list.extend([
Tamir Duberstein21a7cf92015-04-12 01:24:24257 Extension(
258 "google.protobuf.pyext._message",
Tamir Duberstein5018c432015-05-08 12:48:40259 glob.glob('google/protobuf/pyext/*.cc'),
Tamir Duberstein21a7cf92015-04-12 01:24:24260 include_dirs=[".", "../src"],
Manjunath Kudlurcf828de2016-03-25 17:58:46261 libraries=libraries,
262 extra_objects=extra_objects,
Tamir Duberstein21a7cf92015-04-12 01:24:24263 library_dirs=['../src/.libs'],
Josh Haberman00700b72015-10-06 21:13:09264 extra_compile_args=extra_compile_args,
Manjunath Kudlurcf828de2016-03-25 17:58:46265 ),
266 Extension(
267 "google.protobuf.internal._api_implementation",
268 glob.glob('google/protobuf/internal/api_implementation.cc'),
Paul Yangc9317432018-04-02 22:55:28269 extra_compile_args=extra_compile_args + ['-DPYTHON_PROTO2_CPP_IMPL_V2'],
Manjunath Kudlurcf828de2016-03-25 17:58:46270 ),
271 ])
Jisi Liuada65562015-02-26 00:39:11272 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
[email protected]33165fe2010-11-02 13:14:58273
Dan O'Reilly3791c802015-08-21 00:49:45274 # Keep this list of dependencies in sync with tox.ini.
Joshua Habermana33aa732021-09-09 20:20:34275 install_requires = []
Dan O'Reilly2621c8a2015-08-15 02:54:53276
Tamir Duberstein21a7cf92015-04-12 01:24:24277 setup(
278 name='protobuf',
279 version=GetVersion(),
280 description='Protocol Buffers',
Feng Xiaoafe98de2018-08-22 18:55:30281 download_url='https://github.com/protocolbuffers/protobuf/releases',
Tamir Duberstein21a7cf92015-04-12 01:24:24282 long_description="Protocol Buffers are Google's data interchange format",
283 url='https://developers.google.com/protocol-buffers/',
284 maintainer='[email protected]',
285 maintainer_email='[email protected]',
Sebastian Schuberth902af082017-02-28 08:58:24286 license='3-Clause BSD License',
Tamir Duberstein21a7cf92015-04-12 01:24:24287 classifiers=[
Adam Cozzette3afc8282021-10-08 23:45:26288 "Programming Language :: Python",
289 "Programming Language :: Python :: 3",
Adam Cozzette3afc8282021-10-08 23:45:26290 "Programming Language :: Python :: 3.5",
291 "Programming Language :: Python :: 3.6",
292 "Programming Language :: Python :: 3.7",
Bu Sun Kimc01cd6e2021-10-15 17:24:49293 "Programming Language :: Python :: 3.8",
294 "Programming Language :: Python :: 3.9",
295 "Programming Language :: Python :: 3.10",
Adam Cozzette3afc8282021-10-08 23:45:26296 ],
Craig Citro0e7c0c22016-03-05 04:40:24297 namespace_packages=['google'],
Tamir Duberstein21a7cf92015-04-12 01:24:24298 packages=find_packages(
Tamir Duberstein21a7cf92015-04-12 01:24:24299 exclude=[
300 'import_test_package',
David L. Jonesff06e232020-08-31 17:40:57301 'protobuf_distutils',
Adam Cozzette3afc8282021-10-08 23:45:26302 ],),
Tamir Duberstein21a7cf92015-04-12 01:24:24303 test_suite='google.protobuf.internal',
304 cmdclass={
305 'clean': clean,
306 'build_py': build_py,
Sandy Zhang215dd132021-09-10 20:04:52307 'build_ext': build_ext,
Josh Haberman325392d2015-08-17 19:30:49308 'test_conformance': test_conformance,
Tamir Duberstein21a7cf92015-04-12 01:24:24309 },
Dan O'Reilly2621c8a2015-08-15 02:54:53310 install_requires=install_requires,
Tamir Duberstein21a7cf92015-04-12 01:24:24311 ext_modules=ext_module_list,
Adam Cozzette3afc8282021-10-08 23:45:26312 python_requires=">=3.5",
Tamir Duberstein21a7cf92015-04-12 01:24:24313 )