Override platform with GOOS and GOARCH (#1618)

* Override platform with GOOS and GOARCH

* Update v2/cmd/wails/internal/commands/build/build.go

Co-authored-by: stffabi <stffabi@users.noreply.github.com>

* Only use GOOS and GOARCH env vars for default target

* Update docs. Add dryrun flag for testing

* Update v2/cmd/wails/internal/commands/build/build.go

Co-authored-by: stffabi <stffabi@users.noreply.github.com>

* Fix scenario where no arch specified

* Fixed banner

Co-authored-by: stffabi <stffabi@users.noreply.github.com>
This commit is contained in:
Lea Anthony
2022-07-21 21:25:57 +10:00
committed by GitHub
co-authored by stffabi
parent ecdda0154f
commit 5a3d45b535
4 changed files with 77 additions and 57 deletions
+63 -49
View File
@@ -49,13 +49,19 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
compressFlags := ""
command.StringFlag("upxflags", "Flags to pass to upx", &compressFlags)
// Setup Platform flag
platform := runtime.GOOS + "/"
if system.IsAppleSilicon {
platform += "arm64"
} else {
platform += runtime.GOARCH
defaultPlatform := os.Getenv("GOOS")
if defaultPlatform == "" {
defaultPlatform = runtime.GOOS
}
defaultArch := os.Getenv("GOARCH")
if defaultArch == "" {
if system.IsAppleSilicon {
defaultArch = "arm64"
} else {
defaultArch = runtime.GOARCH
}
}
platform := defaultPlatform + "/" + defaultArch
command.StringFlag("platform", "Platform to target. Comma separate multiple platforms", &platform)
@@ -105,6 +111,9 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
windowsConsole := false
command.BoolFlag("windowsconsole", "Keep the console when building for Windows", &windowsConsole)
dryRun := false
command.BoolFlag("dryrun", "Dry run, prints the config for the command that would be executed", &dryRun)
command.Action(func() error {
quiet := verbosity == 0
@@ -191,30 +200,31 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
}
// Start a new tabwriter
w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
if !quiet {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
// Write out the system information
_, _ = fmt.Fprintf(w, "App Type: \t%s\n", buildOptions.OutputType)
_, _ = fmt.Fprintf(w, "Platforms: \t%s\n", platform)
_, _ = fmt.Fprintf(w, "Compiler: \t%s\n", compilerPath)
_, _ = fmt.Fprintf(w, "Build Mode: \t%s\n", modeString)
_, _ = fmt.Fprintf(w, "Skip Frontend: \t%t\n", skipFrontend)
_, _ = fmt.Fprintf(w, "Compress: \t%t\n", buildOptions.Compress)
_, _ = fmt.Fprintf(w, "Package: \t%t\n", buildOptions.Pack)
_, _ = fmt.Fprintf(w, "Clean Build Dir: \t%t\n", buildOptions.CleanBuildDirectory)
_, _ = fmt.Fprintf(w, "LDFlags: \t\"%s\"\n", buildOptions.LDFlags)
_, _ = fmt.Fprintf(w, "Tags: \t[%s]\n", strings.Join(buildOptions.UserTags, ","))
_, _ = fmt.Fprintf(w, "Race Detector: \t%t\n", buildOptions.RaceDetector)
if len(buildOptions.OutputFile) > 0 && targets.Length() == 1 {
_, _ = fmt.Fprintf(w, "Output File: \t%s\n", buildOptions.OutputFile)
// Write out the system information
_, _ = fmt.Fprintf(w, "App Type: \t%s\n", buildOptions.OutputType)
_, _ = fmt.Fprintf(w, "Platforms: \t%s\n", platform)
_, _ = fmt.Fprintf(w, "Compiler: \t%s\n", compilerPath)
_, _ = fmt.Fprintf(w, "Build Mode: \t%s\n", modeString)
_, _ = fmt.Fprintf(w, "Skip Frontend: \t%t\n", skipFrontend)
_, _ = fmt.Fprintf(w, "Compress: \t%t\n", buildOptions.Compress)
_, _ = fmt.Fprintf(w, "Package: \t%t\n", buildOptions.Pack)
_, _ = fmt.Fprintf(w, "Clean Build Dir: \t%t\n", buildOptions.CleanBuildDirectory)
_, _ = fmt.Fprintf(w, "LDFlags: \t\"%s\"\n", buildOptions.LDFlags)
_, _ = fmt.Fprintf(w, "Tags: \t[%s]\n", strings.Join(buildOptions.UserTags, ","))
_, _ = fmt.Fprintf(w, "Race Detector: \t%t\n", buildOptions.RaceDetector)
if len(buildOptions.OutputFile) > 0 && targets.Length() == 1 {
_, _ = fmt.Fprintf(w, "Output File: \t%s\n", buildOptions.OutputFile)
}
_, _ = fmt.Fprintf(w, "\n")
err = w.Flush()
if err != nil {
return err
}
}
_, _ = fmt.Fprintf(w, "\n")
err = w.Flush()
if err != nil {
return err
}
err = checkGoModVersion(logger, updateGoMod)
if err != nil {
return err
@@ -269,15 +279,11 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
// Calculate platform and arch
platformSplit := strings.Split(platform, "/")
buildOptions.Platform = platformSplit[0]
buildOptions.Arch = runtime.GOARCH
if system.IsAppleSilicon {
buildOptions.Arch = "arm64"
}
if len(platformSplit) == 2 {
buildOptions.Arch = defaultArch
if len(platformSplit) > 1 {
buildOptions.Arch = platformSplit[1]
}
banner := "Building target: " + platform
banner := "Building target: " + buildOptions.Platform + "/" + buildOptions.Arch
logger.Println(banner)
logger.Println(strings.Repeat("-", len(banner)))
@@ -323,22 +329,26 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
buildOptions.OutputFile = outputFilename
}
// Start Time
start := time.Now()
if !dryRun {
outputFilename, err := build.Build(buildOptions)
if err != nil {
logger.Println("Error: %s", err.Error())
targetErr = err
return
// Start Time
start := time.Now()
outputFilename, err := build.Build(buildOptions)
if err != nil {
logger.Println("Error: %s", err.Error())
targetErr = err
return
}
buildOptions.IgnoreFrontend = true
buildOptions.CleanBuildDirectory = false
// Output stats
buildOptions.Logger.Println(fmt.Sprintf("Built '%s' in %s.\n", outputFilename, time.Since(start).Round(time.Millisecond).String()))
} else {
logger.Println("Dry run: skipped build.")
}
buildOptions.IgnoreFrontend = true
buildOptions.CleanBuildDirectory = false
// Output stats
buildOptions.Logger.Println(fmt.Sprintf("Built '%s' in %s.\n", outputFilename, time.Since(start).Round(time.Millisecond).String()))
outputBinaries[buildOptions.Platform+"/"+buildOptions.Arch] = outputFilename
})
@@ -346,6 +356,10 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
return targetErr
}
if dryRun {
return nil
}
if nsis {
amd64Binary := outputBinaries["windows/amd64"]
arm64Binary := outputBinaries["windows/arm64"]
+8 -2
View File
@@ -14,9 +14,12 @@ func TestUpdateEnv(t *testing.T) {
newEnv = upsertEnv(newEnv, "three", func(v string) string {
return "3"
})
newEnv = upsertEnv(newEnv, "GOARCH", func(v string) string {
return "amd64"
})
if len(newEnv) != 4 {
t.Errorf("expected: 4, got: %d", len(newEnv))
if len(newEnv) != 5 {
t.Errorf("expected: 5, got: %d", len(newEnv))
}
if newEnv[1] != "two=a=b+added" {
t.Errorf("expected: \"two=a=b+added\", got: %q", newEnv[1])
@@ -27,5 +30,8 @@ func TestUpdateEnv(t *testing.T) {
if newEnv[3] != "newVar=added" {
t.Errorf("expected: \"newVar=added\", got: %q", newEnv[3])
}
if newEnv[4] != "GOARCH=amd64" {
t.Errorf("expected: \"newVar=added\", got: %q", newEnv[4])
}
}
+3 -3
View File
@@ -164,7 +164,7 @@ func Build(options *Options) (string, error) {
options.OutputFile = amd64Filename
options.CleanBuildDirectory = false
if options.Verbosity == VERBOSE {
outputLogger.Println("\nBuilding AMD64 Target:", filepath.Join(options.BuildDirectory, options.OutputFile))
outputLogger.Println("\nBuilding AMD64 Target: %s", filepath.Join(options.BuildDirectory, options.OutputFile))
}
err = builder.CompileProject(options)
@@ -176,7 +176,7 @@ func Build(options *Options) (string, error) {
options.OutputFile = arm64Filename
options.CleanBuildDirectory = false
if options.Verbosity == VERBOSE {
outputLogger.Println("Building ARM64 Target:", filepath.Join(options.BuildDirectory, options.OutputFile))
outputLogger.Println("Building ARM64 Target: %s", filepath.Join(options.BuildDirectory, options.OutputFile))
}
err = builder.CompileProject(options)
@@ -185,7 +185,7 @@ func Build(options *Options) (string, error) {
}
// Run lipo
if options.Verbosity == VERBOSE {
outputLogger.Println(" Running lipo: ", "lipo", "-create", "-output", outputFile, amd64Filename, arm64Filename)
outputLogger.Println(" Running lipo: lipo -create -output %s %s %s", outputFile, amd64Filename, arm64Filename)
}
_, stderr, err := shell.RunCommand(options.BuildDirectory, "lipo", "-create", "-output", outputFile, amd64Filename, arm64Filename)
if err != nil {
+3 -3
View File
@@ -54,13 +54,13 @@ A list of community maintained templates can be found [here](../community/templa
| Flag | Description | Default |
| :------------------- | :-------------------------------------- | :------------------------- |
| -platform | Build for the given (comma delimited) [platforms](../reference/cli.mdx#platforms) eg. `windows/arm64`. Note, if you do not give the architecture, `runtime.GOARCH` is used. | runtime.GOOS/runtime.GOARCH |
| -platform | Build for the given (comma delimited) [platforms](../reference/cli.mdx#platforms) eg. `windows/arm64`. Note, if you do not give the architecture, `runtime.GOARCH` is used. | platform = `GOOS` environment variable if given else `runtime.GOOS`.<br/>arch = `GOARCH` envrionment variable if given else `runtime.GOARCH`. |
| -clean | Cleans the `build/bin` directory | |
| -compiler "compiler"| Use a different go compiler to build, eg go1.15beta1 | go |
| -ldflags "flags" | Additional ldflags to pass to the compiler | |
| -nopackage | Do not package application | |
| -o filename | Output filename | |
| -s | Skip building the frontend | |
| -s | Skip building the frontend | false |
| -f | Force build application | false |
| -tags "extra tags" | Build tags to pass to compiler (quoted and space separated) | |
| -upx | Compress final binary using "upx" | |
@@ -177,9 +177,9 @@ Your system is ready for Wails development!
| -devserver "host:port" | The address to bind the wails dev server to | "localhost:34115" |
| -frontenddevserverurl "url" | Use 3rd party dev server url to serve assets, EG Vite | "" |
| -appargs "args" | Arguments passed to the application in shell style | |
| -platform "platform" | Platform/Arch to target | `runtime.GOOS` |
| -save | Saves the given `assetdir`, `reloaddirs`, `wailsjsdir`, `debounce`, `devserver` and `frontenddevserverurl` flags in `wails.json` to become the defaults for subsequent invocations. | |
| -race | Build with Go's race detector | false |
| -s | Skip building the frontend | false |
Example: