From b4b93b36245a88a3efa19261638f5da61a9be44f Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Thu, 26 Dec 2024 08:55:09 -0800 Subject: [PATCH] Split protoc apart from libprotoc in our cmake configs. This avoids installing every header required by protoc, and only installs the ones we've explicitly marked as public in bazel (and their transitive dependencies). PiperOrigin-RevId: 709815861 --- cmake/install.cmake | 2 +- cmake/installed_include_golden.txt | 109 ------------------ cmake/tests.cmake | 14 ++- pkg/BUILD.bazel | 18 +++ src/google/protobuf/compiler/rust/BUILD.bazel | 1 - .../protobuf/compiler/rust/crate_mapping.cc | 28 ++++- src/google/protobuf/testing/BUILD.bazel | 1 + 7 files changed, 56 insertions(+), 117 deletions(-) diff --git a/cmake/install.cmake b/cmake/install.cmake index 906ded5c1599e..076d45f5e3b98 100644 --- a/cmake/install.cmake +++ b/cmake/install.cmake @@ -87,7 +87,7 @@ endif () include(${protobuf_SOURCE_DIR}/src/file_lists.cmake) set(protobuf_HEADERS ${libprotobuf_hdrs} - ${libprotoc_hdrs} + ${libprotoc_public_hdrs} ${wkt_protos_files} ${cpp_features_proto_proto_srcs} ${descriptor_proto_proto_srcs} diff --git a/cmake/installed_include_golden.txt b/cmake/installed_include_golden.txt index 740b36b2f4a17..57c8ca73cc444 100644 --- a/cmake/installed_include_golden.txt +++ b/cmake/installed_include_golden.txt @@ -12,130 +12,28 @@ google/protobuf/arenaz_sampler.h google/protobuf/compiler/code_generator.h google/protobuf/compiler/code_generator_lite.h google/protobuf/compiler/command_line_interface.h -google/protobuf/compiler/cpp/enum.h -google/protobuf/compiler/cpp/extension.h -google/protobuf/compiler/cpp/field.h -google/protobuf/compiler/cpp/field_generators/generators.h -google/protobuf/compiler/cpp/file.h -google/protobuf/compiler/cpp/generator.h google/protobuf/compiler/cpp/helpers.h -google/protobuf/compiler/cpp/ifndef_guard.h -google/protobuf/compiler/cpp/message.h -google/protobuf/compiler/cpp/message_layout_helper.h google/protobuf/compiler/cpp/names.h -google/protobuf/compiler/cpp/namespace_printer.h google/protobuf/compiler/cpp/options.h -google/protobuf/compiler/cpp/padding_optimizer.h -google/protobuf/compiler/cpp/parse_function_generator.h -google/protobuf/compiler/cpp/service.h -google/protobuf/compiler/cpp/tracker.h -google/protobuf/compiler/csharp/csharp_doc_comment.h -google/protobuf/compiler/csharp/csharp_enum.h -google/protobuf/compiler/csharp/csharp_enum_field.h -google/protobuf/compiler/csharp/csharp_field_base.h -google/protobuf/compiler/csharp/csharp_generator.h -google/protobuf/compiler/csharp/csharp_helpers.h -google/protobuf/compiler/csharp/csharp_map_field.h -google/protobuf/compiler/csharp/csharp_message.h -google/protobuf/compiler/csharp/csharp_message_field.h -google/protobuf/compiler/csharp/csharp_options.h -google/protobuf/compiler/csharp/csharp_primitive_field.h -google/protobuf/compiler/csharp/csharp_reflection_class.h -google/protobuf/compiler/csharp/csharp_repeated_enum_field.h -google/protobuf/compiler/csharp/csharp_repeated_message_field.h -google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h -google/protobuf/compiler/csharp/csharp_source_generator_base.h -google/protobuf/compiler/csharp/csharp_wrapper_field.h google/protobuf/compiler/csharp/names.h google/protobuf/compiler/importer.h google/protobuf/compiler/java/context.h google/protobuf/compiler/java/doc_comment.h -google/protobuf/compiler/java/field_common.h -google/protobuf/compiler/java/file.h -google/protobuf/compiler/java/full/enum.h -google/protobuf/compiler/java/full/enum_field.h -google/protobuf/compiler/java/full/extension.h -google/protobuf/compiler/java/full/field_generator.h -google/protobuf/compiler/java/full/generator_factory.h -google/protobuf/compiler/java/full/make_field_gens.h -google/protobuf/compiler/java/full/map_field.h -google/protobuf/compiler/java/full/message.h -google/protobuf/compiler/java/full/message_builder.h -google/protobuf/compiler/java/full/message_field.h -google/protobuf/compiler/java/full/primitive_field.h -google/protobuf/compiler/java/full/service.h -google/protobuf/compiler/java/full/string_field.h google/protobuf/compiler/java/generator.h -google/protobuf/compiler/java/generator_common.h -google/protobuf/compiler/java/generator_factory.h google/protobuf/compiler/java/helpers.h google/protobuf/compiler/java/java_features.pb.h -google/protobuf/compiler/java/lite/enum.h -google/protobuf/compiler/java/lite/enum_field.h -google/protobuf/compiler/java/lite/extension.h -google/protobuf/compiler/java/lite/field_generator.h -google/protobuf/compiler/java/lite/generator_factory.h -google/protobuf/compiler/java/lite/make_field_gens.h -google/protobuf/compiler/java/lite/map_field.h -google/protobuf/compiler/java/lite/message.h -google/protobuf/compiler/java/lite/message_builder.h -google/protobuf/compiler/java/lite/message_field.h -google/protobuf/compiler/java/lite/primitive_field.h -google/protobuf/compiler/java/lite/string_field.h -google/protobuf/compiler/java/message_serialization.h google/protobuf/compiler/java/name_resolver.h google/protobuf/compiler/java/names.h google/protobuf/compiler/java/options.h -google/protobuf/compiler/java/shared_code_generator.h -google/protobuf/compiler/kotlin/field.h -google/protobuf/compiler/kotlin/file.h -google/protobuf/compiler/kotlin/generator.h -google/protobuf/compiler/kotlin/message.h -google/protobuf/compiler/objectivec/enum.h -google/protobuf/compiler/objectivec/enum_field.h -google/protobuf/compiler/objectivec/extension.h -google/protobuf/compiler/objectivec/field.h -google/protobuf/compiler/objectivec/file.h -google/protobuf/compiler/objectivec/generator.h -google/protobuf/compiler/objectivec/helpers.h -google/protobuf/compiler/objectivec/import_writer.h google/protobuf/compiler/objectivec/line_consumer.h -google/protobuf/compiler/objectivec/map_field.h -google/protobuf/compiler/objectivec/message.h -google/protobuf/compiler/objectivec/message_field.h google/protobuf/compiler/objectivec/names.h google/protobuf/compiler/objectivec/nsobject_methods.h -google/protobuf/compiler/objectivec/oneof.h -google/protobuf/compiler/objectivec/options.h -google/protobuf/compiler/objectivec/primitive_field.h -google/protobuf/compiler/objectivec/tf_decode_data.h google/protobuf/compiler/parser.h google/protobuf/compiler/php/names.h -google/protobuf/compiler/php/php_generator.h google/protobuf/compiler/plugin.h google/protobuf/compiler/plugin.pb.h google/protobuf/compiler/plugin.proto -google/protobuf/compiler/python/generator.h -google/protobuf/compiler/python/helpers.h -google/protobuf/compiler/python/pyi_generator.h google/protobuf/compiler/retention.h -google/protobuf/compiler/ruby/ruby_generator.h -google/protobuf/compiler/rust/accessors/accessor_case.h -google/protobuf/compiler/rust/accessors/accessors.h -google/protobuf/compiler/rust/accessors/default_value.h -google/protobuf/compiler/rust/accessors/generator.h -google/protobuf/compiler/rust/accessors/with_presence.h -google/protobuf/compiler/rust/context.h -google/protobuf/compiler/rust/crate_mapping.h -google/protobuf/compiler/rust/enum.h -google/protobuf/compiler/rust/generator.h -google/protobuf/compiler/rust/message.h -google/protobuf/compiler/rust/naming.h -google/protobuf/compiler/rust/oneof.h -google/protobuf/compiler/rust/relative_path.h -google/protobuf/compiler/rust/rust_field_type.h -google/protobuf/compiler/rust/rust_keywords.h -google/protobuf/compiler/rust/upb_helpers.h google/protobuf/compiler/scc.h google/protobuf/compiler/subprocess.h google/protobuf/compiler/versions.h @@ -224,7 +122,6 @@ google/protobuf/stubs/common.h google/protobuf/stubs/platform_macros.h google/protobuf/stubs/port.h google/protobuf/stubs/status_macros.h -google/protobuf/testing/file.h google/protobuf/text_format.h google/protobuf/thread_safe_arena.h google/protobuf/timestamp.pb.h @@ -285,12 +182,8 @@ upb/mini_table/file.h upb/mini_table/message.h upb/mini_table/sub.h upb/port/atomic.h -upb/port/atomic.h -upb/port/def.inc upb/port/def.inc upb/port/undef.inc -upb/port/undef.inc -upb/port/vsnprintf_compat.h upb/port/vsnprintf_compat.h upb/reflection/common.h upb/reflection/def.h @@ -322,7 +215,5 @@ upb/wire/encode.h upb/wire/eps_copy_input_stream.h upb/wire/reader.h upb/wire/types.h -upb_generator/common/names.h -upb_generator/minitable/names.h utf8_range.h utf8_validity.h diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 7976546fec35f..2aaedb5d9a965 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -225,19 +225,23 @@ file(GLOB_RECURSE _local_upb_hdrs "${PROJECT_SOURCE_DIR}/upb/*.h" ) +# Exclude test library headers. +list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs} + ${compiler_test_utils_hdrs} ${upb_test_util_files} ${libprotoc_hdrs}) +foreach(_hdr ${_exclude_hdrs}) + list(REMOVE_ITEM _local_hdrs ${_hdr}) + list(REMOVE_ITEM _local_upb_hdrs ${_hdr}) +endforeach() +list(APPEND _local_hdrs ${libprotoc_public_hdrs}) + # Exclude the bootstrapping that are directly used by tests. set(_exclude_hdrs "${protobuf_SOURCE_DIR}/src/google/protobuf/cpp_features.pb.h" "${protobuf_SOURCE_DIR}/src/google/protobuf/descriptor.pb.h" "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/plugin.pb.h" "${protobuf_SOURCE_DIR}/src/google/protobuf/compiler/java/java_features.pb.h") - -# Exclude test library headers. -list(APPEND _exclude_hdrs ${test_util_hdrs} ${lite_test_util_hdrs} ${common_test_hdrs} - ${compiler_test_utils_hdrs} ${upb_test_util_files}) foreach(_hdr ${_exclude_hdrs}) list(REMOVE_ITEM _local_hdrs ${_hdr}) - list(REMOVE_ITEM _local_upb_hdrs ${_hdr}) endforeach() foreach(_hdr ${_local_hdrs}) diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 6285a835fe086..3cb3e1000c5c6 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -110,6 +110,7 @@ gen_file_lists( # Libraries: ":protobuf": "libprotobuf", ":protobuf_lite": "libprotobuf_lite", + ":protoc_public": "libprotoc_public", ":protoc": "libprotoc", ":upb": "libupb", ":protoc-gen-upb": "protoc-gen-upb", @@ -209,6 +210,23 @@ cc_dist_library( ], ) +cc_dist_library( + name = "protoc_public", + dist_deps = [ + ":protobuf", + ":protobuf_lite", + ], + tags = ["manual"], + deps = [ + "//src/google/protobuf/compiler:command_line_interface", + "//src/google/protobuf/compiler/cpp:names", + "//src/google/protobuf/compiler/csharp:names", + "//src/google/protobuf/compiler/java:names", + "//src/google/protobuf/compiler/objectivec:names", + "//src/google/protobuf/compiler/php:names", + ], +) + cc_dist_library( name = "upb", tags = ["manual"], diff --git a/src/google/protobuf/compiler/rust/BUILD.bazel b/src/google/protobuf/compiler/rust/BUILD.bazel index b0ad41a555f72..13752ec6b3a51 100644 --- a/src/google/protobuf/compiler/rust/BUILD.bazel +++ b/src/google/protobuf/compiler/rust/BUILD.bazel @@ -52,7 +52,6 @@ cc_library( deps = [ ":context", "//src/google/protobuf", - "//src/google/protobuf/testing:file", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", diff --git a/src/google/protobuf/compiler/rust/crate_mapping.cc b/src/google/protobuf/compiler/rust/crate_mapping.cc index ebea7dc2633c6..ae881fc41a034 100644 --- a/src/google/protobuf/compiler/rust/crate_mapping.cc +++ b/src/google/protobuf/compiler/rust/crate_mapping.cc @@ -7,7 +7,6 @@ #include #include -#include "google/protobuf/testing/file.h" #include "absl/container/flat_hash_map.h" #include "absl/status/status.h" #include "absl/status/statusor.h" @@ -22,6 +21,33 @@ namespace protobuf { namespace compiler { namespace rust { +// We would love to use //file/base here, but that creates a dependency cycle, +// since //file/base transitively depends on protoc. +namespace { +struct File { + static absl::Status ReadFileToString(const std::string& name, + std::string* output, bool text_mode) { + char buffer[1024]; + FILE* file = fopen(name.c_str(), text_mode ? "rt" : "rb"); + if (file == nullptr) return absl::NotFoundError("Could not open file"); + + while (true) { + size_t n = fread(buffer, 1, sizeof(buffer), file); + if (n <= 0) break; + output->append(buffer, n); + } + + int error = ferror(file); + if (fclose(file) != 0) return absl::InternalError("Failed to close file"); + if (error != 0) { + return absl::InternalError(absl::StrCat("Failed to read the file ", name, + ". Error code: ", error)); + } + return absl::OkStatus(); + } +}; +} // namespace + absl::StatusOr> GetImportPathToCrateNameMap(const Options* opts) { absl::flat_hash_map mapping; diff --git a/src/google/protobuf/testing/BUILD.bazel b/src/google/protobuf/testing/BUILD.bazel index 66b4f4abe9954..71bf03a3c11c5 100644 --- a/src/google/protobuf/testing/BUILD.bazel +++ b/src/google/protobuf/testing/BUILD.bazel @@ -11,6 +11,7 @@ package( cc_library( name = "file", + testonly = 1, srcs = ["file.cc"], hdrs = ["file.h"], copts = COPTS,