Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | inherit goarch |
| 8 | inherit linuxloader |
| 9 | |
| 10 | GO_PARALLEL_BUILD ?= "${@oe.utils.parallel_make_argument(d, '-p %d')}" |
| 11 | |
| 12 | export GODEBUG = "gocachehash=1" |
| 13 | |
| 14 | GOROOT:class-native = "${STAGING_LIBDIR_NATIVE}/go" |
| 15 | GOROOT:class-nativesdk = "${STAGING_DIR_TARGET}${libdir}/go" |
| 16 | GOROOT = "${STAGING_LIBDIR}/go" |
| 17 | export GOROOT |
| 18 | export GOROOT_FINAL = "${libdir}/go" |
| 19 | export GOCACHE = "${B}/.cache" |
| 20 | |
| 21 | export GOARCH = "${TARGET_GOARCH}" |
| 22 | export GOOS = "${TARGET_GOOS}" |
| 23 | export GOHOSTARCH="${BUILD_GOARCH}" |
| 24 | export GOHOSTOS="${BUILD_GOOS}" |
| 25 | |
| 26 | GOARM[export] = "0" |
| 27 | GOARM:arm:class-target = "${TARGET_GOARM}" |
| 28 | GOARM:arm:class-target[export] = "1" |
| 29 | |
| 30 | GO386[export] = "0" |
| 31 | GO386:x86:class-target = "${TARGET_GO386}" |
| 32 | GO386:x86:class-target[export] = "1" |
| 33 | |
| 34 | GOMIPS[export] = "0" |
| 35 | GOMIPS:mips:class-target = "${TARGET_GOMIPS}" |
| 36 | GOMIPS:mips:class-target[export] = "1" |
| 37 | |
| 38 | DEPENDS_GOLANG:class-target = "virtual/${TUNE_PKGARCH}-go virtual/${TARGET_PREFIX}go-runtime" |
| 39 | DEPENDS_GOLANG:class-native = "go-native" |
Andrew Geissler | 028142b | 2023-05-05 11:29:21 -0500 | [diff] [blame] | 40 | DEPENDS_GOLANG:class-nativesdk = "virtual/${TARGET_PREFIX}go virtual/${TARGET_PREFIX}go-runtime" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 41 | |
| 42 | DEPENDS:append = " ${DEPENDS_GOLANG}" |
| 43 | |
| 44 | GO_LINKSHARED ?= "${@'-linkshared' if d.getVar('GO_DYNLINK') else ''}" |
| 45 | GO_RPATH_LINK = "${@'-Wl,-rpath-link=${STAGING_DIR_TARGET}${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}" |
| 46 | GO_RPATH = "${@'-r ${libdir}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}" |
| 47 | GO_RPATH:class-native = "${@'-r ${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}" |
| 48 | GO_RPATH_LINK:class-native = "${@'-Wl,-rpath-link=${STAGING_LIBDIR_NATIVE}/go/pkg/${TARGET_GOTUPLE}_dynlink' if d.getVar('GO_DYNLINK') else ''}" |
| 49 | GO_EXTLDFLAGS ?= "${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS} ${GO_RPATH_LINK} ${LDFLAGS}" |
| 50 | GO_LINKMODE ?= "" |
| 51 | GO_LINKMODE:class-nativesdk = "--linkmode=external" |
| 52 | GO_LINKMODE:class-native = "--linkmode=external" |
| 53 | GO_EXTRA_LDFLAGS ?= "" |
| 54 | GO_LINUXLOADER ?= "-I ${@get_linuxloader(d)}" |
| 55 | # Use system loader. If uninative is used, the uninative loader will be patched automatically |
| 56 | GO_LINUXLOADER:class-native = "" |
| 57 | GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} ${GO_LINUXLOADER} ${GO_EXTRA_LDFLAGS} -extldflags '${GO_EXTLDFLAGS}'"' |
| 58 | export GOBUILDFLAGS ?= "-v ${GO_LDFLAGS} -trimpath" |
| 59 | export GOPATH_OMIT_IN_ACTIONID ?= "1" |
| 60 | export GOPTESTBUILDFLAGS ?= "${GOBUILDFLAGS} -c" |
| 61 | export GOPTESTFLAGS ?= "" |
| 62 | GOBUILDFLAGS:prepend:task-compile = "${GO_PARALLEL_BUILD} " |
| 63 | |
| 64 | export GO = "${HOST_PREFIX}go" |
| 65 | GOTOOLDIR = "${STAGING_LIBDIR_NATIVE}/${TARGET_SYS}/go/pkg/tool/${BUILD_GOTUPLE}" |
| 66 | GOTOOLDIR:class-native = "${STAGING_LIBDIR_NATIVE}/go/pkg/tool/${BUILD_GOTUPLE}" |
| 67 | export GOTOOLDIR |
| 68 | |
| 69 | export CGO_ENABLED ?= "1" |
| 70 | export CGO_CFLAGS ?= "${CFLAGS}" |
| 71 | export CGO_CPPFLAGS ?= "${CPPFLAGS}" |
| 72 | export CGO_CXXFLAGS ?= "${CXXFLAGS}" |
| 73 | export CGO_LDFLAGS ?= "${LDFLAGS}" |
| 74 | |
| 75 | GO_INSTALL ?= "${GO_IMPORT}/..." |
| 76 | GO_INSTALL_FILTEROUT ?= "${GO_IMPORT}/vendor/" |
| 77 | |
| 78 | B = "${WORKDIR}/build" |
| 79 | export GOPATH = "${B}" |
| 80 | export GOENV = "off" |
| 81 | export GOTMPDIR ?= "${WORKDIR}/build-tmp" |
| 82 | GOTMPDIR[vardepvalue] = "" |
| 83 | |
| 84 | python go_do_unpack() { |
| 85 | src_uri = (d.getVar('SRC_URI') or "").split() |
| 86 | if len(src_uri) == 0: |
| 87 | return |
| 88 | |
| 89 | fetcher = bb.fetch2.Fetch(src_uri, d) |
| 90 | for url in fetcher.urls: |
| 91 | if fetcher.ud[url].type == 'git': |
| 92 | if fetcher.ud[url].parm.get('destsuffix') is None: |
| 93 | s_dirname = os.path.basename(d.getVar('S')) |
| 94 | fetcher.ud[url].parm['destsuffix'] = os.path.join(s_dirname, 'src', d.getVar('GO_IMPORT')) + '/' |
| 95 | fetcher.unpack(d.getVar('WORKDIR')) |
| 96 | } |
| 97 | |
| 98 | go_list_packages() { |
| 99 | ${GO} list -f '{{.ImportPath}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \ |
| 100 | egrep -v '${GO_INSTALL_FILTEROUT}' |
| 101 | } |
| 102 | |
| 103 | go_list_package_tests() { |
| 104 | ${GO} list -f '{{.ImportPath}} {{.TestGoFiles}}' ${GOBUILDFLAGS} ${GO_INSTALL} | \ |
| 105 | grep -v '\[\]$' | \ |
| 106 | egrep -v '${GO_INSTALL_FILTEROUT}' | \ |
| 107 | awk '{ print $1 }' |
| 108 | } |
| 109 | |
| 110 | go_do_configure() { |
| 111 | ln -snf ${S}/src ${B}/ |
| 112 | } |
| 113 | do_configure[dirs] =+ "${GOTMPDIR}" |
| 114 | |
| 115 | go_do_compile() { |
| 116 | export TMPDIR="${GOTMPDIR}" |
| 117 | if [ -n "${GO_INSTALL}" ]; then |
| 118 | if [ -n "${GO_LINKSHARED}" ]; then |
| 119 | ${GO} install ${GOBUILDFLAGS} `go_list_packages` |
| 120 | rm -rf ${B}/bin |
| 121 | fi |
| 122 | ${GO} install ${GO_LINKSHARED} ${GOBUILDFLAGS} `go_list_packages` |
| 123 | fi |
| 124 | } |
| 125 | do_compile[dirs] =+ "${GOTMPDIR}" |
| 126 | do_compile[cleandirs] = "${B}/bin ${B}/pkg" |
| 127 | |
| 128 | go_do_install() { |
| 129 | install -d ${D}${libdir}/go/src/${GO_IMPORT} |
| 130 | tar -C ${S}/src/${GO_IMPORT} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' . | \ |
| 131 | tar -C ${D}${libdir}/go/src/${GO_IMPORT} --no-same-owner -xf - |
| 132 | tar -C ${B} -cf - --exclude-vcs --exclude '*.test' --exclude 'testdata' pkg | \ |
| 133 | tar -C ${D}${libdir}/go --no-same-owner -xf - |
| 134 | |
| 135 | if [ -n "`ls ${B}/${GO_BUILD_BINDIR}/`" ]; then |
| 136 | install -d ${D}${bindir} |
| 137 | install -m 0755 ${B}/${GO_BUILD_BINDIR}/* ${D}${bindir}/ |
| 138 | fi |
| 139 | } |
| 140 | |
| 141 | go_stage_testdata() { |
| 142 | oldwd="$PWD" |
| 143 | cd ${S}/src |
| 144 | find ${GO_IMPORT} -depth -type d -name testdata | while read d; do |
| 145 | if echo "$d" | grep -q '/vendor/'; then |
| 146 | continue |
| 147 | fi |
| 148 | parent=`dirname $d` |
| 149 | install -d ${D}${PTEST_PATH}/$parent |
| 150 | cp --preserve=mode,timestamps -R $d ${D}${PTEST_PATH}/$parent/ |
| 151 | done |
| 152 | cd "$oldwd" |
| 153 | } |
| 154 | |
| 155 | EXPORT_FUNCTIONS do_unpack do_configure do_compile do_install |
| 156 | |
| 157 | FILES:${PN}-dev = "${libdir}/go/src" |
| 158 | FILES:${PN}-staticdev = "${libdir}/go/pkg" |
| 159 | |
| 160 | INSANE_SKIP:${PN} += "ldflags" |
| 161 | |
| 162 | # Add -buildmode=pie to GOBUILDFLAGS to satisfy "textrel" QA checking, but mips |
| 163 | # doesn't support -buildmode=pie, so skip the QA checking for mips/rv32 and its |
| 164 | # variants. |
| 165 | python() { |
| 166 | if 'mips' in d.getVar('TARGET_ARCH') or 'riscv32' in d.getVar('TARGET_ARCH'): |
| 167 | d.appendVar('INSANE_SKIP:%s' % d.getVar('PN'), " textrel") |
| 168 | else: |
| 169 | d.appendVar('GOBUILDFLAGS', ' -buildmode=pie') |
| 170 | } |