From a4bbd6a2ebff89d4e723e396ce27eef433a4f23c Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Mon, 9 Oct 2023 08:44:25 +0300 Subject: Fix Linux/Mac binary package build PyInstaller now puts libraries in a subdirectory. Use --strip instead of manually stripping debug symbols. Needed also changes in how builtin protoc include path is constructed. --- generator/nanopb_generator.py | 8 ++++++-- generator/proto/_utils.py | 36 ++++++++++++++++++++++++++++++------ tools/make_linux_package.sh | 8 +++----- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index 82aa6e4..d148640 100755 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -2290,8 +2290,8 @@ optparser = OptionParser( usage = "Usage: nanopb_generator.py [options] file.pb ...", epilog = "Compile file.pb from file.proto by: 'protoc -ofile.pb file.proto'. " + "Output will be written to file.pb.h and file.pb.c.") -optparser.add_option("--version", dest="version", action="store_true", - help="Show version info and exit") +optparser.add_option("-V", "--version", dest="version", action="store_true", + help="Show version info and exit (add -v for protoc version info)") optparser.add_option("-x", dest="exclude", metavar="FILE", action="append", default=[], help="Exclude file from generated #include list.") optparser.add_option("-e", "--extension", dest="extension", metavar="EXTENSION", default=".pb", @@ -2351,6 +2351,10 @@ def process_cmdline(args, is_plugin): sys.stderr.write('%s\n' % (nanopb_version)) else: print(nanopb_version) + + if options.verbose: + proto.print_versions() + sys.exit(0) if not filenames and not is_plugin: diff --git a/generator/proto/_utils.py b/generator/proto/_utils.py index f9c8c94..1d0d2b1 100644 --- a/generator/proto/_utils.py +++ b/generator/proto/_utils.py @@ -12,6 +12,29 @@ def has_grpcio_protoc(): return False return True +def get_proto_builtin_include_path(): + """Find include path for standard google/protobuf includes and for + nanopb.proto. + """ + + if getattr(sys, 'frozen', False): + # Pyinstaller package + paths = [ + os.path.join(os.path.dirname(os.path.abspath(sys.executable)), 'proto'), + os.path.join(os.path.dirname(os.path.abspath(sys.executable)), 'grpc_tools', '_proto') + ] + + else: + # Stand-alone script + paths = [ + os.path.dirname(os.path.abspath(__file__)) + ] + + if has_grpcio_protoc(): + import pkg_resources + paths.append(pkg_resources.resource_filename('grpc_tools', '_proto')) + + return paths def invoke_protoc(argv): # type: (list) -> typing.Any @@ -30,15 +53,11 @@ def invoke_protoc(argv): argv.append("-I.") # Add default protoc include paths - nanopb_include = os.path.dirname(os.path.abspath(__file__)) - argv.append('-I' + nanopb_include) + for incpath in get_proto_builtin_include_path(): + argv.append('-I' + incpath) if has_grpcio_protoc(): import grpc_tools.protoc as protoc - import pkg_resources - proto_include = pkg_resources.resource_filename('grpc_tools', '_proto') - argv.append('-I' + proto_include) - return protoc.main(argv) else: return subprocess.call(argv) @@ -55,6 +74,11 @@ def print_versions(): except Exception as e: sys.stderr.write("Failed to determine protoc version: " + str(e) + "\n") + try: + sys.stderr.write("protoc builtin include path: " + str(get_proto_builtin_include_path()) + "\n") + except Exception as e: + sys.stderr.write("Failed to construct protoc include path: " + str(e) + "\n") + try: import google.protobuf sys.stderr.write("Python version " + sys.version + "\n") diff --git a/tools/make_linux_package.sh b/tools/make_linux_package.sh index dd22b0d..1fa293f 100755 --- a/tools/make_linux_package.sh +++ b/tools/make_linux_package.sh @@ -21,12 +21,13 @@ git archive HEAD | tar x -C $DEST ( cd $DEST/generator; python3 nanopb_generator.py ||: ) # Package the Python libraries -( cd $DEST/generator; python3 -m PyInstaller nanopb_generator.py ) -( cd $DEST/generator; python3 -m PyInstaller protoc ) +( cd $DEST/generator; python3 -m PyInstaller --strip nanopb_generator.py ) +( cd $DEST/generator; python3 -m PyInstaller --strip protoc ) mv $DEST/generator/dist/nanopb_generator $DEST/generator-bin cp $DEST/generator/dist/protoc/protoc $DEST/generator-bin # Include Google's descriptor.proto and nanopb.proto +mkdir -p $DEST/generator-bin/grpc_tools/ cp -pr $(python3 -c 'import grpc_tools, os.path; print(os.path.dirname(grpc_tools.__file__))')/_proto $DEST/generator-bin/grpc_tools/ cp -pr $DEST/generator/proto $DEST/generator-bin/proto @@ -36,9 +37,6 @@ rm -rf $DEST/generator/dist $DEST/generator/build $DEST/generator/nanopb_generat # Make the nanopb generator available as a protoc plugin cp $DEST/generator-bin/nanopb_generator $DEST/generator-bin/protoc-gen-nanopb -# Remove debugging symbols to reduce size of package -( cd $DEST/generator-bin; strip *.so* ) - # Tar it all up ( cd dist; tar -czf $VERSION.tar.gz $VERSION ) -- cgit v1.2.3