Skip to content

Commit

Permalink
Split protoc apart from libprotoc in our cmake configs.
Browse files Browse the repository at this point in the history
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: 709133494
  • Loading branch information
mkruskal-google authored and copybara-github committed Dec 24, 2024
1 parent 1223341 commit 9246d5d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 111 deletions.
3 changes: 1 addition & 2 deletions cmake/install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -120,7 +120,6 @@ foreach(_header ${protobuf_HEADERS})
break()
endif()
endforeach()
message(${_from_dir} "-" ${_header})

# Escape _from_dir for regex special characters in the directory name.
string(REGEX REPLACE "([$^.[|*+?()]|])" "\\\\\\1" _from_dir_regexp "${_from_dir}")
Expand Down
106 changes: 0 additions & 106 deletions cmake/installed_include_golden.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -285,12 +183,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
Expand Down
3 changes: 2 additions & 1 deletion cmake/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,12 @@ set(_exclude_hdrs

# 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})
${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})

foreach(_hdr ${_local_hdrs})
string(REPLACE "${protobuf_SOURCE_DIR}/src" "" _file ${_hdr})
Expand Down
18 changes: 18 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"],
Expand Down
1 change: 0 additions & 1 deletion src/google/protobuf/compiler/rust/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 27 additions & 1 deletion src/google/protobuf/compiler/rust/crate_mapping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <string>
#include <vector>

#include "google/protobuf/testing/file.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
Expand All @@ -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<absl::flat_hash_map<std::string, std::string>>
GetImportPathToCrateNameMap(const Options* opts) {
absl::flat_hash_map<std::string, std::string> mapping;
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package(

cc_library(
name = "file",
testonly = 1,
srcs = ["file.cc"],
hdrs = ["file.h"],
copts = COPTS,
Expand Down

0 comments on commit 9246d5d

Please sign in to comment.