Skip to content

Commit

Permalink
[RFC] Add ability to use PathToUnderscores in proto to Swift gen
Browse files Browse the repository at this point in the history
Add the ability to provide `--features swift.generate_path_to_underscores_from_proto_files` to change the `FileNaming` param passed to protoc from `FullPath` to `PathToUnderscores`.

Fixes #552.
  • Loading branch information
mattrobmattrob committed Oct 12, 2022
1 parent fd39daf commit f0957a3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions swift/internal/feature_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ SWIFT_FEATURE_NO_EMBED_DEBUG_MODULE = "swift.no_embed_debug_module"
# remains opt in.
SWIFT_FEATURE_GENERATE_FROM_RAW_PROTO_FILES = "swift.generate_from_raw_proto_files"

# If enabled, the toolchain will use `--swift_opt=FileNaming=PathToUnderscores`
# (instead of `--swift_opt=FileNaming=FullPath`) for the protoc arguments when
# generating a Swift file from a proto file.
SWIFT_FEATURE_GENERATE_PATH_TO_UNDERSCORES_FROM_PROTO_FILES = "swift.generate_path_to_underscores_from_proto_files"

# If enabled and whole module optimisation is being used, the `*.swiftdoc`,
# `*.swiftmodule` and `*-Swift.h` are generated with a separate action
# rather than as part of the compilation.
Expand Down
8 changes: 7 additions & 1 deletion swift/internal/proto_gen_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ load("@bazel_skylib//lib:paths.bzl", "paths")
def declare_generated_files(
name,
actions,
generate_path_to_underscores_from_proto_files,
extension_fragment,
proto_source_root,
proto_srcs):
Expand All @@ -27,6 +28,7 @@ def declare_generated_files(
Args:
name: The name of the target currently being analyzed.
actions: The context's actions object.
generate_path_to_underscores_from_proto_files: TBD
extension_fragment: An extension fragment that precedes `.swift` on the
end of the generated files. In other words, the file `foo.proto`
will generate a file named `foo.{extension_fragment}.swift`.
Expand All @@ -42,6 +44,7 @@ def declare_generated_files(
actions.declare_file(
_generated_file_path(
name,
generate_path_to_underscores_from_proto_files,
extension_fragment,
proto_source_root,
f,
Expand Down Expand Up @@ -83,7 +86,7 @@ def extract_generated_dir_path(
return None

first_path = generated_files[0].path
dir_name = _generated_file_path(name, extension_fragment, proto_source_root)
dir_name = _generated_file_path(name, False, extension_fragment, proto_source_root)
offset = first_path.find(dir_name)
return first_path[:offset + len(dir_name)]

Expand Down Expand Up @@ -141,6 +144,7 @@ def proto_import_path(f, proto_source_root):

def _generated_file_path(
name,
generate_path_to_underscores_from_proto_files,
extension_fragment,
proto_source_root,
proto_file = None):
Expand All @@ -156,6 +160,7 @@ def _generated_file_path(
Args:
name: The name of the target currently being analyzed.
generate_path_to_underscores_from_proto_files: TBD
extension_fragment: An extension fragment that precedes `.swift` on the
end of the generated files. In other words, the file `foo.proto`
will generate a file named `foo.{extension_fragment}.swift`.
Expand All @@ -178,6 +183,7 @@ def _generated_file_path(
proto_import_path(proto_file, proto_source_root),
".{}.swift".format(extension_fragment),
)
generated_file_path = generated_file_path.replace("/", "_") if generate_path_to_underscores_from_proto_files else generated_file_path
return paths.join(dir_path, generated_file_path)
return dir_path

Expand Down
16 changes: 15 additions & 1 deletion swift/internal/swift_protoc_gen_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ load(
"SWIFT_FEATURE_ENABLE_LIBRARY_EVOLUTION",
"SWIFT_FEATURE_ENABLE_TESTING",
"SWIFT_FEATURE_GENERATE_FROM_RAW_PROTO_FILES",
"SWIFT_FEATURE_GENERATE_PATH_TO_UNDERSCORES_FROM_PROTO_FILES",
)
load(":linking.bzl", "new_objc_provider")
load(
Expand Down Expand Up @@ -105,6 +106,7 @@ def _register_pbswift_generate_action(
transitive_descriptor_sets,
module_mapping_file,
generate_from_proto_sources,
generate_path_to_underscores_from_proto_files,
mkdir_and_run,
protoc_executable,
protoc_plugin_executable,
Expand All @@ -128,6 +130,7 @@ def _register_pbswift_generate_action(
from proto source file vs just via the DescriptorSets. The Sets
don't have source info, so the generated sources won't have
comments (https://github.com/bazelbuild/bazel/issues/9337).
generate_path_to_underscores_from_proto_files: TBD
mkdir_and_run: The `File` representing the `mkdir_and_run` executable.
protoc_executable: The `File` representing the `protoc` executable.
protoc_plugin_executable: The `File` representing the `protoc` plugin
Expand All @@ -142,6 +145,7 @@ def _register_pbswift_generate_action(
generated_files = declare_generated_files(
label.name,
actions,
generate_path_to_underscores_from_proto_files,
"pb",
proto_source_root,
direct_srcs,
Expand Down Expand Up @@ -171,7 +175,12 @@ def _register_pbswift_generate_action(
format = "--plugin=protoc-gen-swift=%s",
)
protoc_args.add(generated_dir_path, format = "--swift_out=%s")
protoc_args.add("--swift_opt=FileNaming=FullPath")

if generate_path_to_underscores_from_proto_files:
protoc_args.add("--swift_opt=FileNaming=PathToUnderscores")
else:
protoc_args.add("--swift_opt=FileNaming=FullPath")

protoc_args.add("--swift_opt=Visibility=Public")
if module_mapping_file:
protoc_args.add(
Expand Down Expand Up @@ -378,6 +387,10 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_GENERATE_FROM_RAW_PROTO_FILES,
)
generate_path_to_underscores_from_proto_files = swift_common.is_enabled(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_GENERATE_PATH_TO_UNDERSCORES_FROM_PROTO_FILES,
)

# Only the files for direct sources should be generated, but the
# transitive descriptor sets are still need to be able to parse/load
Expand All @@ -403,6 +416,7 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
transitive_descriptor_sets,
transitive_module_mapping_file,
generate_from_proto_sources,
generate_path_to_underscores_from_proto_files,
aspect_ctx.executable._mkdir_and_run,
aspect_ctx.executable._protoc,
aspect_ctx.executable._protoc_gen_swift,
Expand Down

0 comments on commit f0957a3

Please sign in to comment.