Search code examples
armstm32freertosmbedtls

How to build mbedtls for arm gcc


I want to use mbedtls for my stm32 projects, but I have some problems with building. I have to build mbedtls with arm-none-gcc compiler, right? My command is :(in build directory).

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 ../

and I have error while compiling test program:

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

Anyone has idea why cmake has problem with compiling test program?


Solution

  • I think you did not configure mbedtls so that it could be built for a bare-metal system. Some default options need to be disabled, because your target is not a general purpose operating system: (no file system, so standard BSD socket interface,...).

    This is the reason why you cannot compile the test programs, and why you need to disable their compilation by specifying -DENABLE_TESTING=OFF in your cmake build command.

    Please refer to the numerous and useful comments in mbedtls-2.25.0/include/mbedtls/config.h in order to get more details on the various configuration options that are available.

    The procedure described hereafter does work, and you should be able to modify your own build procedure by using it as a working example.

    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")
    

    Build procedure:

    # 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