blob: ab0e84c550f4505f8b3eb71f3162b2db6e58d4ce [file] [log] [blame]
Julian Halldf86fce2020-11-23 18:09:55 +01001This patch fixes two issues:
2
31. On windows the python3 executable is not allways called "python3". As a result
4 "protoc" execution can fail due to the shebang in the file. This patch fixes
5 this by running protoc with the intepreter.
6
72. In addition when not running from a virtualenv, the install path for python file
8 is set to the "user site-packages" to avoid needing elevated access rights.
9
10diff --git a/CMakeLists.txt b/CMakeLists.txt
11index 31c86e7..e827015 100644
12--- a/CMakeLists.txt
13+++ b/CMakeLists.txt
14@@ -54,13 +54,25 @@ if(nanopb_BUILD_GENERATOR)
15 string(REGEX REPLACE "([^;]+)" "\\1_pb2.py" generator_proto_py_file "${generator_proto}")
16 add_custom_command(
17 OUTPUT ${generator_proto_py_file}
18- COMMAND ${nanopb_PROTOC_PATH} --python_out=${PROJECT_BINARY_DIR} -I${PROJECT_SOURCE_DIR}/generator/proto ${generator_proto_file}
19+ COMMAND ${Python_EXECUTABLE} ${nanopb_PROTOC_PATH} --python_out=${PROJECT_BINARY_DIR} -I${PROJECT_SOURCE_DIR}/generator/proto ${generator_proto_file}
20 DEPENDS ${generator_proto_file}
21 )
22 add_custom_target("generate_${generator_proto_py_file}" ALL DEPENDS ${generator_proto_py_file})
23+
24+ if (DEFINED ENV{VIRTUAL_ENV})
25+ set(PYTHON_INSTALL_DIR ${Python_SITELIB} CACHE PATH "Install location for generated python modules.")
26+ else()
27+ execute_process(
28+ COMMAND ${Python_EXECUTABLE} -m site --user-site
29+ OUTPUT_VARIABLE PYTHON_USER_SITE
30+ OUTPUT_STRIP_TRAILING_WHITESPACE
31+ )
32+ set(PYTHON_INSTALL_DIR ${PYTHON_USER_SITE} CACHE PATH "Install location for generated python modules.")
33+ endif()
34+
35 install(
36 FILES ${PROJECT_BINARY_DIR}/${generator_proto_py_file}
37- DESTINATION ${Python_SITELIB}
38+ DESTINATION ${PYTHON_INSTALL_DIR}
39 )
40 endforeach()
41 endif()