1#-------------------------------------------------------------------------------
2# Copyright (c) 2021-2023, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8add_library(bl1_1_shared_lib STATIC)
9add_library(bl1_1_shared_lib_interface INTERFACE)
10
11target_include_directories(bl1_1_shared_lib_interface
12    INTERFACE
13        interface
14        $<$<BOOL:${TFM_BL1_DEFAULT_OTP}>:${CMAKE_CURRENT_SOURCE_DIR}/otp>
15        $<$<BOOL:${TFM_BL1_PQ_CRYPTO}>:${MBEDCRYPTO_PATH}/include>
16        $<$<BOOL:${TFM_BL1_PQ_CRYPTO}>:${CMAKE_CURRENT_SOURCE_DIR}/pq_crypto>
17)
18
19target_link_libraries(bl1_1_shared_lib_interface
20    INTERFACE
21        psa_interface
22        tfm_fih_headers
23)
24
25target_sources(bl1_1_shared_lib
26    PRIVATE
27        $<$<BOOL:${TFM_BL1_SOFTWARE_CRYPTO}>:crypto/crypto_mbedcrypto>
28        $<$<BOOL:${TFM_BL1_DUMMY_TRNG}>:trng/trng_dummy.c>
29        ./util.c
30        $<$<BOOL:${TFM_BL1_DEFAULT_OTP}>:./otp/otp_default.c>
31        $<$<BOOL:${TFM_BL1_PQ_CRYPTO}>:./pq_crypto/pq_crypto_psa.c>
32        $<$<BOOL:${TFM_BL1_PQ_CRYPTO}>:${MBEDCRYPTO_PATH}/library/lms.c>
33        $<$<BOOL:${TFM_BL1_PQ_CRYPTO}>:${MBEDCRYPTO_PATH}/library/lmots.c>
34        $<$<BOOL:${TFM_BL1_PQ_CRYPTO}>:${MBEDCRYPTO_PATH}/library/platform_util.c>
35        $<$<BOOL:${TFM_BL1_PQ_CRYPTO}>:${MBEDCRYPTO_PATH}/library/psa_util.c>
36)
37
38target_link_libraries(bl1_1_shared_lib
39    PUBLIC
40        bl1_1_shared_lib_interface
41        tfm_fih
42    PRIVATE
43        platform_bl1_1
44        $<$<BOOL:${TFM_BL1_SOFTWARE_CRYPTO}>:bl1_1_crypto_mbedcrypto>
45)
46
47target_compile_options(bl1_1_shared_lib_interface
48    INTERFACE
49        #Prevents warnings caused by C99 static assert workaround
50        -Wno-unused-local-typedefs
51)
52
53target_compile_definitions(bl1_1_shared_lib_interface
54    INTERFACE
55        $<$<BOOL:${TEST_BL1_1}>:TEST_BL1_1>
56        MCUBOOT_FIH_PROFILE_HIGH
57        $<$<BOOL:${TFM_BL1_PQ_CRYPTO}>:MBEDTLS_CONFIG_FILE="mbedtls-pq-cfg.h">
58)
59
60if(TFM_BL1_SOFTWARE_CRYPTO)
61    cmake_policy(SET CMP0079 NEW)
62
63    set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
64    set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
65    set(ENABLE_TESTING OFF)
66    set(ENABLE_PROGRAMS OFF)
67    set(MBEDTLS_FATAL_WARNINGS OFF)
68    set(ENABLE_DOCS OFF)
69    set(INSTALL_MBEDTLS_HEADERS OFF)
70    set(LIB_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/mbedcrypto/install)
71
72    set(lib_target bl1_1_crypto_mbedcrypto_libs)
73    set(mbedcrypto_target bl1_1_crypto_mbedcrypto)
74    set(mbedtls_target bl1_1_crypto_mbedtls)
75    set(mbedx509_target bl1_1_crypto_mbedx509)
76    set(MBEDTLS_TARGET_PREFIX bl1_1_crypto_)
77
78    # Mbedcrypto is quite a large lib, and it uses too much memory for it to be
79    # reasonable to build it in debug info. As a compromise, if `debug` build type
80    # is selected mbedcrypto will build under `relwithdebinfo` which preserved debug
81    # symbols whild optimizing space.
82    set(SAVED_BUILD_TYPE ${CMAKE_BUILD_TYPE})
83    set(CMAKE_BUILD_TYPE ${MBEDCRYPTO_BUILD_TYPE})
84    add_subdirectory(${MBEDCRYPTO_PATH} ${CMAKE_CURRENT_BINARY_DIR}/mbedcrypto)
85    set(CMAKE_BUILD_TYPE ${SAVED_BUILD_TYPE} CACHE STRING "Build type: [Debug, Release, RelWithDebInfo, MinSizeRel]" FORCE)
86
87    if(NOT TARGET bl1_1_crypto_mbedcrypto)
88        message(FATAL_ERROR "Target bl1_1_crypto_mbedcrypto does not exist. Have the patches in ${CMAKE_SOURCE_DIR}/lib/ext/mbedcrypto been applied to the mbedcrypto repo at ${MBEDCRYPTO_PATH} ?
89        Hint: The command might be `cd ${MBEDCRYPTO_PATH} && git apply ${CMAKE_SOURCE_DIR}/lib/ext/mbedcrypto/*.patch`")
90    endif()
91
92    target_include_directories(bl1_1_crypto_mbedcrypto
93        PUBLIC
94            ./crypto
95    )
96
97    target_compile_definitions(bl1_1_crypto_mbedcrypto
98        PUBLIC
99            MBEDTLS_CONFIG_FILE="mbedcrypto_config.h"
100            # Workaround for https://github.com/ARMmbed/mbedtls/issues/1077
101            $<$<OR:$<STREQUAL:${CMAKE_SYSTEM_ARCHITECTURE},armv8-m.base>,$<STREQUAL:${CMAKE_SYSTEM_ARCHITECTURE},armv6-m>>:MULADDC_CANNOT_USE_R7>
102    )
103
104    target_compile_options(bl1_1_crypto_mbedcrypto
105        PRIVATE
106            $<$<C_COMPILER_ID:GNU>:-Wno-unused-parameter>
107            $<$<C_COMPILER_ID:ARMClang>:-Wno-unused-parameter>
108    )
109endif()
110