Fix: Stop opteesp environment using default libc

The opteesp environment uses the libc implementation part of the
SPDEV-KIT. Projects targeting the opteesp environment shall be
configured to stop using the standard library and its header files
part of the compiler package.
This patch:
  - adds the missing compiler and linker options
  - adds header files missing from SPDEV-KITs libc implementation
  - extends the external component cmake files to align build options
    with the main project
  - moves environment specific mandatory flags to a common file
  - fixes the supported C standard from gnuc99 to c99

The following files where forked from TF-A
 (https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git; SHA: 7737fdf0ed):
- stdarg.h
- stdbool.h
- stddef.h
- stddef_.h

Signed-off-by: Andrew Beggs <andrew.beggs@arm.com>
Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
Change-Id: I3890453a29b003c60c8f44dfd19d553e96b1796c
diff --git a/environments/opteesp/component.cmake b/environments/opteesp/component.cmake
index d840249..f7e391f 100644
--- a/environments/opteesp/component.cmake
+++ b/environments/opteesp/component.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -12,3 +12,8 @@
 target_sources(${TGT} PRIVATE
 	"${CMAKE_CURRENT_LIST_DIR}/libsp_entry.c"
 	)
+
+target_include_directories(${TGT}
+         PUBLIC
+                "${CMAKE_CURRENT_LIST_DIR}/include"
+        )
diff --git a/environments/opteesp/default_toolchain_file.cmake b/environments/opteesp/default_toolchain_file.cmake
index 7cec0e4..c73345f 100644
--- a/environments/opteesp/default_toolchain_file.cmake
+++ b/environments/opteesp/default_toolchain_file.cmake
@@ -20,3 +20,19 @@
 #set(CMAKE_EXE_LINKER_FLAGS_INIT --specs=nosys.specs)
 
 include($ENV{TS_ROOT}/tools/cmake/compiler/GCC.cmake REQUIRED)
+
+# Set mandatory compiler and linker flags for this environment:
+#   - This environment uses a libc implementation from SPDEV-KIT. Disable standard
+#     include search paths, startup files and default libraries.
+string(APPEND CMAKE_C_FLAGS_INIT " -nostartfiles -nodefaultlibs -nostdinc -I ${CMAKE_CURRENT_LIST_DIR}/include")
+#   - Compile position independent code
+string(APPEND CMAKE_C_FLAGS_INIT " -fpic")
+#   -set entry point
+#   -disable link time optimization
+#   -link position independent executable
+string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " -e __sp_entry -fno-lto -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)
diff --git a/environments/opteesp/include/stdarg.h b/environments/opteesp/include/stdarg.h
new file mode 100644
index 0000000..e260b9b
--- /dev/null
+++ b/environments/opteesp/include/stdarg.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2012-2017 Roberto E. Vargas Caballero
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+/*
+ * Portions copyright (c) 2018, ARM Limited and Contributors.
+ * All rights reserved.
+ */
+
+#ifndef STDARG_H
+#define STDARG_H
+
+#define va_list __builtin_va_list
+#define va_start(ap, last) __builtin_va_start(ap, last)
+#define va_end(ap) __builtin_va_end(ap)
+#define va_copy(to, from) __builtin_va_copy(to, from)
+#define va_arg(to, type) __builtin_va_arg(to, type)
+
+#endif /* STDARG_H */
diff --git a/environments/opteesp/include/stdbool.h b/environments/opteesp/include/stdbool.h
new file mode 100644
index 0000000..b58334c
--- /dev/null
+++ b/environments/opteesp/include/stdbool.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef STDBOOL_H
+#define STDBOOL_H
+
+#define bool	_Bool
+
+#define true	(0 < 1)
+#define false	(0 > 1)
+
+#define __bool_true_false_are_defined	1
+
+#endif /* STDBOOL_H */
diff --git a/environments/opteesp/include/stddef.h b/environments/opteesp/include/stddef.h
new file mode 100644
index 0000000..58a519e
--- /dev/null
+++ b/environments/opteesp/include/stddef.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2012-2017 Roberto E. Vargas Caballero
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+/*
+ * Portions copyright (c) 2018-2019, ARM Limited and Contributors.
+ * All rights reserved.
+ */
+
+#ifndef STDDEF_H
+#define STDDEF_H
+
+#include <stddef_.h>
+
+#ifndef _PTRDIFF_T
+typedef long ptrdiff_t;
+#define _PTRDIFF_T
+#endif
+
+#ifndef NULL
+#define NULL ((void *) 0)
+#endif
+
+#define offsetof(st, m) __builtin_offsetof(st, m)
+
+#endif /* STDDEF_H */
diff --git a/environments/opteesp/include/stddef_.h b/environments/opteesp/include/stddef_.h
new file mode 100644
index 0000000..6ecc606
--- /dev/null
+++ b/environments/opteesp/include/stddef_.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef STDDEF__H
+#define STDDEF__H
+
+#ifndef SIZET_
+typedef unsigned long size_t;
+#define SIZET_
+#endif
+
+#endif /* STDDEF__H */