Skip to content

Commit

Permalink
Merge pull request #128 from yunxuanhao/master
Browse files Browse the repository at this point in the history
add hidewindow support
  • Loading branch information
crazy-max authored Aug 16, 2024
2 parents fcf4da2 + c0e74a9 commit 3719267
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
1 change: 1 addition & 0 deletions doc/usage/build-flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ A handful of flags can be passed to `go build`. The currently supported ones are
* `-buildmode=<mode>`: binary type to produce by the compiler
* `-buildvcs=<value>`: whether to stamp binaries with version control information
* `-trimpath`: remove all file system paths from the resulting executable
* `-hidewindow`: hide the console window during GUI execution on Windows
40 changes: 21 additions & 19 deletions rootfs/usr/local/bin/xgo-build
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
# Usage: xgo-build <import path>
#
# Needed environment variables:
# REPO_REMOTE - Optional VCS remote if not the primary repository is needed
# REPO_BRANCH - Optional VCS branch to use, if not the master branch
# DEPS - Optional list of C dependency packages to build
# ARGS - Optional arguments to pass to C dependency configure scripts
# PACK - Optional sub-package, if not the import path is being built
# OUT - Optional output prefix to override the package name
# FLAG_V - Optional verbosity flag to set on the Go builder
# FLAG_X - Optional flag to print the build progress commands
# FLAG_RACE - Optional race flag to set on the Go builder
# FLAG_TAGS - Optional tag flag to set on the Go builder
# FLAG_LDFLAGS - Optional ldflags flag to set on the Go builder
# FLAG_BUILDMODE - Optional buildmode flag to set on the Go builder
# FLAG_BUILDVCS - Optional buildvcs flag to set on the Go builder
# FLAG_TRIMPATH - Optional trimpath flag to remove all file system paths
# TARGETS - Comma separated list of build targets to compile for
# GO_VERSION - Bootstrapped version of Go to disable uncupported targets
# EXT_GOPATH - GOPATH elements mounted from the host filesystem
# REPO_REMOTE - Optional VCS remote if not the primary repository is needed
# REPO_BRANCH - Optional VCS branch to use, if not the master branch
# DEPS - Optional list of C dependency packages to build
# ARGS - Optional arguments to pass to C dependency configure scripts
# PACK - Optional sub-package, if not the import path is being built
# OUT - Optional output prefix to override the package name
# FLAG_V - Optional verbosity flag to set on the Go builder
# FLAG_X - Optional flag to print the build progress commands
# FLAG_RACE - Optional race flag to set on the Go builder
# FLAG_TAGS - Optional tag flag to set on the Go builder
# FLAG_LDFLAGS - Optional ldflags flag to set on the Go builder
# FLAG_BUILDMODE - Optional buildmode flag to set on the Go builder
# FLAG_BUILDVCS - Optional buildvcs flag to set on the Go builder
# FLAG_TRIMPATH - Optional trimpath flag to remove all file system paths
# FLAG_HIDEWINDOW - Optional flag to hide the console window on Windows
# TARGETS - Comma separated list of build targets to compile for
# GO_VERSION - Bootstrapped version of Go to disable uncupported targets
# EXT_GOPATH - GOPATH elements mounted from the host filesystem

