Index: Mk/Uses/go.mk =================================================================== --- Mk/Uses/go.mk +++ Mk/Uses/go.mk @@ -93,6 +93,9 @@ GOARM?= ${ARCH:C/armv//} .endif +GO_GOPROXY?= https://proxy.golang.org +GO_GOSUMDB?= sum.golang.org + # Read-only variables GO_CMD= ${LOCALBASE}/bin/go @@ -103,14 +106,31 @@ GOARM=${GOARM} .if ${go_ARGS:Mmodules} -GO_BUILDFLAGS+= -mod=vendor -GO_TESTFLAGS+= -mod=vendor GO_WRKSRC= ${WRKSRC} -GO_ENV+= GOPATH="" \ +GO_ENV+= GOPATH="${DISTDIR}/go/${PKGORIGIN}" \ GOBIN="${GO_WRKDIR_BIN}" \ GO111MODULE=on \ - GOPROXY=off \ - GO_NO_VENDOR_CHECKS=1 + GOFLAGS=-modcacherw \ + GOSUMDB=${GO_GOSUMDB} +. if defined(GO_MODULE) +GO_BUILDFLAGS+= -mod=readonly +GO_TESTFLAGS+= -mod=readonly +GO_MODNAME= ${GO_MODULE:C/^([^@]*)(@([^@]*)?)/\1/} +GO_MODVERSION= ${GO_MODULE:C/^([^@]*)(@([^@]*)?)/\2/:M@*:S/^@//:S/^$/${DISTVERSIONFULL}/} +GO_MODFILE= ${GO_MODVERSION}.mod +GO_DISTFILE= ${GO_MODVERSION}.zip +DIST_SUBDIR= go/${PKGORIGIN}/${DISTNAME} +MASTER_SITES= ${GO_GOPROXY}/${GO_MODNAME}/@v/ +DISTFILES= ${GO_MODFILE} ${GO_DISTFILE} +EXTRACT_ONLY= ${GO_DISTFILE} +WRKSRC= ${WRKDIR}/${GO_MODNAME}@${GO_MODVERSION} +USES+= zip +. else +GO_BUILDFLAGS+= -mod=vendor +GO_TESTFLAGS+= -mod=vendor +GO_ENV+= GO_NO_VENDOR_CHECKS=1 \ + GOPROXY=off +. endif .else GO_WRKSRC= ${WRKDIR}/src/${GO_PKGNAME} GO_ENV+= GOPATH="${WRKDIR}" \ @@ -121,6 +141,7 @@ GO_PORT?= lang/go BUILD_DEPENDS+= ${GO_CMD}:${GO_PORT} +FETCH_DEPENDS+= ${GO_CMD}:${GO_PORT} .if ${go_ARGS:Mrun} RUN_DEPENDS+= ${GO_CMD}:${GO_PORT} .endif @@ -131,6 +152,14 @@ .if defined(_POSTMKINCLUDED) && !defined(_INCLUDE_USES_GO_POST_MK) _INCLUDE_USES_GO_POST_MK= yes +.if !target(post-fetch) && ${go_ARGS:Mmodules} && defined(GO_MODULE) +post-fetch: + @${ECHO_MSG} "===> Fetching ${GO_MODNAME} dependencies"; + @(cd ${DISTDIR}/${DIST_SUBDIR}; \ + ${RLN} ${GO_MODFILE} go.mod; \ + ${SETENV} ${GO_ENV} ${GO_CMD} mod download -x) +.endif + .if !target(post-extract) && empty(go_ARGS) post-extract: @${MKDIR} ${GO_WRKSRC:H} @@ -176,6 +205,16 @@ done) .endif +gomod-clean: + @${ECHO_MSG} "===> Cleaning Go module cache" + @${SETENV} ${GO_ENV} ${GO_CMD} clean -modcache + +# Hook up to distclean +.if !make(clean) +post-clean: gomod-clean + @${RM} -r ${DISTDIR}/${DIST_SUBDIR} +.endif + # Helper targets for port maintainers .if ${go_ARGS:Mmodules} @@ -189,11 +228,11 @@ fi gomod-vendor: gomod-vendor-deps patch - @cd ${WRKSRC}; ${SETENV} GOPATH=${WRKDIR}/.gopath GOFLAGS=-modcacherw ${GO_CMD} mod vendor; \ + @cd ${WRKSRC}; ${SETENV} ${GO_ENV:NGOPROXY*} ${GO_CMD} mod vendor; \ [ -r vendor/modules.txt ] && ${_MODULES2TUPLE_CMD} vendor/modules.txt gomod-vendor-diff: gomod-vendor-deps patch - @cd ${WRKSRC}; ${SETENV} GOPATH=${WRKDIR}/.gopath GOFLAGS=-modcacherw ${GO_CMD} mod vendor; \ + @cd ${WRKSRC}; ${SETENV} ${GO_ENV:NGOPROXY*} ${GO_CMD} mod vendor; \ [ -r vendor/modules.txt ] && ${_MODULES2TUPLE_CMD} vendor/modules.txt | ${SED} 's|GH_TUPLE=| |; s| \\$$||' | ${GREP} -v ' \\' > ${WRKDIR}/GH_TUPLE-new.txt && \ echo ${GH_TUPLE} | ${TR} -s " " "\n" | ${SED} "s|^| |" > ${WRKDIR}/GH_TUPLE-old.txt && \ ${DIFF} ${WRKDIR}/GH_TUPLE-old.txt ${WRKDIR}/GH_TUPLE-new.txt || exit 0