战魂小筑

讨论群:309800774 知乎关注:http://zhihu.com/people/sunicdavy 开源项目:https://github.com/davyxu

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  257 随笔 :: 0 文章 :: 506 评论 :: 0 Trackbacks

使用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( &timestamp, 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( &timestamp, 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
posted on 2011-08-16 11:38 战魂小筑 阅读(3800) 评论(1)  编辑 收藏 引用 所属分类: C++/ 编程语言工具使用及设计

评论

# re: 让protobuf生成器支持时间戳检查[未登录] 2011-11-11 16:02 frank28_nfls
为什么不用make来做这个呢?呵呵  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理