Change qcbor integration to use PropertyCopy

Pass libc specific compilation settings to qcbor build using
PropertyCopy. This way not only system include and include paths are
handled but compilation flags and linker flags too.
This change makes qcbor build use the -nostdinc flag, which is
needed for proper newlib integration and was missing.

Change-Id: I6d678428120155c6c1bde068e0554f71e296bebe
Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
diff --git a/deployments/attestation/attestation.cmake b/deployments/attestation/attestation.cmake
index 4f245d4..7184990 100644
--- a/deployments/attestation/attestation.cmake
+++ b/deployments/attestation/attestation.cmake
@@ -59,7 +59,6 @@
 get_property(LIBC_INCLUDE_PATH TARGET c PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
 
 # Qcbor
-set (QCBOR_EXTERNAL_INCLUDE_PATHS ${LIBC_INCLUDE_PATH})
 include(${TS_ROOT}/external/qcbor/qcbor.cmake)
 target_link_libraries(attestation PRIVATE qcbor)
 
diff --git a/external/qcbor/0001-Add-3rd-party-settings.patch b/external/qcbor/0001-Add-3rd-party-settings.patch
deleted file mode 100644
index d909e10..0000000
--- a/external/qcbor/0001-Add-3rd-party-settings.patch
+++ /dev/null
@@ -1,28 +0,0 @@
-From daaecfc924678cfd1000c87827bb5b8d4653c8e9 Mon Sep 17 00:00:00 2001
-From: Julian Hall <julian.hall@arm.com>
-Date: Mon, 5 Jul 2021 05:21:00 +0000
-Subject: [PATCH 1/2] Add 3rd party settings
-
-Introduce a way to allow defining include paths and macro
-definitions externally.
----
- CMakeLists.txt | 3 +++
- 1 file changed, 3 insertions(+)
-
-diff --git a/CMakeLists.txt b/CMakeLists.txt
-index 8a5bcd0..4e4756d 100644
---- a/CMakeLists.txt
-+++ b/CMakeLists.txt
-@@ -8,6 +8,9 @@ set(CMAKE_C_FLAGS "-pedantic -Wall -O3 -ffunction-sections")
- 
- include_directories(inc)
- 
-+include_directories(${thirdparty_inc})
-+add_definitions(${thirdparty_def})
-+
- set(SOURCE
- 	src/ieee754.c
- 	src/qcbor_decode.c
--- 
-2.17.1
-
diff --git a/external/qcbor/0001-Introduce-a-way-to-allow-setting-macro-definitions-e.patch b/external/qcbor/0001-Introduce-a-way-to-allow-setting-macro-definitions-e.patch
new file mode 100644
index 0000000..25bd279
--- /dev/null
+++ b/external/qcbor/0001-Introduce-a-way-to-allow-setting-macro-definitions-e.patch
@@ -0,0 +1,35 @@
+From c435e364e7fc505e59db2f1ec40bb5f62e591f80 Mon Sep 17 00:00:00 2001
+From: Gyorgy Szing <Gyorgy.Szing@arm.com>
+Date: Wed, 18 May 2022 22:54:47 +0000
+Subject: [PATCH 1/3] Introduce a way to allow setting macro definitions
+ externally.
+
+Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
+---
+ CMakeLists.txt | 5 ++++-
+ 1 file changed, 4 insertions(+), 1 deletion(-)
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 8a5bcd0..59dbb54 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -8,12 +8,15 @@ set(CMAKE_C_FLAGS "-pedantic -Wall -O3 -ffunction-sections")
+ 
+ include_directories(inc)
+ 
++
++add_definitions(${thirdparty_def})
++
+ set(SOURCE
+ 	src/ieee754.c
+ 	src/qcbor_decode.c
+ 	src/qcbor_encode.c
+ 	src/qcbor_err_to_str.c
+ 	src/UsefulBuf.c
+-) 
++)
+ 
+ add_library(qcbor ${SOURCE})
+-- 
+2.17.1
+
diff --git a/external/qcbor/0002-Add-install-definition.patch b/external/qcbor/0002-Add-install-definition.patch
index f6f0033..d23093d 100644
--- a/external/qcbor/0002-Add-install-definition.patch
+++ b/external/qcbor/0002-Add-install-definition.patch
@@ -1,19 +1,21 @@
-From cb011e7c8ad650bb0dd24930bf19da4a4620e30b Mon Sep 17 00:00:00 2001
+From f118cb26ec8f13e9ff0e0edb1550664d86ecb49a Mon Sep 17 00:00:00 2001
 From: Gyorgy Szing <Gyorgy.Szing@arm.com>
 Date: Mon, 5 Jul 2021 06:45:47 +0000
-Subject: [PATCH 2/2] Add install definition
+Subject: [PATCH 2/3] Add install definition
 
 Add install() calls to define stable way to access build artifacts.
+
+Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
 ---
  CMakeLists.txt | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)
 
 diff --git a/CMakeLists.txt b/CMakeLists.txt
-index 4e4756d..b26edce 100644
+index 1b4c95c..c4d91c5 100644
 --- a/CMakeLists.txt
 +++ b/CMakeLists.txt
-@@ -20,3 +20,20 @@ set(SOURCE
- ) 
+@@ -19,3 +19,20 @@ set(SOURCE
+ )
  
  add_library(qcbor ${SOURCE})
 +