# Define a function that figures out the binary extension
function extension {
Expand Down Expand Up @@ -183,6 +184,7 @@ if [ "$FLAG_RACE" == "true" ]; then R=-race; fi
if [ "$FLAG_TAGS" != "" ]; then T=(--tags "$FLAG_TAGS"); fi
if [ "$FLAG_LDFLAGS" != "" ]; then LD="$FLAG_LDFLAGS"; fi
if [ "$FLAG_TRIMPATH" == "true" ]; then TP=-trimpath; fi
if [ "$FLAG_HIDEWINDOW" == "true" ]; then H="-H=windowsgui"; fi

if [ "$FLAG_BUILDMODE" != "" ] && [ "$FLAG_BUILDMODE" != "default" ]; then BM="--buildmode=$FLAG_BUILDMODE"; fi
if [ "$(semver compare "$GO_VERSION" "1.18.0")" -ge 0 ] && [ "$FLAG_BUILDVCS" != "" ]; then VCS="-buildvcs=$FLAG_BUILDVCS"; fi
Expand Down Expand Up @@ -441,7 +443,7 @@ for TARGET in $TARGETS; do
CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go get $V $X $TP $VCS "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
fi
ext=$(extension windows)
(set -x ; CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X $TP $VCS $MOD "${T[@]}" --ldflags="$V $LD" $R $BM -o "/build/$NAME-windows-amd64$R$ext" $PACK_RELPATH)
(set -x ; CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X $TP $VCS $MOD "${T[@]}" --ldflags="$V $H $LD" $R $BM -o "/build/$NAME-windows-amd64$R$ext" $PACK_RELPATH)
fi
if [ $XGOARCH == "." ] || [ $XGOARCH == "386" ]; then
echo "Compiling for windows$PLATFORM_SUFFIX/386..."
Expand All @@ -452,7 +454,7 @@ for TARGET in $TARGETS; do
CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ GOOS=windows GOARCH=386 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go get $V $X $TP $VCS "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
fi
ext=$(extension windows)
(set -x ; CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ GOOS=windows GOARCH=386 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X $TP $VCS $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-windows-386$ext" $PACK_RELPATH)
(set -x ; CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ GOOS=windows GOARCH=386 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X $TP $VCS $MOD "${T[@]}" --ldflags="$V $H $LD" $BM -o "/build/$NAME-windows-386$ext" $PACK_RELPATH)
fi
# FIXME: gcc_libinit_windows.c:8:10: fatal error: 'windows.h' file not found
# if [ $XGOARCH == "." ] || [ $XGOARCH == "arm64" ]; then
Expand Down
52 changes: 28 additions & 24 deletions xgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,28 @@ type ConfigFlags struct {

// Command line arguments to pass to go build
var (
buildVerbose = flag.Bool("v", false, "Print the names of packages as they are compiled")
buildSteps = flag.Bool("x", false, "Print the command as executing the builds")
buildRace = flag.Bool("race", false, "Enable data race detection (supported only on amd64)")
buildTags = flag.String("tags", "", "List of build tags to consider satisfied during the build")
buildLdFlags = flag.String("ldflags", "", "Arguments to pass on each go tool link invocation")
buildMode = flag.String("buildmode", "default", "Indicates which kind of object file to build")
buildVCS = flag.String("buildvcs", "", "Whether to stamp binaries with version control information")
buildTrimPath = flag.Bool("trimpath", false, "Remove all file system paths from the resulting executable")
buildVerbose = flag.Bool("v", false, "Print the names of packages as they are compiled")
buildSteps = flag.Bool("x", false, "Print the command as executing the builds")
buildRace = flag.Bool("race", false, "Enable data race detection (supported only on amd64)")
buildTags = flag.String("tags", "", "List of build tags to consider satisfied during the build")
buildLdFlags = flag.String("ldflags", "", "Arguments to pass on each go tool link invocation")
buildMode = flag.String("buildmode", "default", "Indicates which kind of object file to build")
buildVCS = flag.String("buildvcs", "", "Whether to stamp binaries with version control information")
buildTrimPath = flag.Bool("trimpath", false, "Remove all file system paths from the resulting executable")
buildHideWindow = flag.Bool("hidewindow", false, "Optional flag to hide the console window on Windows")
)

// BuildFlags is a simple collection of flags to fine tune a build.
type BuildFlags struct {
Verbose bool // Print the names of packages as they are compiled
Steps bool // Print the command as executing the builds
Race bool // Enable data race detection (supported only on amd64)
Tags string // List of build tags to consider satisfied during the build
LdFlags string // Arguments to pass on each go tool link invocation
Mode string // Indicates which kind of object file to build
VCS string // Whether to stamp binaries with version control information
TrimPath bool // Remove all file system paths from the resulting executable
Verbose bool // Print the names of packages as they are compiled
Steps bool // Print the command as executing the builds
Race bool // Enable data race detection (supported only on amd64)
Tags string // List of build tags to consider satisfied during the build
LdFlags string // Arguments to pass on each go tool link invocation
Mode string // Indicates which kind of object file to build
VCS string // Whether to stamp binaries with version control information
TrimPath bool // Remove all file system paths from the resulting executable
HideWindow bool // Hide the console window on Windows
}

func main() {
Expand Down Expand Up @@ -163,14 +165,15 @@ func main() {
}
log.Printf("DBG: config: %+v", config)
flags := &BuildFlags{
Verbose: *buildVerbose,
Steps: *buildSteps,
Race: *buildRace,
Tags: *buildTags,
LdFlags: *buildLdFlags,
Mode: *buildMode,
VCS: *buildVCS,
TrimPath: *buildTrimPath,
Verbose: *buildVerbose,
Steps: *buildSteps,
Race: *buildRace,
Tags: *buildTags,
LdFlags: *buildLdFlags,
Mode: *buildMode,
VCS: *buildVCS,
TrimPath: *buildTrimPath,
HideWindow: *buildHideWindow,
}
log.Printf("DBG: flags: %+v", flags)
folder, err := os.Getwd()
Expand Down Expand Up @@ -311,6 +314,7 @@ func compile(image string, config *ConfigFlags, flags *BuildFlags, folder string
"-e", fmt.Sprintf("FLAG_BUILDMODE=%s", flags.Mode),
"-e", fmt.Sprintf("FLAG_BUILDVCS=%s", flags.VCS),
"-e", fmt.Sprintf("FLAG_TRIMPATH=%v", flags.TrimPath),
"-e", fmt.Sprintf("FLAG_HIDEWINDOW=%v", flags.HideWindow),
"-e", "TARGETS=" + strings.Replace(strings.Join(config.Targets, " "), "*", ".", -1),
}
if usesModules {
Expand Down

0 comments on commit 3719267

Please sign in to comment.