Build: Pre-include mbedTLS headers

Use a pre-included header when bulding the mbedTLS
to prefix the global names used in the library with
a service specific name. This will allow multiple
services to link against their own private copy of
the mbedTLS library without colliding.

Change-Id: Ib6613b40b5175f2566fd10a86d13d8afff27205f
Signed-off-by: Antonio de Angelis <antonio.deangelis@arm.com>
diff --git a/BuildMbedtls.cmake b/BuildMbedtls.cmake
index 8df2b78..0584a3d 100644
--- a/BuildMbedtls.cmake
+++ b/BuildMbedtls.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2017, Arm Limited. All rights reserved.
+# Copyright (c) 2017-2018, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -33,6 +33,14 @@
 	message(FATAL_ERROR "Please set MBEDTLS_TARGET_NAME before including this file.")
 endif()
 
+#Based on preinclude input variables, decide if preinclude flags need to be appended
+if((NOT DEFINED MBEDTLS_PREINCLUDE_PREFIX) OR (NOT DEFINED MBEDTLS_PREINCLUDE_HEADER))
+	message(STATUS "Building mbedTLS without pre-included headers and global symbols prefixing.")
+else()
+	set(MBEDTLS_PREINCLUDE_C_FLAGS " -DLIB_PREFIX_NAME=${MBEDTLS_PREINCLUDE_PREFIX} -include ${MBEDTLS_PREINCLUDE_HEADER}")
+	string(APPEND MBEDTLS_C_FLAGS ${MBEDTLS_PREINCLUDE_C_FLAGS})
+endif()
+
 string(APPEND MBEDTLS_C_FLAGS ${CMAKE_C_FLAGS})
 
 if (TARGET ${MBEDTLS_TARGET_NAME})