Add option to disable/enable PIC/PIE

When attempting to import and compile the project from the TF-A
unit testing project, the linker throws some errors requiring the
code to be position-independent:

  /usr/bin/ld: CMakeFiles/CppUTestExtTests.dir/MockSupport_cTestCFile.c.o:
   relocation R_X86_64_32 against `.rodata.str1.1' can not be used
   when making a PIE object; recompile with -fPIC
  /usr/bin/ld: CMakeFiles/CppUTestExtTests.dir/OrderedTestTest_c.c.o:
   relocation R_X86_64_32 against `.rodata.str1.8' can not be used
   when making a PIE object; recompile with -fPIC

This is due to the fact that newer versions of the C++ compiler produce
position independent code by default, while the C compiler does not, and
therefore this leads to code incompatibility when linking.

This patch adds a flag (POSITION_INDEPENDENT_CODE) to allow the user decide
whether they want to use PIE/PIC in their code or not. Depending on the
value of the option, the project will be set up accordingly.

Signed-off-by: Juan Pablo Conde <juanpablo.conde@arm.com>
Change-Id: If88d0678e19a31558cf6834a0ba10fd82ca46fa8
diff --git a/cmake/UnitTest.cmake b/cmake/UnitTest.cmake
index d61ed9a..27ce31d 100644
--- a/cmake/UnitTest.cmake
+++ b/cmake/UnitTest.cmake
@@ -4,6 +4,7 @@
 # SPDX-License-Identifier: BSD-3-Clause
 #
 
+
 #[===[.rst:
 UnitTest CMake module
 ---------------------
@@ -92,6 +93,18 @@
 
 set(UNIT_TEST_COMMON_SOURCES ${CMAKE_CURRENT_LIST_DIR}/../common/main.cpp CACHE STRING "List of common test source files")
 
+if (POSITION_INDEPENDENT_CODE)
+	string(APPEND CPPUTEST_CXX_FLAGS -fPIC " " -pie)
+	string(APPEND CPPUTEST_C_FLAGS -fPIC " " -pie)
+	SET(TEST_COMPILE_OPTIONS ${TEST_COMPILE_OPTIONS} -fPIC -pie)
+	SET(TEST_LINK_OPTIONS ${TEST_LINK_OPTIONS} -fPIC -pie)
+else()
+	string(APPEND CPPUTEST_CXX_FLAGS -no-pie)
+	string(APPEND CPPUTEST_C_FLAGS -no-pie)
+	string(APPEND TEST_COMPILE_OPTIONS -no-pie)
+	string(APPEND TEST_LINK_OPTIONS -no-pie)
+endif()
+
 # Checking git
 find_program(GIT_COMMAND "git")
 if (NOT GIT_COMMAND)
@@ -169,6 +182,8 @@
 			-DLONGLONG=ON
 			-DC++11=ON
 			-DCMAKE_INSTALL_PREFIX=${CPPUTEST_INSTALL_PATH}
+			-DCPPUTEST_CXX_FLAGS=${CPPUTEST_CXX_FLAGS}
+			-DCPPUTEST_C_FLAGS=${CPPUTEST_C_FLAGS}
 			-GUnix\ Makefiles
 			${cpputest_SOURCE_DIR}
 		WORKING_DIRECTORY
@@ -315,6 +330,8 @@
 
 	target_include_directories(${TEST_NAME} PRIVATE ${TEST_INCLUDE_DIRECTORIES})
 	target_compile_definitions(${TEST_NAME} PRIVATE ${TEST_COMPILE_DEFINITIONS})
+	target_compile_options(${TEST_NAME} PRIVATE ${TEST_COMPILE_OPTIONS})
+	target_link_options(${TEST_NAME} PRIVATE ${TEST_LINK_OPTIONS})
 	if (TEST_DEPENDS)
 		add_dependencies(${TEST_NAME} ${TEST_DEPENDS})
 	endif()