如何为 arm gcc 构建 mbedtls

How to build mbedtls for arm gcc

我想为我的 stm32 项目使用 mbedtls,但我在构建时遇到了一些问题。 我必须使用 arm-none-gcc 编译器构建 mbedtls,对吗? 我的命令是:(在构建目录中)。

CC=/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc CFLAGS='-fstack-protector-strong' cmake -DUSE_SHARED_MBEDTLS_LIBRARY=On ../

编译测试程序时出现错误:

none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc CFLAGS='-fstack-protector-strong' cmake - 
DUSE_SHARED_MBEDTLS_LIBRARY=On ../
-- The C compiler identification is GNU 10.2.1
-- Check for working C compiler: 
/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4- 
major/bin/arm-none-eabi-gcc
-- Check for working C compiler: 
/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4- 
major/bin/arm-none-eabi-gcc -- broken
CMake Error at /usr/share/cmake-3.16/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler

"/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4- 
major/bin/arm-none-eabi-gcc"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: /home/jareeeeczek/Arczbit/Projects/Parkometr/software/FreeRtos/FreeRTOS- 
Plus/ThirdParty/mbedtls/build/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/make cmTC_47d0a/fast && /usr/bin/make -f 
CMakeFiles/cmTC_47d0a.dir/build.make CMakeFiles/cmTC_47d0a.dir/build
make[1]: Entering directory 
'/home/jareeeeczek/Arczbit/Projects/Parkometr/software/FreeRtos/FreeRTOS- 
Plus/ThirdParty/mbedtls/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_47d0a.dir/testCCompiler.c.o
/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4- 
major/bin/arm-none-eabi-gcc   -fstack-protector-strong    -o 
CMakeFiles/cmTC_47d0a.dir/testCCompiler.c.o   -c 
/home/jareeeeczek/Arczbit/Projects/Parkometr/software/FreeRtos/FreeRTOS- 
Plus/ThirdParty/mbedtls/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_47d0a
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_47d0a.dir/link.txt --verbose=1
/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4- 
major/bin/arm-none-eabi-gcc -fstack-protector-strong     -rdynamic 
CMakeFiles/cmTC_47d0a.dir/testCCompiler.c.o  -o cmTC_47d0a 
arm-none-eabi-gcc: error: unrecognized command-line option '-rdynamic'
make[1]: *** [CMakeFiles/cmTC_47d0a.dir/build.make:87: cmTC_47d0a] Error 1
make[1]: Leaving directory 
'/home/jareeeeczek/Arczbit/Projects/Parkometr/software/FreeRtos/FreeRTOS  - 
Plus/ThirdParty/mbedtls/build/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_47d0a/fast] Error 2

有人知道为什么 cmake 在编译测试程序时有问题吗?

我认为您没有配置 mbedtls 以便它可以为裸机系统构建。需要禁用一些默认选项,因为您的目标不是通用操作系统:(没有文件系统,所以标准 BSD 套接字接口,...)。

这就是您无法编译测试程序的原因,以及您需要通过在 cmake 构建命令中指定 -DENABLE_TESTING=OFF 来禁用它们的编译的原因。

请参阅 mbedtls-2.25.0/include/mbedtls/config.h 中的大量有用评论,以获得有关可用的各种配置选项的更多详细信息。

下文描述的过程确实有效,您应该能够通过将其用作工作示例来修改您自己的构建过程。

toochain.cmake:

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR armv7-m)
set(CMAKE_STAGING_PREFIX /tmp/staging)
set(CMAKE_C_COMPILER /opt/arm/10/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER /opt/arm/10/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-g++)
set(CMAKE_MAKE_PROGRAM=/usr/bin/make)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_C_FLAGS=-mtune=cortex-m3)
set(CMAKE_EXE_LINKER_FLAGS " --specs=nosys.specs")

构建过程:

# extracting
tar zxf v2.25.0.tar.gz

# modifying default configuration using config.py:
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h set MBEDTLS_NO_PLATFORM_ENTROPY 1
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_FS_IO
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_PSA_CRYPTO_STORAGE_C
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_TIMING_C
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_NET_C
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_PSA_CRYPTO_CONFIG
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_PSA_ITS_FILE_C

