Update build scripts for error.c generation

This commit updates the build scripts for error.c generation to be
generated in the Mbed TLS library directory.

Signed-off-by: Harry Ramsey <harry.ramsey@arm.com>
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index 5cb5ec8..6af4f0f 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -6,6 +6,7 @@
 endif()
 
 set(src_x509
+    error.c
     pkcs7.c
     x509.c
     x509_create.c
@@ -41,6 +42,26 @@
 )
 
 if(GEN_FILES)
+    find_package(Perl REQUIRED)
+
+    file(GLOB crypto_error_headers ${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/*.h)
+    file(GLOB tls_error_headers ${MBEDTLS_DIR}/include/mbedtls/*.h)
+    add_custom_command(
+        OUTPUT
+            ${CMAKE_CURRENT_BINARY_DIR}/error.c
+        COMMAND
+            ${PERL_EXECUTABLE}
+                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_errors.pl
+                ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/drivers/builtin/include/mbedtls
+                ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls
+                ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files
+                ${CMAKE_CURRENT_BINARY_DIR}/${TF_PSA_CRYPTO_DRIVERS_BUILTIN_SRC_DIR}/error.c
+        DEPENDS
+            ${MBEDTLS_DIR}/scripts/generate_errors.pl
+            ${crypto_error_headers}
+            ${tls_error_headers}
+            ${MBEDTLS_DIR}/scripts/data_files/error.fmt
+    )
     add_custom_command(
         OUTPUT
             ${CMAKE_CURRENT_BINARY_DIR}/version_features.c
@@ -69,6 +90,7 @@
             ${tls_error_headers}
     )
 else()
+    link_to_source(error.c)
     link_to_source(version_features.c)
     link_to_source(ssl_debug_helpers_generated.c)
 endif()