Giter Site home page Giter Site logo

vpetrigo / arm-cmake-toolchains Goto Github PK

View Code? Open in Web Editor NEW
261.0 20.0 49.0 366 KB

CMake toolchain configurations for ARM

License: BSD 3-Clause "New" or "Revised" License

CMake 82.45% Shell 17.55%
arm cmake gcc clang cortex-m efm32 stm32 toolchain-cmake arm-gcc cmake-files

arm-cmake-toolchains's Introduction

Example projects build GitHub

CMake toolchain files for ARM GCC compiler

This repo contains CMake toolchain files that utilizes ARM GCC. MCU families that can be used:

  • STM32 (F0, F1, F2, F3, F4, F7, G0, G4, H7, L0, L1, L4, L5, U5, WB, WL)
  • nRF51/nRF52 (nRF52832, nRF52840, nRF51822)
  • NXP Kinetis (LPC800, LPC5500, K-series like K64)
  • Many more (any Cortex-M based) ๐Ÿ˜„

One of them arm-gcc-toolchain.cmake uses arm-none-eabi-gcc suite to build CMake project. To use it the toolchain must be added to the system PATH variable.

Example for Ninja generator, Debug build:

PATH=<path/to/arm-none-eabi>:$PATH cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=arm-gcc-toolchain.cmake -DCMAKE_BUILD_TYPE=Debug

clang-arm-gcc-toolchain.cmake uses Clang front-end as a compiler that has some additional code analyzer features with comprehensive warning/errormessages.

Example projects

Customization of toolchain file

Now the arm-gcc-toolchain.cmake and clang-arm-gcc-toolchain.cmake files use arm-none-eabi compiler triple and prefix. In case you want to use another compiler you should change the TOOLCHAIN_PREFIX variable in both CMake toolchain files and TOOLCHAIN_TRIPLE for Clang-specific file only:

# arm-gcc-toolchain.cmake and clang-arm-gcc-toolchain.cmake
set(TOOLCHAIN_PREFIX <your-compiler-prefix>)
# clang-arm-gcc-toolchain.cmake ONLY
set(TOOLCHAIN_TRIPLE <your-compiler-triple>)

Also some options which is essential for Cortex-M specific compiler (arm-none-eabi) might be not relevant for your compiler. In that case you should simply delete obsolete lines from toolchain CMake file. For example the line below is not relevant for uClinux compiler:

set(CMAKE_EXE_LINKER_FLAGS_INIT "--specs=nosys.specs")

And that line can be deleted while you use arm-uclinuxeabi compiler without any side effect.

Additional macros/functions in utils.cmake

In the utils.cmake file you can find some useful macros/functions that you can add into your CMake files. To do that you need to include utils.cmake:

# CMakeLists.txt somewhere in your project
include(<path-to-utils-file>/utils.cmake)

It might be a good idea to include that file in your project's root CMakeLists.txt to allow other CMake files, which may reside in subdirectories, to use provided features.

  • subdirlist:
# store all subfolder names in a ${result} variable
# might be useful when you have lots of subdirectories with
# headers and do not want to type them manually
# IMPORTANT: ${result} contains paths relative to a ${current_dir}
subdirlist(result current_dir)

target_include_directories(<target> ${result})
  • prepend_cur_dir:
# prepend ${CMAKE_CURRENT_SOURCE_DIR} to a directory
# so that you have the full path to a header/source file
# and store the result in the provided ${variable}
prepend_cur_dir(full_path_to_dir dir)
# the ${full_path_to_dir} for dir/ might be like:
${full_path_to_dir} -> C:/Users/SpecificUser/Project/dir
  • firmware_size:
# function that adds custom target that will
# output resulted executable image size by using 
# binutils size command
add_executable(hello_world main.c)
...
firmware_size(hello_world)
# Possible output:
# text    data    bss     dec     hex     filename
# 294880  81920   11592   388392  5ed28   hello_world
  • generate_object:
# function that adds custom target that will
# generate additional output file with specified
# type by using binutils objcopy
add_executable(hello_world main.c)
...
generate_object(hello_world .hex ihex)
# in addition to .elf file the hex file for
# hello_world firmware would be generated

arm-cmake-toolchains's People

Contributors

andersm avatar mattkang avatar vpetrigo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

arm-cmake-toolchains's Issues

No Makefile

I'm new to this, so far I've only used IDE. That's why I'm probably asking a stupid question: I did everything according to the instructions, but I only get a bunch of files for Visual Studio as an output. No Makefile. Probably the problem is in the settings that I don't know? Can you help with that?

