让protobuf生成器支持时间戳检查
- Ease - C++博客-首页原创精华区使用protobuf的生成器可以对proto文件进行解析后生成指定的目标语言代码.随着项目的不断扩大, 协议修改变的非常频繁, 因此每次重编变的异常耗时. 模仿C/C++编译器的时间戳检查生成机制,我给protobuf生成器添加了时间戳检查功能. 此功能不是必须的, 可以通过命令行指定—timestampfile FILE 来指定要生成的proto文件对应的时间戳.
使用protobuf的生成器可以对proto文件进行解析后生成指定的目标语言代码.随着项目的不断扩大, 协议修改变的非常频繁, 因此每次重编变的异常耗时. 模仿C/C++编译器的时间戳检查生成机制,我给protobuf生成器添加了时间戳检查功能. 下面说下原理:
此功能不是必须的, 可以通过命令行指定—timestampfile FILE 来指定要生成的proto文件对应的时间戳
每次运行解析器时, 解析器通过命令行获取proto文件, 我们可以先读取之前保存的时间戳, 然后与这个文件的当前修改时间进行对比
如果时间戳不相等,说明文件已经被修改, 需要重新生成.之后更新时间戳文件即可.
下面是在protobuf的protoc工程里添加的代码:
command_line_interface.cc
void CommandLineInterface::ReadTimeStampFile( const string& timestampfile ) { FILE* f = fopen( timestampfile.c_str(), "rb" ); if ( f == NULL ) return; time_stamp_map_.clear( ); while( !feof( f ) ) { size_t read = 0; int strlen = 0; read = fread( &strlen, 1 , sizeof strlen, f ); if ( read == 0 ) break; std::string filename; filename.resize( strlen ); read = fread( (void*)filename.data(), 1, sizeof(char) * strlen, f ); if ( read == 0 ) break; __time64_t timestamp; read = fread( ×tamp, 1, sizeof(timestamp), f ); if ( read == 0 ) break; time_stamp_map_[filename] = timestamp; } fclose( f ); } void CommandLineInterface::WriteTimeStampFile( const string& timestampfile ) { FILE* f = fopen( timestampfile.c_str(), "wb" ); if ( f == NULL ) return; for ( time_stamp_map::iterator it = time_stamp_map_.begin(); it != time_stamp_map_.end(); it++) { const std::string& filename = it->first; __time64_t timestamp = it->second; int strlen = filename.length(); fwrite( &strlen, 1, sizeof(strlen), f ); fwrite( (void*)filename.data(), 1, sizeof(char)*strlen, f ); fwrite( ×tamp, 1, sizeof( timestamp ), f ); } fclose( f ); } bool CommandLineInterface::NeedGenerate( const std::string& filename ) { struct _stat buf; if ( _stat( filename.c_str(), &buf ) != 0 ) { // file error return true; } time_stamp_map::iterator it = time_stamp_map_.find( filename ); if ( it != time_stamp_map_.end() ) { __time64_t& timestamp = it->second; if ( timestamp == buf.st_mtime ) return false; } // don't hold the time stamp or time stamp not match, generate it! // save to map , then write time stamp time_stamp_map_[filename] = buf.st_mtime; return true; } void CommandLineInterface::CheckFileStamp( const string& timestampfile ) { ReadTimeStampFile( timestampfile ); for (vector<string>::iterator it = input_files_.begin(); it != input_files_.end();) { if ( !NeedGenerate( *it) ) { it = input_files_.erase( it ); } else { ++it; } } WriteTimeStampFile( timestampfile ); }
以上代码为核心逻辑, 之后在int CommandLineInterface::Run(int argc, const char* const argv[]) 函数中添加代码:
int CommandLineInterface::Run(int argc, const char* const argv[]) { Clear(); if (!ParseArguments(argc, argv)) return 1; // Set up the source tree. DiskSourceTree source_tree; for (int i = 0; i < proto_path_.size(); i++) { source_tree.MapPath(proto_path_[i].first, proto_path_[i].second); } // Map input files to virtual paths if necessary. if (!inputs_are_proto_path_relative_) { if (!MakeInputsBeProtoPathRelative(&source_tree)) { return 1; } } // Allocate the Importer. ErrorPrinter error_collector(error_format_, &source_tree); Importer importer(&source_tree, &error_collector); vector<const FileDescriptor*> parsed_files; if ( time_stamp_filename_ != "" ) CheckFileStamp( time_stamp_filename_ );
加黑部分为添加的代码, 这里是检查时间戳的入口
同时,我们还需要添加命令行解析开关, 这里在
bool CommandLineInterface::InterpretArgument(const string& name,
const string& value) {
函数中,找到:
} else if (name == "--error_format") { if (value == "gcc") { error_format_ = ERROR_FORMAT_GCC; } else if (value == "msvs") { error_format_ = ERROR_FORMAT_MSVS; } else { cerr << "Unknown error format: " << value << endl; return false; } } else if ( name == "--timestampfile" ){ time_stamp_filename_ = value; } else if (name == "--plugin") { if (plugin_prefix_.empty()) { cerr << "This compiler does not support plugins." << endl; return false; }
加黑部分为解析开关
之后在头文件中添加声明代码:
// Time stamp function void ReadTimeStampFile( const string& timestampfile ); void WriteTimeStampFile( const string& timestampfile ); bool NeedGenerate( const std::string& filename ); void CheckFileStamp( const string& timestampfile ); typedef std::map<std::string, __time64_t> time_stamp_map; // proto file name as key, timestamp as value time_stamp_map time_stamp_map_; string time_stamp_filename_;
转载请注明: 战魂小筑http://www.cppblog.com/sunicdavy