minimal dependency cmake toolchain to easily compile your teensy sketches and libraries, and optionally link with c++ std libraries.
- custom teensy toolchain using
cmake
andarm-none-eabi-gcc
- based on ronj/teensy-cmake-template
- targetting Teensy 4.x, tested on Teensy 4.1 (should be easy to extend for 3.x)
- compiles library code to .a archive files to avoid unnecessary recompiling
install build dependencies (click to expand)
- arm-none-eabi-gcc
- cmake
- teensy-cmake-macros
> git clone https://github.com/newdigate/teensy-cmake-macros.git > cd teensy-cmake-macros > mkdir cmake-build-debug > cd cmake-build-debug > cmake .. > sudo make install
clone runtime dependencies (click to expand)
- clone necessary dependencies to a chosen location
${DEPSPATH}
> cd /home/nic/midi-smf-reader/deps
> git clone https://github.com/PaulStoffregen/cores.git
> git clone https://github.com/PaulStoffregen/Audio.git
> git clone -b Juse_Use_SdFat https://github.com/PaulStoffregen/SD.git
> git clone https://github.com/PaulStoffregen/Wire.git
> git clone https://github.com/PaulStoffregen/SPI.git
> git clone https://github.com/PaulStoffregen/SerialFlash.git
> git clone https://github.com/PaulStoffregen/arm_math.git
> git clone https://github.com/greiman/SdFat.git
custom cmake toolchain (click to expand)
- add a custom cmake toolchain file to your project
cmake/toolchains/teensy41.toolchain.cmake
set(TEENSY_VERSION 41 CACHE STRING "Set to the Teensy version corresponding to your board (40 or 41 allowed)" FORCE)
set(CPU_CORE_SPEED 600000000 CACHE STRING "Set to 600000000, 24000000, 48000000, 72000000 or 96000000 to set CPU core speed" FORCE) # Derived variables
set(COMPILERPATH "/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/")
set(DEPSPATH "/home/nic/midi-smf-reader/deps")
set(COREPATH "${DEPSPATH}/cores/teensy4/")
find_package(teensy_cmake_macros)
- update
DEPSPATH
,COMPILERPATH
andCOREPATH
to your dependencies folder, arm-none-eabi-gcc bin folder and path to teensy4 cores
add CMakeLists.txt (click to expand)
- create a
CMakeLists.txt
file in the root directory of your project
cmake_minimum_required(VERSION 3.5)
project(midi_smf_reader C CXX)
import_arduino_library(cores ${COREPATH})
import_arduino_library(SPI ${DEPSPATH}/SPI)
import_arduino_library(SdFat ${DEPSPATH}/SdFat/src common DigitalIO ExFatLib FatLib FsLib iostream SdCard SpiDriver)
import_arduino_library(SD ${DEPSPATH}/SD/src)
# add custom library
teensy_add_library(my_teensy_library my_teensy_library.cpp)
teensy_add_executable(my_firmware sketch.ino)
teensy_target_link_libraries(my_firmware my_teensy_library SD SdFat SPI cores) # order is IMPORTANT because we are garbage collecting symbols --gc-collect
# if you need to link to std library (using <Vector>, etc)
set(CMAKE_EXE_LINKER_FLAGS "--specs=nano.specs" CACHE INTERNAL "")
target_link_libraries(my_firmware.elf stdc++)
build (click to expand)
- run from a terminal in your repository root directory
> mkdir cmake-build-debug
> cd cmake-build-debug
> cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE:FILEPATH="../cmake/toolchains/teensy41.toolchain.cmake"
> make
-
teensy_add_executable
(TARGET
files...
)teensy_add_executable(myapplication midiread.cpp)
-
teensy_add_library
(TARGET
files...
)teensy_add_library(mylibrary library1.cpp)
-
import_arduino_library
(LibraryName
LibraryPath
additionalRelativeSourceFolders
)import_arduino_library(cores ${COREPATH}) import_arduino_library(SPI ${DEPSPATH}/SPI) # SPI@Juse_Use_SdFat import_arduino_library(SdFat ${DEPSPATH}/SdFat/src common DigitalIO ExFatLib FatLib FsLib iostream SdCard SpiDriver) import_arduino_library(SD ${DEPSPATH}/SD/src)
-
teensy_target_link_libraries
(TARGET
libraries...
)
teensy_target_link_libraries(my_firmware mylibrary SD SdFat SPI cores)
- link to std library
set(CMAKE_EXE_LINKER_FLAGS "--specs=nano.specs" CACHE INTERNAL "")
target_link_libraries(my_firmware.elf stdc++)
teensy_include_directories
(paths...
)
teensy_include_directories(../../src)