1. Russel Winder
  2. SCons_Protobuf

Commits

Russel Winder  committed cb5c32a

The Epoch.

This code taken from http://www.scons.org/wiki/ProtocBuilder

  • Participants
  • Branches default

Comments (0)

Files changed (3)

File README.rst

View file
  • Ignore whitespace
+Generating Protocol Buffers files with SCons
+============================================
+
+This SCons builder will allow you to generate Google Protocol Buffers .pb.cc, .pb.h, and _pb2.py files using
+SCons from a .proto source file.
+
+It doesn't currently support Java, but that should be easy to add.
+
+Setup
+=====
+
+Clone this repository to the site_scons/site_tools directory of your project or to your
+~/site_scons/site_tools directory named protoc and it is ready to use for the one project or all your
+project respectively.
+
+Usage
+====
+
+Here is a sample SConstruct Scott used (some MSVC/Windows-specific stuff, but the Protoc builder is
+OS-agnostic).  Note that you must make sure the protoc executable is available in your PATH or it won't be
+able to execute it.
+
+import os
+
+    env = Environment(
+        ENV = {'PATH' : os.environ['PATH']},
+        CPPPATH = ['C:\wc\protobuf-trunk\src' ],
+        CPPFLAGS = ['/EHsc', '/MDd'],
+        tools=['default', 'protoc']
+        )
+
+    proto_files = env.Protoc(
+        [],
+        "config.proto",
+        PROTOCPROTOPATH=['.',r'C:\wc\protobuf-trunk\src',],
+        PROTOCPYTHONOUTDIR='python', # set to None to not generate python
+        PROTOCOUTDIR = 'build', # defaults to same directory as .proto
+        # PROTOCCPPOUTFLAGS = "dllexport_decl=PROTOCONFIG_EXPORT:", too 
+    )
+
+    env.Program('test', ['test.cpp', proto_files[0]],
+        LIBPATH=[r'C:\wc\protobuf-trunk\vsprojects\Debug'],
+        LIBS=['libprotobuf.lib'],
+        )

File __init__.py

View file
  • Ignore whitespace
+#
+# Copyright (c) 2011 Russel Winder
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+#  Author : Russel Winder <russel@russel.org.uk>
+#  Date : 2011-10-23 13:28:33+01:00
+
+"""
+The package file to turn this package into a standardly loadable SCons tool.
+"""
+
+__author__ = 'Russel Winder <russel@russel.org.uk>'
+
+from protoc import exists , generate

File protoc.py

View file
  • Ignore whitespace
+#
+# Copyright (c) 2009  Scott Stafford
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+#  Author : Scott Stafford
+#  Date : 2009-12-09 20:36:14
+
+""" 
+protoc.py: Protoc Builder for SCons
+
+This Builder invokes protoc to generate C++ and Python  from a .proto file.  
+
+NOTE: Java is not currently supported.
+"""
+
+__author__ = "Scott Stafford"
+
+import SCons.Action
+import SCons.Builder
+import SCons.Defaults
+import SCons.Node.FS
+import SCons.Util
+
+from SCons.Script import File, Dir
+
+import os.path
+
+protocs = 'protoc'
+
+ProtocAction = SCons.Action.Action('$PROTOCCOM', '$PROTOCCOMSTR')
+def ProtocEmitter(target, source, env):
+    dirOfCallingSConscript = Dir('.').srcnode()
+    env.Prepend(PROTOCPROTOPATH = dirOfCallingSConscript.path)
+
+    source_with_corrected_path = []
+    for src in source:
+        commonprefix = os.path.commonprefix([dirOfCallingSConscript.path, src.srcnode().path])
+        if len(commonprefix)>0:
+            source_with_corrected_path.append( src.srcnode().path[len(commonprefix + os.sep):] )
+        else:
+            source_with_corrected_path.append( src.srcnode().path )
+
+    source = source_with_corrected_path
+
+    for src in source:
+        modulename = os.path.splitext(src)[0]
+
+        if env['PROTOCOUTDIR']:
+            base = os.path.join(env['PROTOCOUTDIR'] , modulename)
+            target.extend( [ base + '.pb.cc', base + '.pb.h' ] )
+
+        if env['PROTOCPYTHONOUTDIR']:
+            base = os.path.join(env['PROTOCPYTHONOUTDIR'] , modulename)
+            target.append( base + '_pb2.py' )
+
+    try:
+        target.append(env['PROTOCFDSOUT'])
+    except KeyError:
+        pass
+
+    #~ print "PROTOC SOURCE:", [str(s) for s in source]
+    #~ print "PROTOC TARGET:", [str(s) for s in target]
+
+    return target, source
+
+ProtocBuilder = SCons.Builder.Builder(action = ProtocAction,
+                                   emitter = ProtocEmitter,
+                                   srcsuffix = '$PROTOCSRCSUFFIX')
+
+def generate(env):
+    """Add Builders and construction variables for protoc to an Environment."""
+    try:
+        bld = env['BUILDERS']['Protoc']
+    except KeyError:
+        bld = ProtocBuilder
+        env['BUILDERS']['Protoc'] = bld
+
+    env['PROTOC']        = env.Detect(protocs) or 'protoc'
+    env['PROTOCFLAGS']   = SCons.Util.CLVar('')
+    env['PROTOCPROTOPATH'] = SCons.Util.CLVar('')
+    env['PROTOCCOM']     = '$PROTOC ${["-I%s"%x for x in PROTOCPROTOPATH]} $PROTOCFLAGS --cpp_out=$PROTOCCPPOUTFLAGS$PROTOCOUTDIR ${PROTOCPYTHONOUTDIR and ("--python_out="+PROTOCPYTHONOUTDIR) or ""} ${PROTOCFDSOUT and ("-o"+PROTOCFDSOUT) or ""} ${SOURCES}'
+    env['PROTOCOUTDIR'] = '${SOURCE.dir}'
+    env['PROTOCPYTHONOUTDIR'] = "python"
+    env['PROTOCSRCSUFFIX']  = '.proto'
+
+def exists(env):
+    return env.Detect(protocs)