Index: Mk/Uses/go.mk =================================================================== --- Mk/Uses/go.mk +++ Mk/Uses/go.mk @@ -127,6 +127,8 @@ . if defined(GO_MODULE) GO_MODNAME= ${GO_MODULE:C/^([^@]*)(@([^@]*)?)/\1/} . if empty(DISTFILES:Mgo.mod*) +# Unless already setup for download by other means, +# arrange to pull go.mod and the distribution archive from GOPROXY. GO_MODVERSION= ${GO_MODULE:C/^([^@]*)(@([^@]*)?)/\2/:M@*:S/^@//:S/^$/${DISTVERSIONFULL}/} GO_MODFILE= ${GO_MODVERSION}.mod GO_DISTFILE= ${GO_MODVERSION}.zip @@ -161,8 +163,17 @@ .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: +. if ${go_ARGS:Mmodules} && defined(GO_MODULE) +_USES_fetch+= 200:go-pre-fetch 800:go-post-fetch +# Check that pkg and go can be installed or are already available, +# otherwise it will be impossible to fetch dependencies. +go-pre-fetch: +. if defined(CLEAN_FETCH_ENV) && !exists(${PKG_BIN}) + @${ECHO_MSG} "===> CLEAN_FETCH_ENV is defined, cannot download Go modules (pkg and go are required)"; \ + exit 1 +. endif +# Download all required build dependencies to GOMODCACHE. +go-post-fetch: @${ECHO_MSG} "===> Fetching ${GO_MODNAME} dependencies"; @(cd ${DISTDIR}/${DIST_SUBDIR}; \ [ -e go.mod ] || ${RLN} ${GO_MODFILE} go.mod; \ @@ -170,15 +181,17 @@ ${SETENV} ${GO_ENV} GOPROXY=${GO_GOPROXY} ${GO_CMD} mod verify) . endif -. if !target(post-extract) -. if empty(go_ARGS) -post-extract: +_USES_extract+= 800:go-post-extract +. if empty(go_ARGS) +# Legacy (GOPATH) build mode, setup directory structure expected by Go for the main module. +go-post-extract: @${MKDIR} ${GO_WRKSRC:H} @${LN} -sf ${WRKSRC} ${GO_WRKSRC} -. elif ${go_ARGS:Mmodules} && defined(GO_MODULE) -post-extract: +. elif ${go_ARGS:Mmodules} && defined(GO_MODULE) +# Module-aware build mode. Although not strictly necessary (all build dependencies should be +# already in MODCACHE), vendor them so we can patch them if needed. +go-post-extract: @(cd ${GO_WRKSRC}; ${SETENV} ${GO_ENV} GOPROXY=off ${GO_CMD} mod vendor -e) -. endif . endif . if !target(do-build) && empty(go_ARGS:Mno_targets)