Giter Club home page Giter Club logo

bazel-cross-example's Introduction

Bazel Cross-compile example

Example how to cross-compile C++ program with Bazel and various toolchains.

These targets are supported:

  1. Local x86_64 using system GCC
  2. External Linaro/aarch64 toolchain in /opt (see below)
  3. embedded Linux (elinux) aarch64 target from TensorFlow (TF)
  4. embedded Linux (elinux) amrhf (32 bit ARM with Hardware Float) target from TensorFlow (TF)

Setup

Tested OS: openSUSE LEAP 15.3/x86_64

You need to install at least packages:

sudo zypper in curl git-core gcc-c++ gcc

To run aarch64 and armhf targets in QEMU emulator, install this package:

sudo zypper in qemu-linux-user

Now install Bazelisk:

mkdir -p ~/bin
curl -fL -o ~/bin/bazel https://github.com/bazelbuild/bazelisk/releases/download/v1.11.0/bazelisk-linux-amd64
chmod +x ~/bin/bazel

Ensure that you are using your Bazelisk (named as bazel):

which bazel

/home/$USER/bin/bazel

Now checkout this source:

mkdir -p ~/projects
cd ~/projects
git clone https://github.com/hpaluch-pil/bazel-cross-example.git
cd bazel-cross-example

Now verify Bazel version - must be 4.2.2 to match .bazelversion in this project:

$ bazel --version

bazel 4.2.2

1. Building: x86_64

It is useful to verify that Bazel is working as expected:

Just run:

# to see available targets
bazel query ...
# build target...
bazel build '//:hello'

You can run this program invoking:

bazel run '//:hello'

Example output:

INFO: Analyzed target //:hello (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:hello up-to-date:
  bazel-bin/hello
INFO: Elapsed time: 1.056s, Critical Path: 0.80s
INFO: 3 processes: 1 internal, 2 linux-sandbox.
INFO: Build completed successfully, 3 total actions
INFO: Build completed successfully, 3 total actions
Hello, x86_64 world!

2. Building: aarch64 external Linaro

At first install Linaro aarch64 cross-compiler:

And run:

bazel build --config=aarch64 '//:hello'

It should build aarch64 binary - you can verify it with command:

$ readelf -h bazel-bin/hello | fgrep Machine:

 Machine:                           AArch64

To run aarch64 binary under QEMU emulation you can try this:

  • and run binary user QEMU USER:
    qemu-aarch64 -L /opt/aarch64-linux-gnu/aarch64-linux-gnu/libc \
       bazel-bin/hello
  • shoud produce output:
    Hello, aarch64 world!
    

3. Building elinux_aarch64

Uses embedded Linux (elinux) aarch64 target from TF. To build this target run script:

./build_elinux_aarch64.sh

To verify target architecture try:

$ readelf -h bazel-bin/hello | fgrep Machine:

  Machine:                           AArch64

$ readelf -d bazel-bin/hello | fgrep NEEDED

 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-aarch64.so.1]

To run elinux_aarch64 binary under QEMU AArch64 emulator try:

./run_qemu_elinux_aarch64.sh

4. Building elinux_armhf

Uses embedded Linux (elinux) 32-bit ARM HF (hardware float) target from TF. To build this target run script:

./build_elinux_armhf.sh

To verify target architecture try:

$ readelf -h bazel-bin/hello | fgrep Machine:

  Machine:                           ARM

$ readelf -d bazel-bin/hello | fgrep NEEDED

 0x00000001 (NEEDED)                     Shared library: [libstdc++.so.6]
 0x00000001 (NEEDED)                     Shared library: [libgcc_s.so.1]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 0x00000001 (NEEDED)                     Shared library: [ld-linux-armhf.so.3]

To run elinux_armhf binary under QEMU ARM try:

./run_qemu_elinux_armhf.sh

Resources

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.