-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proto_common.direct_source_infos
This change adds a helper to extract information about direct sources of `ProtoInfo` providers. Design doc: https://docs.google.com/document/d/1u95vlQ1lWeQNR4bUw5T4cMeHTGJla2_e1dHHx7v4Dvg/edit# Note that this was originally named `proto_common.protos_to_generate`, but was renamed because it applies more broadly.
- Loading branch information
Showing
12 changed files
with
457 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Intentionally left empty (for now). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2019 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Contains the implementation for `proto_common.direct_source_infos`.""" | ||
|
||
ProtoFileInfo = provider( | ||
doc = "Encapsulates information about a single `.proto` file.", | ||
fields = { | ||
"file": "The source file.", | ||
"import_path": "The import path of the `.proto` file.", | ||
}, | ||
) | ||
|
||
# TODO(yannic): Remove this suppression when | ||
# https://github.com/bazelbuild/buildtools/issues/774 has been fixed. | ||
# buildifier: disable=function-docstring-args | ||
def direct_source_infos(proto_info, *, provided_sources = []): | ||
"""Returns sequence of `ProtoFileInfo` for `proto_info`'s direct sources. | ||
Files that are both in `proto_info`'s direct sources and in | ||
`provided_sources` are skipped. This is useful, e.g., for well-known | ||
protos that are already provided by the Protobuf runtime. | ||
Args: | ||
proto_info: An instance of `ProtoInfo`. | ||
provided_sources: Optional. A sequence of import paths to ignore. | ||
Returns: A sequence of `ProtoFileInfo` containing information about | ||
`proto_info`'s direct sources. | ||
""" | ||
|
||
files = [_info(f, proto_info) for f in proto_info.direct_sources] | ||
return [f for f in files if f.import_path not in provided_sources] | ||
|
||
def _info(file, proto_info): | ||
"""Computes `ProtoFileInfo` for a single file. | ||
Args: | ||
file: The proto file to generate `ProtoFileInfo` for. Must be in | ||
`direct_sources` of `proto_info`. | ||
proto_info: An instance of `ProtoInfo`. | ||
Returns: An instance of `ProtoFileInfo`. | ||
""" | ||
|
||
if "." == proto_info.proto_source_root: | ||
# The `proto_library` didn't specify `import_prefix` or | ||
# `strip_import_prefix`, and `file` is a regular source file | ||
# (i.e. not generated) in the main workspace. | ||
|
||
return ProtoFileInfo( | ||
file = file, | ||
import_path = file.path, | ||
) | ||
|
||
source_root = proto_info.proto_source_root | ||
offset = len(source_root) + 1 # + '/'. | ||
return ProtoFileInfo( | ||
file = file, | ||
import_path = file.path[offset:], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,115 @@ | ||
load("//proto:defs.bzl", "proto_library") | ||
|
||
WORKSPACE_PREFIX = "" # copybara-workspace-prefix(WORKSPACE_PREFIX) | ||
|
||
proto_library( | ||
name = "multiple_srcs_proto", | ||
srcs = [ | ||
"empty.proto", | ||
"prefix/empty.proto", | ||
], | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_proto", | ||
srcs = [ | ||
"empty.proto", | ||
], | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_with_prefix_proto", | ||
srcs = [ | ||
"prefix/empty.proto", | ||
], | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_with_absolute_strip_prefix_proto", | ||
srcs = [ | ||
"empty.proto", | ||
], | ||
strip_import_prefix = "/" + WORKSPACE_PREFIX + "tests", | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_with_relative_strip_prefix_proto", | ||
srcs = [ | ||
"prefix/empty.proto", | ||
], | ||
strip_import_prefix = "prefix", | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_with_empty_strip_prefix_proto", | ||
srcs = [ | ||
"empty.proto", | ||
], | ||
strip_import_prefix = "", | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_with_add_and_absolute_strip_prefix_proto", | ||
srcs = [ | ||
"empty.proto", | ||
], | ||
import_prefix = "foo", | ||
strip_import_prefix = "/" + WORKSPACE_PREFIX + "tests", | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_with_add_and_relative_strip_prefix_proto", | ||
srcs = [ | ||
"prefix/empty.proto", | ||
], | ||
import_prefix = "foo", | ||
strip_import_prefix = "prefix", | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_with_add_and_empty_strip_prefix_proto", | ||
srcs = [ | ||
"empty.proto", | ||
], | ||
import_prefix = "foo", | ||
strip_import_prefix = "", | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) | ||
|
||
proto_library( | ||
name = "empty_with_add_and_empty_strip_prefix_proto_in_subdir", | ||
srcs = [ | ||
"prefix/empty.proto", | ||
], | ||
import_prefix = "foo", | ||
strip_import_prefix = "", | ||
visibility = [ | ||
"//tests:__subpackages__", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
load("//tests/analysis:proto_common_contains_native_fields_test.bzl", "proto_common_contains_native_fields_test") | ||
|
||
proto_common_contains_native_fields_test( | ||
name = "proto_common_contains_native_fields_test", | ||
) |
45 changes: 45 additions & 0 deletions
45
tests/analysis/proto_common_contains_native_fields_test.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2019 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Unit-test to verify that the Starlark `proto_common` shim contains all fields | ||
of the native version of `proto_common`, excluding fields that are considered | ||
to be an implementation detail (thus not part of the public API). | ||
""" | ||
|
||
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") | ||
load("//proto:defs.bzl", "proto_common") | ||
load("//proto/private:native.bzl", "native_proto_common") # buildifier: disable=bzl-visibility | ||
|
||
def _impl(ctx): | ||
"""Verifies that the Starlark `proto_common` contains all necessary fields. | ||
Args: | ||
ctx: The rule context. | ||
Returns: A (not further specified) sequence of providers. | ||
""" | ||
|
||
env = unittest.begin(ctx) | ||
|
||
for field in dir(native_proto_common): | ||
# Starlark `proto_common` only exports fields that are part of the | ||
# stable and supported API of native `proto_common`. | ||
if not field.endswith("_do_not_use_or_we_will_break_you_without_mercy"): | ||
asserts.true(env, hasattr(proto_common, field)) | ||
else: | ||
asserts.false(env, hasattr(proto_common, field)) | ||
|
||
return unittest.end(env) | ||
|
||
proto_common_contains_native_fields_test = unittest.make(_impl) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2019 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
syntax = "proto3"; | ||
|
||
package rules_proto.tests.prefix; |
Oops, something went wrong.