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', 119 False) 120 GenProto( 121 'google/protobuf/internal/import_test_package/import_public_nested.proto', 122 False) 123 GenProto('google/protobuf/internal/import_test_package/inner.proto', False) 124 GenProto('google/protobuf/internal/import_test_package/outer.proto', False) 125 GenProto('google/protobuf/internal/missing_enum_values.proto', False) 126 GenProto('google/protobuf/internal/message_set_extensions.proto', False) 127 GenProto('google/protobuf/internal/more_extensions.proto', False) 128 GenProto('google/protobuf/internal/more_extensions_dynamic.proto', False) 129 GenProto('google/protobuf/internal/more_messages.proto', False) 130 GenProto('google/protobuf/internal/no_package.proto', False) 131 GenProto('google/protobuf/internal/packed_field_test.proto', False) 132 GenProto('google/protobuf/internal/test_bad_identifiers.proto', False) 133 GenProto('google/protobuf/internal/test_proto3_optional.proto', False) 134 GenProto('google/protobuf/pyext/python.proto', False) 135 136 137class CleanCmd(_clean): 138 """Custom clean command for building the protobuf extension.""" 139 140 def run(self): 141 # Delete generated files in the code tree. 142 for (dirpath, unused_dirnames, filenames) in os.walk('.'): 143 for filename in filenames: 144 filepath = os.path.join(dirpath, filename) 145 if (filepath.endswith('_pb2.py') or filepath.endswith('.pyc') or 146 filepath.endswith('.so') or filepath.endswith('.o')): 147 os.remove(filepath) 148 # _clean is an old-style class, so super() doesn't work. 149 _clean.run(self) 150 151 152class BuildPyCmd(_build_py): 153 """Custom build_py command for building the protobuf runtime.""" 154 155 def run(self): 156 # Generate necessary .proto file if it doesn't exist. 157 GenProto('../src/google/protobuf/descriptor.proto') 158 GenProto('../src/google/protobuf/compiler/plugin.proto') 159 GenProto('../src/google/protobuf/any.proto') 160 GenProto('../src/google/protobuf/api.proto') 161 GenProto('../src/google/protobuf/duration.proto') 162 GenProto('../src/google/protobuf/empty.proto') 163 GenProto('../src/google/protobuf/field_mask.proto') 164 GenProto('../src/google/protobuf/source_context.proto') 165 GenProto('../src/google/protobuf/struct.proto') 166 GenProto('../src/google/protobuf/timestamp.proto') 167 GenProto('../src/google/protobuf/type.proto') 168 GenProto('../src/google/protobuf/wrappers.proto') 169 GenerateUnittestProtos() 170 171 # _build_py is an old-style class, so super() doesn't work. 172 _build_py.run(self) 173 174 def find_package_modules(self, package, package_dir): 175 exclude = ( 176 '*test*', 177 'google/protobuf/internal/*_pb2.py', 178 'google/protobuf/internal/_parameterized.py', 179 'google/protobuf/pyext/python_pb2.py', 180 ) 181 modules = _build_py.find_package_modules(self, package, package_dir) 182 return [(pkg, mod, fil) for (pkg, mod, fil) in modules 183 if not any(fnmatch.fnmatchcase(fil, pat=pat) for pat in exclude)] 184 185 186class BuildExtCmd(_build_ext): 187 """Command class for building the protobuf Python extension.""" 188 189 def get_ext_filename(self, ext_name): 190 # since python3.5, python extensions' shared libraries use a suffix that 191 # corresponds to the value of sysconfig.get_config_var('EXT_SUFFIX') and 192 # contains info about the architecture the library targets. E.g. on x64 193 # linux the suffix is ".cpython-XYZ-x86_64-linux-gnu.so" When 194 # crosscompiling python wheels, we need to be able to override this 195 # suffix so that the resulting file name matches the target architecture 196 # and we end up with a well-formed wheel. 197 filename = _build_ext.get_ext_filename(self, ext_name) 198 orig_ext_suffix = sysconfig.get_config_var('EXT_SUFFIX') 199 new_ext_suffix = os.getenv('PROTOCOL_BUFFERS_OVERRIDE_EXT_SUFFIX') 200 if new_ext_suffix and filename.endswith(orig_ext_suffix): 201 filename = filename[:-len(orig_ext_suffix)] + new_ext_suffix 202 return filename 203 204 205class TestConformanceCmd(_build_py): 206 target = 'test_python' 207 208 def run(self): 209 # Python 2.6 dodges these extra failures. 210 os.environ['CONFORMANCE_PYTHON_EXTRA_FAILURES'] = ( 211 '--failure_list failure_list_python-post26.txt') 212 cmd = 'cd ../conformance && make %s' % (TestConformanceCmd.target) 213 subprocess.check_call(cmd, shell=True) 214 215 216def GetOptionFromArgv(option_str): 217 if option_str in sys.argv: 218 sys.argv.remove(option_str) 219 return True 220 return False 221 222 223if __name__ == '__main__': 224 ext_module_list = [] 225 warnings_as_errors = '--warnings_as_errors' 226 if GetOptionFromArgv('--cpp_implementation'): 227 # Link libprotobuf.a and libprotobuf-lite.a statically with the 228 # extension. Note that those libraries have to be compiled with 229 # -fPIC for this to work. 230 compile_static_ext = GetOptionFromArgv('--compile_static_extension') 231 libraries = ['protobuf'] 232 extra_objects = None 233 if compile_static_ext: 234 libraries = None 235 extra_objects = ['../src/.libs/libprotobuf.a', 236 '../src/.libs/libprotobuf-lite.a'] 237 TestConformanceCmd.target = 'test_python_cpp' 238 239 extra_compile_args = [] 240 241 message_extra_link_args = None 242 api_implementation_link_args = None 243 if 'darwin' in sys.platform: 244 if sys.version_info[0] == 2: 245 message_init_symbol = 'init_message' 246 api_implementation_init_symbol = 'init_api_implementation' 247 else: 248 message_init_symbol = 'PyInit__message' 249 api_implementation_init_symbol = 'PyInit__api_implementation' 250 message_extra_link_args = [ 251 '-Wl,-exported_symbol,_%s' % message_init_symbol 252 ] 253 api_implementation_link_args = [ 254 '-Wl,-exported_symbol,_%s' % api_implementation_init_symbol 255 ] 256 257 if sys.platform != 'win32': 258 extra_compile_args.append('-Wno-write-strings') 259 extra_compile_args.append('-Wno-invalid-offsetof') 260 extra_compile_args.append('-Wno-sign-compare') 261 extra_compile_args.append('-Wno-unused-variable') 262 extra_compile_args.append('-std=c++11') 263 264 if sys.platform == 'darwin': 265 extra_compile_args.append('-Wno-shorten-64-to-32') 266 extra_compile_args.append('-Wno-deprecated-register') 267 268 # https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes 269 # C++ projects must now migrate to libc++ and are recommended to set a 270 # deployment target of macOS 10.9 or later, or iOS 7 or later. 271 if sys.platform == 'darwin': 272 mac_target = str(sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')) 273 if mac_target and (pkg_resources.parse_version(mac_target) < 274 pkg_resources.parse_version('10.9.0')): 275 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9' 276 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub( 277 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1', 278 util.get_platform()) 279 280 # https://github.com/Theano/Theano/issues/4926 281 if sys.platform == 'win32': 282 extra_compile_args.append('-D_hypot=hypot') 283 284 # https://github.com/tpaviot/pythonocc-core/issues/48 285 if sys.platform == 'win32' and '64 bit' in sys.version: 286 extra_compile_args.append('-DMS_WIN64') 287 288 # MSVS default is dymanic 289 if sys.platform == 'win32': 290 extra_compile_args.append('/MT') 291 292 if 'clang' in os.popen('$CC --version 2> /dev/null').read(): 293 extra_compile_args.append('-Wno-shorten-64-to-32') 294 295 if warnings_as_errors in sys.argv: 296 extra_compile_args.append('-Werror') 297 sys.argv.remove(warnings_as_errors) 298 299 # C++ implementation extension 300 ext_module_list.extend([ 301 Extension( 302 'google.protobuf.pyext._message', 303 glob.glob('google/protobuf/pyext/*.cc'), 304 include_dirs=['.', '../src'], 305 libraries=libraries, 306 extra_objects=extra_objects, 307 extra_link_args=message_extra_link_args, 308 library_dirs=['../src/.libs'], 309 extra_compile_args=extra_compile_args, 310 ), 311 Extension( 312 'google.protobuf.internal._api_implementation', 313 glob.glob('google/protobuf/internal/api_implementation.cc'), 314 extra_compile_args=(extra_compile_args + 315 ['-DPYTHON_PROTO2_CPP_IMPL_V2']), 316 extra_link_args=api_implementation_link_args, 317 ), 318 ]) 319 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' 320 321 # Keep this list of dependencies in sync with tox.ini. 322 install_requires = [] 323 324 setup( 325 name='protobuf', 326 version=GetVersion(), 327 description='Protocol Buffers', 328 download_url='https://github.com/protocolbuffers/protobuf/releases', 329 long_description="Protocol Buffers are Google's data interchange format", 330 url='https://developers.google.com/protocol-buffers/', 331 maintainer='[email protected]', 332 maintainer_email='[email protected]', 333 license='BSD-3-Clause', 334 classifiers=[ 335 'Programming Language :: Python', 336 'Programming Language :: Python :: 3', 337 'Programming Language :: Python :: 3.7', 338 'Programming Language :: Python :: 3.8', 339 'Programming Language :: Python :: 3.9', 340 'Programming Language :: Python :: 3.10', 341 ], 342 namespace_packages=['google'], 343 packages=find_packages( 344 exclude=[ 345 'import_test_package', 346 'protobuf_distutils', 347 ],), 348 test_suite='google.protobuf.internal', 349 cmdclass={ 350 'clean': CleanCmd, 351 'build_py': BuildPyCmd, 352 'build_ext': BuildExtCmd, 353 'test_conformance': TestConformanceCmd, 354 }, 355 install_requires=install_requires, 356 ext_modules=ext_module_list, 357 python_requires='>=3.7', 358 ) 359