简体   繁体   中英

how to manage external dependencies of a golang project in a yocto recipe

I want to write a yocto recipe for a cross-compiled golang application with Yocto 2.4.1 but I cannot get external dependencies to work. Can anyone help me?

current RECIPE_FILE: hello-world_%.bb
LICENSE = "CLOSED"
LIC_FILES_CHKSUM = ""
DESCRIPTION = "Hello world test with golang."

inherit go

COMPATIBLE_MACHINE = "(<machine>)"
DEPENDS = "go-cross-${TARGET_ARCH}"
GO_IMPORT = "hello-world"
SRC_URI = "<git_url>/${GO_IMPORT}.git;branch=${SRCBRANCH};tag=${PV}" 
SRCBRANCH = "master"
PV = "0.01"
S = "${WORKDIR}/git"

do_compile() {
  export GOPATH="${WORKDIR}/build"
  export GOARCH="<machine_arch>"
  export GOOS="linux"
  export CGO_ENABLED="0"
  go build src/${GO_IMPORT}/hello-world.go
}

 do_install() {
   install -d "${D}/${bindir}"
   install -m 0755 "${WORKDIR}/build/hello-world" "${D}/${bindir}/hello-world"
 }

RDEPENDS_${PN}-dev += "bash"

This recipe works fine for internal dependencies only. But how do I integrate external dependencies like "github.com/golang/protobuf/ptypes"?

PROJECT_FILE: hello-world.go

package main

import (
    "fmt"
    "github.com/golang/protobuf/ptypes"
)

func main() {
    timestamp := ptypes.TimestampNow()
    fmt.Println(timestamp)
}

Does anyone knows a solution for this use case?

Or does anyone know how "go-dep" could handle this?

Best regards

I used go dep for the deps, here's an example. Most trouble was about the proxy which is also solved in the recipe:

inherit go

LICENSE = "CLOSED"
LIC_FILES_CHKSUM = ""
DESCRIPTION = "Hello world test with golang."

COMPATIBLE_MACHINE = "(<machine>)"
DEPENDS += "go-dep-native"
GO_LINKSHARED = ""

GO_IMPORT = "<git_url>/hello-world.git"
SRC_URI = "<git_url>/${GO_IMPORT}.git;branch=${SRCBRANCH};tag=${PV}" 
SRCBRANCH = "master"

do_compile() {
    export SSH_AUTH_SOCK="${SSH_AUTH_SOCK}"
    export HTTP_PROXY="${HTTP_PROXY}"
    ( cd ${WORKDIR}/build/src/${GO_IMPORT} && dep ensure -v )
}

do_compile[vardepsexclude] += "SSH_AUTH_SOCK HTTP_PROXY"

do_install() {
    install -d "${D}/${bindir}"
    install -m 0755 "${WORKDIR}/bin/<arch>/hello-world" "${D}/${bindir}/hello-world"
}

I believe there is only two types of dependencies
1. host dependencies (dependencies when the app compiling time in yocto)
in yocto recipe(.bb file) keep DEPENDS = "some lib"

  1. target dependencies (dependencies when the app running time on board)
    your yocto recipe RDEPENDS = "some lib"

hello.bb

DESCRIPTION =  
LIC =  
SRC_URI =  
DEPENDS ="sqlite3"   
inherit autools

you can add external go dependencies with SRC_URI :

SRC_URI = "\
           <git_url>/${GO_IMPORT}.git;branch=${SRCBRANCH};tag=${PV} \
           git://github.com/golang/protobuf/ptypes;protocol=https;name=ptype;destsuffix=${PN}-${PV}/src/github.com/golang/protobuf/ptypes \
"

SRCREV_ptype = "v0.1.0" <-- whatever revision you need (branch, tag, sha)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM