• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python
2#
3# See README for usage instructions.
4
5# pylint:disable=missing-module-docstring
6# pylint:disable=g-bad-import-order
7from distutils import util
8import fnmatch
9import glob
10import os
11import pkg_resources
12import re
13import subprocess
14import sys
15import sysconfig
16
17# pylint:disable=g-importing-member
18# pylint:disable=g-multiple-import
19
20# We must use setuptools, not distutils, because we need to use the
21# namespace_packages option for the "google" package.
22from setuptools import setup, Extension, find_packages
23
24from distutils.command.build_ext import build_ext as _build_ext
25from distutils.command.build_py import build_py as _build_py
26from distutils.command.clean import clean as _clean
27from distutils.spawn import find_executable
28
29# Find the Protocol Compiler.
30if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
31  protoc = os.environ['PROTOC']
32elif os.path.exists('../src/protoc'):
33  protoc = '../src/protoc'
34elif os.path.exists('../src/protoc.exe'):
35  protoc = '../src/protoc.exe'
36elif os.path.exists('../vsprojects/Debug/protoc.exe'):
37  protoc = '../vsprojects/Debug/protoc.exe'
38elif os.path.exists('../vsprojects/Release/protoc.exe'):
39  protoc = '../vsprojects/Release/protoc.exe'
40else:
41  protoc = find_executable('protoc')
42
43
44def GetVersion():
45  """Reads and returns the version from google/protobuf/__init__.py.
46
47  Do not import google.protobuf.__init__ directly, because an installed
48  protobuf library may be loaded instead.
49
50  Returns:
51      The version.
52  """
53
54  with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file:
55    exec(version_file.read(), globals())  # pylint:disable=exec-used
56    return __version__  # pylint:disable=undefined-variable
57
58
59def GenProto(source, require=True):
60  """Generates a _pb2.py from the given .proto file.
61
62  Does nothing if the output already exists and is newer than the input.
63
64  Args:
65      source: the .proto file path.
66      require: if True, exit immediately when a path is not found.
67  """
68
69  if not require and not os.path.exists(source):
70    return
71
72  output = source.replace('.proto', '_pb2.py').replace('../src/', '')
73
74  if (not os.path.exists(output) or
75      (os.path.exists(source) and
76       os.path.getmtime(source) > os.path.getmtime(output))):
77    print('Generating %s...' % output)
78
79    if not os.path.exists(source):
80      sys.stderr.write("Can't find required file: %s\n" % source)
81      sys.exit(-1)
82
83    if protoc is None:
84      sys.stderr.write(
85          'protoc is not installed nor found in ../src.  Please compile it '
86          'or install the binary package.\n')
87      sys.exit(-1)
88
89    protoc_command = [protoc, '-I../src', '-I.', '--python_out=.', source]
90    if subprocess.call(protoc_command) != 0:
91      sys.exit(-1)
92
93
94def GenerateUnittestProtos():
95  """Generates protobuf code for unittests."""
96  GenProto('../src/google/protobuf/any_test.proto', False)
97  GenProto('../src/google/protobuf/map_proto2_unittest.proto', False)
98  GenProto('../src/google/protobuf/map_unittest.proto', False)
99  GenProto('../src/google/protobuf/test_messages_proto3.proto', False)
100  GenProto('../src/google/protobuf/test_messages_proto2.proto', False)
101  GenProto('../src/google/protobuf/unittest_arena.proto', False)
102  GenProto('../src/google/protobuf/unittest.proto', False)
103  GenProto('../src/google/protobuf/unittest_custom_options.proto', False)
104  GenProto('../src/google/protobuf/unittest_import.proto', False)
105  GenProto('../src/google/protobuf/unittest_import_public.proto', False)
106  GenProto('../src/google/protobuf/unittest_mset.proto', False)
107  GenProto('../src/google/protobuf/unittest_mset_wire_format.proto', False)
108  GenProto('../src/google/protobuf/unittest_no_generic_services.proto', False)
109  GenProto('../src/google/protobuf/unittest_proto3_arena.proto', False)
110  GenProto('../src/google/protobuf/util/json_format.proto', False)
111  GenProto('../src/google/protobuf/util/json_format_proto3.proto', False)
112  GenProto('google/protobuf/internal/any_test.proto', False)
113  GenProto('google/protobuf/internal/descriptor_pool_test1.proto', False)
114  GenProto('google/protobuf/internal/descriptor_pool_test2.proto', False)
115  GenProto('google/protobuf/internal/factory_test1.proto', False)
116  GenProto('google/protobuf/internal/factory_test2.proto', False)
117  GenProto('google/protobuf/internal/file_options_test.proto', False)
118  GenProto('google/protobuf/internal/import_test_package/import_public.proto', False)
119  GenProto('google/protobuf/internal/import_test_package/import_public_nested.proto', False)
120  GenProto('google/protobuf/internal/import_test_package/inner.proto', False)
121  GenProto('google/protobuf/internal/import_test_package/outer.proto', False)
122  GenProto('google/protobuf/internal/missing_enum_values.proto', False)
123  GenProto('google/protobuf/internal/message_set_extensions.proto', False)
124  GenProto('google/protobuf/internal/more_extensions.proto', False)
125  GenProto('google/protobuf/internal/more_extensions_dynamic.proto', False)
126  GenProto('google/protobuf/internal/more_messages.proto', False)
127  GenProto('google/protobuf/internal/no_package.proto', False)
128  GenProto('google/protobuf/internal/packed_field_test.proto', False)
129  GenProto('google/protobuf/internal/test_bad_identifiers.proto', False)
130  GenProto('google/protobuf/internal/test_proto3_optional.proto', False)
131  GenProto('google/protobuf/pyext/python.proto', False)
132
133
134class CleanCmd(_clean):
135  """Custom clean command for building the protobuf extension."""
136
137  def run(self):
138    # Delete generated files in the code tree.
139    for (dirpath, unused_dirnames, filenames) in os.walk('.'):
140      for filename in filenames:
141        filepath = os.path.join(dirpath, filename)
142        if (filepath.endswith('_pb2.py') or filepath.endswith('.pyc') or
143            filepath.endswith('.so') or filepath.endswith('.o')):
144          os.remove(filepath)
145    # _clean is an old-style class, so super() doesn't work.
146    _clean.run(self)
147
148
149class BuildPyCmd(_build_py):
150  """Custom build_py command for building the protobuf runtime."""
151
152  def run(self):
153    # Generate necessary .proto file if it doesn't exist.
154    GenProto('../src/google/protobuf/descriptor.proto')
155    GenProto('../src/google/protobuf/compiler/plugin.proto')
156    GenProto('../src/google/protobuf/any.proto')
157    GenProto('../src/google/protobuf/api.proto')
158    GenProto('../src/google/protobuf/duration.proto')
159    GenProto('../src/google/protobuf/empty.proto')
160    GenProto('../src/google/protobuf/field_mask.proto')
161    GenProto('../src/google/protobuf/source_context.proto')
162    GenProto('../src/google/protobuf/struct.proto')
163    GenProto('../src/google/protobuf/timestamp.proto')
164    GenProto('../src/google/protobuf/type.proto')
165    GenProto('../src/google/protobuf/wrappers.proto')
166    GenerateUnittestProtos()
167
168    # _build_py is an old-style class, so super() doesn't work.
169    _build_py.run(self)
170
171  def find_package_modules(self, package, package_dir):
172    exclude = (
173        '*test*',
174        'google/protobuf/internal/*_pb2.py',
175        'google/protobuf/internal/_parameterized.py',
176        'google/protobuf/pyext/python_pb2.py',
177    )
178    modules = _build_py.find_package_modules(self, package, package_dir)
179    return [(pkg, mod, fil) for (pkg, mod, fil) in modules
180            if not any(fnmatch.fnmatchcase(fil, pat=pat) for pat in exclude)]
181
182
183class BuildExtCmd(_build_ext):
184  """Command class for building the protobuf Python extension."""
185
186  def get_ext_filename(self, ext_name):
187    # since python3.5, python extensions' shared libraries use a suffix that
188    # corresponds to the value of sysconfig.get_config_var('EXT_SUFFIX') and
189    # contains info about the architecture the library targets.  E.g. on x64
190    # linux the suffix is ".cpython-XYZ-x86_64-linux-gnu.so" When
191    # crosscompiling python wheels, we need to be able to override this
192    # suffix so that the resulting file name matches the target architecture
193    # and we end up with a well-formed wheel.
194    filename = _build_ext.get_ext_filename(self, ext_name)
195    orig_ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
196    new_ext_suffix = os.getenv('PROTOCOL_BUFFERS_OVERRIDE_EXT_SUFFIX')
197    if new_ext_suffix and filename.endswith(orig_ext_suffix):
198      filename = filename[:-len(orig_ext_suffix)] + new_ext_suffix
199    return filename
200
201
202class TestConformanceCmd(_build_py):
203  target = 'test_python'
204
205  def run(self):
206    # Python 2.6 dodges these extra failures.
207    os.environ['CONFORMANCE_PYTHON_EXTRA_FAILURES'] = (
208        '--failure_list failure_list_python-post26.txt')
209    cmd = 'cd ../conformance && make %s' % (TestConformanceCmd.target)
210    subprocess.check_call(cmd, shell=True)
211
212
213def GetOptionFromArgv(option_str):
214  if option_str in sys.argv:
215    sys.argv.remove(option_str)
216    return True
217  return False
218
219
220if __name__ == '__main__':
221  ext_module_list = []
222  warnings_as_errors = '--warnings_as_errors'
223  if GetOptionFromArgv('--cpp_implementation'):
224    # Link libprotobuf.a and libprotobuf-lite.a statically with the
225    # extension. Note that those libraries have to be compiled with
226    # -fPIC for this to work.
227    compile_static_ext = GetOptionFromArgv('--compile_static_extension')
228    libraries = ['protobuf']
229    extra_objects = None
230    if compile_static_ext:
231      libraries = None
232      extra_objects = ['../src/.libs/libprotobuf.a',
233                       '../src/.libs/libprotobuf-lite.a']
234    TestConformanceCmd.target = 'test_python_cpp'
235
236    extra_compile_args = []
237
238    message_extra_link_args = None
239    api_implementation_link_args = None
240    if 'darwin' in sys.platform:
241      if sys.version_info[0] == 2:
242        message_init_symbol = 'init_message'
243        api_implementation_init_symbol = 'init_api_implementation'
244      else:
245        message_init_symbol = 'PyInit__message'
246        api_implementation_init_symbol = 'PyInit__api_implementation'
247      message_extra_link_args = [
248          '-Wl,-exported_symbol,_%s' % message_init_symbol
249      ]
250      api_implementation_link_args = [
251          '-Wl,-exported_symbol,_%s' % api_implementation_init_symbol
252      ]
253
254    if sys.platform != 'win32':
255      extra_compile_args.append('-Wno-write-strings')
256      extra_compile_args.append('-Wno-invalid-offsetof')
257      extra_compile_args.append('-Wno-sign-compare')
258      extra_compile_args.append('-Wno-unused-variable')
259      extra_compile_args.append('-std=c++11')
260
261    if sys.platform == 'darwin':
262      extra_compile_args.append('-Wno-shorten-64-to-32')
263      extra_compile_args.append('-Wno-deprecated-register')
264
265    # https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes
266    # C++ projects must now migrate to libc++ and are recommended to set a
267    # deployment target of macOS 10.9 or later, or iOS 7 or later.
268    if sys.platform == 'darwin':
269      mac_target = str(sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET'))
270      if mac_target and (pkg_resources.parse_version(mac_target) <
271                         pkg_resources.parse_version('10.9.0')):
272        os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9'
273        os.environ['_PYTHON_HOST_PLATFORM'] = re.sub(
274            r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1',
275            util.get_platform())
276
277    # https://github.com/Theano/Theano/issues/4926
278    if sys.platform == 'win32':
279      extra_compile_args.append('-D_hypot=hypot')
280
281    # https://github.com/tpaviot/pythonocc-core/issues/48
282    if sys.platform == 'win32' and  '64 bit' in sys.version:
283      extra_compile_args.append('-DMS_WIN64')
284
285    # MSVS default is dymanic
286    if sys.platform == 'win32':
287      extra_compile_args.append('/MT')
288
289    if 'clang' in os.popen('$CC --version 2> /dev/null').read():
290      extra_compile_args.append('-Wno-shorten-64-to-32')
291
292    if warnings_as_errors in sys.argv:
293      extra_compile_args.append('-Werror')
294      sys.argv.remove(warnings_as_errors)
295
296    # C++ implementation extension
297    ext_module_list.extend([
298        Extension(
299            'google.protobuf.pyext._message',
300            glob.glob('google/protobuf/pyext/*.cc'),
301            include_dirs=['.', '../src'],
302            libraries=libraries,
303            extra_objects=extra_objects,
304            extra_link_args=message_extra_link_args,
305            library_dirs=['../src/.libs'],
306            extra_compile_args=extra_compile_args,
307        ),
308        Extension(
309            'google.protobuf.internal._api_implementation',
310            glob.glob('google/protobuf/internal/api_implementation.cc'),
311            extra_compile_args=(extra_compile_args +
312                                ['-DPYTHON_PROTO2_CPP_IMPL_V2']),
313            extra_link_args=api_implementation_link_args,
314        ),
315    ])
316    os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
317
318  # Keep this list of dependencies in sync with tox.ini.
319  install_requires = []
320
321  setup(
322      name='protobuf',
323      version=GetVersion(),
324      description='Protocol Buffers',
325      download_url='https://github.com/protocolbuffers/protobuf/releases',
326      long_description="Protocol Buffers are Google's data interchange format",
327      url='https://developers.google.com/protocol-buffers/',
328      maintainer='protobuf@googlegroups.com',
329      maintainer_email='protobuf@googlegroups.com',
330      license='BSD-3-Clause',
331      classifiers=[
332          'Programming Language :: Python',
333          'Programming Language :: Python :: 3',
334          'Programming Language :: Python :: 3.7',
335          'Programming Language :: Python :: 3.8',
336          'Programming Language :: Python :: 3.9',
337          'Programming Language :: Python :: 3.10',
338      ],
339      namespace_packages=['google'],
340      packages=find_packages(
341          exclude=[
342              'import_test_package',
343              'protobuf_distutils',
344          ],),
345      test_suite='google.protobuf.internal',
346      cmdclass={
347          'clean': CleanCmd,
348          'build_py': BuildPyCmd,
349          'build_ext': BuildExtCmd,
350          'test_conformance': TestConformanceCmd,
351      },
352      install_requires=install_requires,
353      ext_modules=ext_module_list,
354      python_requires='>=3.7',
355  )
356