Bitbake / Yocto recipes for cross compiling golang program

For compiling simple golang helloworld program as part of yocto build framework, we need to download meta-golang from https://github.com/madisongh/meta-golang and use this meta layer along with poky to build our written bitbake recipes for cross compilation of golang program.

 $ git clone https://github.com/madisongh/meta-golang.git 
 $ cd meta-golang 

Now, we need to create files with following directory structure as,

 $ tree recipes-devtools/examples/ 
recipes-devtools/examples/
├── files
│   └── helloworld.go
├── go-examples.inc
└── go-helloworld_0.1.bb

1 directory, 3 files

Now, lets create those files as below,

 $ vim recipes-devtools/examples/files/helloworld.go 
package main

import "fmt"

func main() {
        fmt.Println("Hello, world !")
}
 $ vim recipes-devtools/examples/go-examples.inc 
DESCRIPTION = "This is a simple example recipe that cross-compiles a Go program."
SECTION = "examples"
HOMEPAGE = "https://golang.org/"

DEPENDS = "golang-cross-arm"

export GOARCH="arm"

FILESEXTRAPATHS_prepend := "${THISDIR}/files:"

LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

S = "${WORKDIR}"
 $ vim recipes-devtools/examples/go-helloworld_0.1.bb 
require go-examples.inc

SRC_URI += " \
  file://helloworld.go \
"

do_compile() {
  go build helloworld.go
}

do_install() {
  install -d "${D}/${bindir}"
  install -m 0755 "${S}/helloworld" "${D}/${bindir}"
}

Now, once you integrate this meta-golang with poky, you will be able to compile this simple go language program using bitbake as,

 $ bitbake go-helloworld 
Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home HTML JAVA Programs Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply