Building C/C++ Project with Travis CI for Multiple OSs and CPUs

Three years ago, I created a CMake project, aiming to build a C/C++ project with Dynamsoft Barcode Reader on Windows, Linux, and macOS. Today, as the world’s top barcode SDK, Dynamsoft Barcode Reader has extended to more operating systems and CPU architectures:

  • ARM32 Linux: Raspberry Pi
  • ARM64 Linux: Jetson Nano

Now, I only have a Windows PC in hand. How can I build the project for all target environments without purchasing new hardware devices or spending time setting up virtual environments?

One of the economical ways is to use Travis CI. Travis CI is a hosted continuous integration service that supplies virtual machines to build, test, and deploy projects.

Building C/C++ project with Travis CI

Download Barcode SDK for Linux(AMD64, ARM64, ARM32), Windows(x86, x64), and macOS

The SDKs are scattered in three places:

CMake Configuration for ARM32 and ARM64 Barcode SDK

If the operating system is the same, we can link a specific library file by evaluating CMAKE_SYSTEM_PROCESSOR:

if (CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
    MESSAGE( STATUS "Link directory: ${PROJECT_SOURCE_DIR}/platforms/linux/" )
    link_directories("${PROJECT_SOURCE_DIR}/platforms/linux/")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l OR ARM32_BUILD)
    MESSAGE( STATUS "Link directory: ${PROJECT_SOURCE_DIR}/platforms/arm32/" )
    link_directories("${PROJECT_SOURCE_DIR}/platforms/arm32/") 
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) 
    MESSAGE( STATUS "Link directory: ${PROJECT_SOURCE_DIR}/platforms/aarch64/" )
    link_directories("${PROJECT_SOURCE_DIR}/platforms/aarch64/") 
endif()

Why is ARM32_BUILD required? According to the online documentation, there is no ARM32 virtual machine available.

travis supported CPU architectures

Therefore, to build the project for ARM32 devices like Raspberry Pi, a workaround is to install a cross-compiler in ARM64 Linux environment and add a CMake option:

option(ARM32_BUILD "Build for ARM32" OFF)

Before taking the next step, make a quick test to assure the CMake project can be built successfully.

Configuring Travis.yml for Different OSs and CPUs

Create a travis.yml file in the project root directory.

The following jobs are used to create different virtual machines:

jobs:
  include:
    - os: linux
      arch: arm64
      env: OS=arm64_linux
    - os: linux
      arch: amd64
      env: OS=amd64_linux
    - os: windows
      arch: amd64
      env: OS=windows
    - os: osx
      arch: amd64
      env: OS=osx
    - os: linux
      arch: arm64
      env: OS=arm32_linux
      before_install:
        - sudo dpkg --add-architecture armhf
        - sudo apt-get update
        - sudo apt-get install crossbuild-essential-armhf libc6:armhf libstdc++6:armhf

The last one is a little bit special, for I need to install some tools for compiling the ARM32 project on ARM64 Linux.

Add scripts to make the build work on all target platforms:

script:
    - mkdir build
    - cd build
    - if [[ ${TRAVIS_OS_NAME} == "windows" ]]; then
        cmake -DCMAKE_GENERATOR_PLATFORM=x64 ..;
      else
        if [ $OS == "arm32_linux" ]; then
          export CC=arm-linux-gnueabihf-gcc;
          export CXX=arm-linux-gnueabihf-g++;
          export AR=arm-linux-gnueabihf-ar;
          cmake -DARM32_BUILD=TRUE ..;
        else
          cmake ..;
        fi
      fi
    - cmake --build . --config release

Finally, do not forget to add the status badge in README.md:

[![status](https://api.travis-ci.com/Dynamsoft/cmake.svg?branch=master)](https://travis-ci.com/github/Dynamsoft/cmake)

status badge

Source Code

https://github.com/Dynamsoft/cmake

Search Blog Posts