blob: 524a312f52a82aece757fbde1f1d88c8312c7b70 [file] [log] [blame]
Tamir Duberstein9d9d0b72015-04-11 20:23:45 -07001#! /usr/bin/env python
temporal40ee5512008-07-10 02:12:20 +00002#
3# See README for usage instructions.
Tamir Duberstein5018c432015-05-08 08:48:40 -04004import glob
temporal40ee5512008-07-10 02:12:20 +00005import os
kenton@google.coma6de64a2009-04-18 02:28:15 +00006import subprocess
Tamir Duberstein5018c432015-05-08 08:48:40 -04007import sys
temporal40ee5512008-07-10 02:12:20 +00008
liujisi@google.com9ced30c2012-08-01 06:22:19 +00009# We must use setuptools, not distutils, because we need to use the
10# namespace_packages option for the "google" package.
Dan O'Reilly3bdfb4b2015-08-20 13:51:26 -040011from setuptools import setup, Extension, find_packages
liujisi@google.com9ced30c2012-08-01 06:22:19 +000012
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070013from distutils.command.clean import clean as _clean
14
15if sys.version_info[0] == 3:
16 # Python 3
17 from distutils.command.build_py import build_py_2to3 as _build_py
18else:
19 # Python 2
20 from distutils.command.build_py import build_py as _build_py
21from distutils.spawn import find_executable
temporal40ee5512008-07-10 02:12:20 +000022
23# Find the Protocol Compiler.
liujisi@google.come34f1f62012-12-05 01:25:12 +000024if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
25 protoc = os.environ['PROTOC']
26elif os.path.exists("../src/protoc"):
temporal40ee5512008-07-10 02:12:20 +000027 protoc = "../src/protoc"
kenton@google.com4152d552009-04-22 03:20:21 +000028elif os.path.exists("../src/protoc.exe"):
29 protoc = "../src/protoc.exe"
kenton@google.com80aa23d2010-09-28 21:58:36 +000030elif os.path.exists("../vsprojects/Debug/protoc.exe"):
31 protoc = "../vsprojects/Debug/protoc.exe"
32elif os.path.exists("../vsprojects/Release/protoc.exe"):
33 protoc = "../vsprojects/Release/protoc.exe"
temporal40ee5512008-07-10 02:12:20 +000034else:
35 protoc = find_executable("protoc")
36
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070037
Jisi Liu4573e112015-03-04 16:45:13 -080038def GetVersion():
39 """Gets the version from google/protobuf/__init__.py
40
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070041 Do not import google.protobuf.__init__ directly, because an installed
42 protobuf library may be loaded instead."""
Jisi Liu4573e112015-03-04 16:45:13 -080043
Jisi Liu4573e112015-03-04 16:45:13 -080044 with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file:
Behzad Tabibian2bf92b32015-05-07 19:04:56 +020045 exec(version_file.read(), globals())
Jisi Liu4573e112015-03-04 16:45:13 -080046 return __version__
47
48
Feng Xiao8e142682015-05-26 00:11:09 -070049def generate_proto(source, require = True):
temporal40ee5512008-07-10 02:12:20 +000050 """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 Xiao8e142682015-05-26 00:11:09 -070054 if not require and not os.path.exists(source):
55 return
56
temporal40ee5512008-07-10 02:12:20 +000057 output = source.replace(".proto", "_pb2.py").replace("../src/", "")
58
temporal40ee5512008-07-10 02:12:20 +000059 if (not os.path.exists(output) or
60 (os.path.exists(source) and
61 os.path.getmtime(source) > os.path.getmtime(output))):
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070062 print("Generating %s..." % output)
temporal40ee5512008-07-10 02:12:20 +000063
liujisi@google.com9ced30c2012-08-01 06:22:19 +000064 if not os.path.exists(source):
65 sys.stderr.write("Can't find required file: %s\n" % source)
66 sys.exit(-1)
67
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070068 if protoc is None:
temporal40ee5512008-07-10 02:12:20 +000069 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.coma6de64a2009-04-18 02:28:15 +000074 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
75 if subprocess.call(protoc_command) != 0:
temporal40ee5512008-07-10 02:12:20 +000076 sys.exit(-1)
77
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000078def GenerateUnittestProtos():
Adam Cozzetted64a2d92016-06-29 15:23:27 -070079 generate_proto("../src/google/protobuf/any_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070080 generate_proto("../src/google/protobuf/map_unittest.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -070081 generate_proto("../src/google/protobuf/unittest_arena.proto", False)
82 generate_proto("../src/google/protobuf/unittest_no_arena.proto", False)
83 generate_proto("../src/google/protobuf/unittest_no_arena_import.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070084 generate_proto("../src/google/protobuf/unittest.proto", False)
85 generate_proto("../src/google/protobuf/unittest_custom_options.proto", False)
86 generate_proto("../src/google/protobuf/unittest_import.proto", False)
87 generate_proto("../src/google/protobuf/unittest_import_public.proto", False)
88 generate_proto("../src/google/protobuf/unittest_mset.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -070089 generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070090 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False)
91 generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False)
Jisi Liu46e8ff62015-10-05 11:59:43 -070092 generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False)
Feng Xiaoe841bac2015-12-11 17:09:20 -080093 generate_proto("google/protobuf/internal/any_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070094 generate_proto("google/protobuf/internal/descriptor_pool_test1.proto", False)
95 generate_proto("google/protobuf/internal/descriptor_pool_test2.proto", False)
96 generate_proto("google/protobuf/internal/factory_test1.proto", False)
97 generate_proto("google/protobuf/internal/factory_test2.proto", False)
Adam Cozzetted64a2d92016-06-29 15:23:27 -070098 generate_proto("google/protobuf/internal/file_options_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070099 generate_proto("google/protobuf/internal/import_test_package/inner.proto", False)
100 generate_proto("google/protobuf/internal/import_test_package/outer.proto", False)
101 generate_proto("google/protobuf/internal/missing_enum_values.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -0700102 generate_proto("google/protobuf/internal/message_set_extensions.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700103 generate_proto("google/protobuf/internal/more_extensions.proto", False)
104 generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False)
105 generate_proto("google/protobuf/internal/more_messages.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -0700106 generate_proto("google/protobuf/internal/packed_field_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700107 generate_proto("google/protobuf/internal/test_bad_identifiers.proto", False)
108 generate_proto("google/protobuf/pyext/python.proto", False)
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000109
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700110
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000111class clean(_clean):
112 def run(self):
113 # Delete generated files in the code tree.
temporal40ee5512008-07-10 02:12:20 +0000114 for (dirpath, dirnames, filenames) in os.walk("."):
115 for filename in filenames:
116 filepath = os.path.join(dirpath, filename)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000117 if filepath.endswith("_pb2.py") or filepath.endswith(".pyc") or \
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000118 filepath.endswith(".so") or filepath.endswith(".o") or \
Feng Xiaob7610f12015-12-22 14:36:04 -0800119 filepath.endswith('google/protobuf/compiler/__init__.py') or \
120 filepath.endswith('google/protobuf/util/__init__.py'):
temporal40ee5512008-07-10 02:12:20 +0000121 os.remove(filepath)
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000122 # _clean is an old-style class, so super() doesn't work.
123 _clean.run(self)
124
125class build_py(_build_py):
126 def run(self):
temporal40ee5512008-07-10 02:12:20 +0000127 # Generate necessary .proto file if it doesn't exist.
temporal40ee5512008-07-10 02:12:20 +0000128 generate_proto("../src/google/protobuf/descriptor.proto")
liujisi@google.com9b7f6c52010-12-08 03:45:27 +0000129 generate_proto("../src/google/protobuf/compiler/plugin.proto")
Jisi Liu7464f402015-10-06 14:20:26 -0700130 generate_proto("../src/google/protobuf/any.proto")
Jisi Liuf6fa5c72015-10-06 14:26:00 -0700131 generate_proto("../src/google/protobuf/api.proto")
132 generate_proto("../src/google/protobuf/duration.proto")
133 generate_proto("../src/google/protobuf/empty.proto")
Jisi Liu7464f402015-10-06 14:20:26 -0700134 generate_proto("../src/google/protobuf/field_mask.proto")
Jisi Liuf6fa5c72015-10-06 14:26:00 -0700135 generate_proto("../src/google/protobuf/source_context.proto")
136 generate_proto("../src/google/protobuf/struct.proto")
137 generate_proto("../src/google/protobuf/timestamp.proto")
138 generate_proto("../src/google/protobuf/type.proto")
139 generate_proto("../src/google/protobuf/wrappers.proto")
jieluo@google.combde4a322014-08-12 21:10:30 +0000140 GenerateUnittestProtos()
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000141
jieluo@google.combde4a322014-08-12 21:10:30 +0000142 # Make sure google.protobuf/** are valid packages.
Feng Xiaob7610f12015-12-22 14:36:04 -0800143 for path in ['', 'internal/', 'compiler/', 'pyext/', 'util/']:
jieluo@google.combde4a322014-08-12 21:10:30 +0000144 try:
145 open('google/protobuf/%s__init__.py' % path, 'a').close()
146 except EnvironmentError:
147 pass
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000148 # _build_py is an old-style class, so super() doesn't work.
149 _build_py.run(self)
jieluo@google.combde4a322014-08-12 21:10:30 +0000150
Josh Haberman325392d2015-08-17 12:30:49 -0700151class test_conformance(_build_py):
152 target = 'test_python'
153 def run(self):
Josh Habermane891c292015-12-30 16:03:49 -0800154 if sys.version_info >= (2, 7):
155 # Python 2.6 dodges these extra failures.
156 os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = (
157 "--failure_list failure_list_python-post26.txt")
Josh Haberman4b31ffa2015-12-03 12:54:54 -0800158 cmd = 'cd ../conformance && make %s' % (test_conformance.target)
159 status = subprocess.check_call(cmd, shell=True)
Josh Haberman325392d2015-08-17 12:30:49 -0700160
temporal40ee5512008-07-10 02:12:20 +0000161
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700162def get_option_from_sys_argv(option_str):
163 if option_str in sys.argv:
164 sys.argv.remove(option_str)
165 return True
166 return False
167
168
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000169if __name__ == '__main__':
jieluo@google.com1eba9d92014-08-25 20:17:53 +0000170 ext_module_list = []
Josh Haberman00700b72015-10-06 14:13:09 -0700171 warnings_as_errors = '--warnings_as_errors'
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700172 if get_option_from_sys_argv('--cpp_implementation'):
173 # Link libprotobuf.a and libprotobuf-lite.a statically with the
174 # extension. Note that those libraries have to be compiled with
175 # -fPIC for this to work.
176 compile_static_ext = get_option_from_sys_argv('--compile_static_extension')
Jisi Liu1bf446c2016-03-31 15:48:25 -0700177 extra_compile_args = ['-Wno-write-strings',
178 '-Wno-invalid-offsetof',
179 '-Wno-sign-compare']
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700180 libraries = ['protobuf']
181 extra_objects = None
182 if compile_static_ext:
183 libraries = None
184 extra_objects = ['../src/.libs/libprotobuf.a',
185 '../src/.libs/libprotobuf-lite.a']
Josh Haberman325392d2015-08-17 12:30:49 -0700186 test_conformance.target = 'test_python_cpp'
Josh Habermand8814ed2015-10-07 11:46:23 -0700187
Josh Habermane1abdf22015-12-22 11:13:03 -0800188 if "clang" in os.popen('$CC --version 2> /dev/null').read():
Josh Habermand8814ed2015-10-07 11:46:23 -0700189 extra_compile_args.append('-Wno-shorten-64-to-32')
Josh Haberman00700b72015-10-06 14:13:09 -0700190
191 if warnings_as_errors in sys.argv:
192 extra_compile_args.append('-Werror')
193 sys.argv.remove(warnings_as_errors)
194
jieluo@google.com1eba9d92014-08-25 20:17:53 +0000195 # C++ implementation extension
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700196 ext_module_list.extend([
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700197 Extension(
198 "google.protobuf.pyext._message",
Tamir Duberstein5018c432015-05-08 08:48:40 -0400199 glob.glob('google/protobuf/pyext/*.cc'),
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700200 include_dirs=[".", "../src"],
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700201 libraries=libraries,
202 extra_objects=extra_objects,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700203 library_dirs=['../src/.libs'],
Josh Haberman00700b72015-10-06 14:13:09 -0700204 extra_compile_args=extra_compile_args,
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700205 ),
206 Extension(
207 "google.protobuf.internal._api_implementation",
208 glob.glob('google/protobuf/internal/api_implementation.cc'),
209 extra_compile_args=['-DPYTHON_PROTO2_CPP_IMPL_V2'],
210 ),
211 ])
Jisi Liuada65562015-02-25 16:39:11 -0800212 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
liujisi@google.com33165fe2010-11-02 13:14:58 +0000213
Dan O'Reilly3791c802015-08-20 20:49:45 -0400214 # Keep this list of dependencies in sync with tox.ini.
Feng Xiao283c40c2015-12-29 14:36:46 -0800215 install_requires = ['six>=1.9', 'setuptools']
Dan O'Reilly2621c8a2015-08-14 22:54:53 -0400216 if sys.version_info <= (2,7):
217 install_requires.append('ordereddict')
218 install_requires.append('unittest2')
219
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700220 setup(
221 name='protobuf',
222 version=GetVersion(),
223 description='Protocol Buffers',
Jisi Liub3bb46c2016-05-17 10:46:54 -0700224 download_url='https://github.com/google/protobuf/releases',
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700225 long_description="Protocol Buffers are Google's data interchange format",
226 url='https://developers.google.com/protocol-buffers/',
227 maintainer='protobuf@googlegroups.com',
228 maintainer_email='protobuf@googlegroups.com',
229 license='New BSD License',
230 classifiers=[
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400231 "Programming Language :: Python",
232 "Programming Language :: Python :: 2",
233 "Programming Language :: Python :: 2.6",
234 "Programming Language :: Python :: 2.7",
235 "Programming Language :: Python :: 3",
236 "Programming Language :: Python :: 3.3",
237 "Programming Language :: Python :: 3.4",
238 ],
Craig Citro0e7c0c22016-03-04 20:40:24 -0800239 namespace_packages=['google'],
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700240 packages=find_packages(
241 exclude=[
242 'import_test_package',
243 ],
244 ),
245 test_suite='google.protobuf.internal',
246 cmdclass={
247 'clean': clean,
248 'build_py': build_py,
Josh Haberman325392d2015-08-17 12:30:49 -0700249 'test_conformance': test_conformance,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700250 },
Dan O'Reilly2621c8a2015-08-14 22:54:53 -0400251 install_requires=install_requires,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700252 ext_modules=ext_module_list,
253 )