Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override golang 1.14.4 and associated bbclasses, no folder changes necessary #158

Merged
merged 2 commits into from Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions classes/go-mod.bbclass
@@ -0,0 +1,20 @@
# Handle Go Modules support
#
# When using Go Modules, the the current working directory MUST be at or below
# the location of the 'go.mod' file when the go tool is used, and there is no
# way to tell it to look elsewhere. It will automatically look upwards for the
# file, but not downwards.
#
# To support this use case, we provide the `GO_WORKDIR` variable, which defaults
# to `GO_IMPORT` but allows for easy override.
#
# Copyright 2020 (C) O.S. Systems Software LTDA.

# The '-modcacherw' option ensures we have write access to the cached objects so
# we avoid errors during clean task as well as when removing the TMPDIR.
export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS} -modcacherw"

inherit go

GO_WORKDIR ?= "${GO_IMPORT}"
do_compile[dirs] += "${B}/src/${GO_WORKDIR}"
54 changes: 54 additions & 0 deletions classes/go-ptest.bbclass
@@ -0,0 +1,54 @@
inherit go ptest

do_compile_ptest_base() {
export TMPDIR="${GOTMPDIR}"
rm -f ${B}/.go_compiled_tests.list
go_list_package_tests | while read pkg; do
cd ${B}/src/$pkg
${GO} test ${GOPTESTBUILDFLAGS} $pkg
find . -mindepth 1 -maxdepth 1 -type f -name '*.test' -exec echo $pkg/{} \; | \
sed -e's,/\./,/,'>> ${B}/.go_compiled_tests.list
done
do_compile_ptest
}

do_compile_ptest_base[dirs] =+ "${GOTMPDIR}"

go_make_ptest_wrapper() {
cat >${D}${PTEST_PATH}/run-ptest <<EOF
#!/bin/sh
RC=0
run_test() (
cd "\$1"
((((./\$2 ${GOPTESTFLAGS}; echo \$? >&3) | sed -r -e"s,^(PASS|SKIP|FAIL)\$,\\1: \$1/\$2," >&4) 3>&1) | (read rc; exit \$rc)) 4>&1
exit \$?)
EOF

}

do_install_ptest_base() {
test -f "${B}/.go_compiled_tests.list" || exit 0
install -d ${D}${PTEST_PATH}
go_stage_testdata
go_make_ptest_wrapper
havetests=""
while read test; do
testdir=`dirname $test`
testprog=`basename $test`
install -d ${D}${PTEST_PATH}/$testdir
install -m 0755 ${B}/src/$test ${D}${PTEST_PATH}/$test
echo "run_test $testdir $testprog || RC=1" >> ${D}${PTEST_PATH}/run-ptest
havetests="yes"
done < ${B}/.go_compiled_tests.list
if [ -n "$havetests" ]; then
echo "exit \$RC" >> ${D}${PTEST_PATH}/run-ptest
chmod +x ${D}${PTEST_PATH}/run-ptest
else
rm -rf ${D}${PTEST_PATH}
fi
do_install_ptest
chown -R root:root ${D}${PTEST_PATH}
}

INSANE_SKIP_${PN}-ptest += "ldflags"

155 changes: 155 additions & 0 deletions classes/go.bbclass
@@ -0,0 +1,155 @@
inherit goarch

GO_PARALLEL_BUILD ?= "${@oe.utils.parallel_make_argument(d, '-p %d')}"

GOROOT_class-native = "${STAGING_LIBDIR_NATIVE}/go"
GOROOT_class-nativesdk = "${STAGING_DIR_TARGET}${libdir}/go"
GOROOT = "${STAGING_LIBDIR}/go"
export GOROOT
export GOROOT_FINAL = "${libdir}/go"
export GOCACHE = "${B}/.cache"

