Enhance standard library integration

This change makes the split between standard library related and non
standard library related build settings cleaner. This may make
enabling other standard libraries to be used in SPs easier
Changes:
  - the standard library target is renamed to stdlib::c
  - newlib specific files moved to newlib component
  - GCC flags changing standard library related search paths moved to
    newlib component
  - modified newlib targets to use transitive dependencies to get
    libnosys.a linked
  - newlib component defines the -nostdinc flag again

Change-Id: I5e9ef72cc42454c70d234a15d516634eea51d494
Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
diff --git a/deployments/crypto/opteesp/CMakeLists.txt b/deployments/crypto/opteesp/CMakeLists.txt
index 16417b1..442c38e 100644
--- a/deployments/crypto/opteesp/CMakeLists.txt
+++ b/deployments/crypto/opteesp/CMakeLists.txt
@@ -87,7 +87,7 @@
 #-------------------------------------------------------------------------------
 
 # Get libc include dir
-get_property(LIBC_INCLUDE_PATH TARGET c PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
+get_property(LIBC_INCLUDE_PATH TARGET stdlib::c PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
 
 # Nanopb
 list(APPEND NANOPB_EXTERNAL_INCLUDE_PATHS ${LIBC_INCLUDE_PATH})
@@ -99,11 +99,8 @@
 list(APPEND MBEDTLS_EXTRA_INCLUDES ${LIBC_INCLUDE_PATH})
 include(../../../external/MbedTLS/MbedTLS.cmake)
 target_link_libraries(crypto-sp PRIVATE mbedcrypto)
-target_link_libraries(mbedcrypto INTERFACE c)
+target_link_libraries(mbedcrypto INTERFACE stdlib::c)
 
-if(CMAKE_CROSSCOMPILING)
-	target_link_libraries(crypto-sp PRIVATE stdc++ gcc m)
-endif()
 
 #################################################################
 
diff --git a/environments/opteesp/component.cmake b/environments/opteesp/component.cmake
index f6a65cd..1b7c418 100644
--- a/environments/opteesp/component.cmake
+++ b/environments/opteesp/component.cmake
@@ -11,9 +11,6 @@
 
 target_sources(${TGT} PRIVATE
 	"${CMAKE_CURRENT_LIST_DIR}/optee_sp_header.c"
-	"${CMAKE_CURRENT_LIST_DIR}/newlib_init.c"
-	"${CMAKE_CURRENT_LIST_DIR}/newlib_sp_assert.c"
-	"${CMAKE_CURRENT_LIST_DIR}/newlib_sp_heap.c"
 	"${CMAKE_CURRENT_LIST_DIR}/sp_entry.c"
 	"${CMAKE_CURRENT_LIST_DIR}/sp_trace.c"
 )
@@ -36,8 +33,7 @@
 include(../../../external/newlib/newlib.cmake)
 
 target_link_libraries(${TGT} PRIVATE
-	c
-	nosys
+ 	stdlib::c
 )
 
 target_link_options(${TGT} PRIVATE
diff --git a/environments/opteesp/default_toolchain_file.cmake b/environments/opteesp/default_toolchain_file.cmake
index 643773c..541cd89 100644
--- a/environments/opteesp/default_toolchain_file.cmake
+++ b/environments/opteesp/default_toolchain_file.cmake
@@ -22,9 +22,4 @@
 string(APPEND CMAKE_C_FLAGS_INIT " -fpic")
 #   - Disable startup files and default libraries.
 #   - Link position independent executable
-string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " -nostartfiles -nodefaultlibs -pie")
-
-#   -link libgcc with full PATH to work around disabled linker search paths.
-gcc_get_lib_location("libgcc.a" _TMP_VAR)
-string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${_TMP_VAR} ")
-unset(_TMP_VAR)
+string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " -pie")
diff --git a/external/newlib/newlib.cmake b/external/newlib/newlib.cmake
index d3bd220..e04f08c 100644
--- a/external/newlib/newlib.cmake
+++ b/external/newlib/newlib.cmake
@@ -13,6 +13,17 @@
 	set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.")
 endif()
 
+if (NOT DEFINED TGT)
+	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+# Compile TS specific newlib porting files.
+target_sources(${TGT} PRIVATE
+	"${CMAKE_CURRENT_LIST_DIR}/newlib_init.c"
+	"${CMAKE_CURRENT_LIST_DIR}/newlib_sp_assert.c"
+	"${CMAKE_CURRENT_LIST_DIR}/newlib_sp_heap.c"
+)
+
 set(NEWLIB_URL "https://sourceware.org/git/newlib-cygwin.git" CACHE STRING "newlib repository URL")
 set(NEWLIB_REFSPEC "newlib-4.1.0" CACHE STRING "newlib git refspec")
 set(NEWLIB_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/newlib_install" CACHE PATH "newlib installation directory")
@@ -43,7 +54,7 @@
 	FetchContent_Populate(newlib)
 endif()
 
-# Extracing compiler prefix without the trailing hyphen from the C compiler name
+# Extracting compiler prefix without the trailing hyphen from the C compiler name
 get_filename_component(COMPILER_PATH ${CMAKE_C_COMPILER} DIRECTORY)
 get_filename_component(COMPILER_NAME ${CMAKE_C_COMPILER} NAME)
 string(REGEX REPLACE "([^-]+-[^-]+-[^-]+).*" "\\1" COMPILER_PREFIX ${COMPILER_NAME})
@@ -100,8 +111,33 @@
 add_library(c STATIC IMPORTED)
 set_property(TARGET c PROPERTY IMPORTED_LOCATION "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/lib/libc.a")
 set_property(TARGET c PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/include")
+target_compile_options(c INTERFACE -nostdinc)
+target_link_options(c INTERFACE -nostartfiles -nodefaultlibs)
 
 # libnosys
 add_library(nosys STATIC IMPORTED)
 set_property(TARGET nosys PROPERTY IMPORTED_LOCATION "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/lib/libnosys.a")
 set_property(TARGET nosys PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/include")
+target_compile_options(nosys INTERFACE -nostdinc)
+target_link_options(nosys INTERFACE -nostartfiles -nodefaultlibs)
+target_link_libraries(c INTERFACE nosys)
+
+# Make standard library available in the build system.
+add_library(stdlib::c ALIAS c)
+
+# -noxxx options above require explicitly naming GCC specific library on the
+# linker command line.
+include(${TS_ROOT}/tools/cmake/compiler/GCC.cmake)
+gcc_get_lib_location(LIBRARY_NAME "libgcc.a" RES _TMP_VAR)
+link_libraries(${_TMP_VAR})
+# Moreover the GCC specific header file include directory is also required.
+# There is no way to stop cmake from filtering out built in compiler include paths
+# from compiler command line (see https://gitlab.kitware.com/cmake/cmake/-/issues/19227)
+# As a workaround copy headers to build directory and set include path to the new
+# location.
+get_filename_component(_TMP_VAR "${_TMP_VAR}" DIRECTORY)
+file(COPY "${_TMP_VAR}/include" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/gcc_include)
+file(COPY "${_TMP_VAR}/include-fixed" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/gcc-include)
+include_directories("${CMAKE_CURRENT_BINARY_DIR}/gcc_include/include")
+include_directories("${CMAKE_CURRENT_BINARY_DIR}/gcc-include/include-fixed")
+unset(_TMP_VAR)
diff --git a/environments/opteesp/newlib_init.c b/external/newlib/newlib_init.c
similarity index 100%
rename from environments/opteesp/newlib_init.c
rename to external/newlib/newlib_init.c
diff --git a/environments/opteesp/newlib_sp_assert.c b/external/newlib/newlib_sp_assert.c
similarity index 100%
rename from environments/opteesp/newlib_sp_assert.c
rename to external/newlib/newlib_sp_assert.c
diff --git a/environments/opteesp/newlib_sp_heap.c b/external/newlib/newlib_sp_heap.c
similarity index 100%
rename from environments/opteesp/newlib_sp_heap.c
rename to external/newlib/newlib_sp_heap.c