From 41df5aeb7f02a47ff7ccf5002140b70b04a4fd46 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 8 May 2019 21:28:14 +0000 Subject: [PATCH] cmd/go: set the "generate" build tag in go generate, per design doc And use it in two internal windows packages, so they don't show up in "go list std" or binary releases on non-Windows platforms. Fixes #31920 Change-Id: Iaa292b6015c9d7310dd677c9e296006440ba5e27 Reviewed-on: https://go-review.googlesource.com/c/go/+/175983 Reviewed-by: Rob Pike Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/cmd/go/alldocs.go | 4 +++- src/cmd/go/internal/generate/generate.go | 7 ++++++- src/internal/syscall/windows/mksyscall.go | 2 ++ src/internal/syscall/windows/registry/mksyscall.go | 2 ++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 051b3daef9e..0ea0bad9a91 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -525,7 +525,9 @@ // they are treated as a single package. Within a package, generate processes the // source files in a package in file name order, one at a time. Within // a source file, generate runs generators in the order they appear -// in the file, one at a time. +// in the file, one at a time. The go generate tool also sets the build +// tag "generate" so that files may be examined by go generate but ignored +// during build. // // If any generator returns an error exit status, "go generate" skips // all further processing for that package. diff --git a/src/cmd/go/internal/generate/generate.go b/src/cmd/go/internal/generate/generate.go index 19597c7a338..f2ae80e5dc6 100644 --- a/src/cmd/go/internal/generate/generate.go +++ b/src/cmd/go/internal/generate/generate.go @@ -114,7 +114,9 @@ one at a time. If the command line lists .go files from a single directory, they are treated as a single package. Within a package, generate processes the source files in a package in file name order, one at a time. Within a source file, generate runs generators in the order they appear -in the file, one at a time. +in the file, one at a time. The go generate tool also sets the build +tag "generate" so that files may be examined by go generate but ignored +during build. If any generator returns an error exit status, "go generate" skips all further processing for that package. @@ -161,6 +163,9 @@ func runGenerate(cmd *base.Command, args []string) { log.Fatalf("generate: %s", err) } } + + cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "generate") + // Even if the arguments are .go files, this loop suffices. printed := false for _, pkg := range load.Packages(args) { diff --git a/src/internal/syscall/windows/mksyscall.go b/src/internal/syscall/windows/mksyscall.go index a8edafb3c3b..0bf87dc95c2 100644 --- a/src/internal/syscall/windows/mksyscall.go +++ b/src/internal/syscall/windows/mksyscall.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build generate + package windows //go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zsyscall_windows.go syscall_windows.go security_windows.go psapi_windows.go symlink_windows.go diff --git a/src/internal/syscall/windows/registry/mksyscall.go b/src/internal/syscall/windows/registry/mksyscall.go index 077215351ef..fa8e27e8ff6 100644 --- a/src/internal/syscall/windows/registry/mksyscall.go +++ b/src/internal/syscall/windows/registry/mksyscall.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build generate + package registry //go:generate go run $GOROOT/src/syscall/mksyscall_windows.go -output zsyscall_windows.go syscall.go