export GOARCH = "${TARGET_GOARCH}"
export GOOS = "${TARGET_GOOS}"
export GOHOSTARCH="${BUILD_GOARCH}"
export GOHOSTOS="${BUILD_GOOS}"

GOARM[export] = "0"
GOARM_arm_class-target = "${TARGET_GOARM}"
GOARM_arm_class-target[export] = "1"

GO386[export] = "0"
GO386_x86_class-target = "${TARGET_GO386}"
GO386_x86_class-target[export] = "1"

GOMIPS[export] = "0"
GOMIPS_mips_class-target = "${TARGET_GOMIPS}"
GOMIPS_mips_class-target[export] = "1"

DEPENDS_GOLANG_class-target = "virtual/${TUNE_PKGARCH}-go virtual/${TARGET_PREFIX}go-runtime"
DEPENDS_GOLANG_class-native = "go-native"
DEPENDS_GOLANG_class-nativesdk = "virtual/${TARGET_PREFIX}go-crosssdk virtual/${TARGET_PREFIX}go-runtime"

DEPENDS_append = " ${DEPENDS_GOLANG}"

GO_LINKSHARED ?= "${@'-linkshared' if d.getVar('GO_DYNLINK') else ''}"
GO_RPATH_LINK = "${@'-Wl,-rpath-link=${STAGING_DIR_TARGET}${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
GO_RPATH = "${@'-r ${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
GO_RPATH_class-native = "${@'-r ${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
GO_RPATH_LINK_class-native = "${@'-Wl,-rpath-link=${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}"
GO_EXTLDFLAGS ?= "${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} ${GO_RPATH_LINK} ${LDFLAGS}"
GO_LINKMODE ?= ""
GO_LINKMODE_class-nativesdk = "--linkmode=external"
GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} -extldflags '${GO_EXTLDFLAGS}'"'
export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS} -trimpath"
export GOPATH_OMIT_IN_ACTIONID ?= "1"
export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c"
export GOPTESTFLAGS ?= ""
GOBUILDFLAGS_prepend_task-compile = "${GO_PARALLEL_BUILD} "

export GO = "${HOST_PREFIX}go"
GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}"
GOTOOLDIR_class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}"
export GOTOOLDIR

export CGO_ENABLED ?= "1"
export CGO_ENABLED_riscv64 = "0"
export CGO_CFLAGS ?= "${CFLAGS}"
export CGO_CPPFLAGS ?= "${CPPFLAGS}"
export CGO_CXXFLAGS ?= "${CXXFLAGS}"
export CGO_LDFLAGS ?= "${LDFLAGS}"

GO_INSTALL ?= "${GO_IMPORT}/..."
GO_INSTALL_FILTEROUT ?= "${GO_IMPORT}/vendor/"

B = "${WORKDIR}/build"
export GOPATH = "${B}"
export GOTMPDIR ?= "${WORKDIR}/go-tmp"
GOTMPDIR[vardepvalue] = ""

python go_do_unpack() {
src_uri = (d.getVar('SRC_URI') or "").split()
if len(src_uri) == 0:
return

fetcher = bb.fetch2.Fetch(src_uri, d)
for url in fetcher.urls:
if fetcher.ud[url].type == 'git':
if fetcher.ud[url].parm.get('destsuffix') is None:
s_dirname = os.path.basename(d.getVar('S'))
fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src', d.getVar('GO_IMPORT')) + '/'
fetcher.unpack(d.getVar('WORKDIR'))
}

