Upgrade nanopb to v0.4.8

Upgrade the nanopb to 0.4.8 and add the following fixes:
  - remove the nanopb patch as it has become obsolete.
  - stop using the nanopb protoc wrapper when building the generator as
    this seems to cause build issues.
  - use the new nanopb_PYTHON_INSTDIR_OVERRIDE variable to set the
    install location for the generator. Modify TS cmake script to search
    the generator in the install content instead of the nanopb source.
  - pass discovered python settings to nanopb build using the initial
    cache. This speeds up the build and allows easier control of python
    discovery for integration systems.
  - solve nanopb os.makedirs race by creating the directory from cmake.

Signed-off-by: Ross Burton <ross.burton@arm.com>
Signed-off-by: Gyorgy Szing <gyorgy.szing@arm.com>
Change-Id: Idc36cf9a446493e4eb994fc9808f96277f11c0bc
diff --git a/external/nanopb/nanopb.cmake b/external/nanopb/nanopb.cmake
index 36465f6..f41b710 100644
--- a/external/nanopb/nanopb.cmake
+++ b/external/nanopb/nanopb.cmake
@@ -10,17 +10,14 @@
 ----------------------------
 
 This module will:
-	- download nanopb if not available locally
-	- build the runtime static library and the generator
-	- import the static library to the build
-	- define a function to provide access to the generator
+	- use LazyFetch to download nanopb and build the static library and the generator.
+	  Usual LazyFetch configuration to use prefetched source or prebuilt binaries apply.
+	- run find_module() to import the static library
+	- run find_executable() import the generator to the build (extend PYTHONPATH) and
+	  define a cmake function to provide access to the generator to build nanopb files.
 
-Note: the python module created by the generator build will be installed under
-Python_SITELIB ("Third-party platform independent installation directory.")
-This means the build may alter the state of your system. Please use virtualenv.
-
-Note: see requirements.txt for dependencies which need to be installed before
-running this module.
+Note: see requirements.txt for dependencies which need to be installed in the build
+environment to use this module.
 
 #]===]
 
@@ -28,7 +25,7 @@
 
 set(NANOPB_URL "https://github.com/nanopb/nanopb.git"
 		CACHE STRING "nanopb repository URL")
-set(NANOPB_REFSPEC "nanopb-0.4.2"
+set(NANOPB_REFSPEC "nanopb-0.4.8"
 		CACHE STRING "nanopb git refspec")
 set(NANOPB_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/nanopb-src"
 		CACHE PATH "nanopb source-code")
@@ -47,9 +44,6 @@
 	GIT_REPOSITORY ${NANOPB_URL}
 	GIT_TAG ${NANOPB_REFSPEC}
 	GIT_SHALLOW FALSE
-	#See the .patch file for details on why it is needed.
-	PATCH_COMMAND git stash
-		COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/fix-pyhon-name.patch
   )
 
 # Only pass libc settings to nanopb if needed. For environments where the standard
@@ -65,6 +59,11 @@
 	unset_saved_properties(LIBC)
 endif()
 
+# Nanopb build depends on python. Discover python here and pass the result to
+# nanopb build through the initial cache file.
+find_package(Python3 REQUIRED COMPONENTS Interpreter)
+
+# Use LazyFetch to manage the external dependency.
 include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
 LazyFetch_MakeAvailable(DEP_NAME nanopb
 	FETCH_OPTIONS ${GIT_OPTIONS}
@@ -80,12 +79,9 @@
 endif()
 
 #### Build access to the protobuf compiler
-#TODO: verify protoc dependencies: python3-protobuf
-find_package(Python3 REQUIRED COMPONENTS Interpreter)
-
-find_file(NANOPB_GENERATOR_PATH
+find_program(NANOPB_GENERATOR_PATH
 			NAMES nanopb_generator.py
-			PATHS ${nanopb_SOURCE_DIR}/generator
+			HINTS ${NANOPB_INSTALL_DIR}/bin ${NANOPB_INSTALL_DIR}/sbin
 			DOC "nanopb protobuf compiler"
 			NO_DEFAULT_PATH
 		)
@@ -178,7 +174,7 @@
 							DEPENDS "${_nanopb_fake_file}")
 		#Add a custom command to the target to create output directory.
 		add_custom_command(OUTPUT "${_nanopb_fake_file}"
-			COMMAND ${CMAKE_COMMAND} -E make_directory ${_OUT_DIR_BASE}
+			COMMAND ${CMAKE_COMMAND} -E make_directory ${_OUT_DIR_BASE}/${_OUT_DIR_REL}
 			COMMENT "Generating source from protobuf definitions for target ${PARAMS_TGT}")
 		#Ensure protobuf build happens before test target.
 		add_dependencies(${PARAMS_TGT} ${_nanopb_target})
@@ -186,11 +182,10 @@
 		target_include_directories(${PARAMS_TGT} PRIVATE ${_OUT_DIR_BASE})
 	endif()
 
-	get_filename_component(NANOPB_GENERATOR_DIR "${NANOPB_GENERATOR_PATH}" DIRECTORY CACHE "Location of nanopb generator.")
 	#Append a protobuf generator command to the nanopb_generate target.
 	add_custom_command(OUTPUT "${_OUT_C}" "${_OUT_H}"
 					   COMMAND
-					   ${CMAKE_COMMAND} -E env PYTHONPATH=${NANOPB_GENERATOR_DIR}
+					   ${CMAKE_COMMAND} -E env PYTHONPATH=${NANOPB_INSTALL_DIR}/lib/python
 					   ${Python3_EXECUTABLE} ${NANOPB_GENERATOR_PATH}
 						  -I ${PARAMS_BASE_DIR}
 						  -D ${_OUT_DIR_BASE}