# building:
mkdir mbedtls
pushd mbedtls
cmake -DCMAKE_BUILD_TYPE=Release -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DUSE_STATIC_MBEDTLS_LIBRARY=ON -DENABLE_ZLIB_SUPPORT=OFF -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake ../mbedtls-2.25.0
make all install
popd

tree /tmp/staging
/tmp/staging
├── include
│   ├── mbedtls
│   │   ├── aes.h
│   │   ├── aesni.h
│   │   ├── arc4.h
│   │   ├── aria.h
│   │   ├── asn1.h
│   │   ├── asn1write.h
│   │   ├── base64.h
│   │   ├── bignum.h
│   │   ├── blowfish.h
│   │   ├── bn_mul.h
│   │   ├── camellia.h
│   │   ├── ccm.h
│   │   ├── certs.h
│   │   ├── chacha20.h
│   │   ├── chachapoly.h
│   │   ├── check_config.h
│   │   ├── cipher.h
│   │   ├── cipher_internal.h
│   │   ├── cmac.h
│   │   ├── compat-1.3.h
│   │   ├── config.h
│   │   ├── config_psa.h
│   │   ├── ctr_drbg.h
│   │   ├── debug.h
│   │   ├── des.h
│   │   ├── dhm.h
│   │   ├── ecdh.h
│   │   ├── ecdsa.h
│   │   ├── ecjpake.h
│   │   ├── ecp.h
│   │   ├── ecp_internal.h
│   │   ├── entropy.h
│   │   ├── entropy_poll.h
│   │   ├── error.h
│   │   ├── gcm.h
│   │   ├── havege.h
│   │   ├── hkdf.h
│   │   ├── hmac_drbg.h
│   │   ├── md2.h
│   │   ├── md4.h
│   │   ├── md5.h
│   │   ├── md.h
│   │   ├── md_internal.h
│   │   ├── memory_buffer_alloc.h
│   │   ├── net.h
│   │   ├── net_sockets.h
│   │   ├── nist_kw.h
│   │   ├── oid.h
│   │   ├── padlock.h
│   │   ├── pem.h
│   │   ├── pkcs11.h
│   │   ├── pkcs12.h
│   │   ├── pkcs5.h
│   │   ├── pk.h
│   │   ├── pk_internal.h
│   │   ├── platform.h
│   │   ├── platform_time.h
│   │   ├── platform_util.h
│   │   ├── poly1305.h
│   │   ├── psa_util.h
│   │   ├── ripemd160.h
│   │   ├── rsa.h
│   │   ├── rsa_internal.h
│   │   ├── sha1.h
│   │   ├── sha256.h
│   │   ├── sha512.h
│   │   ├── ssl_cache.h
│   │   ├── ssl_ciphersuites.h
│   │   ├── ssl_cookie.h
│   │   ├── ssl.h
│   │   ├── ssl_internal.h
│   │   ├── ssl_ticket.h
│   │   ├── threading.h
│   │   ├── timing.h
│   │   ├── version.h
│   │   ├── x509_crl.h
│   │   ├── x509_crt.h
│   │   ├── x509_csr.h
│   │   ├── x509.h
│   │   └── xtea.h
│   └── psa
│       ├── crypto_accel_driver.h
│       ├── crypto_compat.h
│       ├── crypto_config.h
│       ├── crypto_driver_common.h
│       ├── crypto_entropy_driver.h
│       ├── crypto_extra.h
│       ├── crypto.h
│       ├── crypto_platform.h
│       ├── crypto_se_driver.h
│       ├── crypto_sizes.h
│       ├── crypto_struct.h
│       ├── crypto_types.h
│       └── crypto_values.h
└── lib
    ├── libmbedcrypto.a
    ├── libmbedtls.a
    └── libmbedx509.a

4 directories, 96 files

我通过将这些行添加到 mbedtls Cmake 解决了这个问题:

set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")

感谢帮助!