blob: 78c2d6880ff05dcb61e4caff73d212993d900564 [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"
23GO386_i586_class-target = "${TARGET_GO386}"
24GO386_i586_class-target[export] = "1"
25
26GOMIPS[export] = "0"
27GOMIPS_mips_class-target = "${TARGET_GOMIPS}"
28GOMIPS_mips_class-target[export] = "1"
29
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080030DEPENDS_GOLANG_class-target = "virtual/${TUNE_PKGARCH}-go virtual/${TARGET_PREFIX}go-runtime"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050031DEPENDS_GOLANG_class-native = "go-native"
32DEPENDS_GOLANG_class-nativesdk = "virtual/${TARGET_PREFIX}go-crosssdk virtual/${TARGET_PREFIX}go-runtime"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033
Brad Bishopd7bf8c12018-02-25 22:55:05 -050034DEPENDS_append = " ${DEPENDS_GOLANG}"
35
36GO_LINKSHARED ?= "${@'-linkshared' if d.getVar('GO_DYNLINK') else ''}"
37GO_RPATH_LINK = "${@'-Wl,-rpath-link=${STAGING_DIR_TARGET}${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
38GO_RPATH = "${@'-r ${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
39GO_RPATH_class-native = "${@'-r ${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
40GO_RPATH_LINK_class-native = "${@'-Wl,-rpath-link=${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
41GO_EXTLDFLAGS ?= "${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} ${GO_RPATH_LINK} ${LDFLAGS}"
42GO_LINKMODE ?= ""
43GO_LINKMODE_class-nativesdk = "--linkmode=external"
44GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} -extldflags '${GO_EXTLDFLAGS}'"'
45export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040046export GOPATH_OMIT_IN_ACTIONID ?= "1"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c"
Brad Bishop316dfdd2018-06-25 12:45:53 -040048export GOPTESTFLAGS ?= ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050049GOBUILDFLAGS_prepend_task-compile = "${GO_PARALLEL_BUILD} "
50
51export GO = "${HOST_PREFIX}go"
52GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}"
53GOTOOLDIR_class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}"
54export GOTOOLDIR
55
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056export CGO_ENABLED ?= "1"
57export CGO_CFLAGS ?= "${CFLAGS}"
58export CGO_CPPFLAGS ?= "${CPPFLAGS}"
59export CGO_CXXFLAGS ?= "${CXXFLAGS}"
60export CGO_LDFLAGS ?= "${LDFLAGS}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061
62GO_INSTALL ?= "${GO_IMPORT}/..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050063GO_INSTALL_FILTEROUT ?= "${GO_IMPORT}/vendor/"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064
Brad Bishopd7bf8c12018-02-25 22:55:05 -050065B = "${WORKDIR}/build"
66export GOPATH = "${B}"
Brad Bishop316dfdd2018-06-25 12:45:53 -040067export GOTMPDIR ?= "${WORKDIR}/go-tmp"
68GOTMPDIR[vardepvalue] = ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069
70python go_do_unpack() {
71 src_uri = (d.getVar('SRC_URI') or "").split()
72 if len(src_uri) == 0:
73 return
74
75 try:
76 fetcher = bb.fetch2.Fetch(src_uri, d)
77 for url in fetcher.urls:
78 if fetcher.ud[url].type == 'git':
79 if fetcher.ud[url].parm.get('destsuffix') is None:
80 s_dirname = os.path.basename(d.getVar('S'))
81 fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src',
82 d.getVar('GO_IMPORT')) + '/'
83 fetcher.unpack(d.getVar('WORKDIR'))
84 except bb.fetch2.BBFetchException as e:
85 raise bb.build.FuncFailed(e)
86}
87
88go_list_packages() {
89 ${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
90 egrep -v '${GO_INSTALL_FILTEROUT}'
91}
92
93go_list_package_tests() {
Brad Bishop316dfdd2018-06-25 12:45:53 -040094 ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095 grep -v '\[\]$' | \
96 egrep -v '${GO_INSTALL_FILTEROUT}' | \
97 awk '{ print $1 }'
98}
99
100go_do_configure() {
101 ln -snf ${S}/src ${B}/
102}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400103do_configure[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500104
105go_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -0400106 export TMPDIR="${GOTMPDIR}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 if [ -n "${GO_INSTALL}" ]; then
Brad Bishop316dfdd2018-06-25 12:45:53 -0400108 if [ -n "${GO_LINKSHARED}" ]; then
109 ${GO} install ${GOBUILDFLAGS} `go_list_packages`
110 rm -rf ${B}/bin
111 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112 ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
113 fi
114}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400115do_compile[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116do_compile[cleandirs] = "${B}/bin ${B}/pkg"
117
Brad Bishop316dfdd2018-06-25 12:45:53 -0400118do_compile_ptest_base() {
119 export TMPDIR="${GOTMPDIR}"
120 rm -f ${B}/.go_compiled_tests.list
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500121 go_list_package_tests | while read pkg; do
122 cd ${B}/src/$pkg
123 ${GO} test ${GOPTESTBUILDFLAGS} $pkg
124 find . -mindepth 1 -maxdepth 1 -type f -name '*.test' -exec echo $pkg/{} \; | \
125 sed -e's,/\./,/,'>> ${B}/.go_compiled_tests.list
126 done
Brad Bishop316dfdd2018-06-25 12:45:53 -0400127 do_compile_ptest
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400129do_compile_ptest_base[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500130
131go_do_install() {
132 install -d ${D}${libdir}/go/src/${GO_IMPORT}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400133 tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500134 tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
135 tar -C ${B} -cf - pkg | tar -C ${D}${libdir}/go --no-same-owner -xf -
136
137 if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
138 install -d ${D}${bindir}
139 install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 fi
141}
142
Brad Bishop316dfdd2018-06-25 12:45:53 -0400143go_make_ptest_wrapper() {
144 cat >${D}${PTEST_PATH}/run-ptest <<EOF
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145#!/bin/sh
Brad Bishop316dfdd2018-06-25 12:45:53 -0400146RC=0
147run_test() (
148 cd "\$1"
149 ((((./\$2 ${GOPTESTFLAGS}; echo \$? >&3) | sed -r -e"s,^(PASS|SKIP|FAIL)\$,\\1: \$1/\$2," >&4) 3>&1) | (read rc; exit \$rc)) 4>&1
150 exit \$?)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500151EOF
Brad Bishop316dfdd2018-06-25 12:45:53 -0400152
153}
154
155go_stage_testdata() {
156 oldwd="$PWD"
157 cd ${S}/src
158 find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
159 if echo "$d" | grep -q '/vendor/'; then
160 continue
161 fi
162 parent=`dirname $d`
163 install -d ${D}${PTEST_PATH}/$parent
164 cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
165 done
166 cd "$oldwd"
167}
168
169do_install_ptest_base() {
170 test -f "${B}/.go_compiled_tests.list" || exit 0
171 install -d ${D}${PTEST_PATH}
172 go_stage_testdata
173 go_make_ptest_wrapper
174 havetests=""
175 while read test; do
176 testdir=`dirname $test`
177 testprog=`basename $test`
178 install -d ${D}${PTEST_PATH}/$testdir
179 install -m 0755 ${B}/src/$test ${D}${PTEST_PATH}/$test
180 echo "run_test $testdir $testprog || RC=1" >> ${D}${PTEST_PATH}/run-ptest
181 havetests="yes"
182 done < ${B}/.go_compiled_tests.list
183 if [ -n "$havetests" ]; then
184 echo "exit \$RC" >> ${D}${PTEST_PATH}/run-ptest
185 chmod +x ${D}${PTEST_PATH}/run-ptest
186 else
187 rm -rf ${D}${PTEST_PATH}
188 fi
189 do_install_ptest
190 chown -R root:root ${D}${PTEST_PATH}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500191}
192
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500193EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500195FILES_${PN}-dev = "${libdir}/go/src"
196FILES_${PN}-staticdev = "${libdir}/go/pkg"
197
198INSANE_SKIP_${PN} += "ldflags"
199INSANE_SKIP_${PN}-ptest += "ldflags"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800200
201# Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips
202# doesn't support -buildmode=pie, so skip the QA checking for mips and its
203# variants.
204python() {
205 if 'mips' in d.getVar('TARGET_ARCH'):
206 d.appendVar('INSANE_SKIP_%s' % d.getVar('PN'), " textrel")
207 else:
208 d.appendVar('GOBUILDFLAGS', ' -buildmode=pie')
209}