Skip to content

Commit a44fc2b

Browse files
compiler: Correct depfile generation when there are no outputs
Prevents the compiler from generating a malformed depfile if an inpout file did not lead to any meaningful generation. This is an edge case that is not exercised often. PiperOrigin-RevId: 542354842
1 parent b0b926a commit a44fc2b

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/google/protobuf/compiler/command_line_interface.cc

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,7 @@ bool CommandLineInterface::GenerateDependencyManifestFile(
25202520
output_filenames.push_back(descriptor_set_out_name_);
25212521
}
25222522

2523+
// Create the depfile, even if it will be empty.
25232524
int fd;
25242525
do {
25252526
fd = open(dependency_out_name_.c_str(),
@@ -2531,30 +2532,34 @@ bool CommandLineInterface::GenerateDependencyManifestFile(
25312532
return false;
25322533
}
25332534

2534-
io::FileOutputStream out(fd);
2535-
io::Printer printer(&out, '$');
2535+
// Only write to the depfile if there is at least one output_filename.
2536+
// Otherwise, the depfile will be malformed.
2537+
if (!output_filenames.empty()) {
2538+
io::FileOutputStream out(fd);
2539+
io::Printer printer(&out, '$');
25362540

2537-
for (int i = 0; i < output_filenames.size(); i++) {
2538-
printer.Print(output_filenames[i]);
2539-
if (i == output_filenames.size() - 1) {
2540-
printer.Print(":");
2541-
} else {
2542-
printer.Print(" \\\n");
2541+
for (size_t i = 0; i < output_filenames.size(); i++) {
2542+
printer.Print(output_filenames[i]);
2543+
if (i == output_filenames.size() - 1) {
2544+
printer.Print(":");
2545+
} else {
2546+
printer.Print(" \\\n");
2547+
}
25432548
}
2544-
}
25452549

2546-
for (int i = 0; i < file_set.file_size(); i++) {
2547-
const FileDescriptorProto& file = file_set.file(i);
2548-
const std::string& virtual_file = file.name();
2549-
std::string disk_file;
2550-
if (source_tree &&
2551-
source_tree->VirtualFileToDiskFile(virtual_file, &disk_file)) {
2552-
printer.Print(" $disk_file$", "disk_file", disk_file);
2553-
if (i < file_set.file_size() - 1) printer.Print("\\\n");
2554-
} else {
2555-
std::cerr << "Unable to identify path for file " << virtual_file
2556-
<< std::endl;
2557-
return false;
2550+
for (int i = 0; i < file_set.file_size(); i++) {
2551+
const FileDescriptorProto& file = file_set.file(i);
2552+
const std::string& virtual_file = file.name();
2553+
std::string disk_file;
2554+
if (source_tree &&
2555+
source_tree->VirtualFileToDiskFile(virtual_file, &disk_file)) {
2556+
printer.Print(" $disk_file$", "disk_file", disk_file);
2557+
if (i < file_set.file_size() - 1) printer.Print("\\\n");
2558+
} else {
2559+
std::cerr << "Unable to identify path for file " << virtual_file
2560+
<< std::endl;
2561+
return false;
2562+
}
25582563
}
25592564
}
25602565

0 commit comments

Comments
 (0)