Change target to output filenames

This commit is contained in:
Bo Yang 2015-02-07 15:28:54 -08:00
parent 1d627f85c3
commit e2555e235f
3 changed files with 73 additions and 25 deletions

View File

@ -48,7 +48,6 @@
#include <iostream> #include <iostream>
#include <ctype.h> #include <ctype.h>
#include <google/protobuf/stubs/hash.h>
#include <memory> #include <memory>
#ifndef _SHARED_PTR_H #ifndef _SHARED_PTR_H
#include <google/protobuf/stubs/shared_ptr.h> #include <google/protobuf/stubs/shared_ptr.h>
@ -254,6 +253,9 @@ class CommandLineInterface::GeneratorContextImpl : public GeneratorContext {
// format, unless one has already been written. // format, unless one has already been written.
void AddJarManifest(); void AddJarManifest();
// Get name of all output files.
void GetOutputFilenames(vector<string>* output_filenames);
// implements GeneratorContext -------------------------------------- // implements GeneratorContext --------------------------------------
io::ZeroCopyOutputStream* Open(const string& filename); io::ZeroCopyOutputStream* Open(const string& filename);
io::ZeroCopyOutputStream* OpenForAppend(const string& filename); io::ZeroCopyOutputStream* OpenForAppend(const string& filename);
@ -441,6 +443,14 @@ void CommandLineInterface::GeneratorContextImpl::AddJarManifest() {
} }
} }
void CommandLineInterface::GeneratorContextImpl::GetOutputFilenames(
vector<string>* output_filenames) {
for (map<string, string*>::iterator iter = files_.begin();
iter != files_.end(); ++iter) {
output_filenames->push_back(iter->first);
}
}
io::ZeroCopyOutputStream* CommandLineInterface::GeneratorContextImpl::Open( io::ZeroCopyOutputStream* CommandLineInterface::GeneratorContextImpl::Open(
const string& filename) { const string& filename) {
return new MemoryOutputStream(this, filename, false); return new MemoryOutputStream(this, filename, false);
@ -670,7 +680,6 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) {
// We construct a separate GeneratorContext for each output location. Note // We construct a separate GeneratorContext for each output location. Note
// that two code generators may output to the same location, in which case // that two code generators may output to the same location, in which case
// they should share a single GeneratorContext so that OpenForInsert() works. // they should share a single GeneratorContext so that OpenForInsert() works.
typedef hash_map<string, GeneratorContextImpl*> GeneratorContextMap;
GeneratorContextMap output_directories; GeneratorContextMap output_directories;
// Generate output. // Generate output.
@ -717,16 +726,17 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) {
} }
} }
STLDeleteValues(&output_directories); if (!dependency_out_name_.empty()) {
if (!GenerateDependencyManifestFile(parsed_files, output_directories,
if (!descriptor_set_name_.empty()) { &source_tree)) {
if (!WriteDescriptorSet(parsed_files)) {
return 1; return 1;
} }
} }
if (!dependency_out_name_.empty()) { STLDeleteValues(&output_directories);
if (!GenerateDependencyManifestFile(parsed_files, &source_tree)) {
if (!descriptor_set_name_.empty()) {
if (!WriteDescriptorSet(parsed_files)) {
return 1; return 1;
} }
} }
@ -878,6 +888,15 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) {
cerr << "Missing output directives." << endl; cerr << "Missing output directives." << endl;
return PARSE_ARGUMENT_FAIL; return PARSE_ARGUMENT_FAIL;
} }
if (mode_ != MODE_COMPILE && !dependency_out_name_.empty()) {
cerr << "Can only use --dependency_out=FILE when generating code." << endl;
return PARSE_ARGUMENT_FAIL;
}
if (!dependency_out_name_.empty() && input_files_.size() > 1) {
cerr << "Can only process one input file when using --dependency_out=FILE."
<< endl;
return PARSE_ARGUMENT_FAIL;
}
if (imports_in_descriptor_set_ && descriptor_set_name_.empty()) { if (imports_in_descriptor_set_ && descriptor_set_name_.empty()) {
cerr << "--include_imports only makes sense when combined with " cerr << "--include_imports only makes sense when combined with "
"--descriptor_set_out." << endl; "--descriptor_set_out." << endl;
@ -1028,11 +1047,6 @@ CommandLineInterface::InterpretArgument(const string& name,
cerr << name << " requires a non-empty value." << endl; cerr << name << " requires a non-empty value." << endl;
return PARSE_ARGUMENT_FAIL; return PARSE_ARGUMENT_FAIL;
} }
if (mode_ != MODE_COMPILE) {
cerr << "Cannot use --encode or --decode and --dependency_out=FILE at "
"the same time." << endl;
return PARSE_ARGUMENT_FAIL;
}
dependency_out_name_ = value; dependency_out_name_ = value;
} else if (name == "--include_imports") { } else if (name == "--include_imports") {
@ -1310,6 +1324,7 @@ bool CommandLineInterface::GenerateOutput(
bool CommandLineInterface::GenerateDependencyManifestFile( bool CommandLineInterface::GenerateDependencyManifestFile(
const vector<const FileDescriptor*>& parsed_files, const vector<const FileDescriptor*>& parsed_files,
const GeneratorContextMap& output_directories,
DiskSourceTree* source_tree) { DiskSourceTree* source_tree) {
FileDescriptorSet file_set; FileDescriptorSet file_set;
@ -1321,6 +1336,18 @@ bool CommandLineInterface::GenerateDependencyManifestFile(
file_set.mutable_file()); file_set.mutable_file());
} }
vector<string> output_filenames;
for (GeneratorContextMap::const_iterator iter = output_directories.begin();
iter != output_directories.end(); ++iter) {
const string& location = iter->first;
GeneratorContextImpl* directory = iter->second;
vector<string> relative_output_filenames;
directory->GetOutputFilenames(&relative_output_filenames);
for (int i = 0; i < relative_output_filenames.size(); i++) {
output_filenames.push_back(location + relative_output_filenames[i]);
}
}
int fd; int fd;
do { do {
fd = open(dependency_out_name_.c_str(), fd = open(dependency_out_name_.c_str(),
@ -1335,15 +1362,15 @@ bool CommandLineInterface::GenerateDependencyManifestFile(
io::FileOutputStream out(fd); io::FileOutputStream out(fd);
io::Printer printer(&out, '$'); io::Printer printer(&out, '$');
if (dependency_out_name_.compare(0, 1, "/") != 0) { for (int i = 0; i < output_filenames.size(); i++) {
// Convert relative path to absolute path before print. printer.Print(output_filenames[i].c_str());
printer.Print("$working_directory$/$output_filename$:", if (i == output_filenames.size() - 1) {
"working_directory", get_current_dir_name(), printer.Print(":");
"output_filename", dependency_out_name_); } else {
} else { printer.Print(" \\\n");
printer.Print("$output_filename$:", }
"output_filename", dependency_out_name_);
} }
for (int i = 0; i < file_set.file_size(); i++) { for (int i = 0; i < file_set.file_size(); i++) {
const FileDescriptorProto& file = file_set.file(i); const FileDescriptorProto& file = file_set.file(i);
const string& virtual_file = file.name(); const string& virtual_file = file.name();

View File

@ -39,6 +39,7 @@
#define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
#include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/hash.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <map> #include <map>
@ -190,6 +191,7 @@ class LIBPROTOC_EXPORT CommandLineInterface {
class ErrorPrinter; class ErrorPrinter;
class GeneratorContextImpl; class GeneratorContextImpl;
class MemoryOutputStream; class MemoryOutputStream;
typedef hash_map<string, GeneratorContextImpl*> GeneratorContextMap;
// Clear state from previous Run(). // Clear state from previous Run().
void Clear(); void Clear();
@ -250,6 +252,7 @@ class LIBPROTOC_EXPORT CommandLineInterface {
// Implements the --dependency_out option // Implements the --dependency_out option
bool GenerateDependencyManifestFile( bool GenerateDependencyManifestFile(
const vector<const FileDescriptor*>& parsed_files, const vector<const FileDescriptor*>& parsed_files,
const GeneratorContextMap& output_directories,
DiskSourceTree* source_tree); DiskSourceTree* source_tree);
// Get all transitive dependencies of the given file (including the file // Get all transitive dependencies of the given file (including the file

View File

@ -959,6 +959,24 @@ TEST_F(CommandLineInterfaceTest, WriteTransitiveDescriptorSetWithSourceInfo) {
EXPECT_TRUE(descriptor_set.file(1).has_source_code_info()); EXPECT_TRUE(descriptor_set.file(1).has_source_code_info());
} }
TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileGivenTwoInputs) {
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"message Foo {}\n");
CreateTempFile("bar.proto",
"syntax = \"proto2\";\n"
"import \"foo.proto\";\n"
"message Bar {\n"
" optional Foo foo = 1;\n"
"}\n");
Run("protocol_compiler --dependency_out=$tmpdir/manifest "
"--test_out=$tmpdir --proto_path=$tmpdir bar.proto foo.proto");
ExpectErrorText(
"Can only process one input file when using --dependency_out=FILE.\n");
}
TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFile) { TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFile) {
CreateTempFile("foo.proto", CreateTempFile("foo.proto",
"syntax = \"proto2\";\n" "syntax = \"proto2\";\n"
@ -976,8 +994,8 @@ TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFile) {
ExpectNoErrors(); ExpectNoErrors();
ExpectFileContent("manifest", ExpectFileContent("manifest",
"$tmpdir/manifest: $tmpdir/foo.proto\\\n" "$tmpdir/bar.proto.MockCodeGenerator.test_generator: "
" $tmpdir/bar.proto"); "$tmpdir/foo.proto\\\n $tmpdir/bar.proto");
} }
TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileForRelativePath) { TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileForRelativePath) {
@ -1000,8 +1018,8 @@ TEST_F(CommandLineInterfaceTest, WriteDependencyManifestFileForRelativePath) {
ExpectNoErrors(); ExpectNoErrors();
ExpectFileContent("manifest", ExpectFileContent("manifest",
"$tmpdir/manifest: $tmpdir/foo.proto\\\n" "$tmpdir/bar.proto.MockCodeGenerator.test_generator: "
" $tmpdir/bar.proto"); "$tmpdir/foo.proto\\\n $tmpdir/bar.proto");
File::ChangeWorkingDirectory(current_working_directory); File::ChangeWorkingDirectory(current_working_directory);
} }