4 from distutils
import util
17 from setuptools
import setup, Extension, find_packages
19 from distutils.command.build_ext
import build_ext
as _build_ext
20 from distutils.command.build_py
import build_py
as _build_py
21 from distutils.command.clean
import clean
as _clean
22 from distutils.spawn
import find_executable
25 if 'PROTOC' in os.environ
and os.path.exists(os.environ[
'PROTOC']):
26 protoc = os.environ[
'PROTOC']
27 elif os.path.exists(
"../src/protoc"):
28 protoc =
"../src/protoc"
29 elif os.path.exists(
"../src/protoc.exe"):
30 protoc =
"../src/protoc.exe"
31 elif os.path.exists(
"../vsprojects/Debug/protoc.exe"):
32 protoc =
"../vsprojects/Debug/protoc.exe"
33 elif os.path.exists(
"../vsprojects/Release/protoc.exe"):
34 protoc =
"../vsprojects/Release/protoc.exe"
40 """Gets the version from google/protobuf/__init__.py
42 Do not import google.protobuf.__init__ directly, because an installed
43 protobuf library may be loaded instead."""
45 with open(os.path.join(
'google',
'protobuf',
'__init__.py'))
as version_file:
46 exec(version_file.read(), globals())
52 """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
56 if not require
and not os.path.exists(source):
59 output = source.replace(
".proto",
"_pb2.py").replace(
"../src/",
"")
61 if (
not os.path.exists(output)
or
62 (os.path.exists(source)
and
63 os.path.getmtime(source) > os.path.getmtime(output))):
64 print(
"Generating %s..." % output)
66 if not os.path.exists(source):
67 sys.stderr.write(
"Can't find required file: %s\n" % source)
72 "protoc is not installed nor found in ../src. Please compile it "
73 "or install the binary package.\n")
76 protoc_command = [ protoc,
"-I../src",
"-I.",
"--python_out=.", source ]
77 if subprocess.call(protoc_command) != 0:
82 generate_proto(
"../src/google/protobuf/map_proto2_unittest.proto",
False)
84 generate_proto(
"../src/google/protobuf/test_messages_proto3.proto",
False)
85 generate_proto(
"../src/google/protobuf/test_messages_proto2.proto",
False)
86 generate_proto(
"../src/google/protobuf/unittest_arena.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)
92 generate_proto(
"../src/google/protobuf/unittest_mset_wire_format.proto",
False)
93 generate_proto(
"../src/google/protobuf/unittest_no_generic_services.proto",
False)
94 generate_proto(
"../src/google/protobuf/unittest_proto3_arena.proto",
False)
95 generate_proto(
"../src/google/protobuf/util/json_format.proto",
False)
96 generate_proto(
"../src/google/protobuf/util/json_format_proto3.proto",
False)
98 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)
102 generate_proto(
"google/protobuf/internal/file_options_test.proto",
False)
103 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)
106 generate_proto(
"google/protobuf/internal/message_set_extensions.proto",
False)
107 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)
110 generate_proto(
"google/protobuf/internal/no_package.proto",
False)
111 generate_proto(
"google/protobuf/internal/packed_field_test.proto",
False)
112 generate_proto(
"google/protobuf/internal/test_bad_identifiers.proto",
False)
113 generate_proto(
"google/protobuf/internal/test_proto3_optional.proto",
False)
120 for (dirpath, dirnames, filenames)
in os.walk(
"."):
121 for filename
in filenames:
122 filepath = os.path.join(dirpath, filename)
123 if filepath.endswith(
"_pb2.py")
or filepath.endswith(
".pyc")
or \
124 filepath.endswith(
".so")
or filepath.endswith(
".o"):
152 "google/protobuf/internal/*_pb2.py",
153 "google/protobuf/internal/_parameterized.py",
154 "google/protobuf/pyext/python_pb2.py",
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)]
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
179 target =
'test_python'
182 os.environ[
"CONFORMANCE_PYTHON_EXTRA_FAILURES"] = (
183 "--failure_list failure_list_python-post26.txt")
184 cmd =
'cd ../conformance && make %s' % (test_conformance.target)
185 status = subprocess.check_call(cmd, shell=
True)
189 if option_str
in sys.argv:
190 sys.argv.remove(option_str)
195 if __name__ ==
'__main__':
197 warnings_as_errors =
'--warnings_as_errors'
203 libraries = [
'protobuf']
205 if compile_static_ext:
207 extra_objects = [
'../src/.libs/libprotobuf.a',
208 '../src/.libs/libprotobuf-lite.a']
209 test_conformance.target =
'test_python_cpp'
211 extra_compile_args = []
213 message_extra_link_args =
None
214 api_implementation_link_args =
None
215 if "darwin" in sys.platform:
216 if sys.version_info[0] == 2:
217 message_init_symbol =
'init_message'
218 api_implementation_init_symbol =
'init_api_implementation'
220 message_init_symbol =
'PyInit__message'
221 api_implementation_init_symbol =
'PyInit__api_implementation'
222 message_extra_link_args = [
'-Wl,-exported_symbol,_%s' % message_init_symbol]
223 api_implementation_link_args = [
'-Wl,-exported_symbol,_%s' % api_implementation_init_symbol]
225 if sys.platform !=
'win32':
226 extra_compile_args.append(
'-Wno-write-strings')
227 extra_compile_args.append(
'-Wno-invalid-offsetof')
228 extra_compile_args.append(
'-Wno-sign-compare')
229 extra_compile_args.append(
'-Wno-unused-variable')
230 extra_compile_args.append(
'-std=c++11')
232 if sys.platform ==
'darwin':
233 extra_compile_args.append(
"-Wno-shorten-64-to-32");
234 extra_compile_args.append(
"-Wno-deprecated-register");
239 if sys.platform ==
'darwin':
240 mac_target =
str(sysconfig.get_config_var(
'MACOSX_DEPLOYMENT_TARGET'))
241 if mac_target
and (pkg_resources.parse_version(mac_target) <
242 pkg_resources.parse_version(
'10.9.0')):
243 os.environ[
'MACOSX_DEPLOYMENT_TARGET'] =
'10.9'
244 os.environ[
'_PYTHON_HOST_PLATFORM'] = re.sub(
245 r'macosx-[0-9]+\.[0-9]+-(.+)',
r'macosx-10.9-\1',
249 if sys.platform ==
'win32':
250 extra_compile_args.append(
'-D_hypot=hypot')
253 if sys.platform ==
'win32' and '64 bit' in sys.version:
254 extra_compile_args.append(
'-DMS_WIN64')
257 if (sys.platform ==
'win32'):
258 extra_compile_args.append(
'/MT')
260 if "clang" in os.popen(
'$CC --version 2> /dev/null').
read():
261 extra_compile_args.append(
'-Wno-shorten-64-to-32')
263 if warnings_as_errors
in sys.argv:
264 extra_compile_args.append(
'-Werror')
265 sys.argv.remove(warnings_as_errors)
268 ext_module_list.extend([
270 "google.protobuf.pyext._message",
271 glob.glob(
'google/protobuf/pyext/*.cc'),
272 include_dirs=[
".",
"../src"],
274 extra_objects=extra_objects,
275 extra_link_args=message_extra_link_args,
276 library_dirs=[
'../src/.libs'],
277 extra_compile_args=extra_compile_args,
280 "google.protobuf.internal._api_implementation",
281 glob.glob(
'google/protobuf/internal/api_implementation.cc'),
282 extra_compile_args=extra_compile_args + [
'-DPYTHON_PROTO2_CPP_IMPL_V2'],
283 extra_link_args=api_implementation_link_args,
286 os.environ[
'PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] =
'cpp'
289 install_requires = []
294 description=
'Protocol Buffers',
295 download_url=
'https://github.com/protocolbuffers/protobuf/releases',
296 long_description=
"Protocol Buffers are Google's data interchange format",
297 url=
'https://developers.google.com/protocol-buffers/',
298 maintainer=
'protobuf@googlegroups.com',
299 maintainer_email=
'protobuf@googlegroups.com',
300 license=
'3-Clause BSD License',
302 "Programming Language :: Python",
303 "Programming Language :: Python :: 3",
304 "Programming Language :: Python :: 3.5",
305 "Programming Language :: Python :: 3.6",
306 "Programming Language :: Python :: 3.7",
307 "Programming Language :: Python :: 3.8",
308 "Programming Language :: Python :: 3.9",
309 "Programming Language :: Python :: 3.10",
311 namespace_packages=[
'google'],
312 packages=find_packages(
314 'import_test_package',
315 'protobuf_distutils',
317 test_suite=
'google.protobuf.internal',
320 'build_py': build_py,
321 'build_ext': build_ext,
322 'test_conformance': test_conformance,
324 install_requires=install_requires,
325 ext_modules=ext_module_list,
326 python_requires=
">=3.5",