Tamir Duberstein | 9d9d0b7 | 2015-04-11 20:23:45 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2 | # |
| 3 | # See README for usage instructions. |
Tamir Duberstein | 5018c43 | 2015-05-08 08:48:40 -0400 | [diff] [blame] | 4 | import glob |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 5 | import os |
kenton@google.com | a6de64a | 2009-04-18 02:28:15 +0000 | [diff] [blame] | 6 | import subprocess |
Tamir Duberstein | 5018c43 | 2015-05-08 08:48:40 -0400 | [diff] [blame] | 7 | import sys |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 9 | # We must use setuptools, not distutils, because we need to use the |
| 10 | # namespace_packages option for the "google" package. |
Dan O'Reilly | 3bdfb4b | 2015-08-20 13:51:26 -0400 | [diff] [blame] | 11 | from setuptools import setup, Extension, find_packages |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 12 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 13 | from distutils.command.clean import clean as _clean |
| 14 | |
| 15 | if sys.version_info[0] == 3: |
| 16 | # Python 3 |
| 17 | from distutils.command.build_py import build_py_2to3 as _build_py |
| 18 | else: |
| 19 | # Python 2 |
| 20 | from distutils.command.build_py import build_py as _build_py |
| 21 | from distutils.spawn import find_executable |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 22 | |
| 23 | # Find the Protocol Compiler. |
liujisi@google.com | e34f1f6 | 2012-12-05 01:25:12 +0000 | [diff] [blame] | 24 | if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']): |
| 25 | protoc = os.environ['PROTOC'] |
| 26 | elif os.path.exists("../src/protoc"): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 27 | protoc = "../src/protoc" |
kenton@google.com | 4152d55 | 2009-04-22 03:20:21 +0000 | [diff] [blame] | 28 | elif os.path.exists("../src/protoc.exe"): |
| 29 | protoc = "../src/protoc.exe" |
kenton@google.com | 80aa23d | 2010-09-28 21:58:36 +0000 | [diff] [blame] | 30 | elif os.path.exists("../vsprojects/Debug/protoc.exe"): |
| 31 | protoc = "../vsprojects/Debug/protoc.exe" |
| 32 | elif os.path.exists("../vsprojects/Release/protoc.exe"): |
| 33 | protoc = "../vsprojects/Release/protoc.exe" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 34 | else: |
| 35 | protoc = find_executable("protoc") |
| 36 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 37 | |
Jisi Liu | 4573e11 | 2015-03-04 16:45:13 -0800 | [diff] [blame] | 38 | def GetVersion(): |
| 39 | """Gets the version from google/protobuf/__init__.py |
| 40 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 41 | Do not import google.protobuf.__init__ directly, because an installed |
| 42 | protobuf library may be loaded instead.""" |
Jisi Liu | 4573e11 | 2015-03-04 16:45:13 -0800 | [diff] [blame] | 43 | |
Jisi Liu | 4573e11 | 2015-03-04 16:45:13 -0800 | [diff] [blame] | 44 | with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file: |
Behzad Tabibian | 2bf92b3 | 2015-05-07 19:04:56 +0200 | [diff] [blame] | 45 | exec(version_file.read(), globals()) |
Jisi Liu | 4573e11 | 2015-03-04 16:45:13 -0800 | [diff] [blame] | 46 | return __version__ |
| 47 | |
| 48 | |
Feng Xiao | 8e14268 | 2015-05-26 00:11:09 -0700 | [diff] [blame] | 49 | def generate_proto(source, require = True): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 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 | |
Feng Xiao | 8e14268 | 2015-05-26 00:11:09 -0700 | [diff] [blame] | 54 | if not require and not os.path.exists(source): |
| 55 | return |
| 56 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 57 | output = source.replace(".proto", "_pb2.py").replace("../src/", "") |
| 58 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 59 | if (not os.path.exists(output) or |
| 60 | (os.path.exists(source) and |
| 61 | os.path.getmtime(source) > os.path.getmtime(output))): |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 62 | print("Generating %s..." % output) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 63 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 64 | if not os.path.exists(source): |
| 65 | sys.stderr.write("Can't find required file: %s\n" % source) |
| 66 | sys.exit(-1) |
| 67 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 68 | if protoc is None: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 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 | |
kenton@google.com | a6de64a | 2009-04-18 02:28:15 +0000 | [diff] [blame] | 74 | protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ] |
| 75 | if subprocess.call(protoc_command) != 0: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 76 | sys.exit(-1) |
| 77 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 78 | def GenerateUnittestProtos(): |
Feng Xiao | 8e14268 | 2015-05-26 00:11:09 -0700 | [diff] [blame] | 79 | generate_proto("../src/google/protobuf/map_unittest.proto", False) |
Jisi Liu | b0f6611 | 2015-08-21 11:18:45 -0700 | [diff] [blame] | 80 | generate_proto("../src/google/protobuf/unittest_arena.proto", False) |
| 81 | generate_proto("../src/google/protobuf/unittest_no_arena.proto", False) |
| 82 | generate_proto("../src/google/protobuf/unittest_no_arena_import.proto", False) |
Feng Xiao | 8e14268 | 2015-05-26 00:11:09 -0700 | [diff] [blame] | 83 | generate_proto("../src/google/protobuf/unittest.proto", False) |
| 84 | generate_proto("../src/google/protobuf/unittest_custom_options.proto", False) |
| 85 | generate_proto("../src/google/protobuf/unittest_import.proto", False) |
| 86 | generate_proto("../src/google/protobuf/unittest_import_public.proto", False) |
| 87 | generate_proto("../src/google/protobuf/unittest_mset.proto", False) |
Jisi Liu | b0f6611 | 2015-08-21 11:18:45 -0700 | [diff] [blame] | 88 | generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False) |
Feng Xiao | 8e14268 | 2015-05-26 00:11:09 -0700 | [diff] [blame] | 89 | generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False) |
| 90 | generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False) |
Jisi Liu | 46e8ff6 | 2015-10-05 11:59:43 -0700 | [diff] [blame] | 91 | generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False) |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 92 | generate_proto("google/protobuf/internal/any_test.proto", False) |
Feng Xiao | 8e14268 | 2015-05-26 00:11:09 -0700 | [diff] [blame] | 93 | generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False) |
| 94 | generate_proto("google/protobuf/internal/descriptor_pool_test2.proto", False) |
| 95 | generate_proto("google/protobuf/internal/factory_test1.proto", False) |
| 96 | generate_proto("google/protobuf/internal/factory_test2.proto", False) |
| 97 | generate_proto("google/protobuf/internal/import_test_package/inner.proto", False) |
| 98 | generate_proto("google/protobuf/internal/import_test_package/outer.proto", False) |
| 99 | generate_proto("google/protobuf/internal/missing_enum_values.proto", False) |
Jisi Liu | b0f6611 | 2015-08-21 11:18:45 -0700 | [diff] [blame] | 100 | generate_proto("google/protobuf/internal/message_set_extensions.proto", False) |
Feng Xiao | 8e14268 | 2015-05-26 00:11:09 -0700 | [diff] [blame] | 101 | generate_proto("google/protobuf/internal/more_extensions.proto", False) |
| 102 | generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False) |
| 103 | generate_proto("google/protobuf/internal/more_messages.proto", False) |
Jisi Liu | b0f6611 | 2015-08-21 11:18:45 -0700 | [diff] [blame] | 104 | generate_proto("google/protobuf/internal/packed_field_test.proto", False) |
Feng Xiao | 8e14268 | 2015-05-26 00:11:09 -0700 | [diff] [blame] | 105 | generate_proto("google/protobuf/internal/test_bad_identifiers.proto", False) |
| 106 | generate_proto("google/protobuf/pyext/python.proto", False) |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 107 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 108 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 109 | class clean(_clean): |
| 110 | def run(self): |
| 111 | # Delete generated files in the code tree. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 112 | for (dirpath, dirnames, filenames) in os.walk("."): |
| 113 | for filename in filenames: |
| 114 | filepath = os.path.join(dirpath, filename) |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 115 | if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \ |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 116 | filepath.endswith(".so") or filepath.endswith(".o") or \ |
Feng Xiao | b7610f1 | 2015-12-22 14:36:04 -0800 | [diff] [blame] | 117 | filepath.endswith('google/protobuf/compiler/__init__.py') or \ |
| 118 | filepath.endswith('google/protobuf/util/__init__.py'): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 119 | os.remove(filepath) |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 120 | # _clean is an old-style class, so super() doesn't work. |
| 121 | _clean.run(self) |
| 122 | |
| 123 | class build_py(_build_py): |
| 124 | def run(self): |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 125 | # Generate necessary .proto file if it doesn't exist. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 126 | generate_proto("../src/google/protobuf/descriptor.proto") |
liujisi@google.com | 9b7f6c5 | 2010-12-08 03:45:27 +0000 | [diff] [blame] | 127 | generate_proto("../src/google/protobuf/compiler/plugin.proto") |
Jisi Liu | 7464f40 | 2015-10-06 14:20:26 -0700 | [diff] [blame] | 128 | generate_proto("../src/google/protobuf/any.proto") |
Jisi Liu | f6fa5c7 | 2015-10-06 14:26:00 -0700 | [diff] [blame] | 129 | generate_proto("../src/google/protobuf/api.proto") |
| 130 | generate_proto("../src/google/protobuf/duration.proto") |
| 131 | generate_proto("../src/google/protobuf/empty.proto") |
Jisi Liu | 7464f40 | 2015-10-06 14:20:26 -0700 | [diff] [blame] | 132 | generate_proto("../src/google/protobuf/field_mask.proto") |
Jisi Liu | f6fa5c7 | 2015-10-06 14:26:00 -0700 | [diff] [blame] | 133 | generate_proto("../src/google/protobuf/source_context.proto") |
| 134 | generate_proto("../src/google/protobuf/struct.proto") |
| 135 | generate_proto("../src/google/protobuf/timestamp.proto") |
| 136 | generate_proto("../src/google/protobuf/type.proto") |
| 137 | generate_proto("../src/google/protobuf/wrappers.proto") |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 138 | GenerateUnittestProtos() |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 139 | |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 140 | # Make sure google.protobuf/** are valid packages. |
Feng Xiao | b7610f1 | 2015-12-22 14:36:04 -0800 | [diff] [blame] | 141 | for path in ['', 'internal/', 'compiler/', 'pyext/', 'util/']: |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 142 | try: |
| 143 | open('google/protobuf/%s__init__.py' % path, 'a').close() |
| 144 | except EnvironmentError: |
| 145 | pass |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 146 | # _build_py is an old-style class, so super() doesn't work. |
| 147 | _build_py.run(self) |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 148 | |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 149 | class test_conformance(_build_py): |
| 150 | target = 'test_python' |
| 151 | def run(self): |
Josh Haberman | e891c29 | 2015-12-30 16:03:49 -0800 | [diff] [blame] | 152 | if sys.version_info >= (2, 7): |
| 153 | # Python 2.6 dodges these extra failures. |
| 154 | os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = ( |
| 155 | "--failure_list failure_list_python-post26.txt") |
Josh Haberman | 4b31ffa | 2015-12-03 12:54:54 -0800 | [diff] [blame] | 156 | cmd = 'cd ../conformance && make %s' % (test_conformance.target) |
| 157 | status = subprocess.check_call(cmd, shell=True) |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 158 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 159 | |
Manjunath Kudlur | cf828de | 2016-03-25 10:58:46 -0700 | [diff] [blame] | 160 | def get_option_from_sys_argv(option_str): |
| 161 | if option_str in sys.argv: |
| 162 | sys.argv.remove(option_str) |
| 163 | return True |
| 164 | return False |
| 165 | |
| 166 | |
liujisi@google.com | 9ced30c | 2012-08-01 06:22:19 +0000 | [diff] [blame] | 167 | if __name__ == '__main__': |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 168 | ext_module_list = [] |
Josh Haberman | 00700b7 | 2015-10-06 14:13:09 -0700 | [diff] [blame] | 169 | warnings_as_errors = '--warnings_as_errors' |
Manjunath Kudlur | cf828de | 2016-03-25 10:58:46 -0700 | [diff] [blame] | 170 | if get_option_from_sys_argv('--cpp_implementation'): |
| 171 | # Link libprotobuf.a and libprotobuf-lite.a statically with the |
| 172 | # extension. Note that those libraries have to be compiled with |
| 173 | # -fPIC for this to work. |
| 174 | compile_static_ext = get_option_from_sys_argv('--compile_static_extension') |
Jisi Liu | 1bf446c | 2016-03-31 15:48:25 -0700 | [diff] [blame] | 175 | extra_compile_args = ['-Wno-write-strings', |
| 176 | '-Wno-invalid-offsetof', |
| 177 | '-Wno-sign-compare'] |
Manjunath Kudlur | cf828de | 2016-03-25 10:58:46 -0700 | [diff] [blame] | 178 | libraries = ['protobuf'] |
| 179 | extra_objects = None |
| 180 | if compile_static_ext: |
| 181 | libraries = None |
| 182 | extra_objects = ['../src/.libs/libprotobuf.a', |
| 183 | '../src/.libs/libprotobuf-lite.a'] |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 184 | test_conformance.target = 'test_python_cpp' |
Josh Haberman | d8814ed | 2015-10-07 11:46:23 -0700 | [diff] [blame] | 185 | |
Josh Haberman | e1abdf2 | 2015-12-22 11:13:03 -0800 | [diff] [blame] | 186 | if "clang" in os.popen('$CC --version 2> /dev/null').read(): |
Josh Haberman | d8814ed | 2015-10-07 11:46:23 -0700 | [diff] [blame] | 187 | extra_compile_args.append('-Wno-shorten-64-to-32') |
Josh Haberman | 00700b7 | 2015-10-06 14:13:09 -0700 | [diff] [blame] | 188 | |
| 189 | if warnings_as_errors in sys.argv: |
| 190 | extra_compile_args.append('-Werror') |
| 191 | sys.argv.remove(warnings_as_errors) |
| 192 | |
jieluo@google.com | 1eba9d9 | 2014-08-25 20:17:53 +0000 | [diff] [blame] | 193 | # C++ implementation extension |
Manjunath Kudlur | cf828de | 2016-03-25 10:58:46 -0700 | [diff] [blame] | 194 | ext_module_list.extend([ |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 195 | Extension( |
| 196 | "google.protobuf.pyext._message", |
Tamir Duberstein | 5018c43 | 2015-05-08 08:48:40 -0400 | [diff] [blame] | 197 | glob.glob('google/protobuf/pyext/*.cc'), |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 198 | include_dirs=[".", "../src"], |
Manjunath Kudlur | cf828de | 2016-03-25 10:58:46 -0700 | [diff] [blame] | 199 | libraries=libraries, |
| 200 | extra_objects=extra_objects, |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 201 | library_dirs=['../src/.libs'], |
Josh Haberman | 00700b7 | 2015-10-06 14:13:09 -0700 | [diff] [blame] | 202 | extra_compile_args=extra_compile_args, |
Manjunath Kudlur | cf828de | 2016-03-25 10:58:46 -0700 | [diff] [blame] | 203 | ), |
| 204 | Extension( |
| 205 | "google.protobuf.internal._api_implementation", |
| 206 | glob.glob('google/protobuf/internal/api_implementation.cc'), |
| 207 | extra_compile_args=['-DPYTHON_PROTO2_CPP_IMPL_V2'], |
| 208 | ), |
| 209 | ]) |
Jisi Liu | ada6556 | 2015-02-25 16:39:11 -0800 | [diff] [blame] | 210 | os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 211 | |
Dan O'Reilly | 3791c80 | 2015-08-20 20:49:45 -0400 | [diff] [blame] | 212 | # Keep this list of dependencies in sync with tox.ini. |
Feng Xiao | 283c40c | 2015-12-29 14:36:46 -0800 | [diff] [blame] | 213 | install_requires = ['six>=1.9', 'setuptools'] |
Dan O'Reilly | 2621c8a | 2015-08-14 22:54:53 -0400 | [diff] [blame] | 214 | if sys.version_info <= (2,7): |
| 215 | install_requires.append('ordereddict') |
| 216 | install_requires.append('unittest2') |
| 217 | |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 218 | setup( |
| 219 | name='protobuf', |
| 220 | version=GetVersion(), |
| 221 | description='Protocol Buffers', |
| 222 | long_description="Protocol Buffers are Google's data interchange format", |
| 223 | url='https://developers.google.com/protocol-buffers/', |
| 224 | maintainer='protobuf@googlegroups.com', |
| 225 | maintainer_email='protobuf@googlegroups.com', |
| 226 | license='New BSD License', |
| 227 | classifiers=[ |
Dan O'Reilly | e47cdd5 | 2015-08-12 23:57:46 -0400 | [diff] [blame] | 228 | "Programming Language :: Python", |
| 229 | "Programming Language :: Python :: 2", |
| 230 | "Programming Language :: Python :: 2.6", |
| 231 | "Programming Language :: Python :: 2.7", |
| 232 | "Programming Language :: Python :: 3", |
| 233 | "Programming Language :: Python :: 3.3", |
| 234 | "Programming Language :: Python :: 3.4", |
| 235 | ], |
Craig Citro | 0e7c0c2 | 2016-03-04 20:40:24 -0800 | [diff] [blame] | 236 | namespace_packages=['google'], |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 237 | packages=find_packages( |
| 238 | exclude=[ |
| 239 | 'import_test_package', |
| 240 | ], |
| 241 | ), |
| 242 | test_suite='google.protobuf.internal', |
| 243 | cmdclass={ |
| 244 | 'clean': clean, |
| 245 | 'build_py': build_py, |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 246 | 'test_conformance': test_conformance, |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 247 | }, |
Dan O'Reilly | 2621c8a | 2015-08-14 22:54:53 -0400 | [diff] [blame] | 248 | install_requires=install_requires, |
Tamir Duberstein | 21a7cf9 | 2015-04-11 18:24:24 -0700 | [diff] [blame] | 249 | ext_modules=ext_module_list, |
| 250 | ) |