| 1 |
''' |
| 2 |
Scons Builder for nanopb .proto definitions. |
| 3 |
|
| 4 |
This tool will locate the nanopb generator and use it to generate .pb.c and |
| 5 |
.pb.h files from the .proto files. |
| 6 |
|
| 7 |
Basic example |
| 8 |
------------- |
| 9 |
# Build myproto.pb.c and myproto.pb.h from myproto.proto |
| 10 |
myproto = env.NanopbProto("myproto") |
| 11 |
|
| 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"]) |
| 15 |
|
| 16 |
Configuration options |
| 17 |
--------------------- |
| 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: |
| 21 |
|
| 22 |
env.SetDefault(NANOPB = "path/to/nanopb") |
| 23 |
|
| 24 |
Additionally, the path to protoc and the options to give to protoc can be |
| 25 |
defined manually: |
| 26 |
|
| 27 |
env.SetDefault(PROTOC = "path/to/protoc") |
| 28 |
env.SetDefault(PROTOCFLAGS = "--plugin=protoc-gen-nanopb=path/to/protoc-gen-nanopb") |
| 29 |
''' |
| 30 |
|
| 31 |
import SCons.Action |
| 32 |
import SCons.Builder |
| 33 |
import SCons.Util |
| 34 |
from SCons.Script import Dir, File |
| 35 |
import os.path |
| 36 |
import platform |
| 37 |
import sys |
| 38 |
|
| 39 |
try: |
| 40 |
warningbase = SCons.Warnings.SConsWarning |
| 41 |
except AttributeError: |
| 42 |
warningbase = SCons.Warnings.Warning |
| 43 |
|
| 44 |
class NanopbWarning(warningbase): |
| 45 |
pass |
| 46 |
SCons.Warnings.enableWarningClass(NanopbWarning) |
| 47 |
|
| 48 |
def _detect_nanopb(env): |
| 49 |
'''Find the path to nanopb root directory.''' |
| 50 |
if 'NANOPB' in env: |
| 51 |
# Use nanopb dir given by user |
| 52 |
return env['NANOPB'] |
| 53 |
|
| 54 |
p = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')) |
| 55 |
if os.path.isdir(p) and os.path.isfile(os.path.join(p, 'pb.h')): |
| 56 |
# Assume we are running under tests/site_scons/site_tools |
| 57 |
return p |
| 58 |
|
| 59 |
raise SCons.Errors.StopError(NanopbWarning, |
| 60 |
"Could not find the nanopb root directory") |
| 61 |
|
| 62 |
def _detect_python(env): |
| 63 |
'''Find Python executable to use.''' |
| 64 |
if 'PYTHON' in env: |
| 65 |
return env['PYTHON'] |
| 66 |
|
| 67 |
p = env.WhereIs('python3') |
| 68 |
if p: |
| 69 |
return env['ESCAPE'](p) |
| 70 |
|
| 71 |
p = env.WhereIs('py.exe') |
| 72 |
if p: |
| 73 |
return env['ESCAPE'](p) + " -3" |
| 74 |
|
| 75 |
return env['ESCAPE'](sys.executable) |
| 76 |
|
| 77 |
def _detect_nanopb_generator(env): |
| 78 |
'''Return command for running nanopb_generator.py''' |
| 79 |
generator_cmd = os.path.join(env['NANOPB'], 'generator-bin', 'nanopb_generator' + env['PROGSUFFIX']) |
| 80 |
if os.path.exists(generator_cmd): |
| 81 |
# Binary package |
| 82 |
return env['ESCAPE'](generator_cmd) |
| 83 |
else: |
| 84 |
# Source package |
| 85 |
return env['PYTHON'] + " " + env['ESCAPE'](os.path.join(env['NANOPB'], 'generator', 'nanopb_generator.py')) |
| 86 |
|
| 87 |
def _detect_protoc(env): |
| 88 |
'''Find the path to the protoc compiler.''' |
| 89 |
if 'PROTOC' in env: |
| 90 |
# Use protoc defined by user |
| 91 |
return env['PROTOC'] |
| 92 |
|
| 93 |
n = _detect_nanopb(env) |
| 94 |
p1 = os.path.join(n, 'generator-bin', 'protoc' + env['PROGSUFFIX']) |
| 95 |
if os.path.exists(p1): |
| 96 |
# Use protoc bundled with binary package |
| 97 |
return env['ESCAPE'](p1) |
| 98 |
|
| 99 |
p = os.path.join(n, 'generator', 'protoc') |
| 100 |
if os.path.exists(p): |
| 101 |
# Use the grcpio-tools protoc wrapper |
| 102 |
if env['PLATFORM'] == 'win32': |
| 103 |
return env['ESCAPE'](p + '.bat') |
| 104 |
else: |
| 105 |
return env['ESCAPE'](p) |
| 106 |
|
| 107 |
p = env.WhereIs('protoc') |
| 108 |
if p: |
| 109 |
# Use protoc from path |
| 110 |
return env['ESCAPE'](p) |
| 111 |
|
| 112 |
raise SCons.Errors.StopError(NanopbWarning, |
| 113 |
"Could not find the protoc compiler") |
| 114 |
|
| 115 |
def _detect_protocflags(env): |
| 116 |
'''Find the options to use for protoc.''' |
| 117 |
if 'PROTOCFLAGS' in env: |
| 118 |
return env['PROTOCFLAGS'] |
| 119 |
|
| 120 |
p = _detect_protoc(env) |
| 121 |
n = _detect_nanopb(env) |
| 122 |
p1 = os.path.join(n, 'generator', 'protoc' + env['PROGSUFFIX']) |
| 123 |
p2 = os.path.join(n, 'generator-bin', 'protoc' + env['PROGSUFFIX']) |
| 124 |
if p in [env['ESCAPE'](p1), env['ESCAPE'](p2)]: |
| 125 |
# Using the bundled protoc, no options needed |
| 126 |
return '' |
| 127 |
|
| 128 |
e = env['ESCAPE'] |
| 129 |
if env['PLATFORM'] == 'win32': |
| 130 |
return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb.bat')) |
| 131 |
else: |
| 132 |
return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb')) |
| 133 |
|
| 134 |
def _nanopb_proto_actions(source, target, env, for_signature): |
| 135 |
esc = env['ESCAPE'] |
| 136 |
|
| 137 |
# Make protoc build inside the SConscript directory |
| 138 |
prefix = os.path.dirname(str(source[-1])) |
| 139 |
srcfile = esc(os.path.relpath(str(source[0]), prefix)) |
| 140 |
include_dirs = '-I.' |
| 141 |
for d in env['PROTOCPATH']: |
| 142 |
d = env.GetBuildPath(d) |
| 143 |
if not os.path.isabs(d): d = os.path.relpath(d, prefix) |
| 144 |
include_dirs += ' -I' + esc(d) |
| 145 |
|
| 146 |
# when generating .pb.cpp sources, instead of pb.h generate .pb.hpp headers |
| 147 |
source_extension = os.path.splitext(str(target[0]))[1] |
| 148 |
header_extension = '.h' + source_extension[2:] |
| 149 |
nanopb_flags = env['NANOPBFLAGS'] |
| 150 |
if nanopb_flags: |
| 151 |
nanopb_flags = '--source-extension=%s,--header-extension=%s,%s:.' % (source_extension, header_extension, nanopb_flags) |
| 152 |
else: |
| 153 |
nanopb_flags = '--source-extension=%s,--header-extension=%s:.' % (source_extension, header_extension) |
| 154 |
|
| 155 |
return SCons.Action.CommandAction('$PROTOC $PROTOCFLAGS %s "--nanopb_out=%s" %s' % (include_dirs, nanopb_flags, srcfile), |
| 156 |
chdir = prefix) |
| 157 |
|
| 158 |
def _nanopb_proto_emitter(target, source, env): |
| 159 |
basename = os.path.splitext(str(source[0]))[0] |
| 160 |
source_extension = os.path.splitext(str(target[0]))[1] |
| 161 |
header_extension = '.h' + source_extension[2:] |
| 162 |
target.append(basename + '.pb' + header_extension) |
| 163 |
|
| 164 |
# This is a bit of a hack. protoc include paths work the sanest |
| 165 |
# when the working directory is the same as the source root directory. |
| 166 |
# To get that directory in _nanopb_proto_actions, we add SConscript to |
| 167 |
# the list of source files. |
| 168 |
source.append(File("SConscript")) |
| 169 |
|
| 170 |
if os.path.exists(basename + '.options'): |
| 171 |
source.append(basename + '.options') |
| 172 |
|
| 173 |
return target, source |
| 174 |
|
| 175 |
_nanopb_proto_builder = SCons.Builder.Builder( |
| 176 |
generator = _nanopb_proto_actions, |
| 177 |
suffix = '.pb.c', |
| 178 |
src_suffix = '.proto', |
| 179 |
emitter = _nanopb_proto_emitter) |
| 180 |
|
| 181 |
_nanopb_proto_cpp_builder = SCons.Builder.Builder( |
| 182 |
generator = _nanopb_proto_actions, |
| 183 |
suffix = '.pb.cpp', |
| 184 |
src_suffix = '.proto', |
| 185 |
emitter = _nanopb_proto_emitter) |
| 186 |
|
| 187 |
def generate(env): |
| 188 |
'''Add Builder for nanopb protos.''' |
| 189 |
|
| 190 |
env['NANOPB'] = _detect_nanopb(env) |
| 191 |
env['PROTOC'] = _detect_protoc(env) |
| 192 |
env['PROTOCFLAGS'] = _detect_protocflags(env) |
| 193 |
env['PYTHON'] = _detect_python(env) |
| 194 |
env['NANOPB_GENERATOR'] = _detect_nanopb_generator(env) |
| 195 |
env.SetDefault(NANOPBFLAGS = '') |
| 196 |
|
| 197 |
env.SetDefault(PROTOCPATH = [".", os.path.join(env['NANOPB'], 'generator', 'proto')]) |
| 198 |
|
| 199 |
env.SetDefault(NANOPB_PROTO_CMD = '$PROTOC $PROTOCFLAGS --nanopb_out=$NANOPBFLAGS:. $SOURCES') |
| 200 |
env['BUILDERS']['NanopbProto'] = _nanopb_proto_builder |
| 201 |
env['BUILDERS']['NanopbProtoCpp'] = _nanopb_proto_cpp_builder |
| 202 |
|
| 203 |
def exists(env): |
| 204 |
return _detect_protoc(env) and _detect_protoc_opts(env) |
| 205 |
|