blob: 9c4c92bffd45554619a4ed932a65df1eda2df0f0 [file] [log] [blame]
Brad Bishop96ff1982019-08-19 13:50:42 -04001inherit goarch
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002
Brad Bishop316dfdd2018-06-25 12:45:53 -04003GO_PARALLEL_BUILD ?= "${@oe.utils.parallel_make_argument(d, '-p %d')}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004
Andrew Geissler595f6302022-01-24 19:11:47 +00005export GODEBUG = "gocachehash=1"
6
Patrick Williams213cb262021-08-07 19:21:33 -05007GOROOT:class-native = "${STAGING_LIBDIR_NATIVE}/go"
8GOROOT:class-nativesdk = "${STAGING_DIR_TARGET}${libdir}/go"
Brad Bishopd7bf8c12018-02-25 22:55:05 -05009GOROOT = "${STAGING_LIBDIR}/go"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010export GOROOT
Brad Bishopd7bf8c12018-02-25 22:55:05 -050011export GOROOT_FINAL = "${libdir}/go"
Brad Bishopc8f47122019-06-24 09:36:18 -040012export GOCACHE = "${B}/.cache"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013
Brad Bishopd89cb5f2019-04-10 09:02:41 -040014export GOARCH = "${TARGET_GOARCH}"
15export GOOS = "${TARGET_GOOS}"
16export GOHOSTARCH="${BUILD_GOARCH}"
17export GOHOSTOS="${BUILD_GOOS}"
18
19GOARM[export] = "0"
Patrick Williams213cb262021-08-07 19:21:33 -050020GOARM:arm:class-target = "${TARGET_GOARM}"
21GOARM:arm:class-target[export] = "1"
Brad Bishopd89cb5f2019-04-10 09:02:41 -040022
23GO386[export] = "0"
Patrick Williams213cb262021-08-07 19:21:33 -050024GO386:x86:class-target = "${TARGET_GO386}"
25GO386:x86:class-target[export] = "1"
Brad Bishopd89cb5f2019-04-10 09:02:41 -040026
27GOMIPS[export] = "0"
Patrick Williams213cb262021-08-07 19:21:33 -050028GOMIPS:mips:class-target = "${TARGET_GOMIPS}"
29GOMIPS:mips:class-target[export] = "1"
Brad Bishopd89cb5f2019-04-10 09:02:41 -040030
Patrick Williams213cb262021-08-07 19:21:33 -050031DEPENDS_GOLANG:class-target = "virtual/${TUNE_PKGARCH}-go virtual/${TARGET_PREFIX}go-runtime"
32DEPENDS_GOLANG:class-native = "go-native"
33DEPENDS_GOLANG:class-nativesdk = "virtual/${TARGET_PREFIX}go-crosssdk virtual/${TARGET_PREFIX}go-runtime"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034
Patrick Williams213cb262021-08-07 19:21:33 -050035DEPENDS:append = " ${DEPENDS_GOLANG}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050036
37GO_LINKSHARED ?= "${@'-linkshared' if d.getVar('GO_DYNLINK') else ''}"
38GO_RPATH_LINK = "${@'-Wl,-rpath-link=${STAGING_DIR_TARGET}${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
39GO_RPATH = "${@'-r ${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
Patrick Williams213cb262021-08-07 19:21:33 -050040GO_RPATH:class-native = "${@'-r ${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
41GO_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 -050042GO_EXTLDFLAGS ?= "${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} ${GO_RPATH_LINK} ${LDFLAGS}"
43GO_LINKMODE ?= ""
Patrick Williams213cb262021-08-07 19:21:33 -050044GO_LINKMODE:class-nativesdk = "--linkmode=external"
45GO_LINKMODE:class-native = "--linkmode=external"
Andrew Geisslereff27472021-10-29 15:35:00 -050046GO_EXTRA_LDFLAGS ?= ""
47GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} ${GO_EXTRA_LDFLAGS} -extldflags '${GO_EXTLDFLAGS}'"'
Andrew Geissler1e34c2d2020-05-29 16:02:59 -050048export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS} -trimpath"
Brad Bishop316dfdd2018-06-25 12:45:53 -040049export GOPATH_OMIT_IN_ACTIONID ?= "1"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c"
Brad Bishop316dfdd2018-06-25 12:45:53 -040051export GOPTESTFLAGS ?= ""
Patrick Williams213cb262021-08-07 19:21:33 -050052GOBUILDFLAGS:prepend:task-compile = "${GO_PARALLEL_BUILD} "
Brad Bishopd7bf8c12018-02-25 22:55:05 -050053
54export GO = "${HOST_PREFIX}go"
55GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}"
Patrick Williams213cb262021-08-07 19:21:33 -050056GOTOOLDIR:class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057export GOTOOLDIR
58
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059export CGO_ENABLED ?= "1"
60export CGO_CFLAGS ?= "${CFLAGS}"
61export CGO_CPPFLAGS ?= "${CPPFLAGS}"
62export CGO_CXXFLAGS ?= "${CXXFLAGS}"
63export CGO_LDFLAGS ?= "${LDFLAGS}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064
65GO_INSTALL ?= "${GO_IMPORT}/..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050066GO_INSTALL_FILTEROUT ?= "${GO_IMPORT}/vendor/"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050067
Brad Bishopd7bf8c12018-02-25 22:55:05 -050068B = "${WORKDIR}/build"
69export GOPATH = "${B}"
Andrew Geissler595f6302022-01-24 19:11:47 +000070export GOTMPDIR ?= "${WORKDIR}/build-tmp"
Brad Bishop316dfdd2018-06-25 12:45:53 -040071GOTMPDIR[vardepvalue] = ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072
73python go_do_unpack() {
74 src_uri = (d.getVar('SRC_URI') or "").split()
75 if len(src_uri) == 0:
76 return
77
Brad Bishop08902b02019-08-20 09:16:51 -040078 fetcher = bb.fetch2.Fetch(src_uri, d)
79 for url in fetcher.urls:
80 if fetcher.ud[url].type == 'git':
81 if fetcher.ud[url].parm.get('destsuffix') is None:
82 s_dirname = os.path.basename(d.getVar('S'))
83 fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src', d.getVar('GO_IMPORT')) + '/'
84 fetcher.unpack(d.getVar('WORKDIR'))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050085}
86
87go_list_packages() {
88 ${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
89 egrep -v '${GO_INSTALL_FILTEROUT}'
90}
91
92go_list_package_tests() {
Brad Bishop316dfdd2018-06-25 12:45:53 -040093 ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094 grep -v '\[\]$' | \
95 egrep -v '${GO_INSTALL_FILTEROUT}' | \
96 awk '{ print $1 }'
97}
98
99go_do_configure() {
100 ln -snf ${S}/src ${B}/
101}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400102do_configure[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500103
104go_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400105 export TMPDIR="${GOTMPDIR}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 if [ -n "${GO_INSTALL}" ]; then
Brad Bishop316dfdd2018-06-25 12:45:53 -0400107 if [ -n "${GO_LINKSHARED}" ]; then
108 ${GO} install ${GOBUILDFLAGS} `go_list_packages`
109 rm -rf ${B}/bin
110 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
112 fi
113}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400114do_compile[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115do_compile[cleandirs] = "${B}/bin ${B}/pkg"
116
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500117go_do_install() {
118 install -d ${D}${libdir}/go/src/${GO_IMPORT}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400119 tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600121 tar -C ${B} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' pkg | \
122 tar -C ${D}${libdir}/go --no-same-owner -xf -
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123
124 if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
125 install -d ${D}${bindir}
126 install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127 fi
128}
129
Brad Bishop316dfdd2018-06-25 12:45:53 -0400130go_stage_testdata() {
131 oldwd="$PWD"
132 cd ${S}/src
133 find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
134 if echo "$d" | grep -q '/vendor/'; then
135 continue
136 fi
137 parent=`dirname $d`
138 install -d ${D}${PTEST_PATH}/$parent
139 cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
140 done
141 cd "$oldwd"
142}
143
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500144EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145
Patrick Williams213cb262021-08-07 19:21:33 -0500146FILES:${PN}-dev = "${libdir}/go/src"
147FILES:${PN}-staticdev = "${libdir}/go/pkg"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500148
Patrick Williams213cb262021-08-07 19:21:33 -0500149INSANE_SKIP:${PN} += "ldflags"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800150
151# Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600152# doesn't support -buildmode=pie, so skip the QA checking for mips/rv32 and its
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800153# variants.
154python() {
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600155 if 'mips' in d.getVar('TARGET_ARCH') or 'riscv32' in d.getVar('TARGET_ARCH'):
Patrick Williams213cb262021-08-07 19:21:33 -0500156 d.appendVar('INSANE_SKIP:%s' % d.getVar('PN'), " textrel")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800157 else:
158 d.appendVar('GOBUILDFLAGS', ' -buildmode=pie')
159}