blob: c2769d04dcb63340e8ae0848032c766ed9e1f399 [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
Paul Yang7f3e2372017-01-31 09:17:32 -08008import platform
temporal40ee5512008-07-10 02:12:20 +00009
liujisi@google.com9ced30c2012-08-01 06:22:19 +000010# We must use setuptools, not distutils, because we need to use the
11# namespace_packages option for the "google" package.
Dan O'Reilly3bdfb4b2015-08-20 13:51:26 -040012from setuptools import setup, Extension, find_packages
liujisi@google.com9ced30c2012-08-01 06:22:19 +000013
Benjamin Peterson188c44b2018-09-10 13:35:38 -070014from distutils.command.build_py import build_py as _build_py
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070015from distutils.command.clean import clean as _clean
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070016from distutils.spawn import find_executable
temporal40ee5512008-07-10 02:12:20 +000017
18# Find the Protocol Compiler.
liujisi@google.come34f1f62012-12-05 01:25:12 +000019if 'PROTOC' in os.environ and os.path.exists(os.environ['PROTOC']):
20 protoc = os.environ['PROTOC']
21elif os.path.exists("../src/protoc"):
temporal40ee5512008-07-10 02:12:20 +000022 protoc = "../src/protoc"
kenton@google.com4152d552009-04-22 03:20:21 +000023elif os.path.exists("../src/protoc.exe"):
24 protoc = "../src/protoc.exe"
kenton@google.com80aa23d2010-09-28 21:58:36 +000025elif os.path.exists("../vsprojects/Debug/protoc.exe"):
26 protoc = "../vsprojects/Debug/protoc.exe"
27elif os.path.exists("../vsprojects/Release/protoc.exe"):
28 protoc = "../vsprojects/Release/protoc.exe"
temporal40ee5512008-07-10 02:12:20 +000029else:
30 protoc = find_executable("protoc")
31
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070032
Jisi Liu4573e112015-03-04 16:45:13 -080033def GetVersion():
34 """Gets the version from google/protobuf/__init__.py
35
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070036 Do not import google.protobuf.__init__ directly, because an installed
37 protobuf library may be loaded instead."""
Jisi Liu4573e112015-03-04 16:45:13 -080038
Jisi Liu4573e112015-03-04 16:45:13 -080039 with open(os.path.join('google', 'protobuf', '__init__.py')) as version_file:
Behzad Tabibian2bf92b32015-05-07 19:04:56 +020040 exec(version_file.read(), globals())
cclauss35567c12018-06-25 19:50:40 +020041 global __version__
Jisi Liu4573e112015-03-04 16:45:13 -080042 return __version__
43
44
Feng Xiao8e142682015-05-26 00:11:09 -070045def generate_proto(source, require = True):
temporal40ee5512008-07-10 02:12:20 +000046 """Invokes the Protocol Compiler to generate a _pb2.py from the given
47 .proto file. Does nothing if the output already exists and is newer than
48 the input."""
49
Feng Xiao8e142682015-05-26 00:11:09 -070050 if not require and not os.path.exists(source):
51 return
52
temporal40ee5512008-07-10 02:12:20 +000053 output = source.replace(".proto", "_pb2.py").replace("../src/", "")
54
temporal40ee5512008-07-10 02:12:20 +000055 if (not os.path.exists(output) or
56 (os.path.exists(source) and
57 os.path.getmtime(source) > os.path.getmtime(output))):
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070058 print("Generating %s..." % output)
temporal40ee5512008-07-10 02:12:20 +000059
liujisi@google.com9ced30c2012-08-01 06:22:19 +000060 if not os.path.exists(source):
61 sys.stderr.write("Can't find required file: %s\n" % source)
62 sys.exit(-1)
63
Tamir Duberstein21a7cf92015-04-11 18:24:24 -070064 if protoc is None:
temporal40ee5512008-07-10 02:12:20 +000065 sys.stderr.write(
66 "protoc is not installed nor found in ../src. Please compile it "
67 "or install the binary package.\n")
68 sys.exit(-1)
69
kenton@google.coma6de64a2009-04-18 02:28:15 +000070 protoc_command = [ protoc, "-I../src", "-I.", "--python_out=.", source ]
71 if subprocess.call(protoc_command) != 0:
temporal40ee5512008-07-10 02:12:20 +000072 sys.exit(-1)
73
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000074def GenerateUnittestProtos():
Adam Cozzetted64a2d92016-06-29 15:23:27 -070075 generate_proto("../src/google/protobuf/any_test.proto", False)
Adam Cozzette13fd0452017-09-12 10:32:01 -070076 generate_proto("../src/google/protobuf/map_proto2_unittest.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070077 generate_proto("../src/google/protobuf/map_unittest.proto", False)
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050078 generate_proto("../src/google/protobuf/test_messages_proto3.proto", False)
Yilun Chong18a0c2c2017-06-27 18:24:15 -070079 generate_proto("../src/google/protobuf/test_messages_proto2.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -070080 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 Xiao8e142682015-05-26 00:11:09 -070083 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 Liub0f66112015-08-21 11:18:45 -070088 generate_proto("../src/google/protobuf/unittest_mset_wire_format.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070089 generate_proto("../src/google/protobuf/unittest_no_generic_services.proto", False)
90 generate_proto("../src/google/protobuf/unittest_proto3_arena.proto", False)
Jisi Liu46e8ff62015-10-05 11:59:43 -070091 generate_proto("../src/google/protobuf/util/json_format_proto3.proto", False)
Feng Xiaoe841bac2015-12-11 17:09:20 -080092 generate_proto("google/protobuf/internal/any_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070093 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)
Adam Cozzetted64a2d92016-06-29 15:23:27 -070097 generate_proto("google/protobuf/internal/file_options_test.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -070098 generate_proto("google/protobuf/internal/import_test_package/inner.proto", False)
99 generate_proto("google/protobuf/internal/import_test_package/outer.proto", False)
100 generate_proto("google/protobuf/internal/missing_enum_values.proto", False)
Jisi Liub0f66112015-08-21 11:18:45 -0700101 generate_proto("google/protobuf/internal/message_set_extensions.proto", False)
Feng Xiao8e142682015-05-26 00:11:09 -0700102 generate_proto("google/protobuf/internal/more_extensions.proto", False)
103 generate_proto("google/protobuf/internal/more_extensions_dynamic.proto", False)
104 generate_proto("google/protobuf/internal/more_messages.proto", False)
Adam Cozzette837c94b2018-03-14 13:01:55 -0700105 generate_proto("google/protobuf/internal/no_package.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 \
Thomas Hisch451e0442018-04-09 21:43:10 +0200118 filepath.endswith(".so") or filepath.endswith(".o"):
temporal40ee5512008-07-10 02:12:20 +0000119 os.remove(filepath)
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000120 # _clean is an old-style class, so super() doesn't work.
121 _clean.run(self)
122
123class build_py(_build_py):
124 def run(self):
temporal40ee5512008-07-10 02:12:20 +0000125 # Generate necessary .proto file if it doesn't exist.
temporal40ee5512008-07-10 02:12:20 +0000126 generate_proto("../src/google/protobuf/descriptor.proto")
liujisi@google.com9b7f6c52010-12-08 03:45:27 +0000127 generate_proto("../src/google/protobuf/compiler/plugin.proto")
Jisi Liu7464f402015-10-06 14:20:26 -0700128 generate_proto("../src/google/protobuf/any.proto")
Jisi Liuf6fa5c72015-10-06 14:26:00 -0700129 generate_proto("../src/google/protobuf/api.proto")
130 generate_proto("../src/google/protobuf/duration.proto")
131 generate_proto("../src/google/protobuf/empty.proto")
Jisi Liu7464f402015-10-06 14:20:26 -0700132 generate_proto("../src/google/protobuf/field_mask.proto")
Jisi Liuf6fa5c72015-10-06 14:26:00 -0700133 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.combde4a322014-08-12 21:10:30 +0000138 GenerateUnittestProtos()
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000139
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000140 # _build_py is an old-style class, so super() doesn't work.
141 _build_py.run(self)
jieluo@google.combde4a322014-08-12 21:10:30 +0000142
Josh Haberman325392d2015-08-17 12:30:49 -0700143class test_conformance(_build_py):
144 target = 'test_python'
145 def run(self):
Yuchen Xie595231d2018-06-26 06:20:53 +0800146 # Python 2.6 dodges these extra failures.
147 os.environ["CONFORMANCE_PYTHON_EXTRA_FAILURES"] = (
148 "--failure_list failure_list_python-post26.txt")
Josh Haberman4b31ffa2015-12-03 12:54:54 -0800149 cmd = 'cd ../conformance && make %s' % (test_conformance.target)
150 status = subprocess.check_call(cmd, shell=True)
Josh Haberman325392d2015-08-17 12:30:49 -0700151
temporal40ee5512008-07-10 02:12:20 +0000152
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700153def get_option_from_sys_argv(option_str):
154 if option_str in sys.argv:
155 sys.argv.remove(option_str)
156 return True
157 return False
158
159
liujisi@google.com9ced30c2012-08-01 06:22:19 +0000160if __name__ == '__main__':
jieluo@google.com1eba9d92014-08-25 20:17:53 +0000161 ext_module_list = []
Josh Haberman00700b72015-10-06 14:13:09 -0700162 warnings_as_errors = '--warnings_as_errors'
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700163 if get_option_from_sys_argv('--cpp_implementation'):
164 # Link libprotobuf.a and libprotobuf-lite.a statically with the
165 # extension. Note that those libraries have to be compiled with
166 # -fPIC for this to work.
167 compile_static_ext = get_option_from_sys_argv('--compile_static_extension')
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700168 libraries = ['protobuf']
169 extra_objects = None
170 if compile_static_ext:
171 libraries = None
172 extra_objects = ['../src/.libs/libprotobuf.a',
173 '../src/.libs/libprotobuf-lite.a']
Josh Haberman325392d2015-08-17 12:30:49 -0700174 test_conformance.target = 'test_python_cpp'
Josh Habermand8814ed2015-10-07 11:46:23 -0700175
Paul Yangc9317432018-04-02 15:55:28 -0700176 extra_compile_args = []
177
178 if sys.platform != 'win32':
179 extra_compile_args.append('-Wno-write-strings')
180 extra_compile_args.append('-Wno-invalid-offsetof')
181 extra_compile_args.append('-Wno-sign-compare')
Feng Xiaoacd5b052018-08-09 21:21:01 -0700182 extra_compile_args.append('-Wno-unused-variable')
Arun Olappamanna Vasudevanc7e0e262018-07-23 13:35:15 -0700183 extra_compile_args.append('-std=c++11')
Paul Yangc9317432018-04-02 15:55:28 -0700184
Feng Xiaoacd5b052018-08-09 21:21:01 -0700185 if sys.platform == 'darwin':
186 extra_compile_args.append("-Wno-shorten-64-to-32");
187
Paul Yangc9317432018-04-02 15:55:28 -0700188 # https://github.com/Theano/Theano/issues/4926
189 if sys.platform == 'win32':
190 extra_compile_args.append('-D_hypot=hypot')
191
192 # https://github.com/tpaviot/pythonocc-core/issues/48
193 if sys.platform == 'win32' and '64 bit' in sys.version:
194 extra_compile_args.append('-DMS_WIN64')
195
196 # MSVS default is dymanic
Paul Yang3b6d0272018-04-06 15:43:32 -0700197 if (sys.platform == 'win32'):
Paul Yangc9317432018-04-02 15:55:28 -0700198 extra_compile_args.append('/MT')
199
Josh Habermane1abdf22015-12-22 11:13:03 -0800200 if "clang" in os.popen('$CC --version 2> /dev/null').read():
Josh Habermand8814ed2015-10-07 11:46:23 -0700201 extra_compile_args.append('-Wno-shorten-64-to-32')
Josh Haberman00700b72015-10-06 14:13:09 -0700202
203 if warnings_as_errors in sys.argv:
204 extra_compile_args.append('-Werror')
205 sys.argv.remove(warnings_as_errors)
206
jieluo@google.com1eba9d92014-08-25 20:17:53 +0000207 # C++ implementation extension
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700208 ext_module_list.extend([
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700209 Extension(
210 "google.protobuf.pyext._message",
Tamir Duberstein5018c432015-05-08 08:48:40 -0400211 glob.glob('google/protobuf/pyext/*.cc'),
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700212 include_dirs=[".", "../src"],
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700213 libraries=libraries,
214 extra_objects=extra_objects,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700215 library_dirs=['../src/.libs'],
Josh Haberman00700b72015-10-06 14:13:09 -0700216 extra_compile_args=extra_compile_args,
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700217 ),
218 Extension(
219 "google.protobuf.internal._api_implementation",
220 glob.glob('google/protobuf/internal/api_implementation.cc'),
Paul Yangc9317432018-04-02 15:55:28 -0700221 extra_compile_args=extra_compile_args + ['-DPYTHON_PROTO2_CPP_IMPL_V2'],
Manjunath Kudlurcf828de2016-03-25 10:58:46 -0700222 ),
223 ])
Jisi Liuada65562015-02-25 16:39:11 -0800224 os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp'
liujisi@google.com33165fe2010-11-02 13:14:58 +0000225
Dan O'Reilly3791c802015-08-20 20:49:45 -0400226 # Keep this list of dependencies in sync with tox.ini.
Feng Xiao283c40c2015-12-29 14:36:46 -0800227 install_requires = ['six>=1.9', 'setuptools']
Dan O'Reilly2621c8a2015-08-14 22:54:53 -0400228 if sys.version_info <= (2,7):
229 install_requires.append('ordereddict')
230 install_requires.append('unittest2')
231
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700232 setup(
233 name='protobuf',
234 version=GetVersion(),
235 description='Protocol Buffers',
Feng Xiaoafe98de2018-08-22 11:55:30 -0700236 download_url='https://github.com/protocolbuffers/protobuf/releases',
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700237 long_description="Protocol Buffers are Google's data interchange format",
238 url='https://developers.google.com/protocol-buffers/',
239 maintainer='protobuf@googlegroups.com',
240 maintainer_email='protobuf@googlegroups.com',
Sebastian Schuberth902af082017-02-28 09:58:24 +0100241 license='3-Clause BSD License',
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700242 classifiers=[
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400243 "Programming Language :: Python",
244 "Programming Language :: Python :: 2",
Dan O'Reillye47cdd52015-08-12 23:57:46 -0400245 "Programming Language :: Python :: 2.7",
246 "Programming Language :: Python :: 3",
247 "Programming Language :: Python :: 3.3",
248 "Programming Language :: Python :: 3.4",
249 ],
Craig Citro0e7c0c22016-03-04 20:40:24 -0800250 namespace_packages=['google'],
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700251 packages=find_packages(
252 exclude=[
253 'import_test_package',
254 ],
255 ),
256 test_suite='google.protobuf.internal',
257 cmdclass={
258 'clean': clean,
259 'build_py': build_py,
Josh Haberman325392d2015-08-17 12:30:49 -0700260 'test_conformance': test_conformance,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700261 },
Dan O'Reilly2621c8a2015-08-14 22:54:53 -0400262 install_requires=install_requires,
Tamir Duberstein21a7cf92015-04-11 18:24:24 -0700263 ext_modules=ext_module_list,
264 )