blob: 7069c5fec0ee50cf9cab2227091bfed569453fa4 [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 Bishop1a4b7ee2018-12-16 17:11:34 -080011DEPENDS_GOLANG_class-target = "virtual/${TUNE_PKGARCH}-go virtual/${TARGET_PREFIX}go-runtime"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012DEPENDS_GOLANG_class-native = "go-native"
13DEPENDS_GOLANG_class-nativesdk = "virtual/${TARGET_PREFIX}go-crosssdk virtual/${TARGET_PREFIX}go-runtime"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015DEPENDS_append = " ${DEPENDS_GOLANG}"
16
17GO_LINKSHARED ?= "${@'-linkshared' if d.getVar('GO_DYNLINK') else ''}"
18GO_RPATH_LINK = "${@'-Wl,-rpath-link=${STAGING_DIR_TARGET}${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
19GO_RPATH = "${@'-r ${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
20GO_RPATH_class-native = "${@'-r ${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
21GO_RPATH_LINK_class-native = "${@'-Wl,-rpath-link=${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
22GO_EXTLDFLAGS ?= "${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} ${GO_RPATH_LINK} ${LDFLAGS}"
23GO_LINKMODE ?= ""
24GO_LINKMODE_class-nativesdk = "--linkmode=external"
25GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} -extldflags '${GO_EXTLDFLAGS}'"'
26export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040027export GOPATH_OMIT_IN_ACTIONID ?= "1"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c"
Brad Bishop316dfdd2018-06-25 12:45:53 -040029export GOPTESTFLAGS ?= ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050030GOBUILDFLAGS_prepend_task-compile = "${GO_PARALLEL_BUILD} "
31
32export GO = "${HOST_PREFIX}go"
33GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}"
34GOTOOLDIR_class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}"
35export GOTOOLDIR
36
Brad Bishopd7bf8c12018-02-25 22:55:05 -050037export CGO_ENABLED ?= "1"
38export CGO_CFLAGS ?= "${CFLAGS}"
39export CGO_CPPFLAGS ?= "${CPPFLAGS}"
40export CGO_CXXFLAGS ?= "${CXXFLAGS}"
41export CGO_LDFLAGS ?= "${LDFLAGS}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050042
43GO_INSTALL ?= "${GO_IMPORT}/..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050044GO_INSTALL_FILTEROUT ?= "${GO_IMPORT}/vendor/"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050045
Brad Bishopd7bf8c12018-02-25 22:55:05 -050046B = "${WORKDIR}/build"
47export GOPATH = "${B}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040048export GOTMPDIR ?= "${WORKDIR}/go-tmp"
49GOTMPDIR[vardepvalue] = ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050
51python go_do_unpack() {
52 src_uri = (d.getVar('SRC_URI') or "").split()
53 if len(src_uri) == 0:
54 return
55
56 try:
57 fetcher = bb.fetch2.Fetch(src_uri, d)
58 for url in fetcher.urls:
59 if fetcher.ud[url].type == 'git':
60 if fetcher.ud[url].parm.get('destsuffix') is None:
61 s_dirname = os.path.basename(d.getVar('S'))
62 fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src',
63 d.getVar('GO_IMPORT')) + '/'
64 fetcher.unpack(d.getVar('WORKDIR'))
65 except bb.fetch2.BBFetchException as e:
66 raise bb.build.FuncFailed(e)
67}
68
69go_list_packages() {
70 ${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
71 egrep -v '${GO_INSTALL_FILTEROUT}'
72}
73
74go_list_package_tests() {
Brad Bishop316dfdd2018-06-25 12:45:53 -040075 ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 grep -v '\[\]$' | \
77 egrep -v '${GO_INSTALL_FILTEROUT}' | \
78 awk '{ print $1 }'
79}
80
81go_do_configure() {
82 ln -snf ${S}/src ${B}/
83}
Brad Bishop316dfdd2018-06-25 12:45:53 -040084do_configure[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050085
86go_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -040087 export TMPDIR="${GOTMPDIR}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050088 if [ -n "${GO_INSTALL}" ]; then
Brad Bishop316dfdd2018-06-25 12:45:53 -040089 if [ -n "${GO_LINKSHARED}" ]; then
90 ${GO} install ${GOBUILDFLAGS} `go_list_packages`
91 rm -rf ${B}/bin
92 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093 ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
94 fi
95}
Brad Bishop316dfdd2018-06-25 12:45:53 -040096do_compile[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097do_compile[cleandirs] = "${B}/bin ${B}/pkg"
98
Brad Bishop316dfdd2018-06-25 12:45:53 -040099do_compile_ptest_base() {
100 export TMPDIR="${GOTMPDIR}"
101 rm -f ${B}/.go_compiled_tests.list
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102 go_list_package_tests | while read pkg; do
103 cd ${B}/src/$pkg
104 ${GO} test ${GOPTESTBUILDFLAGS} $pkg
105 find . -mindepth 1 -maxdepth 1 -type f -name '*.test' -exec echo $pkg/{} \; | \
106 sed -e's,/\./,/,'>> ${B}/.go_compiled_tests.list
107 done
Brad Bishop316dfdd2018-06-25 12:45:53 -0400108 do_compile_ptest
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500109}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400110do_compile_ptest_base[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111
112go_do_install() {
113 install -d ${D}${libdir}/go/src/${GO_IMPORT}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400114 tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115 tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
116 tar -C ${B} -cf - pkg | tar -C ${D}${libdir}/go --no-same-owner -xf -
117
118 if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
119 install -d ${D}${bindir}
120 install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 fi
122}
123
Brad Bishop316dfdd2018-06-25 12:45:53 -0400124go_make_ptest_wrapper() {
125 cat >${D}${PTEST_PATH}/run-ptest <<EOF
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126#!/bin/sh
Brad Bishop316dfdd2018-06-25 12:45:53 -0400127RC=0
128run_test() (
129 cd "\$1"
130 ((((./\$2 ${GOPTESTFLAGS}; echo \$? >&3) | sed -r -e"s,^(PASS|SKIP|FAIL)\$,\\1: \$1/\$2," >&4) 3>&1) | (read rc; exit \$rc)) 4>&1
131 exit \$?)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132EOF
Brad Bishop316dfdd2018-06-25 12:45:53 -0400133
134}
135
136go_stage_testdata() {
137 oldwd="$PWD"
138 cd ${S}/src
139 find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
140 if echo "$d" | grep -q '/vendor/'; then
141 continue
142 fi
143 parent=`dirname $d`
144 install -d ${D}${PTEST_PATH}/$parent
145 cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
146 done
147 cd "$oldwd"
148}
149
150do_install_ptest_base() {
151 test -f "${B}/.go_compiled_tests.list" || exit 0
152 install -d ${D}${PTEST_PATH}
153 go_stage_testdata
154 go_make_ptest_wrapper
155 havetests=""
156 while read test; do
157 testdir=`dirname $test`
158 testprog=`basename $test`
159 install -d ${D}${PTEST_PATH}/$testdir
160 install -m 0755 ${B}/src/$test ${D}${PTEST_PATH}/$test
161 echo "run_test $testdir $testprog || RC=1" >> ${D}${PTEST_PATH}/run-ptest
162 havetests="yes"
163 done < ${B}/.go_compiled_tests.list
164 if [ -n "$havetests" ]; then
165 echo "exit \$RC" >> ${D}${PTEST_PATH}/run-ptest
166 chmod +x ${D}${PTEST_PATH}/run-ptest
167 else
168 rm -rf ${D}${PTEST_PATH}
169 fi
170 do_install_ptest
171 chown -R root:root ${D}${PTEST_PATH}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500172}
173
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500174EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500175
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500176FILES_${PN}-dev = "${libdir}/go/src"
177FILES_${PN}-staticdev = "${libdir}/go/pkg"
178
179INSANE_SKIP_${PN} += "ldflags"
180INSANE_SKIP_${PN}-ptest += "ldflags"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800181
182# Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips
183# doesn't support -buildmode=pie, so skip the QA checking for mips and its
184# variants.
185python() {
186 if 'mips' in d.getVar('TARGET_ARCH'):
187 d.appendVar('INSANE_SKIP_%s' % d.getVar('PN'), " textrel")
188 else:
189 d.appendVar('GOBUILDFLAGS', ' -buildmode=pie')
190}