blob: d33d83ea7f1a9da1e0fe8bec87b9c26d1746f51f [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 Bishopd7bf8c12018-02-25 22:55:05 -050011DEPENDS_GOLANG_class-target = "virtual/${TARGET_PREFIX}go virtual/${TARGET_PREFIX}go-runtime"
12DEPENDS_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 GOCACHE = "off"
49export GOTMPDIR ?= "${WORKDIR}/go-tmp"
50GOTMPDIR[vardepvalue] = ""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050051
52python go_do_unpack() {
53 src_uri = (d.getVar('SRC_URI') or "").split()
54 if len(src_uri) == 0:
55 return
56
57 try:
58 fetcher = bb.fetch2.Fetch(src_uri, d)
59 for url in fetcher.urls:
60 if fetcher.ud[url].type == 'git':
61 if fetcher.ud[url].parm.get('destsuffix') is None:
62 s_dirname = os.path.basename(d.getVar('S'))
63 fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src',
64 d.getVar('GO_IMPORT')) + '/'
65 fetcher.unpack(d.getVar('WORKDIR'))
66 except bb.fetch2.BBFetchException as e:
67 raise bb.build.FuncFailed(e)
68}
69
70go_list_packages() {
71 ${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
72 egrep -v '${GO_INSTALL_FILTEROUT}'
73}
74
75go_list_package_tests() {
Brad Bishop316dfdd2018-06-25 12:45:53 -040076 ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 grep -v '\[\]$' | \
78 egrep -v '${GO_INSTALL_FILTEROUT}' | \
79 awk '{ print $1 }'
80}
81
82go_do_configure() {
83 ln -snf ${S}/src ${B}/
84}
Brad Bishop316dfdd2018-06-25 12:45:53 -040085do_configure[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050086
87go_do_compile() {
Brad Bishop316dfdd2018-06-25 12:45:53 -040088 export TMPDIR="${GOTMPDIR}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 if [ -n "${GO_INSTALL}" ]; then
Brad Bishop316dfdd2018-06-25 12:45:53 -040090 if [ -n "${GO_LINKSHARED}" ]; then
91 ${GO} install ${GOBUILDFLAGS} `go_list_packages`
92 rm -rf ${B}/bin
93 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094 ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
95 fi
96}
Brad Bishop316dfdd2018-06-25 12:45:53 -040097do_compile[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050098do_compile[cleandirs] = "${B}/bin ${B}/pkg"
99
Brad Bishop316dfdd2018-06-25 12:45:53 -0400100do_compile_ptest_base() {
101 export TMPDIR="${GOTMPDIR}"
102 rm -f ${B}/.go_compiled_tests.list
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500103 go_list_package_tests | while read pkg; do
104 cd ${B}/src/$pkg
105 ${GO} test ${GOPTESTBUILDFLAGS} $pkg
106 find . -mindepth 1 -maxdepth 1 -type f -name '*.test' -exec echo $pkg/{} \; | \
107 sed -e's,/\./,/,'>> ${B}/.go_compiled_tests.list
108 done
Brad Bishop316dfdd2018-06-25 12:45:53 -0400109 do_compile_ptest
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400111do_compile_ptest_base[dirs] =+ "${GOTMPDIR}"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112
113go_do_install() {
114 install -d ${D}${libdir}/go/src/${GO_IMPORT}
Brad Bishop316dfdd2018-06-25 12:45:53 -0400115 tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500116 tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
117 tar -C ${B} -cf - pkg | tar -C ${D}${libdir}/go --no-same-owner -xf -
118
119 if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
120 install -d ${D}${bindir}
121 install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 fi
123}
124
Brad Bishop316dfdd2018-06-25 12:45:53 -0400125go_make_ptest_wrapper() {
126 cat >${D}${PTEST_PATH}/run-ptest <<EOF
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500127#!/bin/sh
Brad Bishop316dfdd2018-06-25 12:45:53 -0400128RC=0
129run_test() (
130 cd "\$1"
131 ((((./\$2 ${GOPTESTFLAGS}; echo \$? >&3) | sed -r -e"s,^(PASS|SKIP|FAIL)\$,\\1: \$1/\$2," >&4) 3>&1) | (read rc; exit \$rc)) 4>&1
132 exit \$?)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133EOF
Brad Bishop316dfdd2018-06-25 12:45:53 -0400134
135}
136
137go_stage_testdata() {
138 oldwd="$PWD"
139 cd ${S}/src
140 find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
141 if echo "$d" | grep -q '/vendor/'; then
142 continue
143 fi
144 parent=`dirname $d`
145 install -d ${D}${PTEST_PATH}/$parent
146 cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
147 done
148 cd "$oldwd"
149}
150
151do_install_ptest_base() {
152 test -f "${B}/.go_compiled_tests.list" || exit 0
153 install -d ${D}${PTEST_PATH}
154 go_stage_testdata
155 go_make_ptest_wrapper
156 havetests=""
157 while read test; do
158 testdir=`dirname $test`
159 testprog=`basename $test`
160 install -d ${D}${PTEST_PATH}/$testdir
161 install -m 0755 ${B}/src/$test ${D}${PTEST_PATH}/$test
162 echo "run_test $testdir $testprog || RC=1" >> ${D}${PTEST_PATH}/run-ptest
163 havetests="yes"
164 done < ${B}/.go_compiled_tests.list
165 if [ -n "$havetests" ]; then
166 echo "exit \$RC" >> ${D}${PTEST_PATH}/run-ptest
167 chmod +x ${D}${PTEST_PATH}/run-ptest
168 else
169 rm -rf ${D}${PTEST_PATH}
170 fi
171 do_install_ptest
172 chown -R root:root ${D}${PTEST_PATH}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500173}
174
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500175EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500176
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500177FILES_${PN}-dev = "${libdir}/go/src"
178FILES_${PN}-staticdev = "${libdir}/go/pkg"
179
180INSANE_SKIP_${PN} += "ldflags"
181INSANE_SKIP_${PN}-ptest += "ldflags"