@@ -34,5 +36,5 @@
 +               COMPONENT qcbor
 +)
 -- 
-2.17.1
+2.35.0.windows.1
 
diff --git a/external/qcbor/0003-Fix-stop-overriding-C_FLAGS-from-environment.patch b/external/qcbor/0003-Fix-stop-overriding-C_FLAGS-from-environment.patch
new file mode 100644
index 0000000..2951c98
--- /dev/null
+++ b/external/qcbor/0003-Fix-stop-overriding-C_FLAGS-from-environment.patch
@@ -0,0 +1,31 @@
+From ac33a37507858f6c5ffac8f34541be56cf6b05d1 Mon Sep 17 00:00:00 2001
+From: Gyorgy Szing <Gyorgy.Szing@arm.com>
+Date: Thu, 12 May 2022 21:45:56 +0200
+Subject: [PATCH 3/3] Fix: stop overriding C_FLAGS from environment.
+
+Directly setting CMAKE_C_FLAGS is bad practice as it is overriding
+values taken from C_FLAGS environment variable or from
+CMAKE_C_FLAGS_INIT. The latter is used in toolchain files or in
+initial cache files.
+
+Signed-off-by: Gyorgy Szing <Gyorgy.Szing@arm.com>
+---
+ CMakeLists.txt | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index c4d91c5..a9a5bef 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -4,7 +4,7 @@ project(qcbor
+ 	LANGUAGES C
+ 	VERSION 1.0.0)
+ 
+-set(CMAKE_C_FLAGS "-pedantic -Wall -O3 -ffunction-sections")
++add_compile_options(-pedantic -Wall -O3 -ffunction-sections)
+ 
+ include_directories(inc)
+ 
+-- 
+2.35.0.windows.1
+
diff --git a/external/qcbor/qcbor-init-cache.cmake.in b/external/qcbor/qcbor-init-cache.cmake.in
index 28c53ec..88afecf 100644
--- a/external/qcbor/qcbor-init-cache.cmake.in
+++ b/external/qcbor/qcbor-init-cache.cmake.in
@@ -8,13 +8,10 @@
 set(CMAKE_INSTALL_PREFIX "@BUILD_INSTALL_DIR@" CACHE STRING "")
 set(CMAKE_TOOLCHAIN_FILE "@TS_EXTERNAL_LIB_TOOLCHAIN_FILE@" CACHE STRING "")
 
-set(QCBOR_EXTERNAL_INCLUDE_PATHS "@QCBOR_EXTERNAL_INCLUDE_PATHS@")
-if (QCBOR_EXTERNAL_INCLUDE_PATHS)
-	set(thirdparty_inc "${QCBOR_EXTERNAL_INCLUDE_PATHS}" CACHE STRING "")
-endif()
-
 # Determine floating point configuration
 set(TS_NO_FLOAT_HW "@TS_NO_FLOAT_HW@")
 if (TS_NO_FLOAT_HW)
 	set(thirdparty_def "-DQCBOR_DISABLE_FLOAT_HW_USE" CACHE STRING "")
 endif()
+
+@_cmake_fragment@
diff --git a/external/qcbor/qcbor.cmake b/external/qcbor/qcbor.cmake
index 11749b6..f2d940a 100644
--- a/external/qcbor/qcbor.cmake
+++ b/external/qcbor/qcbor.cmake
@@ -18,10 +18,24 @@
 
 	PATCH_COMMAND git stash
 		COMMAND git branch -f bf-patch
-		COMMAND git am ${CMAKE_CURRENT_LIST_DIR}/0001-Add-3rd-party-settings.patch ${CMAKE_CURRENT_LIST_DIR}/0002-Add-install-definition.patch
+		COMMAND git am ${CMAKE_CURRENT_LIST_DIR}/0001-Introduce-a-way-to-allow-setting-macro-definitions-e.patch
+					   ${CMAKE_CURRENT_LIST_DIR}/0002-Add-install-definition.patch
+					   ${CMAKE_CURRENT_LIST_DIR}/0003-Fix-stop-overriding-C_FLAGS-from-environment.patch
 		COMMAND git reset bf-patch
 )
 
+# Only pass libc settings to qcbor if needed. For environments where the standard
+# library is not overridden, this is not needed.
+if(TARGET stdlib::c)
+	include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake)
+
+	# Save libc settings
+	save_interface_target_properties(TGT stdlib::c PREFIX LIBC)
+	# Translate libc settings to cmake code fragment. Will be inserted into
+	# qcbor-init-cache.cmake.in when LazyFetch configures the file.
+	translate_interface_target_properties(PREFIX LIBC RES _cmake_fragment)
+	unset_saved_properties(LIBC)
+endif()
 
 include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
 LazyFetch_MakeAvailable(DEP_NAME qcbor
@@ -30,6 +44,7 @@
 	CACHE_FILE "${CMAKE_CURRENT_LIST_DIR}/qcbor-init-cache.cmake.in"
 	SOURCE_DIR "${QCBOR_SOURCE_DIR}"
 	)
+unset(_cmake_fragment)
 
 # Create an imported target to have clean abstraction in the build-system.
 add_library(qcbor STATIC IMPORTED)