makefile: allow to build and link test suites against psasim

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/Makefile b/Makefile
index 67ad0b7..74e328a 100644
--- a/Makefile
+++ b/Makefile
@@ -31,7 +31,10 @@
 lib:
 	$(MAKE) -C library
 
-tests: lib mbedtls_test
+ifndef PSASIM
+tests: lib
+endif
+tests: mbedtls_test
 	$(MAKE) -C tests
 
 mbedtls_test:
@@ -168,7 +171,10 @@
 	if exist visualc\VS2017\mbedTLS.sln del /Q /F visualc\VS2017\mbedTLS.sln
 endif
 
-check: lib tests
+ifndef PSASIM
+check: lib
+endif
+check: tests
 	$(MAKE) -C tests check
 
 test: check
diff --git a/scripts/common.make b/scripts/common.make
index ead1334..077ac6f 100644
--- a/scripts/common.make
+++ b/scripts/common.make
@@ -4,6 +4,8 @@
 MBEDTLS_PATH := ..
 endif
 
+PSASIM_PATH=$(MBEDTLS_PATH)/tests/psa-client-server/psasim
+
 ifeq (,$(wildcard $(MBEDTLS_PATH)/framework/exported.make))
     # Use the define keyword to get a multi-line message.
     # GNU make appends ".  Stop.", so tweak the ending of our message accordingly.
@@ -26,19 +28,38 @@
                -I$(MBEDTLS_PATH)/tf-psa-crypto/drivers/builtin/include \
                -D_FILE_OFFSET_BITS=64
 LOCAL_CXXFLAGS = $(WARNING_CXXFLAGS) -I$(MBEDTLS_PATH)/include -I$(MBEDTLS_PATH)/tests/include -D_FILE_OFFSET_BITS=64
+
+ifdef PSASIM
+LOCAL_LDFLAGS = ${MBEDTLS_TEST_OBJS} 		\
+		-L$(PSASIM_PATH)/client_libs			\
+		-lpsaclient \
+		-lmbedtls$(SHARED_SUFFIX)	\
+		-lmbedx509$(SHARED_SUFFIX)	\
+		-lmbedcrypto$(SHARED_SUFFIX)
+else
 LOCAL_LDFLAGS = ${MBEDTLS_TEST_OBJS} 		\
 		-L$(MBEDTLS_PATH)/library			\
 		-lmbedtls$(SHARED_SUFFIX)	\
 		-lmbedx509$(SHARED_SUFFIX)	\
 		-lmbedcrypto$(SHARED_SUFFIX)
+endif
 
 include $(MBEDTLS_PATH)/3rdparty/Makefile.inc
 LOCAL_CFLAGS+=$(THIRDPARTY_INCLUDES)
 
-ifndef SHARED
-MBEDLIBS=$(MBEDTLS_PATH)/library/libmbedcrypto.a $(MBEDTLS_PATH)/library/libmbedx509.a $(MBEDTLS_PATH)/library/libmbedtls.a
+ifdef PSASIM
+MBEDLIBS=$(PSASIM_PATH)/client_libs/libmbedcrypto.a \
+				$(PSASIM_PATH)/client_libs/libmbedx509.a \
+				$(PSASIM_PATH)/client_libs/libmbedtls.a \
+				$(PSASIM_PATH)/client_libs/libpsaclient.a
+else ifndef SHARED
+MBEDLIBS=$(MBEDTLS_PATH)/library/libmbedcrypto.a \
+				$(MBEDTLS_PATH)/library/libmbedx509.a \
+				$(MBEDTLS_PATH)/library/libmbedtls.a
 else
-MBEDLIBS=$(MBEDTLS_PATH)/library/libmbedcrypto.$(DLEXT) $(MBEDTLS_PATH)/library/libmbedx509.$(DLEXT) $(MBEDTLS_PATH)/library/libmbedtls.$(DLEXT)
+MBEDLIBS=$(MBEDTLS_PATH)/library/libmbedcrypto.$(DLEXT) \
+				$(MBEDTLS_PATH)/library/libmbedx509.$(DLEXT) \
+				$(MBEDTLS_PATH)/library/libmbedtls.$(DLEXT)
 endif
 
 ifdef DEBUG
