Kconfig: Update defaults with platform-specific component options

* Kconfig by default ignores platform-specific component options.
* This causes incorrect default builds.
* Set Kconfig defaults to match options in config_tfm_target.h

Change-Id: I672509c171c2b1fbdd4567825c8adce030822c33
Signed-off-by: Igor Silin <igor.silin@arm.com>
diff --git a/Kconfig b/Kconfig
index ac44d48..a4cad01 100644
--- a/Kconfig
+++ b/Kconfig
@@ -38,6 +38,7 @@
     rsource "$(TFM_SOURCE_DIR)/config/tests/Kconfig.test_psa_api"
 endif
 
+osource "$(PLATFORM_PATH)/Kconfig.comp"
 rsource "secure_fw/spm/Kconfig.comp"
 
 menu "Secure Partitions component configs"
diff --git a/config/kconfig.cmake b/config/kconfig.cmake
index 58ba940..5a27bb5 100644
--- a/config/kconfig.cmake
+++ b/config/kconfig.cmake
@@ -103,6 +103,42 @@
     set(${out_var} ${local_var} PARENT_SCOPE)
 endfunction()
 
+# This function parses the input ${header_file} to get C Header constants and their values in the
+# format of "#define _CONST_ _VALUE_ " and converts them to Kconfig options
+function(convert_header_config_to_kconfig header_file out_var)
+    # Operate on a local var and write back to the "out_var" later.
+    set(local_var "")
+
+    # Read out the strings of the file. Binary data in the file are ignored
+    file(STRINGS ${header_file} CONTENTS)
+
+    # Exclude lines of comments (started with "/*", or "*" if within multi-line comment)
+    set(CONTENTS_WITHOUT_COMMENTS "")
+
+    foreach(LINE ${CONTENTS})
+        string(REGEX MATCH "^.\\*.*" OUT_STRING ${LINE})
+        if(NOT OUT_STRING)
+            string(APPEND CONTENTS_WITHOUT_COMMENTS "${LINE}\n")
+        endif()
+    endforeach()
+
+    # Search for strings matching #define _CONST_ _VALUE_
+    string(REGEX MATCHALL
+    "#define[ \t\r\n]+([A-Za-z0-9_]+)[ \t\r\n]+([A-Za-z0-9_]+)"
+    OUT_STRINGS ${CONTENTS_WITHOUT_COMMENTS})
+
+    # Convert each matching C Header constant into a Kconfig option
+    foreach(MATCHED_CONST ${OUT_STRINGS})
+        string(REGEX REPLACE
+            "#define[ \t\r\n]+([A-Za-z0-9_]+)[ \t\r\n]+([A-Za-z0-9_]+)"
+            "config \\1\n default \\2\n"
+            MATCHED_CONST ${MATCHED_CONST})
+        string(APPEND local_var ${MATCHED_CONST})
+    endforeach()
+
+    set(${out_var} ${local_var} PARENT_SCOPE)
+endfunction()
+
 # This function goes through the CMake cache variables and convert them to .config format.
 # The function distinguishes command-line variables and other ones and it can only handle one of
 # them in the same time.
@@ -180,10 +216,19 @@
 file(REMOVE ${CACHE_VAR_CONFIG_FILE})
 
 if(NOT EXISTS ${PLATFORM_KCONFIG} AND NOT EXISTS ${DOTCONFIG_FILE})
-    # Parse platform's cpuarch.cmake and config.cmake to get config options.
+    # Parse platform's config_tfm_target.h, cpuarch.cmake and config.cmake to get config options.
     set(PLATFORM_KCONFIG_OPTIONS "")
     set(PLATFORM_KCONFIG ${KCONFIG_OUTPUT_DIR}/platform/Kconfig)
 
+    set(PLATFORM_KCONFIG_COMP_OPTIONS "")
+    set(PLATFORM_KCONFIG_COMP ${KCONFIG_OUTPUT_DIR}/platform/Kconfig.comp)
+
+    if(EXISTS ${TARGET_PLATFORM_PATH}/config_tfm_target.h)
+        # Convert constants from config_tfm_target.h into Kconfig options and save them in component options Kconfig file
+        convert_header_config_to_kconfig(${TARGET_PLATFORM_PATH}/config_tfm_target.h PLATFORM_KCONFIG_COMP_OPTIONS)
+        file(WRITE ${PLATFORM_KCONFIG_COMP} ${PLATFORM_KCONFIG_COMP_OPTIONS})
+    endif()
+
     convert_normal_cmake_config_to_kconfig(${TARGET_PLATFORM_PATH}/cpuarch.cmake PLATFORM_KCONFIG_OPTIONS)
     file(WRITE ${PLATFORM_KCONFIG} ${PLATFORM_KCONFIG_OPTIONS})