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