blob: b6d9372c5696f9d54e4a02a7a0f5e3e1b7c4114a [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001inherit goarch ptest
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
5GOROOT_class-native = "${STAGING_LIBDIR_NATIVE}/go"
Brad Bishopd7bf8c12018-02-25 22:55:05 -05006GOROOT_class-nativesdk = "${STAGING_DIR_TARGET}${libdir}/go"
7GOROOT = "${STAGING_LIBDIR}/go"
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008export GOROOT
Brad Bishopd7bf8c12018-02-25 22:55:05 -05009export GOROOT_FINAL = "${libdir}/go"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010
Brad Bishopd89cb5f2019-04-10 09:02:41 -040011export GOARCH = "${TARGET_GOARCH}"
12export GOOS = "${TARGET_GOOS}"
13export GOHOSTARCH="${BUILD_GOARCH}"
14export GOHOSTOS="${BUILD_GOOS}"
15
16GOARM[export] = "0"
17GOARM_arm_class-target = "${TARGET_GOARM}"
18GOARM_arm_class-target[export] = "1"
19
20GO386[export] = "0"
21GO386_x86_class-target = "${TARGET_GO386}"
22GO386_x86_class-target[export] = "1"
Brad Bishopd89cb5f2019-04-10 09:02:41 -040023
24GOMIPS[export] = "0"
25GOMIPS_mips_class-target = "${TARGET_GOMIPS}"
26GOMIPS_mips_class-target[export] = "1"
27
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080028DEPENDS_GOLANG_class-target = "virtual/${TUNE_PKGARCH}-go virtual/${TARGET_PREFIX}go-runtime"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029DEPENDS_GOLANG_class-native = "go-native"
30DEPENDS_GOLANG_class-nativesdk = "virtual/${TARGET_PREFIX}go-crosssdk virtual/${TARGET_PREFIX}go-runtime"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032DEPENDS_append = " ${DEPENDS_GOLANG}"
33
34GO_LINKSHARED ?= "${@'-linkshared' if d.getVar('GO_DYNLINK') else ''}"
35GO_RPATH_LINK = "${@'-Wl,-rpath-link=${STAGING_DIR_TARGET}${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
36GO_RPATH = "${@'-r ${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
37GO_RPATH_class-native = "${@'-r ${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
38GO_RPATH_LINK_class-native = "${@'-Wl,-rpath-link=${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
39GO_EXTLDFLAGS ?= "${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} ${GO_RPATH_LINK} ${LDFLAGS}"
40GO_LINKMODE ?= ""
41GO_LINKMODE_class-nativesdk = "--linkmode=external"
42GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} -extldflags '${GO_EXTLDFLAGS}'"'
43export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040044export GOPATH_OMIT_IN_ACTIONID ?= "1"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050045export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c"
Brad Bishop316dfdd2018-06-25 12:45:53 -040046export GOPTESTFLAGS ?= ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047GOBUILDFLAGS_prepend_task-compile = "${GO_PARALLEL_BUILD} "
48
49export GO = "${HOST_PREFIX}go"
50GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}"
51GOTOOLDIR_class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}"
52export GOTOOLDIR
53
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054export CGO_ENABLED ?= "1"
55export CGO_CFLAGS ?= "${CFLAGS}"
56export CGO_CPPFLAGS ?= "${CPPFLAGS}"
57export CGO_CXXFLAGS ?= "${CXXFLAGS}"
58export CGO_LDFLAGS ?= "${LDFLAGS}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050059
60GO_INSTALL ?= "${GO_IMPORT}/..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061GO_INSTALL_FILTEROUT ?= "${GO_IMPORT}/vendor/"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062
Brad Bishopd7bf8c12018-02-25 22:55:05 -050063B = "${WORKDIR}/build"
64export GOPATH = "${B}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040065export GOTMPDIR ?= "${WORKDIR}/go-tmp"
66GOTMPDIR[vardepvalue] = ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050067
68python go_do_unpack() {
69 src_uri = (d.getVar('SRC_URI') or "").split()
70 if len(src_uri) == 0:
71 return
72
73 try:
74 fetcher = bb.fetch2.Fetch(src_uri, d)
75 for url in fetcher.urls:
76 if fetcher.ud[url].type == 'git':
77 if fetcher.ud[url].parm.get('destsuffix') is None:
78 s_dirname = os.path.basename(d.getVar('S'))
79 fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src',
80 d.getVar('GO_IMPORT')) + '/'
81 fetcher.unpack(d.getVar('WORKDIR'))
82 except bb.fetch2.BBFetchException as e:
83 raise bb.build.FuncFailed(e)
84}
85
86go_list_packages() {
87 ${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
88 egrep -v '${GO_INSTALL_FILTEROUT}'
89}
90
91go_list_package_tests() {
Brad Bishop316dfdd2018-06-25 12:45:53 -040092 ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093 grep -v '\[\]$' | \
94 egrep -v '${GO_INSTALL_FILTEROUT}' | \
95 awk '{ print $1 }'
96}
97
98go_do_configure() {
99 ln -snf ${S}/src ${B}/
100}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400101do_configure[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102
103go_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400104 export TMPDIR="${GOTMPDIR}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 if [ -n "${GO_INSTALL}" ]; then
Brad Bishop316dfdd2018-06-25 12:45:53 -0400106 if [ -n "${GO_LINKSHARED}" ]; then
107 ${GO} install ${GOBUILDFLAGS} `go_list_packages`
108 rm -rf ${B}/bin
109 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110 ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
111 fi
112}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400113do_compile[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500114do_compile[cleandirs] = "${B}/bin ${B}/pkg"
115
Brad Bishop316dfdd2018-06-25 12:45:53 -0400116do_compile_ptest_base() {
117 export TMPDIR="${GOTMPDIR}"
118 rm -f ${B}/.go_compiled_tests.list
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119 go_list_package_tests | while read pkg; do
120 cd ${B}/src/$pkg
121 ${GO} test ${GOPTESTBUILDFLAGS} $pkg
122 find . -mindepth 1 -maxdepth 1 -type f -name '*.test' -exec echo $pkg/{} \; | \
123 sed -e's,/\./,/,'>> ${B}/.go_compiled_tests.list
124 done
Brad Bishop316dfdd2018-06-25 12:45:53 -0400125 do_compile_ptest
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400127do_compile_ptest_base[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128
129go_do_install() {
130 install -d ${D}${libdir}/go/src/${GO_IMPORT}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400131 tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132 tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
Brad Bishopc342db32019-05-15 21:57:59 -0400133 tar -C ${B} -cf - --exclude-vcs pkg | tar -C ${D}${libdir}/go --no-same-owner -xf -
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500134
135 if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
136 install -d ${D}${bindir}
137 install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500138 fi
139}
140
Brad Bishop316dfdd2018-06-25 12:45:53 -0400141go_make_ptest_wrapper() {
142 cat >${D}${PTEST_PATH}/run-ptest <<EOF
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143#!/bin/sh
Brad Bishop316dfdd2018-06-25 12:45:53 -0400144RC=0
145run_test() (
146 cd "\$1"
147 ((((./\$2 ${GOPTESTFLAGS}; echo \$? >&3) | sed -r -e"s,^(PASS|SKIP|FAIL)\$,\\1: \$1/\$2," >&4) 3>&1) | (read rc; exit \$rc)) 4>&1
148 exit \$?)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500149EOF
Brad Bishop316dfdd2018-06-25 12:45:53 -0400150
151}
152
153go_stage_testdata() {
154 oldwd="$PWD"
155 cd ${S}/src
156 find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
157 if echo "$d" | grep -q '/vendor/'; then
158 continue
159 fi
160 parent=`dirname $d`
161 install -d ${D}${PTEST_PATH}/$parent
162 cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
163 done
164 cd "$oldwd"
165}
166
167do_install_ptest_base() {
168 test -f "${B}/.go_compiled_tests.list" || exit 0
169 install -d ${D}${PTEST_PATH}
170 go_stage_testdata
171 go_make_ptest_wrapper
172 havetests=""
173 while read test; do
174 testdir=`dirname $test`
175 testprog=`basename $test`
176 install -d ${D}${PTEST_PATH}/$testdir
177 install -m 0755 ${B}/src/$test ${D}${PTEST_PATH}/$test
178 echo "run_test $testdir $testprog || RC=1" >> ${D}${PTEST_PATH}/run-ptest
179 havetests="yes"
180 done < ${B}/.go_compiled_tests.list
181 if [ -n "$havetests" ]; then
182 echo "exit \$RC" >> ${D}${PTEST_PATH}/run-ptest
183 chmod +x ${D}${PTEST_PATH}/run-ptest
184 else
185 rm -rf ${D}${PTEST_PATH}
186 fi
187 do_install_ptest
188 chown -R root:root ${D}${PTEST_PATH}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500189}
190
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500191EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500192
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500193FILES_${PN}-dev = "${libdir}/go/src"
194FILES_${PN}-staticdev = "${libdir}/go/pkg"
195
196INSANE_SKIP_${PN} += "ldflags"
197INSANE_SKIP_${PN}-ptest += "ldflags"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800198
199# Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips
200# doesn't support -buildmode=pie, so skip the QA checking for mips and its
201# variants.
202python() {
203 if 'mips' in d.getVar('TARGET_ARCH'):
204 d.appendVar('INSANE_SKIP_%s' % d.getVar('PN'), " textrel")
205 else:
206 d.appendVar('GOBUILDFLAGS', ' -buildmode=pie')
207}