Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # encoding: utf-8 |
| 3 | # Philipp Bender, 2012 |
| 4 | # Matt Clarkson, 2012 |
| 5 | |
| 6 | import re |
| 7 | from waflib.Task import Task |
| 8 | from waflib.TaskGen import extension |
| 9 | |
| 10 | """ |
| 11 | A simple tool to integrate protocol buffers into your build system. |
| 12 | |
| 13 | Example:: |
| 14 | |
| 15 | def configure(conf): |
| 16 | conf.load('compiler_cxx cxx protoc') |
| 17 | |
| 18 | def build(bld): |
| 19 | bld( |
| 20 | features = 'cxx cxxprogram' |
| 21 | source = 'main.cpp file1.proto proto/file2.proto', |
| 22 | include = '. proto', |
| 23 | target = 'executable') |
| 24 | |
| 25 | Notes when using this tool: |
| 26 | |
| 27 | - protoc command line parsing is tricky. |
| 28 | |
| 29 | The generated files can be put in subfolders which depend on |
| 30 | the order of the include paths. |
| 31 | |
| 32 | Try to be simple when creating task generators |
| 33 | containing protoc stuff. |
| 34 | |
| 35 | """ |
| 36 | |
| 37 | class protoc(Task): |
| 38 | # protoc expects the input proto file to be an absolute path. |
| 39 | run_str = '${PROTOC} ${PROTOC_FLAGS} ${PROTOC_ST:INCPATHS} ${SRC[0].abspath()}' |
| 40 | color = 'BLUE' |
| 41 | ext_out = ['.h', 'pb.cc'] |
| 42 | def scan(self): |
| 43 | """ |
| 44 | Scan .proto dependencies |
| 45 | """ |
| 46 | node = self.inputs[0] |
| 47 | |
| 48 | nodes = [] |
| 49 | names = [] |
| 50 | seen = [] |
| 51 | |
| 52 | if not node: return (nodes, names) |
| 53 | |
| 54 | search_paths = [self.generator.path.find_node(x) for x in self.generator.includes] |
| 55 | |
| 56 | def parse_node(node): |
| 57 | if node in seen: |
| 58 | return |
| 59 | seen.append(node) |
| 60 | code = node.read().split("\n") |
| 61 | for line in code: |
| 62 | m = re.search(r'^import\s+"(.*)";.*(//)?.*', line) |
| 63 | if m: |
| 64 | dep = m.groups()[0] |
| 65 | for incpath in search_paths: |
| 66 | found = incpath.find_resource(dep) |
| 67 | if found: |
| 68 | nodes.append(found) |
| 69 | parse_node(found) |
| 70 | else: |
| 71 | names.append(dep) |
| 72 | |
| 73 | parse_node(node) |
| 74 | return (nodes, names) |
| 75 | |
| 76 | @extension('.proto') |
| 77 | def process_protoc(self, node): |
| 78 | cpp_node = node.change_ext('.pb.cc') |
| 79 | hpp_node = node.change_ext('.pb.h') |
| 80 | self.create_task('protoc', node, [cpp_node, hpp_node]) |
| 81 | self.source.append(cpp_node) |
| 82 | |
| 83 | if 'cxx' in self.features and not self.env.PROTOC_FLAGS: |
| 84 | self.env.PROTOC_FLAGS = '--cpp_out=%s' % node.parent.get_bld().abspath() |
| 85 | |
| 86 | use = getattr(self, 'use', '') |
| 87 | if not 'PROTOBUF' in use: |
| 88 | self.use = self.to_list(use) + ['PROTOBUF'] |
| 89 | |
| 90 | def configure(conf): |
| 91 | conf.check_cfg(package="protobuf", uselib_store="PROTOBUF", args=['--cflags', '--libs']) |
| 92 | conf.find_program('protoc', var='PROTOC') |
| 93 | conf.env.PROTOC_ST = '-I%s' |
| 94 | |