Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
421 lines
11 KiB
421 lines
11 KiB
4 years ago
|
#!/usr/bin/python
|
||
|
#
|
||
1 year ago
|
# Protocol Buffers - Google's data interchange format
|
||
|
# Copyright 2023 Google LLC. All rights reserved.
|
||
|
# https://developers.google.com/protocol-buffers/
|
||
4 years ago
|
#
|
||
|
# Redistribution and use in source and binary forms, with or without
|
||
1 year ago
|
# modification, are permitted provided that the following conditions are
|
||
|
# met:
|
||
|
#
|
||
4 years ago
|
# * Redistributions of source code must retain the above copyright
|
||
1 year ago
|
# notice, this list of conditions and the following disclaimer.
|
||
|
# * Redistributions in binary form must reproduce the above
|
||
|
# copyright notice, this list of conditions and the following disclaimer
|
||
|
# in the documentation and/or other materials provided with the
|
||
|
# distribution.
|
||
|
# * Neither the name of Google LLC nor the names of its
|
||
|
# contributors may be used to endorse or promote products derived from
|
||
|
# this software without specific prior written permission.
|
||
4 years ago
|
#
|
||
1 year ago
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
6 years ago
|
|
||
3 years ago
|
"""A tool to convert {WORKSPACE, BUILD} -> CMakeLists.txt.
|
||
6 years ago
|
|
||
3 years ago
|
This tool is very upb-specific at the moment, and should not be seen as a
|
||
|
generic Bazel -> CMake converter.
|
||
6 years ago
|
"""
|
||
|
|
||
|
from __future__ import absolute_import
|
||
|
from __future__ import division
|
||
|
from __future__ import print_function
|
||
|
|
||
|
import sys
|
||
|
import textwrap
|
||
6 years ago
|
import os
|
||
6 years ago
|
|
||
3 years ago
|
def StripFirstChar(deps):
|
||
|
return [dep[1:] for dep in deps]
|
||
6 years ago
|
|
||
|
def IsSourceFile(name):
|
||
|
return name.endswith(".c") or name.endswith(".cc")
|
||
|
|
||
1 year ago
|
|
||
|
ADD_LIBRARY_FORMAT = """
|
||
|
add_library(%(name)s %(type)s
|
||
|
%(sources)s
|
||
|
)
|
||
|
target_include_directories(%(name)s %(keyword)s
|
||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
|
||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../cmake>
|
||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINRARY_DIR}>
|
||
|
)
|
||
|
"""
|
||
|
|
||
|
|
||
6 years ago
|
class BuildFileFunctions(object):
|
||
|
def __init__(self, converter):
|
||
|
self.converter = converter
|
||
|
|
||
6 years ago
|
def _add_deps(self, kwargs, keyword=""):
|
||
6 years ago
|
if "deps" not in kwargs:
|
||
|
return
|
||
6 years ago
|
self.converter.toplevel += "target_link_libraries(%s%s\n %s)\n" % (
|
||
6 years ago
|
kwargs["name"],
|
||
6 years ago
|
keyword,
|
||
3 years ago
|
"\n ".join(StripFirstChar(kwargs["deps"]))
|
||
6 years ago
|
)
|
||
|
|
||
|
def load(self, *args):
|
||
|
pass
|
||
1 year ago
|
|
||
6 years ago
|
def cc_library(self, **kwargs):
|
||
5 years ago
|
if kwargs["name"].endswith("amalgamation"):
|
||
6 years ago
|
return
|
||
5 years ago
|
if kwargs["name"] == "upbc_generator":
|
||
|
return
|
||
|
if kwargs["name"] == "lupb":
|
||
5 years ago
|
return
|
||
2 years ago
|
if "testonly" in kwargs:
|
||
|
return
|
||
6 years ago
|
files = kwargs.get("srcs", []) + kwargs.get("hdrs", [])
|
||
6 years ago
|
found_files = []
|
||
3 years ago
|
pregenerated_files = [
|
||
|
"CMakeLists.txt", "descriptor.upb.h", "descriptor.upb.c"
|
||
|
]
|
||
6 years ago
|
for file in files:
|
||
3 years ago
|
if os.path.basename(file) in pregenerated_files:
|
||
|
found_files.append("../cmake/" + file)
|
||
|
else:
|
||
|
found_files.append("../" + file)
|
||
6 years ago
|
|
||
6 years ago
|
if list(filter(IsSourceFile, files)):
|
||
6 years ago
|
# Has sources, make this a normal library.
|
||
1 year ago
|
self.converter.toplevel += ADD_LIBRARY_FORMAT % {
|
||
|
"name": kwargs["name"],
|
||
|
"type": "",
|
||
|
"keyword": "PUBLIC",
|
||
|
"sources": "\n ".join(found_files),
|
||
|
}
|
||
6 years ago
|
self._add_deps(kwargs)
|
||
|
else:
|
||
|
# Header-only library, have to do a couple things differently.
|
||
|
# For some info, see:
|
||
|
# http://mariobadr.com/creating-a-header-only-library-with-cmake.html
|
||
1 year ago
|
self.converter.toplevel += ADD_LIBRARY_FORMAT % {
|
||
|
"name": kwargs["name"],
|
||
|
"type": "INTERFACE",
|
||
|
"keyword": "INTERFACE",
|
||
|
"sources": "",
|
||
|
}
|
||
6 years ago
|
self._add_deps(kwargs, " INTERFACE")
|
||
6 years ago
|
|
||
|
def cc_binary(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def cc_test(self, **kwargs):
|
||
6 years ago
|
# Disable this until we properly support upb_proto_library().
|
||
|
# self.converter.toplevel += "add_executable(%s\n %s)\n" % (
|
||
|
# kwargs["name"],
|
||
|
# "\n ".join(kwargs["srcs"])
|
||
|
# )
|
||
|
# self.converter.toplevel += "add_test(NAME %s COMMAND %s)\n" % (
|
||
|
# kwargs["name"],
|
||
|
# kwargs["name"],
|
||
|
# )
|
||
|
|
||
|
# if "data" in kwargs:
|
||
|
# for data_dep in kwargs["data"]:
|
||
|
# self.converter.toplevel += textwrap.dedent("""\
|
||
|
# add_custom_command(
|
||
|
# TARGET %s POST_BUILD
|
||
|
# COMMAND ${CMAKE_COMMAND} -E copy
|
||
|
# ${CMAKE_SOURCE_DIR}/%s
|
||
|
# ${CMAKE_CURRENT_BINARY_DIR}/%s)\n""" % (
|
||
|
# kwargs["name"], data_dep, data_dep
|
||
|
# ))
|
||
|
|
||
|
# self._add_deps(kwargs)
|
||
|
pass
|
||
6 years ago
|
|
||
3 years ago
|
def cc_fuzz_test(self, **kwargs):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def pkg_files(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def py_library(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def py_binary(self, **kwargs):
|
||
|
pass
|
||
|
|
||
5 years ago
|
def lua_proto_library(self, **kwargs):
|
||
6 years ago
|
pass
|
||
|
|
||
|
def sh_test(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def make_shell_script(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def exports_files(self, files, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def proto_library(self, **kwargs):
|
||
|
pass
|
||
|
|
||
4 years ago
|
def cc_proto_library(self, **kwargs):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def staleness_test(self, **kwargs):
|
||
6 years ago
|
pass
|
||
|
|
||
|
def upb_amalgamation(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def upb_proto_library(self, **kwargs):
|
||
|
pass
|
||
|
|
||
4 years ago
|
def upb_proto_library_copts(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def upb_proto_reflection_library(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def upb_proto_srcs(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def genrule(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def config_setting(self, **kwargs):
|
||
|
pass
|
||
|
|
||
4 years ago
|
def upb_fasttable_enabled(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def select(self, arg_dict):
|
||
|
return []
|
||
|
|
||
2 years ago
|
def glob(self, *args, **kwargs):
|
||
6 years ago
|
return []
|
||
|
|
||
6 years ago
|
def licenses(self, *args):
|
||
6 years ago
|
pass
|
||
|
|
||
6 years ago
|
def filegroup(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def map_dep(self, arg):
|
||
|
return arg
|
||
|
|
||
3 years ago
|
def package_group(self, **kwargs):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def bool_flag(self, **kwargs):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def bootstrap_upb_proto_library(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def bootstrap_cc_library(self, **kwargs):
|
||
|
pass
|
||
|
|
||
1 year ago
|
def alias(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
|
||
|
class WorkspaceFileFunctions(object):
|
||
|
def __init__(self, converter):
|
||
|
self.converter = converter
|
||
|
|
||
2 years ago
|
def load(self, *args, **kwargs):
|
||
6 years ago
|
pass
|
||
|
|
||
|
def workspace(self, **kwargs):
|
||
|
self.converter.prelude += "project(%s)\n" % (kwargs["name"])
|
||
4 years ago
|
self.converter.prelude += "set(CMAKE_C_STANDARD 99)\n"
|
||
6 years ago
|
|
||
2 years ago
|
def maybe(self, rule, **kwargs):
|
||
|
if kwargs["name"] == "utf8_range":
|
||
|
self.converter.utf8_range_commit = kwargs["commit"]
|
||
|
pass
|
||
|
|
||
6 years ago
|
def http_archive(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def git_repository(self, **kwargs):
|
||
|
pass
|
||
|
|
||
4 years ago
|
def new_git_repository(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def bazel_version_repository(self, **kwargs):
|
||
|
pass
|
||
|
|
||
3 years ago
|
def protobuf_deps(self):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def utf8_range_deps(self):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def pip_parse(self, **kwargs):
|
||
2 years ago
|
pass
|
||
|
|
||
3 years ago
|
def rules_fuzzing_dependencies(self):
|
||
|
pass
|
||
|
|
||
|
def rules_fuzzing_init(self):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def rules_pkg_dependencies(self):
|
||
|
pass
|
||
|
|
||
3 years ago
|
def system_python(self, **kwargs):
|
||
3 years ago
|
pass
|
||
|
|
||
2 years ago
|
def register_system_python(self, **kwargs):
|
||
|
pass
|
||
|
|
||
3 years ago
|
def register_toolchains(self, toolchain):
|
||
|
pass
|
||
|
|
||
3 years ago
|
def python_source_archive(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def python_nuget_package(self, **kwargs):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def install_deps(self):
|
||
|
pass
|
||
|
|
||
|
def fuzzing_py_install_deps(self):
|
||
|
pass
|
||
|
|
||
2 years ago
|
def googletest_deps(self):
|
||
|
pass
|
||
|
|
||
6 years ago
|
|
||
|
class Converter(object):
|
||
|
def __init__(self):
|
||
|
self.prelude = ""
|
||
|
self.toplevel = ""
|
||
|
self.if_lua = ""
|
||
2 years ago
|
self.utf8_range_commit = ""
|
||
6 years ago
|
|
||
|
def convert(self):
|
||
|
return self.template % {
|
||
|
"prelude": converter.prelude,
|
||
|
"toplevel": converter.toplevel,
|
||
2 years ago
|
"utf8_range_commit": converter.utf8_range_commit,
|
||
6 years ago
|
}
|
||
|
|
||
|
template = textwrap.dedent("""\
|
||
|
# This file was generated from BUILD using tools/make_cmakelists.py.
|
||
|
|
||
1 year ago
|
cmake_minimum_required(VERSION 3.10...3.24)
|
||
6 years ago
|
|
||
|
%(prelude)s
|
||
|
|
||
|
# Prevent CMake from setting -rdynamic on Linux (!!).
|
||
|
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
|
||
|
SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
|
||
|
|
||
|
# Set default build type.
|
||
|
if(NOT CMAKE_BUILD_TYPE)
|
||
|
message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
|
||
|
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
|
||
|
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
|
||
|
FORCE)
|
||
|
endif()
|
||
|
|
||
|
# When using Ninja, compiler output won't be colorized without this.
|
||
|
include(CheckCXXCompilerFlag)
|
||
|
CHECK_CXX_COMPILER_FLAG(-fdiagnostics-color=always SUPPORTS_COLOR_ALWAYS)
|
||
|
if(SUPPORTS_COLOR_ALWAYS)
|
||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
|
||
|
endif()
|
||
|
|
||
|
# Implement ASAN/UBSAN options
|
||
|
if(UPB_ENABLE_ASAN)
|
||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
|
||
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
|
||
|
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
|
||
|
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
|
||
|
endif()
|
||
|
|
||
|
if(UPB_ENABLE_UBSAN)
|
||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
|
||
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
|
||
|
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
|
||
|
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
|
||
|
endif()
|
||
|
|
||
2 years ago
|
if(NOT TARGET utf8_range)
|
||
|
if(EXISTS ../external/utf8_range)
|
||
|
# utf8_range is already installed
|
||
|
include_directories(../external/utf8_range)
|
||
1 year ago
|
elseif(EXISTS ../../utf8_range)
|
||
|
include_directories(../../utf8_range)
|
||
2 years ago
|
else()
|
||
|
include(FetchContent)
|
||
|
FetchContent_Declare(
|
||
|
utf8_range
|
||
|
GIT_REPOSITORY "https://github.com/protocolbuffers/utf8_range.git"
|
||
|
GIT_TAG "%(utf8_range_commit)s"
|
||
|
)
|
||
|
FetchContent_GetProperties(utf8_range)
|
||
|
if(NOT utf8_range_POPULATED)
|
||
|
FetchContent_Populate(utf8_range)
|
||
|
include_directories(${utf8_range_SOURCE_DIR})
|
||
|
endif()
|
||
2 years ago
|
endif()
|
||
|
endif()
|
||
|
|
||
6 years ago
|
if(APPLE)
|
||
|
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup -flat_namespace")
|
||
|
elseif(UNIX)
|
||
|
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id")
|
||
|
endif()
|
||
|
|
||
|
enable_testing()
|
||
|
|
||
|
%(toplevel)s
|
||
|
|
||
|
""")
|
||
|
|
||
|
data = {}
|
||
|
converter = Converter()
|
||
|
|
||
|
def GetDict(obj):
|
||
|
ret = {}
|
||
4 years ago
|
ret["UPB_DEFAULT_COPTS"] = [] # HACK
|
||
3 years ago
|
ret["UPB_DEFAULT_CPPOPTS"] = [] # HACK
|
||
6 years ago
|
for k in dir(obj):
|
||
|
if not k.startswith("_"):
|
||
|
ret[k] = getattr(obj, k);
|
||
|
return ret
|
||
|
|
||
|
globs = GetDict(converter)
|
||
|
|
||
2 years ago
|
workspace_dict = GetDict(WorkspaceFileFunctions(converter))
|
||
1 year ago
|
# We take all file paths as command-line arguments to ensure that we can find
|
||
|
# each file regardless of how exactly Bazel was invoked.
|
||
|
exec(open(sys.argv[1]).read(), workspace_dict) # workspace_deps.bzl
|
||
|
exec(open(sys.argv[2]).read(), workspace_dict) # WORKSPACE
|
||
|
exec(open(sys.argv[3]).read(), GetDict(BuildFileFunctions(converter))) # BUILD
|
||
6 years ago
|
|
||
1 year ago
|
with open(sys.argv[4], "w") as f:
|
||
6 years ago
|
f.write(converter.convert())
|