Compare commits
10 Commits
ccf63de0c6
...
b2ec9e29b3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2ec9e29b3 | ||
|
|
55bd3d706b | ||
|
|
a5006cc612 | ||
|
|
a0f91bbeeb | ||
|
|
7723a30bd6 | ||
|
|
98e01b2b33 | ||
|
|
985f1b4943 | ||
|
|
df3fd72337 | ||
|
|
5ef9d5b698 | ||
|
|
4ecf27c817 |
@@ -3,28 +3,37 @@
|
||||
'''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.'''
|
||||
nanopb_version = "nanopb-0.2.5-dev"
|
||||
|
||||
import sys
|
||||
|
||||
try:
|
||||
# Add some dummy imports to keep packaging tools happy.
|
||||
import google, distutils.util # bbfreeze seems to need these
|
||||
import pkg_resources # pyinstaller / protobuf 2.5 seem to need these
|
||||
except:
|
||||
# Don't care, we will error out later if it is actually important.
|
||||
pass
|
||||
|
||||
try:
|
||||
import google.protobuf.text_format as text_format
|
||||
except:
|
||||
print
|
||||
print "*************************************************************"
|
||||
print "*** Could not import the Google protobuf Python libraries ***"
|
||||
print "*** Try installing package 'python-protobuf' or similar. ***"
|
||||
print "*************************************************************"
|
||||
print
|
||||
sys.stderr.write('''
|
||||
*************************************************************
|
||||
*** Could not import the Google protobuf Python libraries ***
|
||||
*** Try installing package 'python-protobuf' or similar. ***
|
||||
*************************************************************
|
||||
''' + '\n')
|
||||
raise
|
||||
|
||||
try:
|
||||
import proto.nanopb_pb2 as nanopb_pb2
|
||||
import proto.descriptor_pb2 as descriptor
|
||||
except:
|
||||
print
|
||||
print "********************************************************************"
|
||||
print "*** Failed to import the protocol definitions for generator. ***"
|
||||
print "*** You have to run 'make' in the nanopb/generator/proto folder. ***"
|
||||
print "********************************************************************"
|
||||
print
|
||||
sys.stderr.write('''
|
||||
********************************************************************
|
||||
*** Failed to import the protocol definitions for generator. ***
|
||||
*** You have to run 'make' in the nanopb/generator/proto folder. ***
|
||||
********************************************************************
|
||||
''' + '\n')
|
||||
raise
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ scons CC=clang CXX=clang++
|
||||
''')
|
||||
|
||||
import os
|
||||
env = Environment(ENV = os.environ)
|
||||
env = Environment(ENV = os.environ, tools = ['default', 'nanopb'])
|
||||
|
||||
# Allow overriding the compiler with scons CC=???
|
||||
if 'CC' in ARGUMENTS: env.Replace(CC = ARGUMENTS['CC'])
|
||||
@@ -33,7 +33,17 @@ env.Append(PROTOCPATH = '#../generator')
|
||||
|
||||
# Check the compilation environment, unless we are just cleaning up.
|
||||
if not env.GetOption('clean'):
|
||||
conf = Configure(env)
|
||||
def check_ccflags(context, flags):
|
||||
'''Check if given CCFLAGS are supported'''
|
||||
context.Message('Checking support for CCFLAGS="%s"... ' % flags)
|
||||
oldflags = context.env['CCFLAGS']
|
||||
context.env.Append(CCFLAGS = flags)
|
||||
result = context.TryCompile("int main() {return 0;}", '.c')
|
||||
context.env.Replace(CCFLAGS = oldflags)
|
||||
context.Result(result)
|
||||
return result
|
||||
|
||||
conf = Configure(env, custom_tests = {'CheckCCFLAGS': check_ccflags})
|
||||
|
||||
# If the platform doesn't support C99, use our own header file instead.
|
||||
stdbool = conf.CheckCHeader('stdbool.h')
|
||||
@@ -62,6 +72,14 @@ if not env.GetOption('clean'):
|
||||
conf.env.Append(CCFLAGS = '-fmudflap')
|
||||
conf.env.Append(LINKFLAGS = '-lmudflap -fmudflap')
|
||||
|
||||
# Check if we can use extra strict warning flags (only with GCC)
|
||||
extra = '-Wcast-qual -Wlogical-op -Wconversion'
|
||||
extra += ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls'
|
||||
extra += ' -Wstack-protector '
|
||||
if 'gcc' in env['CC']:
|
||||
if conf.CheckCCFLAGS(extra):
|
||||
conf.env.Append(CORECFLAGS = extra)
|
||||
|
||||
# End the config stuff
|
||||
env = conf.Finish()
|
||||
|
||||
@@ -71,15 +89,11 @@ if 'gcc' in env['CC']:
|
||||
|
||||
# Debug info, warnings as errors
|
||||
env.Append(CFLAGS = '-ansi -pedantic -g -O0 -Wall -Werror --coverage -fstack-protector-all')
|
||||
env.Append(CORECFLAGS = '-Wextra')
|
||||
env.Append(LINKFLAGS = '--coverage')
|
||||
|
||||
# We currently need uint64_t anyway, even though ANSI C90 otherwise..
|
||||
env.Append(CFLAGS = '-Wno-long-long')
|
||||
|
||||
# More strict checks on the nanopb core
|
||||
env.Append(CORECFLAGS = '-Wextra -Wcast-qual -Wlogical-op -Wconversion')
|
||||
env.Append(CORECFLAGS = ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls')
|
||||
env.Append(CORECFLAGS = ' -Wstack-protector')
|
||||
elif 'clang' in env['CC']:
|
||||
# CLang
|
||||
env.Append(CFLAGS = '-ansi -g -O0 -Wall -Werror')
|
||||
|
||||
@@ -12,6 +12,6 @@ env.NanopbProto("person")
|
||||
# These are built using more strict warning flags.
|
||||
strict = env.Clone()
|
||||
strict.Append(CFLAGS = strict['CORECFLAGS'])
|
||||
strict.Object("pb_decode.o", "#../pb_decode.c")
|
||||
strict.Object("pb_encode.o", "#../pb_encode.c")
|
||||
strict.Object("pb_decode.o", "$NANOPB/pb_decode.c")
|
||||
strict.Object("pb_encode.o", "$NANOPB/pb_encode.c")
|
||||
|
||||
|
||||
@@ -12,42 +12,6 @@ except ImportError:
|
||||
def add_nanopb_builders(env):
|
||||
'''Add the necessary builder commands for nanopb tests.'''
|
||||
|
||||
# Build command for building .pb from .proto using protoc
|
||||
def proto_actions(source, target, env, for_signature):
|
||||
esc = env['ESCAPE']
|
||||
dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
|
||||
return '$PROTOC $PROTOCFLAGS %s -o%s %s' % (dirs, esc(str(target[0])), esc(str(source[0])))
|
||||
|
||||
proto_file_builder = Builder(generator = proto_actions,
|
||||
suffix = '.pb',
|
||||
src_suffix = '.proto')
|
||||
env.Append(BUILDERS = {'Proto': proto_file_builder})
|
||||
env.SetDefault(PROTOC = 'protoc')
|
||||
env.SetDefault(PROTOCPATH = ['.'])
|
||||
|
||||
# Build command for running nanopb generator
|
||||
import os.path
|
||||
def nanopb_targets(target, source, env):
|
||||
basename = os.path.splitext(str(source[0]))[0]
|
||||
target.append(basename + '.pb.h')
|
||||
return target, source
|
||||
|
||||
nanopb_file_builder = Builder(action = '$NANOPB_GENERATOR $NANOPB_FLAGS $SOURCE',
|
||||
suffix = '.pb.c',
|
||||
src_suffix = '.pb',
|
||||
emitter = nanopb_targets)
|
||||
env.Append(BUILDERS = {'Nanopb': nanopb_file_builder})
|
||||
gen_path = env['ESCAPE'](env.GetBuildPath("#../generator/nanopb_generator.py"))
|
||||
env.SetDefault(NANOPB_GENERATOR = 'python ' + gen_path)
|
||||
env.SetDefault(NANOPB_FLAGS = '-q')
|
||||
|
||||
# Combined method to run both protoc and nanopb generator
|
||||
def run_protoc_and_nanopb(env, source):
|
||||
b1 = env.Proto(source)
|
||||
b2 = env.Nanopb(source)
|
||||
return b1 + b2
|
||||
env.AddMethod(run_protoc_and_nanopb, "NanopbProto")
|
||||
|
||||
# Build command that runs a test program and saves the output
|
||||
def run_test(target, source, env):
|
||||
if len(source) > 1:
|
||||
|
||||
125
tests/site_scons/site_tools/nanopb.py
Normal file
125
tests/site_scons/site_tools/nanopb.py
Normal file
@@ -0,0 +1,125 @@
|
||||
'''
|
||||
Scons Builder for nanopb .proto definitions.
|
||||
|
||||
This tool will locate the nanopb generator and use it to generate .pb.c and
|
||||
.pb.h files from the .proto files.
|
||||
|
||||
Basic example
|
||||
-------------
|
||||
# Build myproto.pb.c and myproto.pb.h from myproto.proto
|
||||
myproto = env.NanopbProto("myproto")
|
||||
|
||||
# Link nanopb core to the program
|
||||
env.Append(CPPPATH = "$NANOB")
|
||||
myprog = env.Program(["myprog.c", myproto, "$NANOPB/pb_encode.c", "$NANOPB/pb_decode.c"])
|
||||
|
||||
Configuration options
|
||||
---------------------
|
||||
Normally, this script is used in the test environment of nanopb and it locates
|
||||
the nanopb generator by a relative path. If this script is used in another
|
||||
application, the path to nanopb root directory has to be defined:
|
||||
|
||||
env.SetDefault(NANOPB = "path/to/nanopb")
|
||||
|
||||
Additionally, the path to protoc and the options to give to protoc can be
|
||||
defined manually:
|
||||
|
||||
env.SetDefault(PROTOC = "path/to/protoc")
|
||||
env.SetDefault(PROTOCFLAGS = "--plugin=protoc-gen-nanopb=path/to/protoc-gen-nanopb")
|
||||
'''
|
||||
|
||||
import SCons.Action
|
||||
import SCons.Builder
|
||||
import SCons.Util
|
||||
import os.path
|
||||
|
||||
class NanopbWarning(SCons.Warnings.Warning):
|
||||
pass
|
||||
SCons.Warnings.enableWarningClass(NanopbWarning)
|
||||
|
||||
def _detect_nanopb(env):
|
||||
'''Find the path to nanopb root directory.'''
|
||||
if env.has_key('NANOPB'):
|
||||
# Use nanopb dir given by user
|
||||
return env['NANOPB']
|
||||
|
||||
p = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
||||
if os.path.isdir(p) and os.path.isfile(os.path.join(p, 'pb.h')):
|
||||
# Assume we are running under tests/site_scons/site_tools
|
||||
return p
|
||||
|
||||
raise SCons.Errors.StopError(NanopbWarning,
|
||||
"Could not find the nanopb root directory")
|
||||
|
||||
def _detect_protoc(env):
|
||||
'''Find the path to the protoc compiler.'''
|
||||
if env.has_key('PROTOC'):
|
||||
# Use protoc defined by user
|
||||
return env['PROTOC']
|
||||
|
||||
p = _detect_nanopb(env)
|
||||
p1 = os.path.join(p, 'generator-bin', 'protoc')
|
||||
if os.path.exists(p1):
|
||||
# Use protoc bundled with binary package
|
||||
return p1
|
||||
|
||||
p = env.WhereIs('protoc')
|
||||
if p:
|
||||
# Use protoc from path
|
||||
return p
|
||||
|
||||
raise SCons.Errors.StopError(NanopbWarning,
|
||||
"Could not find the protoc compiler")
|
||||
|
||||
def _detect_protocflags(env):
|
||||
'''Find the options to use for protoc.'''
|
||||
if env.has_key('PROTOCFLAGS'):
|
||||
return env['PROTOCFLAGS']
|
||||
|
||||
p = _detect_protoc(env)
|
||||
n = _detect_nanopb(env)
|
||||
if p == os.path.join(n, 'generator-bin', 'protoc'):
|
||||
# Using the bundled protoc, no options needed
|
||||
return ''
|
||||
|
||||
e = env['ESCAPE']
|
||||
if env['PLATFORM'] == 'win32':
|
||||
return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb.bat'))
|
||||
else:
|
||||
return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb'))
|
||||
|
||||
def _nanopb_proto_actions(source, target, env, for_signature):
|
||||
esc = env['ESCAPE']
|
||||
dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
|
||||
return '$PROTOC $PROTOCFLAGS %s --nanopb_out=. %s' % (dirs, esc(str(source[0])))
|
||||
|
||||
def _nanopb_proto_emitter(target, source, env):
|
||||
basename = os.path.splitext(str(source[0]))[0]
|
||||
target.append(basename + '.pb.h')
|
||||
|
||||
if os.path.exists(basename + '.options'):
|
||||
source.append(basename + '.options')
|
||||
|
||||
return target, source
|
||||
|
||||
_nanopb_proto_builder = SCons.Builder.Builder(
|
||||
generator = _nanopb_proto_actions,
|
||||
suffix = '.pb.c',
|
||||
src_suffix = '.proto',
|
||||
emitter = _nanopb_proto_emitter)
|
||||
|
||||
def generate(env):
|
||||
'''Add Builder for nanopb protos.'''
|
||||
|
||||
env['NANOPB'] = _detect_nanopb(env)
|
||||
env['PROTOC'] = _detect_protoc(env)
|
||||
env['PROTOCFLAGS'] = _detect_protocflags(env)
|
||||
|
||||
env.SetDefault(PROTOCPATH = ['.', os.path.join(env['NANOPB'], 'generator', 'proto')])
|
||||
|
||||
env.SetDefault(NANOPB_PROTO_CMD = '$PROTOC $PROTOC_OPTS --nanopb_out=. $SOURCE')
|
||||
env['BUILDERS']['NanopbProto'] = _nanopb_proto_builder
|
||||
|
||||
def exists(env):
|
||||
return _detect_protoc(env) and _detect_protoc_opts(env)
|
||||
|
||||
@@ -2,6 +2,5 @@
|
||||
|
||||
Import('env')
|
||||
|
||||
env.Proto("funny-proto+name has.characters.proto")
|
||||
env.Nanopb("funny-proto+name has.characters.pb.c", "funny-proto+name has.characters.pb")
|
||||
env.NanopbProto("funny-proto+name has.characters.proto")
|
||||
env.Object("funny-proto+name has.characters.pb.c")
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
set -e
|
||||
set -x
|
||||
|
||||
VERSION=`git describe --always`
|
||||
VERSION=`git describe --always`-linux-x86
|
||||
DEST=dist/$VERSION
|
||||
|
||||
rm -rf $DEST
|
||||
@@ -18,12 +18,20 @@ git archive HEAD | tar x -C $DEST
|
||||
# Rebuild the Python .proto files
|
||||
make -BC $DEST/generator/proto
|
||||
|
||||
# Make the nanopb generator available as a protoc plugin
|
||||
cp $DEST/generator/nanopb_generator.py $DEST/generator/protoc-gen-nanopb.py
|
||||
|
||||
# Package the Python libraries
|
||||
( cd $DEST/generator; bbfreeze nanopb_generator.py )
|
||||
( cd $DEST/generator; bbfreeze nanopb_generator.py protoc-gen-nanopb.py )
|
||||
mv $DEST/generator/dist $DEST/generator-bin
|
||||
|
||||
# Remove temp file
|
||||
rm $DEST/generator/protoc-gen-nanopb.py
|
||||
|
||||
# Package the protoc compiler
|
||||
cp `which protoc` $DEST/generator-bin/protoc.bin
|
||||
LIBPROTOC=$(ldd `which protoc` | grep -o '/.*libprotoc[^ ]*')
|
||||
cp $LIBPROTOC $DEST/generator-bin/
|
||||
cat > $DEST/generator-bin/protoc << EOF
|
||||
#!/bin/bash
|
||||
SCRIPTDIR=\$(dirname \$(readlink -f \$0))
|
||||
@@ -33,9 +41,6 @@ exec \$SCRIPTDIR/protoc.bin "\$@"
|
||||
EOF
|
||||
chmod +x $DEST/generator-bin/protoc
|
||||
|
||||
# Make the nanopb generator available as a protoc plugin
|
||||
ln -s nanopb-generator $DEST/generator-bin/protoc-gen-nanopb
|
||||
|
||||
# Tar it all up
|
||||
( cd dist; tar -czf $VERSION.tar.gz $VERSION )
|
||||
|
||||
|
||||
49
tools/make_mac_package.sh
Executable file
49
tools/make_mac_package.sh
Executable file
@@ -0,0 +1,49 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Run this script in the top nanopb directory to create a binary package
|
||||
# for Mac OS X users.
|
||||
|
||||
# Requires: protobuf, python-protobuf, pyinstaller
|
||||
|
||||
set -e
|
||||
set -x
|
||||
|
||||
VERSION=`git describe --always`-macosx-x86
|
||||
DEST=dist/$VERSION
|
||||
|
||||
rm -rf $DEST
|
||||
mkdir -p $DEST
|
||||
|
||||
# Export the files from newest commit
|
||||
git archive HEAD | tar x -C $DEST
|
||||
|
||||
# Rebuild the Python .proto files
|
||||
make -BC $DEST/generator/proto
|
||||
|
||||
# Package the Python libraries
|
||||
( cd $DEST/generator; pyinstaller nanopb_generator.py )
|
||||
mv $DEST/generator/dist/nanopb_generator $DEST/generator-bin
|
||||
|
||||
# Remove temp files
|
||||
rm -rf $DEST/generator/dist $DEST/generator/build $DEST/generator/nanopb_generator.spec
|
||||
|
||||
# Make the nanopb generator available as a protoc plugin
|
||||
cp $DEST/generator-bin/nanopb_generator $DEST/generator-bin/protoc-gen-nanopb
|
||||
|
||||
# Package the protoc compiler
|
||||
cp `which protoc` $DEST/generator-bin/protoc.bin
|
||||
LIBPROTOC=$(otool -L `which protoc` | grep -o '/.*libprotoc[^ ]*')
|
||||
LIBPROTOBUF=$(otool -L `which protoc` | grep -o '/.*libprotobuf[^ ]*')
|
||||
cp $LIBPROTOC $LIBPROTOBUF $DEST/generator-bin/
|
||||
cat > $DEST/generator-bin/protoc << EOF
|
||||
#!/bin/bash
|
||||
SCRIPTDIR=\$(cd \$(dirname \$0); pwd)
|
||||
export DYLD_LIBRARY_PATH=\$SCRIPTDIR
|
||||
export PATH=\$SCRIPTDIR:\$PATH
|
||||
exec \$SCRIPTDIR/protoc.bin "\$@"
|
||||
EOF
|
||||
chmod +x $DEST/generator-bin/protoc
|
||||
|
||||
# Tar it all up
|
||||
( cd dist; tar -czf $VERSION.tgz $VERSION )
|
||||
|
||||
8
tools/make_windows_package.sh
Normal file → Executable file
8
tools/make_windows_package.sh
Normal file → Executable file
@@ -7,7 +7,7 @@
|
||||
set -e
|
||||
set -x
|
||||
|
||||
VERSION=`git describe --always`
|
||||
VERSION=`git describe --always`-windows-x86
|
||||
DEST=dist/$VERSION
|
||||
|
||||
rm -rf $DEST
|
||||
@@ -26,6 +26,9 @@ cp $DEST/generator/nanopb_generator.py $DEST/generator/protoc-gen-nanopb.py
|
||||
( cd $DEST/generator; bbfreeze nanopb_generator.py protoc-gen-nanopb.py )
|
||||
mv $DEST/generator/dist $DEST/generator-bin
|
||||
|
||||
# Remove temp file
|
||||
rm $DEST/generator/protoc-gen-nanopb.py
|
||||
|
||||
# The python interpreter requires MSVCR90.dll.
|
||||
# FIXME: Find a way around hardcoding this path
|
||||
cp /c/windows/winsxs/x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4974_none_50940634bcb759cb/MSVCR90.DLL $DEST/generator-bin/
|
||||
@@ -38,9 +41,6 @@ cat > $DEST/generator-bin/Microsoft.VC90.CRT.manifest <<EOF
|
||||
</assembly>
|
||||
EOF
|
||||
|
||||
# Remove temp file
|
||||
rm $DEST/generator/protoc-gen-nanopb.py
|
||||
|
||||
# Package the protoc compiler
|
||||
cp `which protoc.exe` $DEST/generator-bin/
|
||||
cp `which MSVCR100.DLL` $DEST/generator-bin/
|
||||
|
||||
Reference in New Issue
Block a user