go_list_packages() {
${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
egrep -v '${GO_INSTALL_FILTEROUT}'
}

go_list_package_tests() {
${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \
grep -v '\[\]$' | \
egrep -v '${GO_INSTALL_FILTEROUT}' | \
awk '{ print $1 }'
}

go_do_configure() {
ln -snf ${S}/src ${B}/
}
do_configure[dirs] =+ "${GOTMPDIR}"

go_do_compile() {
export TMPDIR="${GOTMPDIR}"
if [ -n "${GO_INSTALL}" ]; then
if [ -n "${GO_LINKSHARED}" ]; then
${GO} install ${GOBUILDFLAGS} `go_list_packages`
rm -rf ${B}/bin
fi
${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages`
fi
}
do_compile[dirs] =+ "${GOTMPDIR}"
do_compile[cleandirs] = "${B}/bin ${B}/pkg"

go_do_install() {
install -d ${D}${libdir}/go/src/${GO_IMPORT}
tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \
tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf -
tar -C ${B} -cf - --exclude-vcs pkg | tar -C ${D}${libdir}/go --no-same-owner -xf -

if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then
install -d ${D}${bindir}
install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/
fi
}

go_stage_testdata() {
oldwd="$PWD"
cd ${S}/src
find ${GO_IMPORT} -depth -type d -name testdata | while read d; do
if echo "$d" | grep -q '/vendor/'; then
continue
fi
parent=`dirname $d`
install -d ${D}${PTEST_PATH}/$parent
cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/
done
cd "$oldwd"
}

EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install

FILES_${PN}-dev = "${libdir}/go/src"
FILES_${PN}-staticdev = "${libdir}/go/pkg"

INSANE_SKIP_${PN} += "ldflags"

# Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips
# doesn't support -buildmode=pie, so skip the QA checking for mips and its
# variants.
python() {
if 'mips' in d.getVar('TARGET_ARCH') or 'riscv' in d.getVar('TARGET_ARCH'):
d.appendVar('INSANE_SKIP_%s' % d.getVar('PN'), " textrel")
else:
d.appendVar('GOBUILDFLAGS', ' -buildmode=pie')
}
119 changes: 119 additions & 0 deletions classes/goarch.bbclass
@@ -0,0 +1,119 @@
BUILD_GOOS = "${@go_map_os(d.getVar('BUILD_OS'), d)}"
BUILD_GOARCH = "${@go_map_arch(d.getVar('BUILD_ARCH'), d)}"
BUILD_GOTUPLE = "${BUILD_GOOS}_${BUILD_GOARCH}"
HOST_GOOS = "${@go_map_os(d.getVar('HOST_OS'), d)}"
HOST_GOARCH = "${@go_map_arch(d.getVar('HOST_ARCH'), d)}"
HOST_GOARM = "${@go_map_arm(d.getVar('HOST_ARCH'), d)}"
HOST_GO386 = "${@go_map_386(d.getVar('HOST_ARCH'), d.getVar('TUNE_FEATURES'), d)}"
HOST_GOMIPS = "${@go_map_mips(d.getVar('HOST_ARCH'), d.getVar('TUNE_FEATURES'), d)}"
HOST_GOARM_class-native = "7"
HOST_GO386_class-native = "sse2"
HOST_GOMIPS_class-native = "hardfloat"
HOST_GOTUPLE = "${HOST_GOOS}_${HOST_GOARCH}"
TARGET_GOOS = "${@go_map_os(d.getVar('TARGET_OS'), d)}"
TARGET_GOARCH = "${@go_map_arch(d.getVar('TARGET_ARCH'), d)}"
TARGET_GOARM = "${@go_map_arm(d.getVar('TARGET_ARCH'), d)}"
TARGET_GO386 = "${@go_map_386(d.getVar('TARGET_ARCH'), d.getVar('TUNE_FEATURES'), d)}"
TARGET_GOMIPS = "${@go_map_mips(d.getVar('TARGET_ARCH'), d.getVar('TUNE_FEATURES'), d)}"
TARGET_GOARM_class-native = "7"
TARGET_GO386_class-native = "sse2"
TARGET_GOMIPS_class-native = "hardfloat"
TARGET_GOTUPLE = "${TARGET_GOOS}_${TARGET_GOARCH}"
GO_BUILD_BINDIR = "${@['bin/${HOST_GOTUPLE}','bin'][d.getVar('BUILD_GOTUPLE') == d.getVar('HOST_GOTUPLE')]}"

# Use the MACHINEOVERRIDES to map ARM CPU architecture passed to GO via GOARM.
# This is combined with *_ARCH to set HOST_GOARM and TARGET_GOARM.
BASE_GOARM = ''
BASE_GOARM_armv7ve = '7'
BASE_GOARM_armv7a = '7'
BASE_GOARM_armv6 = '6'
BASE_GOARM_armv5 = '5'

# Go supports dynamic linking on a limited set of architectures.
# See the supportsDynlink function in go/src/cmd/compile/internal/gc/main.go
GO_DYNLINK = ""
GO_DYNLINK_arm = "1"
GO_DYNLINK_aarch64 = "1"
GO_DYNLINK_x86 = "1"
GO_DYNLINK_x86-64 = "1"
GO_DYNLINK_powerpc64 = "1"
GO_DYNLINK_powerpc64le = "1"
GO_DYNLINK_class-native = ""
GO_DYNLINK_class-nativesdk = ""

# define here because everybody inherits this class
#
COMPATIBLE_HOST_linux-gnux32 = "null"
COMPATIBLE_HOST_linux-muslx32 = "null"
COMPATIBLE_HOST_powerpc = "null"
COMPATIBLE_HOST_powerpc64 = "null"
COMPATIBLE_HOST_powerpc64le = "null"
COMPATIBLE_HOST_mipsarchn32 = "null"

ARM_INSTRUCTION_SET_armv4 = "arm"
ARM_INSTRUCTION_SET_armv5 = "arm"
ARM_INSTRUCTION_SET_armv6 = "arm"

TUNE_CCARGS_remove = "-march=mips32r2"
SECURITY_NOPIE_CFLAGS ??= ""

# go can't be built with ccache:
# gcc: fatal error: no input files
CCACHE_DISABLE ?= "1"

def go_map_arch(a, d):
import re
if re.match('i.86', a):
return '386'
elif a == 'x86_64':
return 'amd64'
elif re.match('arm.*', a):
return 'arm'
elif re.match('aarch64.*', a):
return 'arm64'
elif re.match('mips64el.*', a):
return 'mips64le'
elif re.match('mips64.*', a):
return 'mips64'
elif a == 'mips':
return 'mips'
elif a == 'mipsel':
return 'mipsle'
elif re.match('p(pc|owerpc)(64)', a):
return 'ppc64'
elif re.match('p(pc|owerpc)(64el)', a):
return 'ppc64le'
elif a == 'riscv64':
return 'riscv64'
else:
raise bb.parse.SkipRecipe("Unsupported CPU architecture: %s" % a)

def go_map_arm(a, d):
if a.startswith("arm"):
return d.getVar('BASE_GOARM')
return ''

def go_map_386(a, f, d):
import re
if re.match('i.86', a):
if ('core2' in f) or ('corei7' in f):
return 'sse2'
else:
return '387'
return ''

def go_map_mips(a, f, d):
import re
if a == 'mips' or a == 'mipsel':
if 'fpu-hard' in f:
return 'hardfloat'
else:
return 'softfloat'
return ''

def go_map_os(o, d):
if o.startswith('linux'):
return 'linux'
return o


7 changes: 7 additions & 0 deletions classes/gobject-introspection-data.bbclass
@@ -0,0 +1,7 @@
# This variable is set to True if gobject-introspection-data is in
# DISTRO_FEATURES and qemu-usermode is in MACHINE_FEATURES, and False otherwise.
#
# It should be used in recipes to determine whether introspection data should be built,
# so that qemu use can be avoided when necessary.
GI_DATA_ENABLED ?= "${@bb.utils.contains('DISTRO_FEATURES', 'gobject-introspection-data', \
bb.utils.contains('MACHINE_FEATURES', 'qemu-usermode', 'True', 'False', d), 'False', d)}"