2 Scons Builder for nanopb .proto definitions.
4 This tool will locate the nanopb generator and use it to generate .pb.c and
5 .pb.h files from the .proto files.
9 # Build myproto.pb.c and myproto.pb.h from myproto.proto
10 myproto = env.NanopbProto("myproto")
12 # Link nanopb core to the program
13 env.Append(CPPPATH = "$NANOB")
14 myprog = env.Program(["myprog.c", myproto, "$NANOPB/pb_encode.c", "$NANOPB/pb_decode.c"])
18 Normally, this script is used in the test environment of nanopb and it locates
19 the nanopb generator by a relative path. If this script is used in another
20 application, the path to nanopb root directory has to be defined:
22 env.SetDefault(NANOPB = "path/to/nanopb")
24 Additionally, the path to protoc and the options to give to protoc can be
27 env.SetDefault(PROTOC = "path/to/protoc")
28 env.SetDefault(PROTOCFLAGS = "--plugin=protoc-gen-nanopb=path/to/protoc-gen-nanopb")
36 class NanopbWarning(SCons.Warnings.Warning):
38 SCons.Warnings.enableWarningClass(NanopbWarning)
40 def _detect_nanopb(env):
41 '''Find the path to nanopb root directory.'''
42 if env.has_key('NANOPB'):
43 # Use nanopb dir given by user
46 p = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
47 if os.path.isdir(p) and os.path.isfile(os.path.join(p, 'pb.h')):
48 # Assume we are running under tests/site_scons/site_tools
51 raise SCons.Errors.StopError(NanopbWarning,
52 "Could not find the nanopb root directory")
54 def _detect_protoc(env):
55 '''Find the path to the protoc compiler.'''
56 if env.has_key('PROTOC'):
57 # Use protoc defined by user
60 p = _detect_nanopb(env)
61 p1 = os.path.join(p, 'generator-bin', 'protoc')
62 if os.path.exists(p1):
63 # Use protoc bundled with binary package
64 return env['ESCAPE'](p1)
66 p = env.WhereIs('protoc')
68 # Use protoc from path
69 return env['ESCAPE'](p)
71 raise SCons.Errors.StopError(NanopbWarning,
72 "Could not find the protoc compiler")
74 def _detect_protocflags(env):
75 '''Find the options to use for protoc.'''
76 if env.has_key('PROTOCFLAGS'):
77 return env['PROTOCFLAGS']
79 p = _detect_protoc(env)
80 n = _detect_nanopb(env)
81 if p == os.path.join(n, 'generator-bin', 'protoc'):
82 # Using the bundled protoc, no options needed
86 if env['PLATFORM'] == 'win32':
87 return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb.bat'))
89 return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb'))
91 def _nanopb_proto_actions(source, target, env, for_signature):
93 dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
94 return '$PROTOC $PROTOCFLAGS %s --nanopb_out=. %s' % (dirs, esc(str(source[0])))
96 def _nanopb_proto_emitter(target, source, env):
97 basename = os.path.splitext(str(source[0]))[0]
98 target.append(basename + '.pb.h')
100 if os.path.exists(basename + '.options'):
101 source.append(basename + '.options')
103 return target, source
105 _nanopb_proto_builder = SCons.Builder.Builder(
106 generator = _nanopb_proto_actions,
108 src_suffix = '.proto',
109 emitter = _nanopb_proto_emitter)
112 '''Add Builder for nanopb protos.'''
114 env['NANOPB'] = _detect_nanopb(env)
115 env['PROTOC'] = _detect_protoc(env)
116 env['PROTOCFLAGS'] = _detect_protocflags(env)
118 env.SetDefault(PROTOCPATH = ['.', os.path.join(env['NANOPB'], 'generator', 'proto')])
120 env.SetDefault(NANOPB_PROTO_CMD = '$PROTOC $PROTOC_OPTS --nanopb_out=. $SOURCE')
121 env['BUILDERS']['NanopbProto'] = _nanopb_proto_builder
124 return _detect_protoc(env) and _detect_protoc_opts(env)