blob: 09b01a84c37d93e5bec1a94a2653b8fe45f79673 [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001inherit goarch ptest
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002
Brad Bishopd7bf8c12018-02-25 22:55:05 -05003def get_go_parallel_make(d):
4 pm = (d.getVar('PARALLEL_MAKE') or '').split()
5 # look for '-j' and throw other options (e.g. '-l') away
6 # because they might have a different meaning in golang
7 while pm:
8 opt = pm.pop(0)
9 if opt == '-j':
10 v = pm.pop(0)
11 elif opt.startswith('-j'):
12 v = opt[2:].strip()
13 else:
14 continue
15
16 return '-p %d' % int(v)
17
18 return ""
19
20GO_PARALLEL_BUILD ?= "${@get_go_parallel_make(d)}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021
22GOROOT_class-native = "${STAGING_LIBDIR_NATIVE}/go"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023GOROOT_class-nativesdk = "${STAGING_DIR_TARGET}${libdir}/go"
24GOROOT = "${STAGING_LIBDIR}/go"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025export GOROOT
Brad Bishopd7bf8c12018-02-25 22:55:05 -050026export GOROOT_FINAL = "${libdir}/go"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028DEPENDS_GOLANG_class-target = "virtual/${TARGET_PREFIX}go virtual/${TARGET_PREFIX}go-runtime"
29DEPENDS_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}"
44export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c"
45export GOPTESTFLAGS ?= "-test.v"
46GOBUILDFLAGS_prepend_task-compile = "${GO_PARALLEL_BUILD} "
47
48export GO = "${HOST_PREFIX}go"
49GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}"
50GOTOOLDIR_class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}"
51export GOTOOLDIR
52
53SECURITY_CFLAGS = "${SECURITY_NOPIE_CFLAGS}"
54SECURITY_LDFLAGS = ""
55
56export 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}"
67GO_TMPDIR ?= "${WORKDIR}/go-tmp"
68GO_TMPDIR[vardepvalue] = ""
69
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() {
94 ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
95 grep -v '\[\]$' | \
96 egrep -v '${GO_INSTALL_FILTEROUT}' | \
97 awk '{ print $1 }'
98}
99
100go_do_configure() {
101 ln -snf ${S}/src ${B}/
102}
103
104go_do_compile() {
105 export TMPDIR="${GO_TMPDIR}"
106 ${GO} env
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 if [ -n "${GO_INSTALL}" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500108 ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
109 fi
110}
111do_compile[dirs] =+ "${GO_TMPDIR}"
112do_compile[cleandirs] = "${B}/bin ${B}/pkg"
113
114do_compile_ptest() {
115 export TMPDIR="${GO_TMPDIR}"
116 rm -f ${B}/.go_compiled_tests.list
117 go_list_package_tests | while read pkg; do
118 cd ${B}/src/$pkg
119 ${GO} test ${GOPTESTBUILDFLAGS} $pkg
120 find . -mindepth 1 -maxdepth 1 -type f -name '*.test' -exec echo $pkg/{} \; | \
121 sed -e's,/\./,/,'>> ${B}/.go_compiled_tests.list
122 done
123}
124do_compile_ptest_base[dirs] =+ "${GO_TMPDIR}"
125
126go_do_install() {
127 install -d ${D}${libdir}/go/src/${GO_IMPORT}
128 tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' . | \
129 tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
130 tar -C ${B} -cf - pkg | tar -C ${D}${libdir}/go --no-same-owner -xf -
131
132 if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
133 install -d ${D}${bindir}
134 install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500135 fi
136}
137
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500138do_install_ptest_base() {
139set -x
140 test -f "${B}/.go_compiled_tests.list" || exit 0
141 tests=""
142 while read test; do
143 tests="$tests${tests:+ }${test%.test}"
144 testdir=`dirname $test`
145 install -d ${D}${PTEST_PATH}/$testdir
146 install -m 0755 ${B}/src/$test ${D}${PTEST_PATH}/$test
147 if [ -d "${B}/src/$testdir/testdata" ]; then
148 cp --preserve=mode,timestamps -R "${B}/src/$testdir/testdata" ${D}${PTEST_PATH}/$testdir
149 fi
150 done < ${B}/.go_compiled_tests.list
151 if [ -n "$tests" ]; then
152 install -d ${D}${PTEST_PATH}
153 cat >${D}${PTEST_PATH}/run-ptest <<EOF
154#!/bin/sh
155ANYFAILED=0
156for t in $tests; do
157 testdir=\`dirname \$t.test\`
158 if ( cd "${PTEST_PATH}/\$testdir"; "${PTEST_PATH}/\$t.test" ${GOPTESTFLAGS} | tee /dev/fd/9 | grep -q "^FAIL" ) 9>&1; then
159 ANYFAILED=1
160 fi
161done
162if [ \$ANYFAILED -ne 0 ]; then
163 echo "FAIL: ${PN}"
164 exit 1
165fi
166echo "PASS: ${PN}"
167exit 0
168EOF
169 chmod +x ${D}${PTEST_PATH}/run-ptest
170 else
171 rm -rf ${D}${PTEST_PATH}
172 fi
173set +x
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500174}
175
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500176EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500178FILES_${PN}-dev = "${libdir}/go/src"
179FILES_${PN}-staticdev = "${libdir}/go/pkg"
180
181INSANE_SKIP_${PN} += "ldflags"
182INSANE_SKIP_${PN}-ptest += "ldflags"