@@ -126,10 +147,17 @@
 endif
 
 # Auxiliary modules used by tests and some sample programs
-MBEDTLS_CORE_TEST_OBJS = $(patsubst %.c,%.o,$(wildcard \
+MBEDTLS_CORE_TEST_OBJS := $(patsubst %.c,%.o,$(wildcard \
     ${MBEDTLS_TEST_PATH}/src/*.c \
     ${MBEDTLS_TEST_PATH}/src/drivers/*.c \
   ))
+# Ignore PSA stubs when building for the client side of PSASIM (i.e.
+# CRYPTO_CLIENT && !CRYPTO_C) otherwise there will be functions duplicates.
+ifdef PSASIM
+MBEDTLS_CORE_TEST_OBJS := $(filter-out \
+    ${MBEDTLS_TEST_PATH}/src/psa_crypto_stubs.o, $(MBEDTLS_CORE_TEST_OBJS)\
+  )
+endif
 # Additional auxiliary modules for TLS testing
 MBEDTLS_TLS_TEST_OBJS = $(patsubst %.c,%.o,$(wildcard \
     ${MBEDTLS_TEST_PATH}/src/test_helpers/*.c \
diff --git a/tests/Makefile b/tests/Makefile
index 7ab4d9c..796d5fc 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -177,6 +177,7 @@
 
 clean:
 ifndef WINDOWS
+	$(MAKE) -C psa-client-server/psasim clean
 	rm -rf $(BINARIES) *.c *.datax
 	rm -f src/*.o src/drivers/*.o src/test_helpers/*.o src/libmbed* src/test_keys.h src/test_certs.h
 	rm -f src/test_keys.h src/test_certs.h
diff --git a/tests/psa-client-server/psasim/Makefile b/tests/psa-client-server/psasim/Makefile
index 02b639f..ec6691f 100644
--- a/tests/psa-client-server/psasim/Makefile
+++ b/tests/psa-client-server/psasim/Makefile
@@ -1,11 +1,11 @@
 CFLAGS += -Wall -Werror -std=c99 -D_XOPEN_SOURCE=1 -D_POSIX_C_SOURCE=200809L
 
 ifeq ($(DEBUG),1)
-CFLAGS += -DDEBUG -O0 -g
+override CFLAGS += -DDEBUG -O0 -g
 endif
 
-LIBPSACLIENT := -Llibpsaclient/ -lmbedcrypto -lmbedx509 -lmbedtls
-LIBPSASERVER := -Llibpsaserver/ -lmbedcrypto
+CLIENT_LIBS := -Lclient_libs -lpsaclient -lmbedtls -lmbedx509 -lmbedcrypto
+SERVER_LIBS := -Lserver_libs -lmbedcrypto
 
 MBEDTLS_ROOT_PATH = ../../..
 COMMON_INCLUDE := -I./include -I$(MBEDTLS_ROOT_PATH)/include \
@@ -16,13 +16,14 @@
 					include/psa_manifest/pid.h \
 					include/psa_manifest/sid.h
 
-PSA_CLIENT_COMMON_SRC = src/psa_ff_client.c \
-		 src/psa_sim_crypto_client.c \
-		 src/psa_sim_serialise.c
+LIBPSACLIENT_SRC = src/psa_ff_client.c \
+		 		src/psa_sim_crypto_client.c \
+		 		src/psa_sim_serialise.c
+LIBPSACLIENT_OBJS=$(LIBPSACLIENT_SRC:.c=.o)
 
-PSA_CLIENT_BASE_SRC = $(PSA_CLIENT_COMMON_SRC) src/client.c
+PSA_CLIENT_BASE_SRC = $(LIBPSACLIENT_SRC) src/client.c
 
-PSA_CLIENT_FULL_SRC = $(PSA_CLIENT_COMMON_SRC) \
+PSA_CLIENT_FULL_SRC = $(LIBPSACLIENT_SRC) \
 				$(wildcard src/aut_*.c)
 
 PARTITION_SERVER_BOOTSTRAP = src/psa_ff_bootstrap_TEST_PARTITION.c
@@ -32,21 +33,28 @@
 				 src/psa_sim_crypto_server.c \
 				 src/psa_sim_serialise.c
 
-.PHONY: all clean libpsaclient libpsaserver
+.PHONY: all clean client_libs server_libs
 
 all:
 
 test/seedfile:
 	dd if=/dev/urandom of=./test/seedfile bs=64 count=1
 
-test/psa_client_base: $(PSA_CLIENT_BASE_SRC) $(GENERATED_H_FILES)
-	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_CLIENT_BASE_SRC) $(LIBPSACLIENT) $(LDFLAGS) -o $@
+src/%.o: src/%.c $(GENERATED_H_FILES)
+	$(CC) $(COMMON_INCLUDE) $(CFLAGS) -c $< $(LDFLAGS) -o $@
 
-test/psa_client_full: $(PSA_CLIENT_FULL_SRC) $(GENERATED_H_FILES)
-	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_CLIENT_FULL_SRC) $(LIBPSACLIENT) $(LDFLAGS) -o $@
+client_libs/libpsaclient: $(LIBPSACLIENT_OBJS)
+	mkdir -p client_libs
+	$(AR) -src client_libs/libpsaclient.a $(LIBPSACLIENT_OBJS)
 
-test/psa_partition: $(PSA_SERVER_SRC) $(GENERATED_H_FILES) test/seedfile
-	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_SERVER_SRC) $(LIBPSASERVER) $(LDFLAGS) -o $@
+test/psa_client_base: $(PSA_CLIENT_BASE_SRC) $(GENERATED_H_FILES) test/seedfile
+	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_CLIENT_BASE_SRC) $(CLIENT_LIBS) $(LDFLAGS) -o $@
+
+test/psa_client_full: $(PSA_CLIENT_FULL_SRC) $(GENERATED_H_FILES) test/seedfile
+	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_CLIENT_FULL_SRC) $(CLIENT_LIBS) $(LDFLAGS) -o $@
+
+test/psa_server: $(PSA_SERVER_SRC) $(GENERATED_H_FILES)
+	$(CC) $(COMMON_INCLUDE) $(CFLAGS) $(PSA_SERVER_SRC) $(SERVER_LIBS) $(LDFLAGS) -o $@
 
 $(PARTITION_SERVER_BOOTSTRAP) $(GENERATED_H_FILES): src/manifest.json src/server.c
 	tools/psa_autogen.py src/manifest.json
@@ -56,17 +64,18 @@
 #
 # Note: these rules assume that mbedtls_config.h is already configured by all.sh.
 # If not using all.sh then the user must do it manually.
-libpsaclient libpsaserver:
+client_libs: client_libs/libpsaclient
+client_libs server_libs:
 	$(MAKE) -C $(MBEDTLS_ROOT_PATH)/library CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" libmbedcrypto.a libmbedx509.a libmbedtls.a
 	mkdir -p $@
 	cp $(MBEDTLS_ROOT_PATH)/library/libmbed*.a $@/
-	$(MAKE) -C $(MBEDTLS_ROOT_PATH) clean
 
-clean:
-	rm -f test/psa_client_base test/psa_client_full test/psa_partition
+clean_server_intermediate_files:
 	rm -f $(PARTITION_SERVER_BOOTSTRAP)
-	rm -rf libpsaclient libpsaserver
 	rm -rf include/psa_manifest
-	rm -f test/psa_service_* test/psa_notify_*
-	rm -f test/*.log
+
+clean: clean_server_intermediate_files
+	rm -f test/psa_client_base test/psa_client_full test/psa_server
+	rm -rf client_libs server_libs
+	rm -f test/psa_service_* test/psa_notify_* test/*.log
 	rm -f test/seedfile