This ports upb's WORKSPACE scraping logic to protobuf, and allows us to dynamically fetch our dependencies at the exact same pinned version as in Bazel via protobuf_FETCH_DEPENDENCIES=ON. This is mostly for development purposes, and is preferable to git submodules. In a later cl we will flip the default behavior to "package" #test-continuous PiperOrigin-RevId: 686265348pull/18806/head
parent
2b0a414573
commit
d3735bc2a0
14 changed files with 266 additions and 603 deletions
@ -0,0 +1,25 @@ |
||||
load("@rules_python//python:defs.bzl", "py_binary") |
||||
load("//upb/cmake:build_defs.bzl", "staleness_test") |
||||
|
||||
py_binary( |
||||
name = "dependencies_generator", |
||||
srcs = ["dependencies_generator.py"], |
||||
) |
||||
|
||||
genrule( |
||||
name = "generate_dependencies", |
||||
srcs = ["//:MODULE.bazel"], |
||||
outs = ["generated-in/dependencies.cmake"], |
||||
cmd = "$(location :dependencies_generator) " + |
||||
"$(location //:MODULE.bazel) $@", |
||||
tools = [":dependencies_generator"], |
||||
) |
||||
|
||||
staleness_test( |
||||
name = "test_dependencies_staleness", |
||||
outs = [ |
||||
"dependencies.cmake", |
||||
], |
||||
generated_pattern = "generated-in/%s", |
||||
tags = ["manual"], |
||||
) |
@ -0,0 +1,143 @@ |
||||
#!/usr/bin/python |
||||
# |
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2023 Google LLC. All rights reserved. |
||||
# https://developers.google.com/protocol-buffers/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# 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. |
||||
# |
||||
# 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. |
||||
|
||||
"""A tool to convert MODULE.bazel -> CMakeLists.txt. |
||||
|
||||
This tool is very protobuf-specific at the moment, and should not be seen as a |
||||
generic Bazel -> CMake converter. |
||||
""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import os |
||||
import sys |
||||
import textwrap |
||||
|
||||
|
||||
class ExtensionFunctions(object): |
||||
"""A fake extension that we can use to get the functions we need.""" |
||||
|
||||
def toolchain(self, *args, **kwargs): |
||||
pass |
||||
|
||||
def parse(self, *args, **kwargs): |
||||
pass |
||||
|
||||
def spec(self, *args, **kwargs): |
||||
pass |
||||
|
||||
def from_specs(self, *args, **kwargs): |
||||
pass |
||||
|
||||
def install(self, *args, **kwargs): |
||||
pass |
||||
|
||||
|
||||
class ModuleFileFunctions(object): |
||||
"""A fake MODULE file that we can exec() to get the functions we need.""" |
||||
|
||||
def __init__(self, converter): |
||||
self.converter = converter |
||||
|
||||
def module(self, *args, **kwargs): |
||||
pass |
||||
|
||||
def bazel_dep(self, name, version, **kwargs): |
||||
self.converter.toplevel += textwrap.dedent( |
||||
"""\ |
||||
set(%(name)s-version "%(version)s") |
||||
""" |
||||
% { |
||||
"name": name, |
||||
"version": version, |
||||
} |
||||
) |
||||
|
||||
def register_toolchains(self, *args): |
||||
pass |
||||
|
||||
def use_repo(self, *args, **kwargs): |
||||
pass |
||||
|
||||
def use_extension(self, *args): |
||||
return ExtensionFunctions() |
||||
|
||||
|
||||
class Converter(object): |
||||
|
||||
def __init__(self): |
||||
self.toplevel = "" |
||||
self.if_lua = "" |
||||
|
||||
def convert(self): |
||||
return self.template % { |
||||
"toplevel": converter.toplevel, |
||||
} |
||||
|
||||
template = textwrap.dedent("""\ |
||||
# Auto-generated by @//cmake:make_dependencies |
||||
# |
||||
# This file contains lists of external dependencies based on our Bazel |
||||
# config. It should be included from a hand-written CMake file that uses |
||||
# them. |
||||
# |
||||
# Changes to this file will be overwritten based on Bazel definitions. |
||||
|
||||
if(${CMAKE_VERSION} VERSION_GREATER 3.10 OR ${CMAKE_VERSION} VERSION_EQUAL 3.10) |
||||
include_guard() |
||||
endif() |
||||
|
||||
%(toplevel)s |
||||
|
||||
""") |
||||
|
||||
|
||||
data = {} |
||||
converter = Converter() |
||||
|
||||
|
||||
def GetDict(obj): |
||||
ret = {} |
||||
for k in dir(obj): |
||||
if not k.startswith("_"): |
||||
ret[k] = getattr(obj, k) |
||||
return ret |
||||
|
||||
|
||||
# We take the MODULE path as a command-line argument to ensure that we can find |
||||
# it regardless of how exactly Bazel was invoked. |
||||
exec(open(sys.argv[1]).read(), GetDict(ModuleFileFunctions(converter))) |
||||
|
||||
with open(sys.argv[2], "w") as f: |
||||
f.write(converter.convert()) |
@ -1,23 +0,0 @@ |
||||
|
||||
# upb CMake build (EXPERIMENTAL) |
||||
|
||||
upb's CMake support is experimental. The core library builds successfully |
||||
under CMake, and this is verified by the Bazel tests in this directory. |
||||
However there is no support for building the upb compiler or for generating |
||||
.upb.c/upb.h files. This means upb's CMake support is incomplete at best, |
||||
unless your application is intended to be purely reflective. |
||||
|
||||
If you find this CMake setup useful in its current state, please consider |
||||
filing an issue so we know. If you have suggestions for how it could be |
||||
more useful (and particularly if you can contribute some code for it) |
||||
please feel free to file an issue for that too. Do keep in mind that upb |
||||
does not currently provide any ABI stability, so we want to avoid providing |
||||
a shared library. |
||||
|
||||
The CMakeLists.txt is generated from the Bazel BUILD files using the Python |
||||
scripts in this directory. We want to avoid having two separate sources of |
||||
truth that both need to be updated when a file is added or removed. |
||||
|
||||
This directory also contains some generated files that would be created |
||||
on the fly during a Bazel build. These are automatically kept in sync by |
||||
the Bazel test `//cmake:test_generated_files`. |
@ -1,346 +0,0 @@ |
||||
#!/usr/bin/python |
||||
# |
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2023 Google LLC. All rights reserved. |
||||
# https://developers.google.com/protocol-buffers/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# 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. |
||||
# |
||||
# 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. |
||||
|
||||
"""A tool to convert BUILD -> CMakeLists.txt. |
||||
|
||||
This tool is very upb-specific at the moment, and should not be seen as a |
||||
generic Bazel -> CMake converter. |
||||
""" |
||||
|
||||
from __future__ import absolute_import |
||||
from __future__ import division |
||||
from __future__ import print_function |
||||
|
||||
import sys |
||||
import textwrap |
||||
import os |
||||
|
||||
def StripFirstChar(deps): |
||||
return [dep[1:] for dep in deps] |
||||
|
||||
def IsSourceFile(name): |
||||
return name.endswith(".c") or name.endswith(".cc") |
||||
|
||||
|
||||
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_BINARY_DIR}> |
||||
) |
||||
""" |
||||
|
||||
|
||||
class BuildFileFunctions(object): |
||||
def __init__(self, converter): |
||||
self.converter = converter |
||||
|
||||
def _add_deps(self, kwargs, keyword=""): |
||||
if "deps" not in kwargs: |
||||
return |
||||
self.converter.toplevel += "target_link_libraries(%s%s\n %s)\n" % ( |
||||
kwargs["name"], |
||||
keyword, |
||||
"\n ".join(StripFirstChar(kwargs["deps"])) |
||||
) |
||||
|
||||
def load(self, *args): |
||||
pass |
||||
|
||||
def cc_library(self, **kwargs): |
||||
if kwargs["name"].endswith("amalgamation"): |
||||
return |
||||
if kwargs["name"] == "upbc_generator": |
||||
return |
||||
if kwargs["name"] == "lupb": |
||||
return |
||||
if "testonly" in kwargs: |
||||
return |
||||
files = kwargs.get("srcs", []) + kwargs.get("hdrs", []) |
||||
found_files = [] |
||||
pregenerated_files = [ |
||||
"CMakeLists.txt", "descriptor.upb.h", "descriptor.upb.c" |
||||
] |
||||
for file in files: |
||||
if os.path.basename(file) in pregenerated_files: |
||||
found_files.append("../cmake/" + file) |
||||
else: |
||||
found_files.append("../" + file) |
||||
|
||||
if list(filter(IsSourceFile, files)): |
||||
# Has sources, make this a normal library. |
||||
self.converter.toplevel += ADD_LIBRARY_FORMAT % { |
||||
"name": kwargs["name"], |
||||
"type": "", |
||||
"keyword": "PUBLIC", |
||||
"sources": "\n ".join(found_files), |
||||
} |
||||
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_FORMAT % { |
||||
"name": kwargs["name"], |
||||
"type": "INTERFACE", |
||||
"keyword": "INTERFACE", |
||||
"sources": "", |
||||
} |
||||
self._add_deps(kwargs, " INTERFACE") |
||||
|
||||
def cc_binary(self, **kwargs): |
||||
pass |
||||
|
||||
def cc_test(self, **kwargs): |
||||
# 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 |
||||
|
||||
def cc_fuzz_test(self, **kwargs): |
||||
pass |
||||
|
||||
def pkg_files(self, **kwargs): |
||||
pass |
||||
|
||||
def py_library(self, **kwargs): |
||||
pass |
||||
|
||||
def py_binary(self, **kwargs): |
||||
pass |
||||
|
||||
def lua_proto_library(self, **kwargs): |
||||
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 |
||||
|
||||
def cc_proto_library(self, **kwargs): |
||||
pass |
||||
|
||||
def staleness_test(self, **kwargs): |
||||
pass |
||||
|
||||
def upb_amalgamation(self, **kwargs): |
||||
pass |
||||
|
||||
def upb_proto_library(self, **kwargs): |
||||
pass |
||||
|
||||
def upb_minitable_proto_library(self, **kwargs): |
||||
pass |
||||
|
||||
def upb_proto_library_copts(self, **kwargs): |
||||
pass |
||||
|
||||
def upb_proto_reflection_library(self, **kwargs): |
||||
pass |
||||
|
||||
def upb_proto_srcs(self, **kwargs): |
||||
pass |
||||
|
||||
def genrule(self, **kwargs): |
||||
pass |
||||
|
||||
def config_setting(self, **kwargs): |
||||
pass |
||||
|
||||
def upb_fasttable_enabled(self, **kwargs): |
||||
pass |
||||
|
||||
def select(self, arg_dict): |
||||
return [] |
||||
|
||||
def glob(self, *args, **kwargs): |
||||
return [] |
||||
|
||||
def licenses(self, *args): |
||||
pass |
||||
|
||||
def filegroup(self, **kwargs): |
||||
pass |
||||
|
||||
def map_dep(self, arg): |
||||
return arg |
||||
|
||||
def package_group(self, **kwargs): |
||||
pass |
||||
|
||||
def bool_flag(self, **kwargs): |
||||
pass |
||||
|
||||
def bootstrap_upb_proto_library(self, **kwargs): |
||||
pass |
||||
|
||||
def bootstrap_cc_library(self, **kwargs): |
||||
pass |
||||
|
||||
def alias(self, **kwargs): |
||||
pass |
||||
|
||||
def package(self, **kwargs): |
||||
pass |
||||
|
||||
class Converter(object): |
||||
def __init__(self): |
||||
self.toplevel = "" |
||||
self.if_lua = "" |
||||
|
||||
def convert(self): |
||||
return self.template % { |
||||
"toplevel": converter.toplevel, |
||||
} |
||||
|
||||
template = textwrap.dedent("""\ |
||||
# This file was generated from BUILD using tools/make_cmakelists.py. |
||||
|
||||
cmake_minimum_required(VERSION 3.10...3.24) |
||||
|
||||
project(upb) |
||||
set(CMAKE_C_STANDARD 99) |
||||
|
||||
# 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() |
||||
|
||||
if(NOT TARGET utf8_range) |
||||
if(EXISTS ../../third_party/utf8_range) |
||||
# utf8_range is already installed |
||||
include_directories(../../third_party/utf8_range) |
||||
else() |
||||
include(FetchContent) |
||||
FetchContent_Declare( |
||||
utf8_range |
||||
GIT_REPOSITORY "https://github.com/protocolbuffers/utf8_range.git" |
||||
GIT_TAG "d863bc33e15cba6d873c878dcca9e6fe52b2f8cb" |
||||
) |
||||
FetchContent_GetProperties(utf8_range) |
||||
if(NOT utf8_range_POPULATED) |
||||
FetchContent_Populate(utf8_range) |
||||
include_directories(${utf8_range_SOURCE_DIR}) |
||||
endif() |
||||
endif() |
||||
endif() |
||||
|
||||
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 = {} |
||||
ret["UPB_DEFAULT_COPTS"] = [] # HACK |
||||
ret["UPB_DEFAULT_CPPOPTS"] = [] # HACK |
||||
for k in dir(obj): |
||||
if not k.startswith("_"): |
||||
ret[k] = getattr(obj, k); |
||||
return ret |
||||
|
||||
globs = GetDict(converter) |
||||
|
||||
# We take the BUILD path as a command-line argument to ensure that we can find |
||||
# it regardless of how exactly Bazel was invoked. |
||||
exec(open(sys.argv[1]).read(), GetDict(BuildFileFunctions(converter))) # BUILD |
||||
|
||||
with open(sys.argv[2], "w") as f: |
||||
f.write(converter.convert()) |
@ -1,73 +0,0 @@ |
||||
#!/bin/bash |
||||
|
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2023 Google LLC. All rights reserved. |
||||
# https://developers.google.com/protocol-buffers/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# 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. |
||||
# |
||||
# 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. |
||||
|
||||
# This script updates checked-in generated files (currently CMakeLists.txt, |
||||
# descriptor.upb.h, and descriptor.upb.c), commits the resulting change, and |
||||
# pushes it. This does not do anything useful when run manually, but should be |
||||
# run by our GitHub action instead. |
||||
|
||||
set -ex |
||||
|
||||
# Exit early if the previous commit was made by the bot. This reduces the risk |
||||
# of a bug causing an infinite loop of auto-generated commits. |
||||
if (git log -1 --pretty=format:'%an' | grep -q "Protobuf Team Bot"); then |
||||
echo "Previous commit was authored by bot" |
||||
exit 0 |
||||
fi |
||||
|
||||
cd $(dirname -- "$0")/.. |
||||
bazel test //cmake:test_generated_files || bazel-bin/cmake/test_generated_files --fix |
||||
|
||||
# Try to determine the most recent pull request number. |
||||
title=$(git log -1 --pretty='%s') |
||||
pr_from_merge=$(echo "$title" | sed -n 's/^Merge pull request #\([0-9]\+\).*/\1/p') |
||||
pr_from_squash=$(echo "$title" | sed -n 's/^.*(#\([0-9]\+\))$/\1/p') |
||||
|
||||
pr_number="" |
||||
if [ ! -z "$pr_from_merge" ]; then |
||||
pr_number="$pr_from_merge" |
||||
elif [ ! -z "$pr_from_squash" ]; then |
||||
pr_number="$pr_from_squash" |
||||
fi |
||||
|
||||
if [ ! -z "$pr_number" ]; then |
||||
commit_message="Auto-generate CMake file lists after PR #$pr_number" |
||||
else |
||||
# If we are unable to determine the pull request number, we fall back on this |
||||
# default commit message. Typically this should not occur, but could happen |
||||
# if a pull request was merged via a rebase. |
||||
commit_message="Auto-generate CMake file lists" |
||||
fi |
||||
|
||||
git add -A |
||||
git diff --staged --quiet || git commit -am "$commit_message" |
||||
git push |
Loading…
Reference in new issue