1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7inherit goarch
8inherit linuxloader
9
10GO_PARALLEL_BUILD ?= "${@oe.utils.parallel_make_argument(d, '-p %d')}"
11
12export GODEBUG = "gocachehash=1"
13
14GOROOT:class-native = "${STAGING_LIBDIR_NATIVE}/go"
15GOROOT:class-nativesdk = "${STAGING_DIR_TARGET}${libdir}/go"
16GOROOT = "${STAGING_LIBDIR}/go"
17export GOROOT
18export GOROOT_FINAL = "${libdir}/go"
19export GOCACHE = "${B}/.cache"
20
21export GOARCH = "${TARGET_GOARCH}"
22export GOOS = "${TARGET_GOOS}"
23export GOHOSTARCH="${BUILD_GOARCH}"
24export GOHOSTOS="${BUILD_GOOS}"
25
26GOARM[export] = "0"
27GOARM:arm:class-target = "${TARGET_GOARM}"
28GOARM:arm:class-target[export] = "1"
29
30GO386[export] = "0"
31GO386:x86:class-target = "${TARGET_GO386}"
32GO386:x86:class-target[export] = "1"
33
34GOMIPS[export] = "0"
35GOMIPS:mips:class-target = "${TARGET_GOMIPS}"
36GOMIPS:mips:class-target[export] = "1"
37
38DEPENDS_GOLANG:class-target = "virtual/${TUNE_PKGARCH}-go virtual/${TARGET_PREFIX}go-runtime"
39DEPENDS_GOLANG:class-native = "go-native"
40DEPENDS_GOLANG:class-nativesdk = "virtual/${TARGET_PREFIX}go virtual/${TARGET_PREFIX}go-runtime"
41
42DEPENDS:append = " ${DEPENDS_GOLANG}"
43
44GO_LINKSHARED ?= "${@'-linkshared' if d.getVar('GO_DYNLINK') else ''}"
45GO_RPATH_LINK = "${@'-Wl,-rpath-link=${STAGING_DIR_TARGET}${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
46GO_RPATH = "${@'-r ${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
47GO_RPATH:class-native = "${@'-r ${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
48GO_RPATH_LINK:class-native = "${@'-Wl,-rpath-link=${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
49GO_EXTLDFLAGS ?= "${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} ${GO_RPATH_LINK} ${LDFLAGS}"
50GO_LINKMODE ?= ""
51GO_LINKMODE:class-nativesdk = "--linkmode=external"
52GO_LINKMODE:class-native = "--linkmode=external"
53GO_EXTRA_LDFLAGS ?= ""
54GO_LINUXLOADER ?= "-I ${@get_linuxloader(d)}"
55# Use system loader. If uninative is used, the uninative loader will be patched automatically
56GO_LINUXLOADER:class-native = ""
57GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} ${GO_LINUXLOADER} ${GO_EXTRA_LDFLAGS} -extldflags '${GO_EXTLDFLAGS}'"'
58export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS} -trimpath"
59export GOPATH_OMIT_IN_ACTIONID ?= "1"
60export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c"
61export GOPTESTFLAGS ?= ""
62GOBUILDFLAGS:prepend:task-compile = "${GO_PARALLEL_BUILD} "
63
64export GO = "${HOST_PREFIX}go"
65GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}"
66GOTOOLDIR:class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}"
67export GOTOOLDIR
68
69export CGO_ENABLED ?= "1"
70export CGO_CFLAGS ?= "${CFLAGS}"
71export CGO_CPPFLAGS ?= "${CPPFLAGS}"
72export CGO_CXXFLAGS ?= "${CXXFLAGS}"
73export CGO_LDFLAGS ?= "${LDFLAGS}"
74
75GO_INSTALL ?= "${GO_IMPORT}/..."
76GO_INSTALL_FILTEROUT ?= "${GO_IMPORT}/vendor/"
77
78B = "${WORKDIR}/build"
79export GOPATH = "${B}"
80export GOENV = "off"
81export GOPROXY ??= "https://proxy.golang.org,direct"
82export GOTMPDIR ?= "${WORKDIR}/build-tmp"
83GOTMPDIR[vardepvalue] = ""
84
85python go_do_unpack() {
86    src_uri = (d.getVar('SRC_URI') or "").split()
87    if len(src_uri) == 0:
88        return
89
90    fetcher = bb.fetch2.Fetch(src_uri, d)
91    for url in fetcher.urls:
92        if fetcher.ud[url].type == 'git':
93            if fetcher.ud[url].parm.get('destsuffix') is None:
94                s_dirname = os.path.basename(d.getVar('S'))
95                fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src', d.getVar('GO_IMPORT')) + '/'
96    fetcher.unpack(d.getVar('WORKDIR'))
97}
98
99go_list_packages() {
100	${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
101		egrep -v '${GO_INSTALL_FILTEROUT}'
102}
103
104go_list_package_tests() {
105	${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
106		grep -v '\[\]$' | \
107		egrep -v '${GO_INSTALL_FILTEROUT}' | \
108		awk '{ print $1 }'
109}
110
111go_do_configure() {
112	ln -snf ${S}/src ${B}/
113}
114do_configure[dirs] =+ "${GOTMPDIR}"
115
116go_do_compile() {
117	export TMPDIR="${GOTMPDIR}"
118	if [ -n "${GO_INSTALL}" ]; then
119		if [ -n "${GO_LINKSHARED}" ]; then
120			${GO} install ${GOBUILDFLAGS} `go_list_packages`
121			rm -rf ${B}/bin
122		fi
123		${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
124	fi
125}
126do_compile[dirs] =+ "${GOTMPDIR}"
127do_compile[cleandirs] = "${B}/bin ${B}/pkg"
128
129go_do_install() {
130	install -d ${D}${libdir}/go/src/${GO_IMPORT}
131	tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
132		tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
133	tar -C ${B} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' pkg | \
134		tar -C ${D}${libdir}/go --no-same-owner -xf -
135
136	if ls ${B}/${GO_BUILD_BINDIR}/* >/dev/null 2>/dev/null ; then
137		install -d ${D}${bindir}
138		install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
139	fi
140}
141
142go_stage_testdata() {
143	oldwd="$PWD"
144	cd ${S}/src
145	find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
146		if echo "$d" | grep -q '/vendor/'; then
147			continue
148		fi
149		parent=`dirname $d`
150		install -d ${D}${PTEST_PATH}/$parent
151		cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
152	done
153	cd "$oldwd"
154}
155
156EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
157
158FILES:${PN}-dev = "${libdir}/go/src"
159FILES:${PN}-staticdev = "${libdir}/go/pkg"
160
161INSANE_SKIP:${PN} += "ldflags"
162
163# Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips
164# doesn't support -buildmode=pie, so skip the QA checking for mips/rv32 and its
165# variants.
166python() {
167    if 'mips' in d.getVar('TARGET_ARCH') or 'riscv32' in d.getVar('TARGET_ARCH'):
168        d.appendVar('INSANE_SKIP:%s' % d.getVar('PN'), " textrel")
169    else:
170        d.appendVar('GOBUILDFLAGS', ' -buildmode=pie')
171}
172