Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert c++ header file to protobuf .proto file

I am wondering, is there any tool that I can use to convert a .h file into a .proto file for c++? What I want to do is: I have several structs into a .h file, and I want to serialize them and send them via protbuf. To do that, I need a .proto file to generate the .pb.h and .pb.cc files and then I will be able serialize the structs and send them.

Does anybody know if there is such a tool? I could do it manually but I will take much time because the header file contains many structs.

Thank you!

I am editing my question to let you know that I haven't found what I was asking for, but I found this https://code.google.com/p/protobuf-csharp-port/wiki/ProtoGen tool which was also very helpful. You give as parameter the .proto file and it generates the c# code. Hope it helps!

like image 336
CompuPlanet Avatar asked Nov 05 '14 11:11

CompuPlanet


1 Answers

You can write a script that will do a good job using libclang Python bindings.

First, generate a compile_commands.json using CMake for your project. Parse the complete compiler command line for your code of interest.

Second, pass the compile command line to libclang from your Python script to parse a compilation unit, that contains your header of interest.

Third, iterate the AST and extract your structs of interest.

Fourth, generate protobuf from the isolated AST sub-trees.

This is just a sketch, but I did something similar to recursively find all headers included in a compilation unit.

like image 69
malik Avatar answered Sep 22 '22 15:09

malik