Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate protocol buffers into WAF

I managed to compile my .proto files like this:

def build(bld):
    bld(rule='protoc --cpp_out=. -I.. ${SRC}', source='a.proto b.proto', name='genproto')

Seems to work nice, when I make changes to the source files, they are recompiled and so on. But the result would be files called build/a.pb.cc and build/b.pb.cc which I need to include into my main programs source list. Of course I know how to manually construct them from my protocol buffers file names, but I don't think this is the way to go. Can anyone provide me a hint?

Best regards, Philipp

UPDATE

With patient help from the IRC people I was able to manage to build a tool, as suggested below.

#!/usr/bin/env python
# encoding: utf-8
# Philipp Bender, 2012

from waflib.Task import Task
from waflib.TaskGen import extension 

"""
A simple tool to integrate protocol buffers into your build system.

    def configure(conf):
        conf.load('compiler_cxx cxx protoc_cxx')

    def build(bld):
    bld.program(source = "main.cpp file1.proto proto/file2.proto", 
            target = "executable") 

"""

class protoc(Task):
    run_str = '${PROTOC} ${SRC} --cpp_out=. -I..'
    color = 'BLUE'
    ext_out = ['.h', 'pb.cc']

@extension('.proto')
def process_protoc(self, node):
    cpp_node = node.change_ext('.pb.cc')
    hpp_node = node.change_ext('.pb.h')
    self.create_task('protoc', node, [cpp_node, hpp_node])
    self.source.append(cpp_node)
    self.env.append_value('INCLUDES', ['.'] )

    self.use = self.to_list(getattr(self, 'use', '')) + ['PROTOBUF']

def configure(conf):
    conf.check_cfg(package="protobuf", uselib_store="PROTOBUF", 
            args=['--cflags', '--libs'])
    conf.find_program('protoc', var='PROTOC')

You can also find it in the bugtracker:

https://code.google.com/p/waf/issues/detail?id=1184

like image 270
wal-o-mat Avatar asked Jun 30 '12 12:06

wal-o-mat


1 Answers

This kind of processing is documented in the Waf book (look for "idl").

However I'm pretty sure a protobuf tool would be welcomed by the community, so I suggest you attempt to create one and submit it for review on the bug tracker or on IRC. This way, you'll have less maintenance burden, a shorter wscript.

I would expect to use the tool like this:

bld(
 name="protobufs",
 features="protoc cxx",
 source=["protobuf/a.proto", "protobuf/b.proto"],
 includes=["protobuf", "..."],
)

bld(
 target="test",
 features="cxx cxxprogram",
 source="test.cpp",
 use="protobufs", # uses the generated C++ code, links to -lprotobuf
)

Or something like that.

like image 164
cJ Zougloub Avatar answered Oct 01 '22 20:10

cJ Zougloub