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.
279 lines
7.1 KiB
279 lines
7.1 KiB
6 years ago
|
#!/usr/bin/env python
|
||
|
|
||
|
"""TODO(haberman): DO NOT SUBMIT without one-line documentation for make_cmakelists.
|
||
|
|
||
|
TODO(haberman): DO NOT SUBMIT without a detailed description of make_cmakelists.
|
||
|
"""
|
||
|
|
||
|
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
|
|
||
|
def StripColons(deps):
|
||
|
return map(lambda x: x[1:], deps)
|
||
|
|
||
|
def IsSourceFile(name):
|
||
|
return name.endswith(".c") or name.endswith(".cc")
|
||
|
|
||
|
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,
|
||
6 years ago
|
"\n ".join(StripColons(kwargs["deps"]))
|
||
|
)
|
||
|
|
||
|
def load(self, *args):
|
||
|
pass
|
||
|
|
||
|
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
|
||
6 years ago
|
files = kwargs.get("srcs", []) + kwargs.get("hdrs", [])
|
||
6 years ago
|
found_files = []
|
||
|
for file in files:
|
||
|
if os.path.isfile(file):
|
||
4 years ago
|
found_files.append("../" + file)
|
||
|
elif os.path.isfile("cmake/" + file):
|
||
|
found_files.append("../cmake/" + file)
|
||
6 years ago
|
else:
|
||
|
print("Warning: no such file: " + file)
|
||
6 years ago
|
|
||
6 years ago
|
if list(filter(IsSourceFile, files)):
|
||
6 years ago
|
# Has sources, make this a normal library.
|
||
|
self.converter.toplevel += "add_library(%s\n %s)\n" % (
|
||
|
kwargs["name"],
|
||
6 years ago
|
"\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
|
||
|
self.converter.toplevel += "add_library(%s INTERFACE)\n" % (
|
||
|
kwargs["name"]
|
||
|
)
|
||
|
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
|
|
||
|
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
|
||
|
|
||
6 years ago
|
def generated_file_staleness_test(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def upb_amalgamation(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def upb_proto_library(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
|
||
|
|
||
|
def select(self, arg_dict):
|
||
|
return []
|
||
|
|
||
6 years ago
|
def glob(self, *args):
|
||
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
|
||
|
|
||
6 years ago
|
|
||
|
class WorkspaceFileFunctions(object):
|
||
|
def __init__(self, converter):
|
||
|
self.converter = converter
|
||
|
|
||
|
def load(self, *args):
|
||
|
pass
|
||
|
|
||
|
def workspace(self, **kwargs):
|
||
|
self.converter.prelude += "project(%s)\n" % (kwargs["name"])
|
||
|
|
||
|
def http_archive(self, **kwargs):
|
||
|
pass
|
||
|
|
||
|
def git_repository(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def bazel_version_repository(self, **kwargs):
|
||
|
pass
|
||
|
|
||
6 years ago
|
def upb_deps(self):
|
||
|
pass
|
||
|
|
||
6 years ago
|
|
||
|
class Converter(object):
|
||
|
def __init__(self):
|
||
|
self.prelude = ""
|
||
|
self.toplevel = ""
|
||
|
self.if_lua = ""
|
||
|
|
||
|
def convert(self):
|
||
|
return self.template % {
|
||
|
"prelude": converter.prelude,
|
||
|
"toplevel": converter.toplevel,
|
||
|
}
|
||
|
|
||
|
template = textwrap.dedent("""\
|
||
|
# This file was generated from BUILD using tools/make_cmakelists.py.
|
||
|
|
||
|
cmake_minimum_required(VERSION 3.1)
|
||
|
|
||
|
if(${CMAKE_VERSION} VERSION_LESS 3.12)
|
||
|
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
|
||
|
else()
|
||
|
cmake_policy(VERSION 3.12)
|
||
|
endif()
|
||
|
|
||
|
cmake_minimum_required (VERSION 3.0)
|
||
|
cmake_policy(SET CMP0048 NEW)
|
||
|
|
||
|
%(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()
|
||
|
|
||
4 years ago
|
include_directories(..)
|
||
|
include_directories(../cmake)
|
||
6 years ago
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||
|
|
||
|
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
|
||
6 years ago
|
for k in dir(obj):
|
||
|
if not k.startswith("_"):
|
||
|
ret[k] = getattr(obj, k);
|
||
|
return ret
|
||
|
|
||
|
globs = GetDict(converter)
|
||
|
|
||
6 years ago
|
exec(open("WORKSPACE").read(), GetDict(WorkspaceFileFunctions(converter)))
|
||
|
exec(open("BUILD").read(), GetDict(BuildFileFunctions(converter)))
|
||
6 years ago
|
|
||
|
with open(sys.argv[1], "w") as f:
|
||
|
f.write(converter.convert())
|