1if (DEFINED ENV{PICO_MBEDTLS_PATH} AND (NOT PICO_MBEDTLS_PATH))
2    set(PICO_MBEDTLS_PATH $ENV{PICO_MBEDTLS_PATH})
3    message("Using PICO_MBEDTLS_PATH from environment ('${PICO_MBEDTLS_PATH}')")
4endif()
5
6set(MBEDTLS_TEST_PATH "library/aes.c")
7if (NOT PICO_MBEDTLS_PATH)
8    set(PICO_MBEDTLS_PATH ${PROJECT_SOURCE_DIR}/lib/mbedtls)
9elseif (NOT EXISTS ${PICO_MBEDTLS_PATH}/${MBEDTLS_TEST_PATH})
10    message(WARNING "PICO_MBEDTLS_PATH specified but content not present.")
11endif()
12
13if (EXISTS ${PICO_MBEDTLS_PATH}/${MBEDTLS_TEST_PATH})
14    message("mbedtls available at ${PICO_MBEDTLS_PATH}")
15
16    pico_register_common_scope_var(PICO_MBEDTLS_PATH)
17
18    set(src_crypto
19        aes.c
20        aesni.c
21        arc4.c
22        aria.c
23        asn1parse.c
24        asn1write.c
25        base64.c
26        bignum.c
27        blowfish.c
28        camellia.c
29        ccm.c
30        chacha20.c
31        chachapoly.c
32        cipher.c
33        cipher_wrap.c
34        constant_time.c
35        cmac.c
36        ctr_drbg.c
37        des.c
38        dhm.c
39        ecdh.c
40        ecdsa.c
41        ecjpake.c
42        ecp.c
43        ecp_curves.c
44        entropy.c
45        entropy_poll.c
46        error.c
47        gcm.c
48        havege.c
49        hkdf.c
50        hmac_drbg.c
51        md.c
52        md2.c
53        md4.c
54        md5.c
55        memory_buffer_alloc.c
56        mps_reader.c
57        mps_trace.c
58        nist_kw.c
59        oid.c
60        padlock.c
61        pem.c
62        pk.c
63        pk_wrap.c
64        pkcs12.c
65        pkcs5.c
66        pkparse.c
67        pkwrite.c
68        platform.c
69        platform_util.c
70        poly1305.c
71        psa_crypto.c
72        psa_crypto_aead.c
73        psa_crypto_cipher.c
74        psa_crypto_client.c
75        psa_crypto_driver_wrappers.c
76        psa_crypto_ecp.c
77        psa_crypto_hash.c
78        psa_crypto_mac.c
79        psa_crypto_rsa.c
80        psa_crypto_se.c
81        psa_crypto_slot_management.c
82        psa_crypto_storage.c
83        psa_its_file.c
84        ripemd160.c
85        rsa.c
86        rsa_internal.c
87        sha1.c
88        sha256.c
89        sha512.c
90        threading.c
91        timing.c
92        version.c
93        version_features.c
94        xtea.c
95    )
96    list(TRANSFORM src_crypto PREPEND ${PICO_MBEDTLS_PATH}/library/)
97    pico_add_library(pico_mbedtls_crypto NOFLAG)
98    target_sources(pico_mbedtls_crypto INTERFACE ${src_crypto})
99
100    set(src_x509
101        certs.c
102        pkcs11.c
103        x509.c
104        x509_create.c
105        x509_crl.c
106        x509_crt.c
107        x509_csr.c
108        x509write_crt.c
109        x509write_csr.c
110    )
111    list(TRANSFORM src_x509 PREPEND ${PICO_MBEDTLS_PATH}/library/)
112    pico_add_library(pico_mbedtls_x509 NOFLAG)
113    target_sources(pico_mbedtls_x509 INTERFACE ${src_x509})
114
115    set(src_tls
116        debug.c
117        net_sockets.c
118        ssl_cache.c
119        ssl_ciphersuites.c
120        ssl_cli.c
121        ssl_cookie.c
122        ssl_msg.c
123        ssl_srv.c
124        ssl_ticket.c
125        ssl_tls.c
126        ssl_tls13_keys.c
127    )
128    list(TRANSFORM src_tls PREPEND ${PICO_MBEDTLS_PATH}/library/)
129    pico_add_library(pico_mbedtls_tls NOFLAG)
130    target_sources(pico_mbedtls_tls INTERFACE ${src_tls})
131
132    pico_add_library(pico_mbedtls NOFLAG)
133    pico_mirrored_target_link_libraries(pico_mbedtls INTERFACE pico_mbedtls_crypto pico_mbedtls_x509 pico_mbedtls_tls pico_rand)
134    if (DEFINED PICO_MBEDTLS_CONFIG_FILE)
135        target_compile_definitions(pico_mbedtls_headers INTERFACE MBEDTLS_CONFIG_FILE="${PICO_MBEDTLS_CONFIG_FILE}")
136    else()
137        target_compile_definitions(pico_mbedtls_headers INTERFACE MBEDTLS_CONFIG_FILE="mbedtls_config.h")
138    endif()
139    target_sources(pico_mbedtls INTERFACE ${CMAKE_CURRENT_LIST_DIR}/pico_mbedtls.c)
140    target_include_directories(pico_mbedtls_headers INTERFACE ${PICO_MBEDTLS_PATH}/include/ ${PICO_MBEDTLS_PATH}/library/)
141
142    function(suppress_mbedtls_warnings)
143        set_source_files_properties(
144            ${PICO_MBEDTLS_PATH}/library/ecdsa.c
145            ${PICO_MBEDTLS_PATH}/library/ecp.c
146            ${PICO_MBEDTLS_PATH}/library/ecp_curves.c
147            ${PICO_MBEDTLS_PATH}/library/pk_wrap.c
148            ${PICO_MBEDTLS_PATH}/library/pkparse.c
149            ${PICO_MBEDTLS_PATH}/library/ssl_cli.c
150            PROPERTIES
151            COMPILE_OPTIONS "-Wno-cast-qual"
152        )
153        set_source_files_properties(
154            ${PICO_MBEDTLS_PATH}/library/psa_crypto_client.c
155            ${PICO_MBEDTLS_PATH}/library/psa_crypto_driver_wrappers.c
156            PROPERTIES
157            COMPILE_OPTIONS "-Wno-redundant-decls"
158        )
159        set_source_files_properties(
160            ${PICO_MBEDTLS_PATH}/library/x509_crt.c
161            PROPERTIES
162            COMPILE_OPTIONS "-Wno-cast-qual;-Wno-null-dereference"
163        )
164        set_source_files_properties(
165            ${PICO_MBEDTLS_PATH}/library/ssl_srv.c
166            ${PICO_MBEDTLS_PATH}/library/ssl_tls.c
167            PROPERTIES
168            COMPILE_OPTIONS "-Wno-null-dereference"
169        )
170    endfunction()
171
172    pico_promote_common_scope_vars()
173endif()
174