blob: b02a83a5610b5e0822a639ea0b5d6145e467bff3 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
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"
Andrew Geissler028142b2023-05-05 11:29:21 -050040DEPENDS_GOLANG:class-nativesdk = "virtual/${TARGET_PREFIX}go virtual/${TARGET_PREFIX}go-runtime"
Patrick Williams92b42cb2022-09-03 06:53:57 -050041
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 GOTMPDIR ?= "${WORKDIR}/build-tmp"
82GOTMPDIR[vardepvalue] = ""
83
84python go_do_unpack() {
85 src_uri = (d.getVar('SRC_URI') or "").split()
86 if len(src_uri) == 0:
87 return
88
89 fetcher = bb.fetch2.Fetch(src_uri, d)
90 for url in fetcher.urls:
91 if fetcher.ud[url].type == 'git':
92 if fetcher.ud[url].parm.get('destsuffix') is None:
93 s_dirname = os.path.basename(d.getVar('S'))
94 fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src', d.getVar('GO_IMPORT')) + '/'
95 fetcher.unpack(d.getVar('WORKDIR'))
96}
97
98go_list_packages() {
99 ${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
100 egrep -v '${GO_INSTALL_FILTEROUT}'
101}
102
103go_list_package_tests() {
104 ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
105 grep -v '\[\]$' | \
106 egrep -v '${GO_INSTALL_FILTEROUT}' | \
107 awk '{ print $1 }'
108}
109
110go_do_configure() {
111 ln -snf ${S}/src ${B}/
112}
113do_configure[dirs] =+ "${GOTMPDIR}"
114
115go_do_compile() {
116 export TMPDIR="${GOTMPDIR}"
117 if [ -n "${GO_INSTALL}" ]; then
118 if [ -n "${GO_LINKSHARED}" ]; then
119 ${GO} install ${GOBUILDFLAGS} `go_list_packages`
120 rm -rf ${B}/bin
121 fi
122 ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
123 fi
124}
125do_compile[dirs] =+ "${GOTMPDIR}"
126do_compile[cleandirs] = "${B}/bin ${B}/pkg"
127
128go_do_install() {
129 install -d ${D}${libdir}/go/src/${GO_IMPORT}
130 tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
131 tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
132 tar -C ${B} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' pkg | \
133 tar -C ${D}${libdir}/go --no-same-owner -xf -
134
135 if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
136 install -d ${D}${bindir}
137 install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
138 fi
139}
140
141go_stage_testdata() {
142 oldwd="$PWD"
143 cd ${S}/src
144 find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
145 if echo "$d" | grep -q '/vendor/'; then
146 continue
147 fi
148 parent=`dirname $d`
149 install -d ${D}${PTEST_PATH}/$parent
150 cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
151 done
152 cd "$oldwd"
153}
154
155EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
156
157FILES:${PN}-dev = "${libdir}/go/src"
158FILES:${PN}-staticdev = "${libdir}/go/pkg"
159
160INSANE_SKIP:${PN} += "ldflags"
161
162# Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips
163# doesn't support -buildmode=pie, so skip the QA checking for mips/rv32 and its
164# variants.
165python() {
166 if 'mips' in d.getVar('TARGET_ARCH') or 'riscv32' in d.getVar('TARGET_ARCH'):
167 d.appendVar('INSANE_SKIP:%s' % d.getVar('PN'), " textrel")
168 else:
169 d.appendVar('GOBUILDFLAGS', ' -buildmode=pie')
170}