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.proto", False) 86 generate_proto("../src/google/protobuf/unittest_custom_options.proto", False) 87 generate_proto("../src/google/protobuf/unittest_import.proto", False) 88 generate_proto("../src/google/protobuf/unittest_import_public.proto", False) 89 generate_proto("../src/google/protobuf/unittest_mset.proto", False) 90 generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False) 91 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False) 92 generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False) 93 generate_proto("../src/google/protobuf/util/json_format.proto", False) 94 generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False) 95 generate_proto("google/protobuf/internal/any_test.proto", False) 96 generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False) 97 generate_proto("google/protobuf/internal/descriptor_pool_test2.proto", False) 98 generate_proto("google/protobuf/internal/factory_test1.proto", False) 99 generate_proto("google/protobuf/internal/factory_test2.proto", False) 100 generate_proto("google/protobuf/internal/file_options_test.proto", False) 101 generate_proto("google/protobuf/internal/import_test_package/inner.proto", False) 102 generate_proto("google/protobuf/internal/import_test_package/outer.proto", False) 103 generate_proto("google/protobuf/internal/missing_enum_values.proto", False) 104 generate_proto("google/protobuf/internal/message_set_extensions.proto", False) 105 generate_proto("google/protobuf/internal/more_extensions.proto", False) 106 generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False) 107 generate_proto("google/protobuf/internal/more_messages.proto", False) 108 generate_proto("google/protobuf/internal/no_package.proto", False) 109 generate_proto("google/protobuf/internal/packed_field_test.proto", False) 110 generate_proto("google/protobuf/internal/test_bad_identifiers.proto", False) 111 generate_proto("google/protobuf/internal/test_proto3_optional.proto", False) 112 generate_proto("google/protobuf/pyext/python.proto", False) 113 114 115class clean(_clean): 116 def run(self): 117 # Delete generated files in the code tree. 118 for (dirpath, dirnames, filenames) in os.walk("."): 119 for filename in filenames: 120 filepath = os.path.join(dirpath, filename) 121 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \ 122 filepath.endswith(".so") or filepath.endswith(".o"): 123 os.remove(filepath) 124 # _clean is an old-style class, so super() doesn't work. 125 _clean.run(self) 126 127class build_py(_build_py): 128 def run(self): 129 # Generate necessary .proto file if it doesn't exist. 130 generate_proto("../src/google/protobuf/descriptor.proto") 131 generate_proto("../src/google/protobuf/compiler/plugin.proto") 132 generate_proto("../src/google/protobuf/any.proto") 133 generate_proto("../src/google/protobuf/api.proto") 134 generate_proto("../src/google/protobuf/duration.proto") 135 generate_proto("../src/google/protobuf/empty.proto") 136 generate_proto("../src/google/protobuf/field_mask.proto") 137 generate_proto("../src/google/protobuf/source_context.proto") 138 generate_proto("../src/google/protobuf/struct.proto") 139 generate_proto("../src/google/protobuf/timestamp.proto") 140 generate_proto("../src/google/protobuf/type.proto") 141 generate_proto("../src/google/protobuf/wrappers.proto") 142 GenerateUnittestProtos() 143 144 # _build_py is an old-style class, so super() doesn't work. 145 _build_py.run(self) 146 147class test_conformance(_build_py): 148 target = 'test_python' 149 def run(self): 150 # Python 2.6 dodges these extra failures. 151 os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = ( 152 "--failure_list failure_list_python-post26.txt") 153 cmd = 'cd ../conformance && make %s' % (test_conformance.target) 154 status = subprocess.check_call(cmd, shell=True) 155 156 157def get_option_from_sys_argv(option_str): 158 if option_str in sys.argv: 159 sys.argv.remove(option_str) 160 return True 161 return False 162 163 164if __name__ == '__main__': 165 ext_module_list = [] 166 warnings_as_errors = '--warnings_as_errors' 167 if get_option_from_sys_argv('--cpp_implementation'): 168 # Link libprotobuf.a and libprotobuf-lite.a statically with the 169 # extension. Note that those libraries have to be compiled with 170 # -fPIC for this to work. 171 compile_static_ext = get_option_from_sys_argv('--compile_static_extension') 172 libraries = ['protobuf'] 173 extra_objects = None 174 if compile_static_ext: 175 libraries = None 176 extra_objects = ['../src/.libs/libprotobuf.a', 177 '../src/.libs/libprotobuf-lite.a'] 178 test_conformance.target = 'test_python_cpp' 179 180 extra_compile_args = [] 181 182 if sys.platform != 'win32': 183 extra_compile_args.append('-Wno-write-strings') 184 extra_compile_args.append('-Wno-invalid-offsetof') 185 extra_compile_args.append('-Wno-sign-compare') 186 extra_compile_args.append('-Wno-unused-variable') 187 extra_compile_args.append('-std=c++11') 188 189 if sys.platform == 'darwin': 190 extra_compile_args.append("-Wno-shorten-64-to-32"); 191 extra_compile_args.append("-Wno-deprecated-register"); 192 193 # https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes 194 # C++ projects must now migrate to libc++ and are recommended to set a 195 # deployment target of macOS 10.9 or later, or iOS 7 or later. 196 if sys.platform == 'darwin': 197 mac_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') 198 if mac_target and (pkg_resources.parse_version(mac_target) < 199 pkg_resources.parse_version('10.9.0')): 200 os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.9' 201 os.environ['_PYTHON_HOST_PLATFORM'] = re.sub( 202 r'macosx-[0-9]+\.[0-9]+-(.+)', r'macosx-10.9-\1', 203 util.get_platform()) 204 205 # https://github.com/Theano/Theano/issues/4926 206 if sys.platform == 'win32': 207 extra_compile_args.append('-D_hypot=hypot') 208 209 # https://github.com/tpaviot/pythonocc-core/issues/48 210 if sys.platform == 'win32' and '64 bit' in sys.version: 211 extra_compile_args.append('-DMS_WIN64') 212 213 # MSVS default is dymanic 214 if (sys.platform == 'win32'): 215 extra_compile_args.append('/MT') 216 217 if "clang" in os.popen('$CC --version 2> /dev/null').read(): 218 extra_compile_args.append('-Wno-shorten-64-to-32') 219 220 if warnings_as_errors in sys.argv: 221 extra_compile_args.append('-Werror') 222 sys.argv.remove(warnings_as_errors) 223 224 # C++ implementation extension 225 ext_module_list.extend([ 226 Extension( 227 "google.protobuf.pyext._message", 228 glob.glob('google/protobuf/pyext/*.cc'), 229 include_dirs=[".", "../src"], 230 libraries=libraries, 231 extra_objects=extra_objects, 232 library_dirs=['../src/.libs'], 233 extra_compile_args=extra_compile_args, 234 ), 235 Extension( 236 "google.protobuf.internal._api_implementation", 237 glob.glob('google/protobuf/internal/api_implementation.cc'), 238 extra_compile_args=extra_compile_args + ['-DPYTHON_PROTO2_CPP_IMPL_V2'], 239 ), 240 ]) 241 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' 242 243 # Keep this list of dependencies in sync with tox.ini. 244 install_requires = ['six>=1.9', 'setuptools'] 245 if sys.version_info <= (2,7): 246 install_requires.append('ordereddict') 247 install_requires.append('unittest2') 248 249 setup( 250 name='protobuf', 251 version=GetVersion(), 252 description='Protocol Buffers', 253 download_url='https://github.com/protocolbuffers/protobuf/releases', 254 long_description="Protocol Buffers are Google's data interchange format", 255 url='https://developers.google.com/protocol-buffers/', 256 maintainer='protobuf@googlegroups.com', 257 maintainer_email='protobuf@googlegroups.com', 258 license='3-Clause BSD License', 259 classifiers=[ 260 "Programming Language :: Python", 261 "Programming Language :: Python :: 2", 262 "Programming Language :: Python :: 2.7", 263 "Programming Language :: Python :: 3", 264 "Programming Language :: Python :: 3.3", 265 "Programming Language :: Python :: 3.4", 266 "Programming Language :: Python :: 3.5", 267 "Programming Language :: Python :: 3.6", 268 "Programming Language :: Python :: 3.7", 269 ], 270 namespace_packages=['google'], 271 packages=find_packages( 272 exclude=[ 273 'import_test_package', 274 ], 275 ), 276 test_suite='google.protobuf.internal', 277 cmdclass={ 278 'clean': clean, 279 'build_py': build_py, 280 'test_conformance': test_conformance, 281 }, 282 setup_requires = ['wheel'], 283 install_requires=install_requires, 284 ext_modules=ext_module_list, 285 ) 286