1#/** @file
2# * Copyright (c) 2021-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# Set the minimum required version of CMake for the project
18cmake_minimum_required(VERSION 3.10)
19# cmake_policy
20cmake_policy(SET CMP0057 NEW)
21PROJECT (psa_adac_tests)
22
23# Find python interpreter version 3 or greater
24find_package(PythonInterp 3 REQUIRED)
25
26get_filename_component(PSA_ROOT_DIR . ABSOLUTE)
27include(${PSA_ROOT_DIR}/tools/cmake/common/Utils.cmake)
28
29set(CMAKE_C_STANDARD 99)
30
31# Check for LINK_LAYER
32if(NOT DEFINED LINK_LAYER_COMM)
33	set(LINK_LAYER_COMM unix_socket)
34    message(STATUS "[PSA] : Link layer not specified. Defaulting to ${LINK_LAYER_COMM}")
35endif()
36
37set(DEPENDS_INC_PATH ${CMAKE_SOURCE_DIR}/platform/hosts/${TARGET}/${LINK_LAYER_COMM}/include)
38if (NOT EXISTS ${DEPENDS_INC_PATH})
39    Message(FATAL_ERROR "Link layer ${LINK_LAYER_COMM} not supported for target ${TARGET}.")
40endif()
41
42if(NOT DEFINED PSA_ADAC_ROOT)
43    get_filename_component(PSA_ADAC_ROOT ${CMAKE_SOURCE_DIR}/psa-adac ABSOLUTE)
44endif()
45include(${PSA_ADAC_ROOT}/cmake/psa_adac.cmake OPTIONAL)
46
47configure_file(${PSA_ADAC_ROOT}/psa-adac/core/include/psa_adac_config.h.in psa_adac_config.h)
48include_directories (
49    ${DEPENDS_INC_PATH}
50    ${CMAKE_BINARY_DIR}
51    ${PSA_ADAC_ROOT}/psa-adac/core/include
52    ${PSA_ADAC_ROOT}/ports/include
53    )
54
55set(MBEDTLS_CONFIG_FILE "${PSA_ADAC_ROOT}/ports/crypto/manager-crypto-config.h")
56add_compile_options(-DMBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}")
57if (UNIX)
58    add_compile_options(-fPIC -fpic)
59else ()
60    # Silence warning about standard C APIs not being secure.
61    add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
62endif ()
63
64# Generate ADAC LIB
65add_subdirectory(${PSA_ADAC_ROOT}/psa-adac/core adac_core)
66add_subdirectory(${PSA_ADAC_ROOT}/psa-adac/sdm adac_sdm)
67add_subdirectory(${PSA_ADAC_ROOT}/ports/crypto/psa-crypto psa_adac_psa_crypto)
68
69set(ADAC_LIBS psa_adac_sdm psa_adac_core psa_adac_psa_crypto mbedcrypto)
70
71if(NOT DEFINED TFM_PROFILE)
72	message(STATUS "[PSA] : Building Default profile")
73list(APPEND PSA_SUITES  #PSA_SUITES
74	"ADAC"
75)
76endif()
77
78# list of VERBOSE options
79list(APPEND PSA_VERBOSE_OPTIONS 1 2 3 4 5)
80
81message(STATUS "[PSA] : ----------Process input arguments- start-------------")
82
83# Check for TARGET command line argument
84_check_arguments("TARGET")
85# Check for SUITE command line argument
86_check_arguments("SUITE")
87
88string(TOLOWER ${SUITE} SUITE_LOWER)
89
90# Check for valid targets
91_get_sub_dir_list(PSA_TARGET_LIST ${PSA_ROOT_DIR}/platform/hosts)
92if(NOT ${TARGET} IN_LIST PSA_TARGET_LIST)
93	message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DTARGET=${TARGET}, supported targets are : ${PSA_TARGET_LIST}")
94else()
95    message(STATUS "[PSA] : TARGET is set to ${TARGET}")
96    message(STATUS "[PSA] : LINK_LAYER is set to ${LINK_LAYER_COMM}")
97endif()
98
99# Check for the presence of required test suite directories
100if(NOT IS_DIRECTORY ${PSA_ROOT_DIR}/tests)
101	message(STATUS "[PSA] : Error: Could not find architecture test suite directories in psa root path ${PSA_ROOT_DIR}")
102endif()
103
104# Check for valid suite cmake argument passed
105if(NOT ${SUITE} IN_LIST PSA_SUITES)
106	message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DSUITE=${SUITE}, select one from supported suites which are : ${PSA_SUITES}")
107else()
108    message(STATUS "[PSA] : SUITE is set to ${SUITE}")
109endif()
110
111# Project variables
112set(ADAC_HOST_VAL_LIB        psa_adac_val)
113set(ADAC_HOST_PAL_LIB        psa_adac_pal)
114set(TEST_COMBINE_LIB		 test_combine)
115set(ADAC_HOST_EXE		     psa_adac_test)
116
117set(PSA_SUITE_DIR		            ${PSA_ROOT_DIR}/tests/${SUITE_LOWER})
118set(PSA_TESTLIST_GENERATOR		    ${PSA_ROOT_DIR}/tools/scripts/gen_tests_list.py)
119set(TESTSUITE_DB			        ${PSA_SUITE_DIR}/testsuite.db)
120set(PSA_TESTLIST_FILE			    ${CMAKE_CURRENT_BINARY_DIR}/${SUITE_LOWER}_testlist.txt)
121set(PSA_TEST_ENTRY_LIST_INC		    ${CMAKE_CURRENT_BINARY_DIR}/test_entry_list.inc)
122set(PSA_TEST_ENTRY_FUN_DECLARE_INC	${CMAKE_CURRENT_BINARY_DIR}/test_entry_fn_declare_list.inc)
123
124# Check for VERBOSE
125if(NOT DEFINED VERBOSE)
126	set(VERBOSE 3 CACHE INTERNAL "Default VERBOSE value" FORCE)
127        message(STATUS "[PSA] : Defaulting VERBOSE to ${VERBOSE}")
128else()
129	if(NOT ${VERBOSE} IN_LIST PSA_VERBOSE_OPTIONS)
130		message(FATAL_ERROR "[PSA] : Error: Unsupported value for -DVERBOSE=${VERBOSE}, supported values are : ${PSA_VERBOSE_OPTIONS}")
131	endif()
132    message(STATUS "[PSA] : VERBOSE is set to ${VERBOSE}")
133endif()
134
135if(NOT DEFINED SUITE_TEST_RANGE)
136	set(SUITE_TEST_RANGE_MIN None)
137	set(SUITE_TEST_RANGE_MAX None)
138else()
139	list(LENGTH SUITE_TEST_RANGE SUITE_TEST_RANGE_LENGTH)
140	if(${SUITE_TEST_RANGE_LENGTH} GREATER "2")
141		message(FATAL_ERROR "[PSA] : -DSUITE_TEST_RANGE=<...> value error! accepts two "
142                                             " numbers in quotes separated with ';'")
143	endif()
144	if(${SUITE_TEST_RANGE_LENGTH} EQUAL "2")
145		list(GET SUITE_TEST_RANGE 0 SUITE_TEST_RANGE_MIN)
146		list(GET SUITE_TEST_RANGE 1 SUITE_TEST_RANGE_MAX)
147		message(STATUS "[PSA] : Testing (${SUITE_TEST_RANGE_MIN}, ${SUITE_TEST_RANGE_MAX}) of ${SUITE} suite")
148	endif()
149	if(${SUITE_TEST_RANGE_LENGTH} EQUAL "1")
150		set(SUITE_TEST_RANGE_MIN ${SUITE_TEST_RANGE})
151		set(SUITE_TEST_RANGE_MAX ${SUITE_TEST_RANGE})
152		message(STATUS "[PSA] : Testing ${SUITE_TEST_RANGE_MIN} of ${SUITE} suite")
153	endif()
154endif()
155
156message(STATUS "[PSA] : ----------Process input arguments- complete-------------")
157
158# Create PSA clean list
159list(APPEND PSA_CLEAN_LIST
160	${PSA_TESTLIST_FILE}
161	${PSA_TEST_ENTRY_LIST_INC}
162	${PSA_TEST_ENTRY_FUN_DECLARE_INC}
163)
164
165# Process testsuite.db
166message(STATUS "[PSA] : Creating testlist.txt 'available at ${PSA_TESTLIST_FILE}'")
167execute_process(COMMAND ${PYTHON_EXECUTABLE} ${PSA_TESTLIST_GENERATOR}
168					${SUITE_LOWER}
169					${TESTSUITE_DB}
170					${PSA_TESTLIST_FILE}
171					${PSA_TEST_ENTRY_LIST_INC}
172					${PSA_TEST_ENTRY_FUN_DECLARE_INC}
173					${SUITE_TEST_RANGE_MIN}
174					${SUITE_TEST_RANGE_MAX})
175
176# Creating CMake list variable from file
177file(READ ${PSA_TESTLIST_FILE} PSA_TEST_LIST)
178if(NOT PSA_TEST_LIST)
179	message(FATAL_ERROR "[PSA] : Invalid test number!")
180endif()
181string(REGEX REPLACE "\n" ";" PSA_TEST_LIST "${PSA_TEST_LIST}")
182
183# Global macro to identify the PSA test suite cmake build
184add_definitions(-D${SUITE})
185add_definitions(-DVERBOSE=${VERBOSE})
186add_definitions(-D${TARGET})
187
188# Build PAL LIB
189include(${CMAKE_SOURCE_DIR}/platform/common/pal.cmake)
190add_subdirectory(${CMAKE_SOURCE_DIR}/platform/hosts/${TARGET}/${LINK_LAYER_COMM} platform_host)
191target_link_libraries(${ADAC_HOST_PAL_LIB} platform_host)
192
193# Generate VAL LIB
194include(${CMAKE_SOURCE_DIR}/val/val.cmake)
195
196# Build test suite
197include(${PSA_SUITE_DIR}/suite.cmake)
198
199#Create single executable
200add_executable(${ADAC_HOST_EXE} ${SUITE_CC_SOURCE})
201target_include_directories(${ADAC_HOST_EXE} PRIVATE
202    ${CMAKE_SOURCE_DIR}/val/include
203	${CMAKE_SOURCE_DIR}/platform/common/include
204    )
205target_link_libraries (${ADAC_HOST_EXE} ${TEST_COMBINE_LIB}
206                                        ${ADAC_HOST_VAL_LIB}
207                                        ${ADAC_HOST_PAL_LIB}
208                                        ${ADAC_LIBS}
209                                        )
210
211# Include the files for make clean
212foreach(clean_item ${PSA_CLEAN_LIST})
213        set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${clean_item})
214endforeach()
215
216