blob: 1a9a0bc1d4269c2b252b9fe2d0dbe2856017e8ac [file] [log] [blame]
Brad Bishop96ff1982019-08-19 13:50:42 -04001inherit goarch
Patrick Williams03907ee2022-05-01 06:28:52 -05002inherit linuxloader
Brad Bishop6e60e8b2018-02-01 10:27:11 -05003
Brad Bishop316dfdd2018-06-25 12:45:53 -04004GO_PARALLEL_BUILD ?= "${@oe.utils.parallel_make_argument(d, '-p %d')}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -05005
Andrew Geissler595f6302022-01-24 19:11:47 +00006export GODEBUG = "gocachehash=1"
7
Patrick Williams213cb262021-08-07 19:21:33 -05008GOROOT:class-native = "${STAGING_LIBDIR_NATIVE}/go"
9GOROOT:class-nativesdk = "${STAGING_DIR_TARGET}${libdir}/go"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050010GOROOT = "${STAGING_LIBDIR}/go"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011export GOROOT
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012export GOROOT_FINAL = "${libdir}/go"
Brad Bishopc8f47122019-06-24 09:36:18 -040013export GOCACHE = "${B}/.cache"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014
Brad Bishopd89cb5f2019-04-10 09:02:41 -040015export GOARCH = "${TARGET_GOARCH}"
16export GOOS = "${TARGET_GOOS}"
17export GOHOSTARCH="${BUILD_GOARCH}"
18export GOHOSTOS="${BUILD_GOOS}"
19
20GOARM[export] = "0"
Patrick Williams213cb262021-08-07 19:21:33 -050021GOARM:arm:class-target = "${TARGET_GOARM}"
22GOARM:arm:class-target[export] = "1"
Brad Bishopd89cb5f2019-04-10 09:02:41 -040023
24GO386[export] = "0"
Patrick Williams213cb262021-08-07 19:21:33 -050025GO386:x86:class-target = "${TARGET_GO386}"
26GO386:x86:class-target[export] = "1"
Brad Bishopd89cb5f2019-04-10 09:02:41 -040027
28GOMIPS[export] = "0"
Patrick Williams213cb262021-08-07 19:21:33 -050029GOMIPS:mips:class-target = "${TARGET_GOMIPS}"
30GOMIPS:mips:class-target[export] = "1"
Brad Bishopd89cb5f2019-04-10 09:02:41 -040031
Patrick Williams213cb262021-08-07 19:21:33 -050032DEPENDS_GOLANG:class-target = "virtual/${TUNE_PKGARCH}-go virtual/${TARGET_PREFIX}go-runtime"
33DEPENDS_GOLANG:class-native = "go-native"
34DEPENDS_GOLANG:class-nativesdk = "virtual/${TARGET_PREFIX}go-crosssdk virtual/${TARGET_PREFIX}go-runtime"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050035
Patrick Williams213cb262021-08-07 19:21:33 -050036DEPENDS:append = " ${DEPENDS_GOLANG}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050037
38GO_LINKSHARED ?= "${@'-linkshared' if d.getVar('GO_DYNLINK') else ''}"
39GO_RPATH_LINK = "${@'-Wl,-rpath-link=${STAGING_DIR_TARGET}${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
40GO_RPATH = "${@'-r ${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
Patrick Williams213cb262021-08-07 19:21:33 -050041GO_RPATH:class-native = "${@'-r ${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
42GO_RPATH_LINK:class-native = "${@'-Wl,-rpath-link=${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043GO_EXTLDFLAGS ?= "${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} ${GO_RPATH_LINK} ${LDFLAGS}"
44GO_LINKMODE ?= ""
Patrick Williams213cb262021-08-07 19:21:33 -050045GO_LINKMODE:class-nativesdk = "--linkmode=external"
46GO_LINKMODE:class-native = "--linkmode=external"
Andrew Geisslereff27472021-10-29 15:35:00 -050047GO_EXTRA_LDFLAGS ?= ""
Patrick Williams03907ee2022-05-01 06:28:52 -050048GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} -I ${@get_linuxloader(d)} ${GO_EXTRA_LDFLAGS} -extldflags '${GO_EXTLDFLAGS}'"'
Andrew Geissler1e34c2d2020-05-29 16:02:59 -050049export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS} -trimpath"
Brad Bishop316dfdd2018-06-25 12:45:53 -040050export GOPATH_OMIT_IN_ACTIONID ?= "1"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050051export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c"
Brad Bishop316dfdd2018-06-25 12:45:53 -040052export GOPTESTFLAGS ?= ""
Patrick Williams213cb262021-08-07 19:21:33 -050053GOBUILDFLAGS:prepend:task-compile = "${GO_PARALLEL_BUILD} "
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054
55export GO = "${HOST_PREFIX}go"
56GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}"
Patrick Williams213cb262021-08-07 19:21:33 -050057GOTOOLDIR:class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058export GOTOOLDIR
59
Brad Bishopd7bf8c12018-02-25 22:55:05 -050060export CGO_ENABLED ?= "1"
61export CGO_CFLAGS ?= "${CFLAGS}"
62export CGO_CPPFLAGS ?= "${CPPFLAGS}"
63export CGO_CXXFLAGS ?= "${CXXFLAGS}"
64export CGO_LDFLAGS ?= "${LDFLAGS}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050065
66GO_INSTALL ?= "${GO_IMPORT}/..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050067GO_INSTALL_FILTEROUT ?= "${GO_IMPORT}/vendor/"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069B = "${WORKDIR}/build"
70export GOPATH = "${B}"
Patrick Williams03907ee2022-05-01 06:28:52 -050071export GOENV = "off"
Andrew Geissler595f6302022-01-24 19:11:47 +000072export GOTMPDIR ?= "${WORKDIR}/build-tmp"
Brad Bishop316dfdd2018-06-25 12:45:53 -040073GOTMPDIR[vardepvalue] = ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074
75python go_do_unpack() {
76 src_uri = (d.getVar('SRC_URI') or "").split()
77 if len(src_uri) == 0:
78 return
79
Brad Bishop08902b02019-08-20 09:16:51 -040080 fetcher = bb.fetch2.Fetch(src_uri, d)
81 for url in fetcher.urls:
82 if fetcher.ud[url].type == 'git':
83 if fetcher.ud[url].parm.get('destsuffix') is None:
84 s_dirname = os.path.basename(d.getVar('S'))
85 fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src', d.getVar('GO_IMPORT')) + '/'
86 fetcher.unpack(d.getVar('WORKDIR'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050087}
88
89go_list_packages() {
90 ${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
91 egrep -v '${GO_INSTALL_FILTEROUT}'
92}
93
94go_list_package_tests() {
Brad Bishop316dfdd2018-06-25 12:45:53 -040095 ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096 grep -v '\[\]$' | \
97 egrep -v '${GO_INSTALL_FILTEROUT}' | \
98 awk '{ print $1 }'
99}
100
101go_do_configure() {
102 ln -snf ${S}/src ${B}/
103}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400104do_configure[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105
106go_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400107 export TMPDIR="${GOTMPDIR}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500108 if [ -n "${GO_INSTALL}" ]; then
Brad Bishop316dfdd2018-06-25 12:45:53 -0400109 if [ -n "${GO_LINKSHARED}" ]; then
110 ${GO} install ${GOBUILDFLAGS} `go_list_packages`
111 rm -rf ${B}/bin
112 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113 ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
114 fi
115}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400116do_compile[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500117do_compile[cleandirs] = "${B}/bin ${B}/pkg"
118
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119go_do_install() {
120 install -d ${D}${libdir}/go/src/${GO_IMPORT}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400121 tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500122 tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600123 tar -C ${B} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' pkg | \
124 tar -C ${D}${libdir}/go --no-same-owner -xf -
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500125
126 if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
127 install -d ${D}${bindir}
128 install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500129 fi
130}
131
Brad Bishop316dfdd2018-06-25 12:45:53 -0400132go_stage_testdata() {
133 oldwd="$PWD"
134 cd ${S}/src
135 find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
136 if echo "$d" | grep -q '/vendor/'; then
137 continue
138 fi
139 parent=`dirname $d`
140 install -d ${D}${PTEST_PATH}/$parent
141 cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
142 done
143 cd "$oldwd"
144}
145
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500146EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147
Patrick Williams213cb262021-08-07 19:21:33 -0500148FILES:${PN}-dev = "${libdir}/go/src"
149FILES:${PN}-staticdev = "${libdir}/go/pkg"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500150
Patrick Williams213cb262021-08-07 19:21:33 -0500151INSANE_SKIP:${PN} += "ldflags"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800152
153# Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600154# doesn't support -buildmode=pie, so skip the QA checking for mips/rv32 and its
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800155# variants.
156python() {
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600157 if 'mips' in d.getVar('TARGET_ARCH') or 'riscv32' in d.getVar('TARGET_ARCH'):
Patrick Williams213cb262021-08-07 19:21:33 -0500158 d.appendVar('INSANE_SKIP:%s' % d.getVar('PN'), " textrel")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800159 else:
160 d.appendVar('GOBUILDFLAGS', ' -buildmode=pie')
161}