Skip to content

Commit

Permalink
CMake: Added cross-compilation support
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviermartin committed May 2, 2016
1 parent 69da222 commit 36ff478
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
@@ -1,5 +1,4 @@
#
#
# GattLib - GATT Library
#
# Copyright (C) 2016 Olivier Martin <olivier@labapart.org>
Expand All @@ -22,6 +21,10 @@

cmake_minimum_required(VERSION 2.6)

# Add Cross-Compilation support when the environment variables
# CROSS_COMPILE and SYSROOT are defined
include(CrossCompilation.cmake)

project(gattlib)

find_package(PkgConfig REQUIRED)
Expand Down Expand Up @@ -78,7 +81,7 @@ set(CPACK_PACKAGE_INSTALL_DIRECTORY /usr CACHE STRING "Install directory (defaul
set(CPACK_PACKAGE_VERSION 0.1)
set(CPACK_PACKAGE_CONTACT "Olivier Martin <olivier@labapart.com>")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Library to access GATT information from Bluetooth Low Energy (BLE) devices")
set(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION}")
set(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}_${CPACK_PACKAGE_VERSION}_${CPACK_PACKAGE_ARCHITECTURE}")

#
# Debian package
Expand Down
105 changes: 105 additions & 0 deletions CrossCompilation.cmake
@@ -0,0 +1,105 @@
#
# Copyright (C) 2016 Olivier Martin <olivier@labapart.org>
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Version: 1.0
# Repository: https://gist.github.com/oliviermartin/
# Description:
#
# Add Cross-Compilation support when the environment variables
# CROSS_COMPILE and SYSROOT are defined

#
# Check required environment variables
#
if ("$ENV{CROSS_COMPILE}" STREQUAL "")
# Cross compilation is not needed

# We still set CPACK_PACKAGE_ARCHITECTURE
if (NOT DEFINED CPACK_PACKAGE_ARCHITECTURE)
execute_process(COMMAND uname -m OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()

return()
endif()
if ("$ENV{SYSROOT}" STREQUAL "")
message(WARNING "Environment variable SYSROOT is not defined.")
endif()

# Trigger cross-compilation in CMake
set(CMAKE_SYSTEM_NAME Linux)

# Specify the cross compiler
set(CMAKE_C_COMPILER $ENV{CROSS_COMPILE}gcc)
set(CMAKE_CXX_COMPILER $ENV{CROSS_COMPILE}g++)

# Specify the target environment
set(CMAKE_FIND_ROOT_PATH $ENV{SYSROOT})

# Retrieve the machine supported by the toolchain
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpmachine OUTPUT_VARIABLE TOOLCHAIN_MACHINE OUTPUT_STRIP_TRAILING_WHITESPACE)

# Add '--sysroot' to the compiler flags
set(ENV{CFLAGS} "--sysroot=$ENV{SYSROOT} $ENV{CFLAGS}")

# Add '-rpath' to the linker flags
set(ENV{LDFLAGS} "-Wl,-rpath,$ENV{SYSROOT}/lib/${TOOLCHAIN_MACHINE} $ENV{LDFLAGS}")

#
# Configure pkg-config for cross-compilation
#
if(IS_DIRECTORY "$ENV{SYSROOT}/usr/lib/pkgconfig")
set(ENV{PKG_CONFIG_PATH} $ENV{SYSROOT}/usr/lib/pkgconfig)
endif()
if(IS_DIRECTORY "$ENV{SYSROOT}/usr/lib/${TOOLCHAIN_MACHINE}/pkgconfig")
set(ENV{PKG_CONFIG_PATH} $ENV{SYSROOT}/usr/lib/${TOOLCHAIN_MACHINE}/pkgconfig)
endif()
if (NOT "$ENV{PKG_CONFIG_PATH}" STREQUAL "")
set(ENV{PKG_CONFIG_SYSROOT_DIR} $ENV{SYSROOT})
# Don't strip -I/usr/include out of cflags
set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} 1)
# Don't strip -L/usr/lib out of libs
set(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} 1)
endif()

# Workaround as some pkgconfig file forgot to add the architecture specific include folder
# such as openssl.pc
include_directories($ENV{SYSROOT}/usr/include $ENV{SYSROOT}/usr/include/${TOOLCHAIN_MACHINE})

# Workaround as some library are installed in $ENV{SYSROOT}/lib/${TOOLCHAIN_MACHINE}
# such as libpcre.so (required by glib-2.0)
link_directories($ENV{SYSROOT}/lib/${TOOLCHAIN_MACHINE})

# search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

#
# Set architecture for CPack
#
if ("${TOOLCHAIN_MACHINE}" STREQUAL "arm-linux-gnueabihf")
set(CPACK_PACKAGE_ARCHITECTURE armhf)
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE armhf)
set(CPACK_RPM_PACKAGE_ARCHITECTURE armv7hl)
endif()
if ("${TOOLCHAIN_MACHINE}" STREQUAL "aarch64-linux-gnu")
set(CPACK_PACKAGE_ARCHITECTURE arm64)
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE arm64)
set(CPACK_RPM_PACKAGE_ARCHITECTURE aarch64)
endif()
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -23,6 +23,24 @@ cmake ..
make
```

### Cross-Compilation

To cross-compile GattLib, you must provide the following environment variables:

- `CROSS_COMPILE`: prefix of your cross-compile toolchain
- `SYSROOT`: an existing system root that contains the libraries and include files required by your application

Example:

```
cd <gattlib-src-root>
mkdir build && cd build
export CROSS_COMPILE=~/Toolchains/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
export SYSROOT=~/Distributions/debian-wheezy
cmake ..
make
```

Package GattLib
===============

Expand Down

0 comments on commit 36ff478

Please sign in to comment.