1#/** @file
2# * Copyright (c) 2019-2022, Arm Limited or its affiliates. All rights reserved.
3# * SPDX-License-Identifier : Apache-2.0
4# *
5# * Licensed under the Apache License, Version 2.0 (the "License");
6# * you may not use this file except in compliance with the License.
7# * You may obtain a copy of the License at
8# *
9# *  http://www.apache.org/licenses/LICENSE-2.0
10# *
11# * Unless required by applicable law or agreed to in writing, software
12# * distributed under the License is distributed on an "AS IS" BASIS,
13# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# * See the License for the specific language governing permissions and
15# * limitations under the License.
16#**/
17
18# Set the minimum required version of CMake for the project
19cmake_minimum_required(VERSION 3.10)
20
21# cmake_policy
22cmake_policy(SET CMP0057 NEW)
23
24# Find python interpreter version 3 or greater
25find_package(PythonInterp 3 REQUIRED)
26# Find Git package
27find_package(Git REQUIRED)
28
29get_filename_component(PSA_ROOT_DIR . ABSOLUTE)
30
31include(${PSA_ROOT_DIR}/tools/cmake/common/Utils.cmake)
32include(${PSA_ROOT_DIR}/tools/cmake/common/CMakeSettings.cmake)
33include(${PSA_ROOT_DIR}/tools/cmake/common/CMakeExternal.cmake)
34
35if(NOT DEFINED TFM_PROFILE)
36	message(STATUS "[PSA] : Building Default profile")
37list(APPEND PSA_SUITES
38        "IPC"
39        "CRYPTO"
40        "INTERNAL_TRUSTED_STORAGE"
41        "PROTECTED_STORAGE"
42        "STORAGE"
43        "INITIAL_ATTESTATION"
44)
45else()
46
47if("${TFM_PROFILE}" STREQUAL "profile_small")
48# list of supported suites
49list(APPEND PSA_SUITES
50	"CRYPTO"
51	"INTERNAL_TRUSTED_STORAGE"
52	"INITIAL_ATTESTATION"
53)
54else()
55# list of supported suites
56list(APPEND PSA_SUITES
57	"IPC"
58	"CRYPTO"
59	"INTERNAL_TRUSTED_STORAGE"
60	"PROTECTED_STORAGE"
61	"STORAGE"
62	"INITIAL_ATTESTATION"
63)
64endif()
65endif()
66# list of ipc files required
67list(APPEND PSA_IPC_FILES
68	"psa/client.h"
69	"psa/service.h"
70	"psa/lifecycle.h"
71	"psa_manifest/sid.h"
72	"psa_manifest/pid.h"
73	"psa_manifest/driver_partition_psa.h"
74	"psa_manifest/client_partition_psa.h"
75	"psa_manifest/server_partition_psa.h"
76)
77
78# list of crypto files required
79list(APPEND PSA_CRYPTO_FILES
80	"psa/crypto.h"
81)
82
83# list of protected_storage files required
84list(APPEND PSA_PROTECTED_STORAGE_FILES
85	"psa/protected_storage.h"
86)
87
88# list of internal_trusted_storage files required
89list(APPEND PSA_INTERNAL_TRUSTED_STORAGE_FILES
90	"psa/internal_trusted_storage.h"
91)
92
93# list of storage files required
94list(APPEND PSA_STORAGE_FILES
95	${PSA_INTERNAL_TRUSTED_STORAGE_FILES}
96	${PSA_PROTECTED_STORAGE_FILES}
97)
98
99# list of initial_attestation files required
100list(APPEND PSA_INITIAL_ATTESTATION_FILES
101	"psa/initial_attestation.h"
102	"psa/crypto.h"
103)
104
105# list of supported toolchains
106list(APPEND PSA_TOOLCHAIN_SUPPORT
107        GNUARM
108        ARMCLANG
109        HOST_GCC
110		GCC_LINUX
111		INHERIT
112)
113
114# list of supported CROSS_COMPILE toolchains
115list(APPEND CROSS_COMPILE_TOOLCHAIN_SUPPORT
116        GNUARM
117		ARMCLANG
118		INHERIT
119)
120
121# list of suported CPU arch
122list(APPEND PSA_CPU_ARCH_SUPPORT
123	armv8m_ml
124	armv8m_bl
125	armv7m
126	armv8a
127)
128
129# list of VERBOSE options
130list(APPEND PSA_VERBOSE_OPTIONS 1 2 3 4 5)
131
132# list of PLATFORM_PSA_ISOLATION_LEVEL options
133if("${TFM_PROFILE}" STREQUAL "profile_medium")
134list(APPEND PLATFORM_PSA_ISOLATION_LEVEL_OPTIONS 1 2)
135else()
136list(APPEND PLATFORM_PSA_ISOLATION_LEVEL_OPTIONS 1 2 3)
137endif()
138
139#list of INCLUDE_PANIC_TESTS options
140list(APPEND PSA_INCLUDE_PANIC_TESTS_OPTIONS 0 1)
141
142# list of available spec version
143if("${SUITE}" STREQUAL "STORAGE" OR ${SUITE} STREQUAL "INTERNAL_TRUSTED_STORAGE" OR ${SUITE} STREQUAL "PROTECTED_STORAGE")
144list(APPEND PSA_SPEC_VERSION
145	1.0-BETA2
146	1.0
147)
148elseif("${SUITE}" STREQUAL "CRYPTO")
149list(APPEND PSA_SPEC_VERSION
150        1.0-BETA1
151        1.0-BETA2
152        1.0-BETA3
153)
154elseif("${SUITE}" STREQUAL "INITIAL_ATTESTATION")
155list(APPEND PSA_SPEC_VERSION
156        1.0-BETA0
157        1.0.0
158        1.0.1
159        1.0.2
160)
161elseif("${SUITE}" STREQUAL "IPC")
162list(APPEND PSA_SPEC_VERSION
163        1.0
164        1.1
165)
166endif()
167
168#list of values available for connection based
169if(${SUITE} STREQUAL "IPC")
170list(APPEND PSA_STATELESS_ROT 0 1)
171endif()
172
173#list of TESTS_COVERAGE available options
174list(APPEND PSA_TESTS_COVERAGE_OPTIONS
175		"ALL"
176        "PASS"
177)
178
179message(STATUS "[PSA] : ----------Process input arguments- start-------------")
180
181# Check for TARGET command line argument
182_check_arguments("TARGET")
183# Check for SUTIE command line argument
184_check_arguments("SUITE")
185# Check for PSA_INCLUDE_PATHS command line argument
186_check_arguments("PSA_INCLUDE_PATHS")
187
188string(TOLOWER ${SUITE} SUITE_LOWER)
189
190# Check for valid targets
191_get_sub_dir_list(PSA_TARGET_LIST ${PSA_ROOT_DIR}/platform/targets)
192if(NOT ${TARGET} IN_LIST PSA_TARGET_LIST)
193	message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DTARGET=${TARGET}, supported targets are : ${PSA_TARGET_LIST}")
194else()
195    message(STATUS "[PSA] : TARGET is set to ${TARGET}")
196endif()
197
198# Check for the presence of required test suite directories
199if((NOT IS_DIRECTORY ${PSA_ROOT_DIR}/dev_apis) OR (NOT IS_DIRECTORY ${PSA_ROOT_DIR}/ff))
200	message(STATUS "[PSA] : Error: Could not find architecture test suite directories in psa root path ${PSA_ROOT_DIR}")
201endif()
202
203if(FALSE)
204# Check for build directory specified
205if(NOT DEFINED BUILD)
206	set(BUILD ${CMAKE_CURRENT_BINARY_DIR}/BUILD CACHE INTERNAL "Defaulting build directory to ${BUILD}" FORCE)
207else()
208	set(BUILD ${CMAKE_CURRENT_BINARY_DIR}/${BUILD}/BUILD CACHE INTERNAL "Defaulting build directory to ${BUILD}" FORCE)
209endif()
210endif()
211
212# Check for valid suite cmake argument passed
213if(NOT ${SUITE} IN_LIST PSA_SUITES)
214	message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DSUITE=${SUITE}, select one from supported suites which are : ${PSA_SUITES}")
215else()
216    message(STATUS "[PSA] : SUITE is set to ${SUITE}")
217endif()
218
219# Project variables
220set(PSA_TARGET_PRE_BUILD		psa_pre_build)
221set(PSA_TARGET_GENERATE_DATABASE_PRE	psa_generate_database_prerequisite)
222set(PSA_TARGET_GENERATE_DATABASE	psa_generate_database)
223set(PSA_TARGET_GENERATE_DATABASE_POST	psa_generate_database_cleanup)
224if(${SUITE} STREQUAL "INITIAL_ATTESTATION")
225	set(PSA_TARGET_QCBOR		${CMAKE_CURRENT_BINARY_DIR}/psa_qcbor	CACHE PATH "Location of Q_CBOR sources.")
226endif()
227set(PSA_TARGET_PAL_NSPE_LIB		pal_nspe)
228set(PSA_TARGET_VAL_NSPE_LIB		val_nspe)
229set(PSA_TARGET_TEST_COMBINE_LIB		test_combine)
230set(PSA_TARGET_DRIVER_PARTITION_LIB	tfm_psa_rot_partition_driver_partition)
231set(PSA_TARGET_CLIENT_PARTITION_LIB	tfm_app_rot_partition_client_partition)
232set(PSA_TARGET_SERVER_PARTITION_LIB	tfm_app_rot_partition_server_partition)
233if(${SUITE} STREQUAL "IPC")
234	set(PSA_SUITE_DIR		${PSA_ROOT_DIR}/ff/${SUITE_LOWER})
235	set(PSA_SUITE_OUT_DIR		${CMAKE_CURRENT_BINARY_DIR}/ff/${SUITE_LOWER})
236elseif((${SUITE} STREQUAL "INTERNAL_TRUSTED_STORAGE") OR (${SUITE} STREQUAL "PROTECTED_STORAGE"))
237	set(PSA_SUITE_DIR		${PSA_ROOT_DIR}/dev_apis/storage)
238	set(PSA_SUITE_OUT_DIR		${CMAKE_CURRENT_BINARY_DIR}/dev_apis/storage)
239else()
240	set(PSA_SUITE_DIR		${PSA_ROOT_DIR}/dev_apis/${SUITE_LOWER})
241	set(PSA_SUITE_OUT_DIR		${CMAKE_CURRENT_BINARY_DIR}/dev_apis/${SUITE_LOWER})
242endif()
243set(PSA_TARGET_CONFIG_HEADER_GENERATOR	${PSA_ROOT_DIR}/tools/scripts/target_cfg/targetConfigGen.py)
244set(PSA_TESTLIST_GENERATOR		${PSA_ROOT_DIR}/tools/scripts/gen_tests_list.py)
245set(TARGET_CONFIGURATION_FILE		${PSA_ROOT_DIR}/platform/targets/${TARGET}/target.cfg)
246set(TGT_CONFIG_SOURCE_C			${CMAKE_CURRENT_BINARY_DIR}/targetConfigGen.c)
247set(OUTPUT_HEADER			target_database.h)
248set(DATABASE_TABLE_NAME			target_database)
249set(DATABASE_TABLE_SECTION_NAME		"NOSECTION")
250set(TARGET_HEADER_GEN_INCLUDE_PATHS     "${PSA_ROOT_DIR}/val/nspe|${PSA_ROOT_DIR}/val/common|${PSA_ROOT_DIR}/platform/targets/common/nspe|${PSA_ROOT_DIR}/platform/targets/common/nspe/crypto|${PSA_ROOT_DIR}/platform/targets/${TARGET}/nspe")
251if(NOT DEFINED SPEC_VERSION)
252	if(${SUITE} STREQUAL "INTERNAL_TRUSTED_STORAGE")
253		set(TESTSUITE_DB			${PSA_SUITE_DIR}/its_testsuite.db)
254	elseif((${SUITE} STREQUAL "PROTECTED_STORAGE") OR (${SUITE} STREQUAL "STORAGE"))
255		set(TESTSUITE_DB			${PSA_SUITE_DIR}/ps_testsuite.db)
256	else()
257		set(TESTSUITE_DB			${PSA_SUITE_DIR}/testsuite.db)
258	endif()
259else()
260	if(${SUITE} STREQUAL "INTERNAL_TRUSTED_STORAGE")
261		if(${SPEC_VERSION} STREQUAL "1.0-BETA2")
262                	set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/its_1.0-beta2_testsuite.db)
263	        endif()
264		if(${SPEC_VERSION} STREQUAL "1.0")
265                	set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/its_1.0_testsuite.db)
266	        endif()
267	elseif((${SUITE} STREQUAL "PROTECTED_STORAGE") OR (${SUITE} STREQUAL "STORAGE"))
268		if(${SPEC_VERSION} STREQUAL "1.0-BETA2")
269                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/ps_1.0-beta2_testsuite.db)
270                endif()
271                if(${SPEC_VERSION} STREQUAL "1.0")
272                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/ps_1.0_testsuite.db)
273                endif()
274	elseif(${SUITE} STREQUAL "CRYPTO")
275                if(${SPEC_VERSION} STREQUAL "1.0-BETA1")
276                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/1.0-beta1_testsuite.db)
277                endif()
278                if(${SPEC_VERSION} STREQUAL "1.0-BETA2")
279                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/1.0-beta2_testsuite.db)
280                endif()
281                if(${SPEC_VERSION} STREQUAL "1.0-BETA3")
282                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/1.0-beta3_testsuite.db)
283                endif()
284        elseif(${SUITE} STREQUAL "INITIAL_ATTESTATION")
285                if(${SPEC_VERSION} STREQUAL "1.0-BETA0")
286                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/1.0-beta0_testsuite.db)
287                endif()
288                if(${SPEC_VERSION} STREQUAL "1.0.0")
289                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/1.0.0_testsuite.db)
290                endif()
291                if(${SPEC_VERSION} STREQUAL "1.0.1")
292                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/1.0.1_testsuite.db)
293                endif()
294                if(${SPEC_VERSION} STREQUAL "1.0.2")
295                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/1.0.2_testsuite.db)
296                endif()
297        elseif(${SUITE} STREQUAL "IPC")
298                if(${SPEC_VERSION} STREQUAL "1.1")
299			if(DEFINED STATELESS_ROT_TESTS)
300				if(${STATELESS_ROT_TESTS} EQUAL 1)
301					set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/stateless_rot_testsuite.db)
302				else()
303					set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/testsuite.db)
304				endif()
305			else()
306                                        set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/testsuite.db)
307			endif()
308		else()
309			set(TESTSUITE_DB                        ${PSA_SUITE_DIR}/testsuite.db)
310                endif()
311	endif()
312endif()
313set(PSA_TESTLIST_FILE			${CMAKE_CURRENT_BINARY_DIR}/${SUITE_LOWER}_testlist.txt)
314set(PSA_TEST_ENTRY_LIST_INC		${CMAKE_CURRENT_BINARY_DIR}/test_entry_list.inc)
315set(PSA_TEST_ENTRY_FUN_DECLARE_INC	${CMAKE_CURRENT_BINARY_DIR}/test_entry_fn_declare_list.inc)
316set(PSA_CLIENT_TEST_LIST_DELCARE_INC	${CMAKE_CURRENT_BINARY_DIR}/client_tests_list_declare.inc)
317set(PSA_CLIENT_TEST_LIST_INC		${CMAKE_CURRENT_BINARY_DIR}/client_tests_list.inc)
318set(PSA_SERVER_TEST_LIST_DECLARE_INC	${CMAKE_CURRENT_BINARY_DIR}/server_tests_list_declare.inc)
319set(PSA_SERVER_TEST_LIST		${CMAKE_CURRENT_BINARY_DIR}/server_tests_list.inc)
320if(${SUITE} STREQUAL "INITIAL_ATTESTATION")
321	set(PSA_QCBOR_INCLUDE_PATH      ${PSA_TARGET_QCBOR}/inc)
322endif()
323
324# Validity check for required files for a given suite
325if(NOT DEFINED PSA_${SUITE}_FILES)
326	message(FATAL_ERROR "[PSA] : List of file/s to verify against ${suite} is not defined")
327endif()
328foreach(file_item ${PSA_${SUITE}_FILES})
329	set(PSA_FILE_FOUND FALSE)
330	foreach(include_path ${PSA_INCLUDE_PATHS})
331		if((EXISTS ${include_path}/${file_item}) AND
332		   (NOT PSA_FILE_FOUND))
333			set(PSA_FILE_FOUND TRUE)
334			break()
335		endif()
336	endforeach()
337	if(NOT PSA_FILE_FOUND)
338		message(FATAL_ERROR "[PSA] : Couldn't find ${file_item} in ${PSA_INCLUDE_PATHS}")
339	endif()
340endforeach()
341
342# Check for TOOLCHAIN command line argument
343if(NOT DEFINED TOOLCHAIN)
344        set(TOOLCHAIN "GNUARM" CACHE INTERNAL "Compiler used" FORCE)
345        message(STATUS "[PSA] : Defaulting compiler to ${TOOLCHAIN}")
346else()
347        message(STATUS "[PSA] : TOOLCHAIN is set to ${TOOLCHAIN}")
348endif()
349
350if(${TOOLCHAIN} STREQUAL "ARMCLANG" OR ${TOOLCHAIN} STREQUAL "GNUARM")
351    if(NOT DEFINED CPU_ARCH)
352	    message(FATAL_ERROR "[PSA] : Error: -DCPU_ARCH option missing")
353    else()
354        # Check for CPU architecture
355        if(NOT ${CPU_ARCH} IN_LIST PSA_CPU_ARCH_SUPPORT)
356	        message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DCPU_ARCH=${CPU_ARCH}, supported CPU arch are : ${PSA_CPU_ARCH_SUPPORT}")
357        endif()
358    endif()
359    message(STATUS "[PSA] : CPU_ARCH is set to ${CPU_ARCH}")
360endif()
361
362# Check for VERBOSE
363if(NOT DEFINED VERBOSE)
364	set(VERBOSE 3 CACHE INTERNAL "Default VERBOSE value" FORCE)
365        message(STATUS "[PSA] : Defaulting VERBOSE to ${VERBOSE}")
366else()
367	if(NOT ${VERBOSE} IN_LIST PSA_VERBOSE_OPTIONS)
368		message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DVERBOSE=${VERBOSE}, supported values are : ${PSA_VERBOSE_OPTIONS}")
369	endif()
370    message(STATUS "[PSA] : VERBOSE is set to ${VERBOSE}")
371endif()
372
373# Check for PLATFORM_PSA_ISOLATION_LEVEL
374if(NOT DEFINED PLATFORM_PSA_ISOLATION_LEVEL)
375	if("${TFM_PROFILE}" STREQUAL "profile_medium")
376	set(PLATFORM_PSA_ISOLATION_LEVEL 2 CACHE INTERNAL "Default PLATFORM_PSA_ISOLATION_LEVEL value" FORCE)
377	else()
378	set(PLATFORM_PSA_ISOLATION_LEVEL 3 CACHE INTERNAL "Default PLATFORM_PSA_ISOLATION_LEVEL value" FORCE)
379	endif()
380	if(${SUITE} STREQUAL "IPC")
381        message(STATUS "[PSA] : Defaulting PLATFORM_PSA_ISOLATION_LEVEL to ${PLATFORM_PSA_ISOLATION_LEVEL}")
382	endif()
383else()
384	if(NOT ${PLATFORM_PSA_ISOLATION_LEVEL} IN_LIST PLATFORM_PSA_ISOLATION_LEVEL_OPTIONS)
385		message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DPLATFORM_PSA_ISOLATION_LEVEL=${PLATFORM_PSA_ISOLATION_LEVEL}, supported values are : ${PLATFORM_PSA_ISOLATION_LEVEL_OPTIONS}")
386	endif()
387	if(${SUITE} STREQUAL "IPC")
388		message(STATUS "[PSA] : PLATFORM_PSA_ISOLATION_LEVEL is set to ${PLATFORM_PSA_ISOLATION_LEVEL}")
389	endif()
390endif()
391
392if(NOT DEFINED INCLUDE_PANIC_TESTS)
393	#By default panic tests are disabled
394	set(INCLUDE_PANIC_TESTS 0 CACHE INTERNAL "Default INCLUDE_PANIC_TESTS value" FORCE)
395        message(STATUS "[PSA] : Defaulting INCLUDE_PANIC_TESTS to ${INCLUDE_PANIC_TESTS}")
396else()
397	if(NOT ${INCLUDE_PANIC_TESTS} IN_LIST PSA_INCLUDE_PANIC_TESTS_OPTIONS)
398		message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DINCLUDE_PANIC_TESTS=${INCLUDE_PANIC_TESTS}, supported values are : ${PSA_INCLUDE_PANIC_TESTS_OPTIONS}")
399	endif()
400	if(INCLUDE_PANIC_TESTS EQUAL 1)
401		message(STATUS "[PSA] : "
402		"INCLUDE_PANIC_TESTS set to 1, therefore including PSA APIs panic tests into the regression,\n"
403		"\tensure that watchdog.num is set to 1 in ${PSA_ROOT_DIR}/platform/targets/${TARGET}/target.cfg")
404	endif()
405endif()
406
407if(NOT DEFINED WATCHDOG_AVAILABLE)
408	#Assuming watchdog is available to program by test suite
409	set(WATCHDOG_AVAILABLE	1 CACHE INTERNAL "Default WATCHDOG_AVAILABLE value" FORCE)
410        message(STATUS "[PSA] : Defaulting WATCHDOG_AVAILABLE to ${WATCHDOG_AVAILABLE}")
411else()
412    message(STATUS "[PSA] : WATCHDOG_AVAILABLE is set to ${WATCHDOG_AVAILABLE}")
413endif()
414
415if((INCLUDE_PANIC_TESTS EQUAL 1) AND
416   (WATCHDOG_AVAILABLE EQUAL 0))
417	message(WARNING "[PSA]: "
418	     "Note that to test PSA APIs panic conditions, test harness may require to access"
419		 "the watchdog timer in oder to recover from panic and to be able to continue with"
420		 "next test. Ignore this warning if system under test has capability to reset the"
421		 "system when it encounters panic condition.")
422endif()
423
424if(NOT DEFINED SP_HEAP_MEM_SUPP)
425	#Are dynamic memory functions available to secure partition?
426	set(SP_HEAP_MEM_SUPP 1 CACHE INTERNAL "Default SP_HEAP_MEM_SUPP value" FORCE)
427        message(STATUS "[PSA] : Defaulting SP_HEAP_MEM_SUPP to ${SP_HEAP_MEM_SUPP}")
428endif()
429
430if(NOT DEFINED SUITE_TEST_RANGE)
431	set(SUITE_TEST_RANGE_MIN None)
432	set(SUITE_TEST_RANGE_MAX None)
433else()
434	list(LENGTH SUITE_TEST_RANGE SUITE_TEST_RANGE_LENGTH)
435	if(${SUITE_TEST_RANGE_LENGTH} GREATER "2")
436		message(FATAL_ERROR "[PSA] : -DSUITE_TEST_RANGE=<...> value error! accepts two "
437                                             " numbers in quotes separated with ';'")
438	endif()
439	if(${SUITE_TEST_RANGE_LENGTH} EQUAL "2")
440		list(GET SUITE_TEST_RANGE 0 SUITE_TEST_RANGE_MIN)
441		list(GET SUITE_TEST_RANGE 1 SUITE_TEST_RANGE_MAX)
442		message(STATUS "[PSA] : Testing (${SUITE_TEST_RANGE_MIN}, ${SUITE_TEST_RANGE_MAX}) of ${SUITE} suite")
443	endif()
444	if(${SUITE_TEST_RANGE_LENGTH} EQUAL "1")
445		set(SUITE_TEST_RANGE_MIN ${SUITE_TEST_RANGE})
446		set(SUITE_TEST_RANGE_MAX ${SUITE_TEST_RANGE})
447		message(STATUS "[PSA] : Testing ${SUITE_TEST_RANGE_MIN} of ${SUITE} suite")
448	endif()
449endif()
450
451if(NOT DEFINED SPEC_VERSION)
452	message(STATUS "[PSA] : Default spec version")
453else()
454	if(NOT ${SPEC_VERSION} IN_LIST PSA_SPEC_VERSION)
455                message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DSPEC_VERSION=${SPEC_VERSION}, supported values are : ${PSA_SPEC_VERSION} for ${SUITE}")
456	else()
457		message(STATUS "[PSA] : Testing ${SUITE} for spec version ${SPEC_VERSION}")
458	endif()
459endif()
460
461if(DEFINED STATELESS_ROT_TESTS)
462	if(NOT ${STATELESS_ROT_TESTS} IN_LIST PSA_STATELESS_ROT)
463                 message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DSTATELESS_ROT_TESTS=${STATELESS_ROT_TESTS}, supported values are : ${PSA_STATELESS_ROT}")
464        elseif(${STATELESS_ROT_TESTS} EQUAL 1)
465                 message(STATUS "[PSA] : Testing ${SUITE} for stateless rot")
466        elseif(${STATELESS_ROT_TESTS} EQUAL 0)
467                 message(STATUS "[PSA] : Testing ${SUITE} for connection based")
468        endif()
469
470	if(NOT DEFINED SPEC_VERSION)
471                message(FATAL_ERROR "[PSA] : Error: SPEC_VERSION is require for STATELESS_ROT_TESTS.")
472	elseif(${SUITE} STREQUAL "IPC")
473		if(${SPEC_VERSION} STREQUAL "1.0")
474			message(FATAL_ERROR "[PSA] : Error: STATELESS_ROT_TESTS is only valid for SPEC_VERSION=1.1.")
475		elseif(${SPEC_VERSION} STREQUAL "1.1")
476			add_definitions(-DSPEC_VERSION=11)
477			if(${STATELESS_ROT_TESTS} EQUAL 1)
478	                        add_definitions(-DSTATELESS_ROT=1)
479			elseif(${STATELESS_ROT_TESTS} EQUAL 0)
480	                        add_definitions(-DSTATELESS_ROT=0)
481			endif()
482		endif()
483	else()
484                message(FATAL_ERROR "[PSA] : Error: STATELESS_ROT_TESTS is only applicable to IPC Test Suite.")
485	endif()
486else()
487	add_definitions(-DSTATELESS_ROT=0)
488	if(DEFINED SPEC_VERSION)
489		if(${SUITE} STREQUAL "IPC")
490                	if(${SPEC_VERSION} STREQUAL "1.0")
491                        	add_definitions(-DSPEC_VERSION=10)
492	                endif()
493        	        if(${SPEC_VERSION} STREQUAL "1.1")
494        	                add_definitions(-DSPEC_VERSION=11)
495        	        endif()
496		endif()
497	endif()
498endif()
499
500if(NOT DEFINED TESTS_COVERAGE)
501	#By default all tests are included
502	set(TESTS_COVERAGE "ALL" CACHE INTERNAL "Default TESTS_COVERAGE value" FORCE)
503        message(STATUS "[PSA] : Defaulting TESTS_COVERAGE to ${TESTS_COVERAGE}")
504else()
505	if(NOT ${TESTS_COVERAGE} IN_LIST PSA_TESTS_COVERAGE_OPTIONS)
506		message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DTESTS_COVERAGE=${TESTS_COVERAGE}, supported values are : ${PSA_TESTS_COVERAGE_OPTIONS}")
507	endif()
508	if(TESTS_COVERAGE STREQUAL ALL)
509                message(STATUS "[PSA] : "
510                "TESTS_COVERAGE set to ALL, therefore all tests are included.")
511        endif()
512	if(TESTS_COVERAGE STREQUAL PASS)
513		message(STATUS "[PSA] : "
514		"TESTS_COVERAGE set to PASS, therefore known failure tests are not included.")
515		add_definitions(-DTESTS_COVERAGE)
516	endif()
517endif()
518
519message(STATUS "[PSA] : ----------Process input arguments- complete-------------")
520
521
522if((${SUITE} STREQUAL "INITIAL_ATTESTATION") AND (NOT EXISTS ${PSA_TARGET_QCBOR}))
523# Clone QCBOR and move to specified tag
524execute_process(COMMAND ${GIT_EXECUTABLE} clone ${PSA_QCBOR_GIT_REPO_LINK} ${PSA_TARGET_QCBOR}
525	RESULT_VARIABLE qcbor_clone_result
526	WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
527if(qcbor_clone_result)
528	message(FATAL_ERROR "git clone failed for ${PSA_QCBOR_GIT_REPO_LINK}")
529endif()
530
531if(NOT qcbor_clone_result)
532execute_process(COMMAND ${GIT_EXECUTABLE} checkout -q "${PSA_QCBOR_GIT_REPO_TAG}"
533	RESULT_VARIABLE qcbor_checkout_result
534	WORKING_DIRECTORY ${PSA_TARGET_QCBOR})
535if(qcbor_checkout_result)
536	message(FATAL_ERROR "git checkout failed for Repo : ${PSA_QCBOR_GIT_REPO_LINK}, Tag : ${PSA_QCBOR_GIT_REPO_TAG}")
537endif()
538endif()
539endif()
540
541# Create PSA clean list
542list(APPEND PSA_CLEAN_LIST
543        ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_HEADER}
544	${PSA_TESTLIST_FILE}
545	${PSA_TEST_ENTRY_LIST_INC}
546	${PSA_TEST_ENTRY_FUN_DECLARE_INC}
547	${PSA_CLIENT_TEST_LIST_DELCARE_INC}
548	${PSA_CLIENT_TEST_LIST_INC}
549	${PSA_SERVER_TEST_LIST_DECLARE_INC}
550	${PSA_SERVER_TEST_LIST}
551)
552
553# Process testsuite.db
554message(STATUS "[PSA] : Creating testlist.txt 'available at ${PSA_TESTLIST_FILE}'")
555execute_process(COMMAND ${PYTHON_EXECUTABLE} ${PSA_TESTLIST_GENERATOR}
556					${SUITE_LOWER}
557					${TESTSUITE_DB}
558					${INCLUDE_PANIC_TESTS}
559					${TESTS_COVERAGE}
560					${PSA_TESTLIST_FILE}
561					${PSA_TEST_ENTRY_LIST_INC}
562					${PSA_TEST_ENTRY_FUN_DECLARE_INC}
563					${PSA_CLIENT_TEST_LIST_DELCARE_INC}
564					${PSA_CLIENT_TEST_LIST_INC}
565					${PSA_SERVER_TEST_LIST_DECLARE_INC}
566					${PSA_SERVER_TEST_LIST}
567					${SUITE_TEST_RANGE_MIN}
568					${SUITE_TEST_RANGE_MAX})
569
570# Creating CMake list variable from file
571file(READ ${PSA_TESTLIST_FILE} PSA_TEST_LIST)
572if(NOT PSA_TEST_LIST)
573	message(FATAL_ERROR "[PSA] : Invalid test number!")
574endif()
575string(REGEX REPLACE "\n" ";" PSA_TEST_LIST "${PSA_TEST_LIST}")
576
577add_custom_target(
578	${PSA_TARGET_GENERATE_DATABASE_PRE}
579	COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/platform
580	COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/val
581	COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/partition
582	COMMAND ${CMAKE_COMMAND} -E make_directory ${PSA_SUITE_OUT_DIR}
583)
584
585# Generate target files from User provided data base
586include(ExternalProject)
587ExternalProject_Add(
588        ${PSA_TARGET_GENERATE_DATABASE}
589        PREFIX ${CMAKE_CURRENT_BINARY_DIR}
590        DOWNLOAD_COMMAND ""
591        UPDATE_COMMAND ""
592        PATCH_COMMAND ""
593        BUILD_COMMAND ""
594        SOURCE_DIR "${PSA_ROOT_DIR}/tools/scripts/target_cfg"
595	CMAKE_ARGS -DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}
596		-DOUT_DIR=${CMAKE_CURRENT_BINARY_DIR}
597		-DTARGET=${TARGET}
598		-DGENERATOR_FILE=${PSA_TARGET_CONFIG_HEADER_GENERATOR}
599		-DINCLUDE_DIR=${PSA_ROOT_DIR}/val/common
600		-DTARGET_CONFIGURATION_FILE=${TARGET_CONFIGURATION_FILE}
601		-DTGT_CONFIG_SOURCE_C=${TGT_CONFIG_SOURCE_C}
602		-DOUTPUT_HEADER=${OUTPUT_HEADER}
603		-DDATABASE_TABLE_NAME=${DATABASE_TABLE_NAME}
604		-DDATABASE_TABLE_SECTION_NAME=${DATABASE_TABLE_SECTION_NAME}
605		-DTARGET_HEADER_GEN_INCLUDE_PATHS=${TARGET_HEADER_GEN_INCLUDE_PATHS}
606	LIST_SEPARATOR |
607        TEST_COMMAND ""
608)
609
610# Add custom target to clean generated files of the external project
611add_custom_target(
612        ${PSA_TARGET_GENERATE_DATABASE_POST}
613        COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/src/${PSA_TARGET_GENERATE_DATABASE}-build/ -- clean
614)
615
616if(NOT DEFINED PSA_TOOLCHAIN_FILE)
617  set(PSA_TOOLCHAIN_FILE ${PSA_ROOT_DIR}/tools/cmake/compiler/${TOOLCHAIN}.cmake)
618endif()
619
620# Check for supported toolchain/s
621if(${TOOLCHAIN} IN_LIST PSA_TOOLCHAIN_SUPPORT)
622	if (DEFINED CROSS_COMPILE)
623		if(NOT (${TOOLCHAIN} IN_LIST CROSS_COMPILE_TOOLCHAIN_SUPPORT))
624			message(FATAL_ERROR "[PSA] : Error: CROSS_COMPILE not supported for this toolchain, supported toolchain are : ${CROSS_COMPILE_TOOLCHAIN_SUPPORT}")
625		endif()
626	endif()
627    include(${PSA_TOOLCHAIN_FILE})
628else()
629	message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DTOOLCHAIN=${TOOLCHAIN}, supported toolchain are : ${PSA_TOOLCHAIN_SUPPORT}")
630endif()
631
632# Global macro to identify the PSA test suite cmake build
633add_definitions(-DPSA_CMAKE_BUILD)
634add_definitions(-D${SUITE})
635add_definitions(-DVERBOSE=${VERBOSE})
636add_definitions(-DPLATFORM_PSA_ISOLATION_LEVEL=${PLATFORM_PSA_ISOLATION_LEVEL})
637add_definitions(-D${TARGET})
638if("${TFM_PROFILE}" STREQUAL "profile_small")
639	message(STATUS "[PSA] : Building SMALL profile")
640	add_definitions(-DTF_M_PROFILE_SMALL)
641elseif("${TFM_PROFILE}" STREQUAL "profile_medium")
642	message(STATUS "[PSA] : Building MEDIUM profile")
643	add_definitions(-DTF_M_PROFILE_MEDIUM)
644endif()
645if(${SP_HEAP_MEM_SUPP} EQUAL 1)
646	add_definitions(-DSP_HEAP_MEM_SUPP)
647endif()
648if(${CC312_LEGACY_DRIVER_API_ENABLED})
649	add_definitions(-DCC312_LEGACY_DRIVER_API_ENABLED)
650endif()
651
652# Build PAL NSPE LIB
653include(${PSA_ROOT_DIR}/platform/targets/${TARGET}/target.cmake)
654# Build VAL NSPE LIB
655#add_definitions(-DVAL_NSPE_BUILD)
656include(${PSA_ROOT_DIR}/val/val_nspe.cmake)
657# Build test
658include(${PSA_SUITE_DIR}/suite.cmake)
659if(${SUITE} STREQUAL "IPC")
660# Build SPE LIB
661include(${PSA_ROOT_DIR}/val/val_spe.cmake)
662endif()
663
664add_dependencies(${PSA_TARGET_GENERATE_DATABASE}	${PSA_TARGET_GENERATE_DATABASE_PRE})
665add_dependencies(${PSA_TARGET_GENERATE_DATABASE_POST}	${PSA_TARGET_GENERATE_DATABASE})
666add_dependencies(${PSA_TARGET_PAL_NSPE_LIB} 		${PSA_TARGET_GENERATE_DATABASE_POST})
667add_dependencies(${PSA_TARGET_VAL_NSPE_LIB}		${PSA_TARGET_PAL_NSPE_LIB})
668add_dependencies(${PSA_TARGET_TEST_COMBINE_LIB}		${PSA_TARGET_VAL_NSPE_LIB})
669if(${SUITE} STREQUAL "IPC")
670add_dependencies(${PSA_TARGET_DRIVER_PARTITION_LIB}	${PSA_TARGET_TEST_COMBINE_LIB})
671add_dependencies(${PSA_TARGET_CLIENT_PARTITION_LIB}	${PSA_TARGET_DRIVER_PARTITION_LIB})
672add_dependencies(${PSA_TARGET_SERVER_PARTITION_LIB}	${PSA_TARGET_CLIENT_PARTITION_LIB})
673endif()
674
675# Include the files for make clean
676foreach(clean_item ${PSA_CLEAN_LIST})
677        set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${clean_item})
678endforeach()
679
680set_property(TARGET ${PSA_TARGET_VAL_NSPE_LIB}          PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/val)
681set_property(TARGET ${PSA_TARGET_PAL_NSPE_LIB}          PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/platform)
682set_property(TARGET ${PSA_TARGET_TEST_COMBINE_LIB}      PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${PSA_SUITE_OUT_DIR})
683if(${SUITE} STREQUAL "IPC")
684set_property(TARGET ${PSA_TARGET_DRIVER_PARTITION_LIB}  PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/partition)
685set_property(TARGET ${PSA_TARGET_CLIENT_PARTITION_LIB}  PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/partition)
686set_property(TARGET ${PSA_TARGET_SERVER_PARTITION_LIB}  PROPERTY ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/partition)
687endif()
688