From 67b64bc39238e09c0324bbfb5c6979407487ed9c Mon Sep 17 00:00:00 2001 From: zacharykeyessonos Date: Fri, 31 Oct 2025 09:55:22 -0700 Subject: [PATCH 1/3] Fix module-import-in-extern-c compiler error This fixes a compiler error when building C++ projects that use the 'modules' C++ language feature consume Mbed TLS. The specific error is module-import-in-extern-c, which occurs when `#include`s occur within `extern C` blocks. ``` $ clang-17 -std=c++20 -fmodules -Itf-psa-crypto/include -Itf-psa-crypto/drivers/builtin/include -o sample sample.cpp In file included from sample.cpp:8: In file included from tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h:44: tf-psa-crypto/drivers/builtin/include/mbedtls/private/chacha20.h:26:1: error: import of C++ module '_Builtin_stdint' appears within extern "C" language linkage specification [-Wmodule-import-in-extern-c] 26 | #include | ^ tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h:35:1: note: extern "C" language linkage specification begins here 35 | extern "C" { | ^ 1 error generated. ``` This seems to primarily be an issue with clang. See https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fmodules Signed-off-by: zacharykeyessonos --- include/mbedtls/timing.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/mbedtls/timing.h b/include/mbedtls/timing.h index 62ae1022d91d..0ec7d0821ed0 100644 --- a/include/mbedtls/timing.h +++ b/include/mbedtls/timing.h @@ -15,10 +15,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - #if !defined(MBEDTLS_TIMING_ALT) // Regular implementation // @@ -43,6 +39,10 @@ typedef struct mbedtls_timing_delay_context { #include "timing_alt.h" #endif /* MBEDTLS_TIMING_ALT */ +#ifdef __cplusplus +extern "C" { +#endif + /* Internal use */ unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset); From 765f9d9d3861970dae0a62e8ca6659fccdf8792d Mon Sep 17 00:00:00 2001 From: zacharykeyessonos Date: Mon, 3 Nov 2025 15:23:35 -0800 Subject: [PATCH 2/3] Add changelog entry Signed-off-by: zacharykeyessonos --- ChangeLog.d/fix-module-import-in-extern-c-compiler-error.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 ChangeLog.d/fix-module-import-in-extern-c-compiler-error.txt diff --git a/ChangeLog.d/fix-module-import-in-extern-c-compiler-error.txt b/ChangeLog.d/fix-module-import-in-extern-c-compiler-error.txt new file mode 100644 index 000000000000..3f7d5f132581 --- /dev/null +++ b/ChangeLog.d/fix-module-import-in-extern-c-compiler-error.txt @@ -0,0 +1,5 @@ +Changes + * Fixes a compiler error when building C++ projects that use the 'modules' + C++ language feature, and consume Mbed TLS. The specific error is + module-import-in-extern-c, which occurs when an #include occurs within + 'extern C' blocks. From 3123f1ec8100ebd2e081828a96291dbe63fc1645 Mon Sep 17 00:00:00 2001 From: zacharykeyessonos Date: Mon, 3 Nov 2025 15:22:31 -0800 Subject: [PATCH 3/3] Add the -fmodules flag to the cpp_dummy_build_cpp test program Signed-off-by: zacharykeyessonos --- programs/test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt index 8a5d6ba822f0..58f81ed02b0e 100644 --- a/programs/test/CMakeLists.txt +++ b/programs/test/CMakeLists.txt @@ -25,6 +25,7 @@ if(TEST_CPP) ) add_executable(cpp_dummy_build "${cpp_dummy_build_cpp}") set_base_compile_options(cpp_dummy_build) + target_compile_options(cpp_dummy_build PRIVATE -fmodules) target_include_directories(cpp_dummy_build PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../tf-psa-crypto/include