freetoair, Thanks !

Provide support for searching CRT files during Clang builds

Right now examples CMakeLists.txt files contain something like this for properly build projects with Clang:

if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
    list(APPEND efm32_test_obj ${ARM_TOOLCHAIN_DIR}/../arm-none-eabi/lib/thumb/v7-m/nofp/crt0.o)
    list(APPEND efm32_test_obj ${ARM_TOOLCHAIN_DIR}/../lib/gcc/arm-none-eabi/8.2.1/thumb/v7-m/nofp/crti.o)
endif()

To avoid that, provide a script that will look for CRT files in a toolchain sysroot.

Trailing space interferes with regex match for GCC version

Using Arch Linux and clang-arm-gcc-toolchain.cmake, the GCC version can't be parsed due to a trailing space on the regex.
Here is the version string generated:

arm-none-eabi-gcc (Arch Repository) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Running cmake generates the following error, since no version can be extracted:

CMake Error at vendor/arm-cmake/clang-arm-gcc-toolchain.cmake:20 (string):
  string sub-command STRIP requires two arguments.

Playing around, I was able to match the version and fix the issue with the following diff:

diff --git a/clang-arm-gcc-toolchain.cmake b/clang-arm-gcc-toolchain.cmake
index 627b879..afacba6 100644
--- a/clang-arm-gcc-toolchain.cmake
+++ b/clang-arm-gcc-toolchain.cmake
@@ -16,7 +16,7 @@ execute_process(COMMAND ${ARM_GCC_C_COMPILER} -print-sysroot
 # get GNU ARM GCC version
 execute_process(COMMAND ${ARM_GCC_C_COMPILER} --version
     OUTPUT_VARIABLE ARM_GCC_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
-string(REGEX MATCH " [0-9]+\.[0-9]+\.[0-9]+ " ARM_GCC_VERSION ${ARM_GCC_VERSION})
+string(REGEX MATCH " [0-9]+\.[0-9]+\.[0-9]+" ARM_GCC_VERSION ${ARM_GCC_VERSION})
 string(STRIP ${ARM_GCC_VERSION} ARM_GCC_VERSION)
 # set compiler triple
 set(triple ${TOOLCHAIN_TRIPLE})

Did not test it on other distributions but I wonder why the trailing spaces can't be removed altogether, it means the STRIP instruction after it would not be needed as well.

Linker script support

Thanks for your cmake files, they have been useful.
It would be great to add support for linker script (aka scatter files).

Do you think you could add such feature ?

Failed to build executable target

Environment

CMake version: 3.18.0
System: ubuntu16.04
arm-none-eabi-gcc version:

(base) machine% arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.9.3 20150529 (release) [ARM/embedded-4_9-branch revision 227977 with DYNAMIC_REENT by Ambarella]
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Problem

Thinks this scenario: CMakeLists.txt contains a static library target ("A"), plus an executable target ("B), and B relies A.

The linking output gives error complains like this:

gcc-arm-none-eabi-4_9-2015q3-amba-20160323/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/libc.a(lib_a-exit.o): In function `exit':
exit.c:(.text.exit+0x1c): undefined reference to `_exit'

In arm-gcc-toolchain.cmake, L20~L24:

if (${CMAKE_VERSION} VERSION_EQUAL "3.6.0" OR ${CMAKE_VERSION} VERSION_GREATER "3.6")
    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
else()
    set(CMAKE_EXE_LINKER_FLAGS_INIT "--specs=nosys.specs")
endif()

If changed them to the following:

set(CMAKE_EXE_LINKER_FLAGS_INIT "--specs=nosys.specs")

Then the linking error disappears.

General clang toolchain advice

Hi,

I'm currently using your toolchain scripts for building my project with ninja and using arm-none-eabi-gcc.

I would like to use clang as well to have another target to test my code, but it seems that I can't get clang to work with my project. I tried to use clang under MacOSX and Ubuntu 18.04 using your clang toolchain script, but got various errors.

Could someone probably give an example how you used clang to compile your projects using the toolchain script? (Which environment you used, how clang was set up, etc.)

LLVM objcopy 6.0 does not produce valid binary image

EFM32GG Simple LED example builds with LLVM 6 objcopy provide valid ELF files that may be upload to STK3700 board and an user can see expected blinking while uploading the produced BIN file result in the board makes nothing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.