Fix: fix parallel build settings for dependencies

- use the "ProcessorCount" module to determine available processors
  and use the value to configure parallel builds instead of constant
  number.
  Pass -DPROCESSOR_COUNT=<n> to cmake to override.
- use cmake --build switches instead of build tool specific parameters
- remove extra cmake run for install step

Change-Id: I04981374be28a14e02dc7c85b230695f412b770e
Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
diff --git a/external/MbedTLS/MbedTLS.cmake b/external/MbedTLS/MbedTLS.cmake
index 6e86e84..45c1251 100644
--- a/external/MbedTLS/MbedTLS.cmake
+++ b/external/MbedTLS/MbedTLS.cmake
@@ -5,6 +5,14 @@
 #
 #-------------------------------------------------------------------------------
 
+# Determine the number of processes to run while running parallel builds.
+# Pass -DPROCESSOR_COUNT=<n> to cmake to override.
+if(NOT DEFINED PROCESSOR_COUNT)
+	include(ProcessorCount)
+	ProcessorCount(PROCESSOR_COUNT)
+	set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.")
+endif()
+
 set(MBEDTLS_URL "https://github.com/ARMmbed/mbedtls.git" CACHE STRING "Mbed TLS repository URL")
 set(MBEDTLS_REFSPEC "mbedtls-2.26.0" CACHE STRING "Mbed TLS git refspec")
 set(MBEDTLS_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/mbedtls_install" CACHE PATH "Mbed TLS installation directory")
@@ -87,12 +95,12 @@
 	endif()
 endif()
 
-#TODO: add dependnecy to generated project on this file!
+#TODO: add dependency to generated project on this file!
 #TODO: add custom target to rebuild Mbed TLS
 
 #Build the library
 execute_process(COMMAND
-		${CMAKE_COMMAND} --build ${mbedtls_BINARY_DIR} -- install -j8
+		${CMAKE_COMMAND} --build ${mbedtls_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
 		RESULT_VARIABLE _exec_error
 	)
 if (_exec_error)
diff --git a/external/nanopb/nanopb.cmake b/external/nanopb/nanopb.cmake
index e74edc2..03e4596 100644
--- a/external/nanopb/nanopb.cmake
+++ b/external/nanopb/nanopb.cmake
@@ -6,7 +6,7 @@
 #-------------------------------------------------------------------------------
 
 #[===[.rst:
-NonoPB integration for cmake
+NanoPB integration for cmake
 ----------------------------
 
 This module will:
@@ -19,11 +19,19 @@
 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 dependnecies which need to be installed before
+Note: see requirements.txt for dependencies which need to be installed before
 running this module.
 
 #]===]
 
+# Determine the number of processes to run while running parallel builds.
+# Pass -DPROCESSOR_COUNT=<n> to cmake to override.
+if(NOT DEFINED PROCESSOR_COUNT)
+	include(ProcessorCount)
+	ProcessorCount(PROCESSOR_COUNT)
+	set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.")
+endif()
+
 #### Get the dependency
 
 set(NANOPB_URL "https://github.com/nanopb/nanopb.git" CACHE STRING "nanopb repository URL")
@@ -114,7 +122,7 @@
 endif()
 
 execute_process(COMMAND
-		${CMAKE_COMMAND} --build ${nanopb_BINARY_DIR} -- install -j8
+		${CMAKE_COMMAND} --build ${nanopb_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
 		RESULT_VARIABLE _exec_error
 	)
 if (_exec_error)
@@ -165,7 +173,7 @@
   Inputs:
 
   ``SRC``
-	Path to of the protobuf file to process. Either absoluto or relative to the
+	Path to of the protobuf file to process. Either absolute or relative to the
 	callers location.
 
   ``TGT``
@@ -176,7 +184,7 @@
 	separating colliding protobuf files.
 
   ``BASE_DIR``
-	Base directory. Generated files are located reletive to this base.
+	Base directory. Generated files are located relative to this base.
 
 #]===]
 function(protobuf_generate)
@@ -200,7 +208,7 @@
 		message(FATAL_ERROR "nanopb_generate(): mandatory parameter BASE_DIR missing.")
 	endif()
 
-	#If SRC is not abolute make it relative to the callers location.
+	#If SRC is not absolute make it relative to the callers location.
 	if (NOT IS_ABSOLUTE ${PARAMS_SRC})
 		set(PARAMS_SRC "${CMAKE_CURRENT_LIST_DIR}/${PARAMS_SRC}")
 	endif()
@@ -228,7 +236,7 @@
 		#Create a custom target which depends on a "fake" file.
 		add_custom_target("${_nanopb_target}"
 							DEPENDS "${_nanopb_fake_file}")
-		#Add a cutom command to the target to create output directory.
+		#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}
 			COMMENT "Generating source from protobuf definitions for target ${PARAMS_TGT}")
@@ -273,7 +281,7 @@
 	separating colliding protobuf files.
 
   ``BASE_DIR``
-	Base directory. Generated files are located reletive to this base.
+	Base directory. Generated files are located relative to this base.
 
 #]===]
 function(protobuf_generate_all)
diff --git a/external/qcbor/qcbor.cmake b/external/qcbor/qcbor.cmake
index 6d69d90..96e5dcb 100644
--- a/external/qcbor/qcbor.cmake
+++ b/external/qcbor/qcbor.cmake
@@ -3,9 +3,17 @@
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
-# QCBOR is a library for encoding and decodingg CBOR objects, as per RFC8949
+# QCBOR is a library for encoding and decoding CBOR objects, as per RFC8949
 #-------------------------------------------------------------------------------
 
+# Determine the number of processes to run while running parallel builds.
+# Pass -DPROCESSOR_COUNT=<n> to cmake to override.
+if(NOT DEFINED PROCESSOR_COUNT)
+	include(ProcessorCount)
+	ProcessorCount(PROCESSOR_COUNT)
+	set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.")
+endif()
+
 # External component details
 set(QCBOR_URL "https://github.com/laurencelundblade/QCBOR.git" CACHE STRING "qcbor repository URL")
 set(QCBOR_REFSPEC "master" CACHE STRING "qcbor git refspec")
@@ -69,15 +77,7 @@
 
 # Build the library
 execute_process(COMMAND
-		${CMAKE_COMMAND} --build ${qcbor_BINARY_DIR} -j8
-		RESULT_VARIABLE _exec_error
-	)
-if (_exec_error)
-	message(FATAL_ERROR "Build step of qcbor failed with ${_exec_error}.")
-endif()
-
-execute_process(COMMAND
-		${CMAKE_COMMAND} --install ${qcbor_BINARY_DIR}
+		${CMAKE_COMMAND} --build ${qcbor_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
 		RESULT_VARIABLE _exec_error
 	)
 if (_exec_error)
diff --git a/external/t_cose/t_cose.cmake b/external/t_cose/t_cose.cmake
index cda7871..0af40bd 100644
--- a/external/t_cose/t_cose.cmake
+++ b/external/t_cose/t_cose.cmake
@@ -6,6 +6,14 @@
 # t_cose is a library for signing CBOR tokens using COSE_Sign1
 #-------------------------------------------------------------------------------
 
+# Determine the number of processes to run while running parallel builds.
+# Pass -DPROCESSOR_COUNT=<n> to cmake to override.
+if(NOT DEFINED PROCESSOR_COUNT)
+	include(ProcessorCount)
+	ProcessorCount(PROCESSOR_COUNT)
+	set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.")
+endif()
+
 # External component details
 set(T_COSE_URL "https://github.com/laurencelundblade/t_cose.git" CACHE STRING "t_cose repository URL")
 set(T_COSE_REFSPEC "master" CACHE STRING "t_cose git refspec")
@@ -40,7 +48,7 @@
 	FetchContent_Populate(t_cose)
 endif()
 
-# Prepare include paths for dependencie that t_codse has on external components
+# Prepare include paths for dependencies that t_codse has on external components
 get_target_property(_qcbor_inc qcbor INTERFACE_INCLUDE_DIRECTORIES)
 set(_ext_inc_paths
 	${_qcbor_inc}
@@ -68,15 +76,7 @@
 
 # Build the library
 execute_process(COMMAND
-		${CMAKE_COMMAND} --build ${t_cose_BINARY_DIR} -j8
-		RESULT_VARIABLE _exec_error
-	)
-if (_exec_error)
-	message(FATAL_ERROR "Build step of t_cose failed with ${_exec_error}.")
-endif()
-
-execute_process(COMMAND
-		${CMAKE_COMMAND} --install ${t_cose_BINARY_DIR}
+		${CMAKE_COMMAND} --build ${t_cose_BINARY_DIR} --parallel ${PROCESSOR_COUNT} --target install
 		RESULT_VARIABLE _exec_error
 	)
 if (_exec_error)