Skip to content

Instantly share code, notes, and snippets.

@cbsmith
Created April 16, 2012 05:15
Show Gist options
  • Save cbsmith/2396439 to your computer and use it in GitHub Desktop.
Save cbsmith/2396439 to your computer and use it in GitHub Desktop.
Makefile dependency generator plugin for protoc.
#!/usr/bin/python
# Save this file as protoc-gen-depends, put it in your path (executable) and add "--depends-out=/whatever/path" to your protoc invocation
from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest,CodeGeneratorResponse
from sys import stdin,stdout
req = CodeGeneratorRequest()
req.MergeFromString(''.join(stdin.readlines()))
res = CodeGeneratorResponse()
def add_depends(fd):
prefix = fd.name[0:-5]
depend_file = CodeGeneratorResponse.File()
depend_file.name = prefix + 'd'
depend_file.content = '{0}pb.cc {0}pb.h: {1} {2}\n'.format(prefix,fd.name,' '.join([f for f in fd.dependency if not f.startswith('google/protobuf')]))
return depend_file
res.file.extend([add_depends(fileDescriptor) for fileDescriptor in req.proto_file if not fileDescriptor.name.startswith('google/protobuf')])
stdout.write(res.SerializeToString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment