Skip to content

Commit

Permalink
Make CMakeLists install as Python module 'nanopb', not 'proto' (nanop…
Browse files Browse the repository at this point in the history
…b#845)

Makes the installation consistent with "pip install nanopb" results,
and avoids naming conflicts with other libraries.
  • Loading branch information
PetteriAimonen committed Oct 27, 2023
1 parent 9766c45 commit 5896c28
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
23 changes: 18 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ if(NOT DEFINED CMAKE_INSTALL_CMAKEDIR)
set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/nanopb")
endif()

# Determine Python module installation path
if (NOT nanopb_PYTHON_INSTDIR_OVERRIDE)
find_package(Python REQUIRED COMPONENTS Interpreter)
execute_process(
COMMAND ${Python_EXECUTABLE} -c
"import os.path, sys, sysconfig; print(os.path.relpath(sysconfig.get_path('purelib'), start=sys.prefix))"
"import os.path, sys, sysconfig; print(sysconfig.get_path('purelib'))"
OUTPUT_VARIABLE PYTHON_INSTDIR
OUTPUT_STRIP_TRAILING_WHITESPACE
)
Expand All @@ -54,6 +55,7 @@ else()
endif()
message(STATUS "Python install dir: ${PYTHON_INSTDIR}")

# Install nanopb generator as Python module 'nanopb'
if(nanopb_BUILD_GENERATOR)
set(generator_protos nanopb)

Expand All @@ -69,27 +71,38 @@ if(nanopb_BUILD_GENERATOR)
install(
FILES ${PROJECT_BINARY_DIR}/${generator_proto_py_file}
${generator_proto_file}
DESTINATION ${PYTHON_INSTDIR}/proto/
DESTINATION ${PYTHON_INSTDIR}/nanopb/generator/proto/
)
endforeach()

install( FILES generator/proto/_utils.py
generator/proto/__init__.py
DESTINATION ${PYTHON_INSTDIR}/proto/ )
DESTINATION ${PYTHON_INSTDIR}/nanopb/generator/proto/ )

install( FILES generator/nanopb_generator.py
generator/__init__.py
DESTINATION ${PYTHON_INSTDIR}/nanopb/generator/ )

install( FILES generator/__init__.py
DESTINATION ${PYTHON_INSTDIR}/nanopb/ )

endif()

# Install small script wrappers to invoke the generator from the installed module
if(WIN32)
install(
PROGRAMS
generator/nanopb_generator.py
extra/script_wrappers/nanopb_generator.py
generator/protoc-gen-nanopb.bat
generator/nanopb_generator.bat
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
else()
install(
PROGRAMS
generator/nanopb_generator.py
extra/script_wrappers/nanopb_generator.py
generator/protoc-gen-nanopb
generator/nanopb_generator
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif()
Expand Down
6 changes: 6 additions & 0 deletions extra/script_wrappers/nanopb_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python3
# This script is a wrapper to invoke nanopb_generator from an installed Python module.
import sys
from nanopb.generator.nanopb_generator import main_cli, main_plugin
if __name__ == '__main__':
sys.exit(main_cli())
Empty file added generator/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions generator/nanopb_generator
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3
# Allow calling nanopb_generator.py as simply nanopb_generator.
# This provides consistency with packages installed through CMake or pip.

from nanopb_generator import *
if __name__ == '__main__':
main_cli()
5 changes: 5 additions & 0 deletions generator/nanopb_generator.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
:: Allow calling nanopb_generator.py as simply nanopb_generator.
:: This provides consistency with packages installed through CMake or pip.
set mydir=%~dp0
python "%mydir%\nanopb_generator.py" %*

0 comments on commit 5896c28

Please sign in to comment.