1#! /usr/bin/env python 2# 3# See README for usage instructions. 4from distutils import util 5import glob 6import os 7import pkg_resources 8import re 9import subprocess 10import sys 11import sysconfig 12import platform 13 14# We must use setuptools, not distutils, because we need to use the 15# namespace_packages option for the "google" package. 16from setuptools import setup, Extension, find_packages 17 18from distutils.command.build_py import build_py as _build_py 19from distutils.command.clean import clean as _clean 20from distutils.spawn import find_executable 21 22# Find the Protocol Compiler. 23if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']): 24 protoc = os.environ['PROTOC'] 25elif os.path.exists("../src/protoc"): 26 protoc = "../src/protoc" 27elif os.path.exists("../src/protoc.exe"): 28 protoc = "../src/protoc.exe" 29elif os.path.exists("../vsprojects/Debug/protoc.exe"): 30 protoc = "../vsprojects/Debug/protoc.exe" 31elif os.path.exists("../vsprojects/Release/protoc.exe"): 32 protoc = "../vsprojects/Release/protoc.exe" 33else: 34 protoc = find_executable("protoc") 35 36 37def GetVersion(): 38 """Gets the version from google/protobuf/__init__.py 39 40 Do not import google.protobuf.__init__ directly, because an installed 41 protobuf library may be loaded instead.""" 42 43 with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file: 44 exec(version_file.read(), globals()) 45 global __version__ 46 return __version__ 47 48 49def generate_proto(source, require = True): 50 """Invokes the Protocol Compiler to generate a _pb2.py from the given 51 .proto file. Does nothing if the output already exists and is newer than 52 the input.""" 53 54 if not require and not os.path.exists(source): 55 return 56 57 output = source.replace(".proto", "_pb2.py").replace("../src/", "") 58 59 if (not os.path.exists(output) or 60 (os.path.exists(source) and 61 os.path.getmtime(source) > os.path.getmtime(output))): 62 print("Generating %s..." % output) 63 64 if not os.path.exists(source): 65 sys.stderr.write("Can't find required file: %s\n" % source) 66 sys.exit(-1) 67 68 if protoc is None: 69 sys.stderr.write( 70 "protoc is not installed nor found in ../src. Please compile it " 71 "or install the binary package.\n") 72 sys.exit(-1) 73 74 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ] 75 if subprocess.call(protoc_command) != 0: 76 sys.exit(-1) 77 78def GenerateUnittestProtos(): 79 generate_proto("../src/google/protobuf/any_test.proto", False) 80 generate_proto("../src/google/protobuf/map_proto2_unittest.proto", False) 81 generate_proto("../src/google/protobuf/map_unittest.proto", False) 82 generate_proto("../src/google/protobuf/test_messages_proto3.proto", False) 83 generate_proto("../src/google/protobuf/test_messages_proto2.proto", False) 84 generate_proto("../src/google/protobuf/unittest_arena.proto", False) 85 generate_proto("../src/google/protobuf/unittest_no_arena.proto", False) 86 generate_proto("../src/google/protobuf/unittest_no_arena_import.proto", False) 87 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) 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) 97 generate_proto("google/protobuf/internal/any_test.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/pyext/python.proto", False) 114 115 116class clean(_clean): 117 def run(self): 118 # Delete generated files in the code tree. 119 for (dirpath, dirnames, filenames) in os.walk("."): 120 for filename in filenames: 121 filepath = os.path.join(dirpath, filename) 122 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \ 123 filepath.endswith(".so") or filepath.endswith(".o"): 124 os.remove(filepath) 125 # _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): 130 # Generate necessary .proto file if it doesn't exist. 131 generate_proto("../src/google/protobuf/descriptor.proto") 132 generate_proto("../src/google/protobuf/compiler/plugin.proto") 133 generate_proto("../src/google/protobuf/any.proto") 134 generate_proto("../src/google/protobuf/api.proto") 135 generate_proto("../src/google/protobuf/duration.proto") 136 generate_proto("../src/google/protobuf/empty.proto") 137 generate_proto("../src/google/protobuf/field_mask.proto") 138 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") 143 GenerateUnittestProtos() 144 145 # _build_py is an old-style class, so super() doesn't work. 146 _build_py.run(self) 147 148class test_conformance(_build_py): 149 target = 'test_python' 150 def run(self): 151 # Python 2.6 dodges these extra failures. 152 os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = ( 153 "--failure_list failure_list_python-post26.txt") 154 cmd = 'cd ../conformance && make %s' % (test_conformance.target) 155 status = subprocess.check_call(cmd, shell=True) 156 157 158def get_option_from_sys_argv(option_str): 159 if option_str in sys.argv: 160 sys.argv.remove(option_str) 161 return True 162 return False 163 164 165if __name__ == '__main__': 166 ext_module_list = [] 167 warnings_as_errors = '--warnings_as_errors' 168 if get_option_from_sys_argv('--cpp_implementation'): 169 # Link libprotobuf.a and libprotobuf-lite.a statically with the 170 # extension. Note that those libraries have to be compiled with 171 # -fPIC for this to work. 172 compile_static_ext = get_option_from_sys_argv('--compile_static_extension') 173 libraries = ['protobuf'] 174 extra_objects = None 175 if compile_static_ext: 176 libraries = None 177 extra_objects = ['../src/.libs/libprotobuf.a', 178 '../src/.libs/libprotobuf-lite.a'] 179 test_conformance.target = 'test_python_cpp' 180 181 extra_compile_args = [] 182 183 if sys.platform != 'win32': 184 extra_compile_args.append('-Wno-write-strings') 185 extra_compile_args.append('-Wno-invalid-offsetof') 186 extra_compile_args.append('-Wno-sign-compare') 187 extra_compile_args.append('-Wno-unused-variable') 188 extra_compile_args.append('-std=c++11') 189 190 if sys.platform == 'darwin': 191 extra_compile_args.append("-Wno-shorten-64-to-32"); 192 extra_compile_args.append("-Wno-deprecated-register"); 193 194 # https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes 195 # C++ projects must now migrate to libc++ and are recommended to set a 196 # deployment target of macOS 10.9 or later, or iOS 7 or later. 197 if sys.platform == 'darwin': 198 mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') 199 if mac_target and (pkg_resources.parse_version(mac_target) < 200 pkg_resources.parse_version('10.9.0')): 201 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9' 202 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub( 203 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1', 204 util.get_platform()) 205 206 # https://github.com/Theano/Theano/issues/4926 207 if sys.platform == 'win32': 208 extra_compile_args.append('-D_hypot=hypot') 209 210 # https://github.com/tpaviot/pythonocc-core/issues/48 211 if sys.platform == 'win32' and '64 bit' in sys.version: 212 extra_compile_args.append('-DMS_WIN64') 213 214 # MSVS default is dymanic 215 if (sys.platform == 'win32'): 216 extra_compile_args.append('/MT') 217 218 if "clang" in os.popen('$CC --version 2> /dev/null').read(): 219 extra_compile_args.append('-Wno-shorten-64-to-32') 220 221 if warnings_as_errors in sys.argv: 222 extra_compile_args.append('-Werror') 223 sys.argv.remove(warnings_as_errors) 224 225 # C++ implementation extension 226 ext_module_list.extend([ 227 Extension( 228 "google.protobuf.pyext._message", 229 glob.glob('google/protobuf/pyext/*.cc'), 230 include_dirs=[".", "../src"], 231 libraries=libraries, 232 extra_objects=extra_objects, 233 library_dirs=['../src/.libs'], 234 extra_compile_args=extra_compile_args, 235 ), 236 Extension( 237 "google.protobuf.internal._api_implementation", 238 glob.glob('google/protobuf/internal/api_implementation.cc'), 239 extra_compile_args=extra_compile_args + ['-DPYTHON_PROTO2_CPP_IMPL_V2'], 240 ), 241 ]) 242 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' 243 244 # Keep this list of dependencies in sync with tox.ini. 245 install_requires = ['six>=1.9', 'setuptools'] 246 if sys.version_info <= (2,7): 247 install_requires.append('ordereddict') 248 install_requires.append('unittest2') 249 250 setup( 251 name='protobuf', 252 version=GetVersion(), 253 description='Protocol Buffers', 254 download_url='https://github.com/protocolbuffers/protobuf/releases', 255 long_description="Protocol Buffers are Google's data interchange format", 256 url='https://developers.google.com/protocol-buffers/', 257 maintainer='protobuf@googlegroups.com', 258 maintainer_email='protobuf@googlegroups.com', 259 license='3-Clause BSD License', 260 classifiers=[ 261 "Programming Language :: Python", 262 "Programming Language :: Python :: 2", 263 "Programming Language :: Python :: 2.7", 264 "Programming Language :: Python :: 3", 265 "Programming Language :: Python :: 3.3", 266 "Programming Language :: Python :: 3.4", 267 "Programming Language :: Python :: 3.5", 268 "Programming Language :: Python :: 3.6", 269 "Programming Language :: Python :: 3.7", 270 ], 271 namespace_packages=['google'], 272 packages=find_packages( 273 exclude=[ 274 'import_test_package', 275 ], 276 ), 277 test_suite='google.protobuf.internal', 278 cmdclass={ 279 'clean': clean, 280 'build_py': build_py, 281 'test_conformance': test_conformance, 282 }, 283 install_requires=install_requires, 284 ext_modules=ext_module_list, 285 ) 286