Skip to content

Instantly share code, notes, and snippets.

@thebashpotato
Created July 19, 2024 02:02
Show Gist options
  • Save thebashpotato/a8c24d552024772655e5a87f681c3282 to your computer and use it in GitHub Desktop.
Save thebashpotato/a8c24d552024772655e5a87f681c3282 to your computer and use it in GitHub Desktop.
A CMake module to inject Cortex A9 poky toolchain into CMake build system
# ToolchainCortexA9.cmake
# This CMake module sets up the cross-compilation environment for Yocto/Poky toolchain.
# It can easily be adpated to any target architecture.
#
# Usage: cmake -D CMAKE_TOOLCHAIN_FILE=path/to/ToolchainCortexA9.cmake -B build
# Ensure the SYSROOTS environment variable points to the root of the Yocto/Poky SDK.
# Example: export SYSROOTS =/opt/poky/4.0.4/your-project/sysroots
if(NOT DEFINED ENV{SYSROOTS})
message(FATAL_ERROR "'SYSROOTS' environment variable is not set. Please set it in your .bashrc [E.G] 'export SYSROOTS =/opt/poky/4.0.4/your-project/sysroots/'")
endif()
set(SYSROOTS $ENV{SYSROOTS})
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR armv7-a)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(TOOLCHAIN_PREFIX ${SYSROOTS}/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-)
if(NOT EXISTS ${TOOLCHAIN_PREFIX}gcc)
message(FATAL_ERROR "GCC compiler not found at ${TOOLCHAIN_PREFIX}gcc. Please check your SYSROOTS environment variable.")
endif()
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_SYSROOT ${SYSROOTS}/cortexa9t2hf-neon-poky-linux-gnueabi)
if(NOT IS_DIRECTORY ${CMAKE_SYSROOT})
message(FATAL_ERROR "Sysroot directory not found at ${CMAKE_SYSROOT}. Please check your SYSROOTS environment variable.")
endif()
set(CMAKE_PREFIX_PATH ${CMAKE_SYSROOT}/usr/lib/cmake)
# Tells the CMake find_*() functions to search for programs, libraries, and headers in the target sysroot directory.
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})
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)
# Compiler flags for ARMv7-A architecture with NEON support.
set(COMPILER_FLAGS "-march=armv7-a -marm -mfpu=neon -mfloat-abi=hard")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILER_FLAGS}" CACHE STRING "C Compiler Flags" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILER_FLAGS}" CACHE STRING "C++ Compiler Flags" FORCE)
# Ensure the pkg-config from the Yocto SDK is used
set(PKG_CONFIG_EXECUTABLE ${SYSROOTS}/x86_64-pokysdk-linux/usr/bin/pkg-config)
if(NOT EXISTS ${PKG_CONFIG_EXECUTABLE})
message(FATAL_ERROR "pkg-config executable not found at ${PKG_CONFIG_EXECUTABLE}. Please check your SYSROOTS environment variable.")
endif()
# Set up PKG_CONFIG_SYSROOT_DIR to tell pkg-config to use the correct sysroot
set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT})
# Optionally, if required, set the PKG_CONFIG_PATH to include the sysroot pkgconfig directories
set(ENV{PKG_CONFIG_PATH} ${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig)
# Include paths for the C++ standard library
include_directories(SYSTEM ${CMAKE_SYSROOT}/usr/include/c++/11.3.0/arm-poky-linux-gnueabi)
include_directories(SYSTEM ${CMAKE_SYSROOT}/usr/include/c++/11.3.0)
include_directories(SYSTEM ${CMAKE_SYSROOT}/usr/include)
# Library paths for the C++ standard library
link_directories(${CMAKE_SYSROOT}/usr/lib)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment