Skip to content

Commit

Permalink
feat: rewrite windows build rules in Go (#1050)
Browse files Browse the repository at this point in the history
This diff rewrites Windows build rules in Go.

See ooni/probe#2401.
  • Loading branch information
bassosimone authored Jan 25, 2023
1 parent 1426f1e commit 2acefa3
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 102 deletions.
37 changes: 0 additions & 37 deletions CLI/check-mingw-w64-version

This file was deleted.

55 changes: 0 additions & 55 deletions CLI/go-build-windows

This file was deleted.

11 changes: 2 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,8 @@ CLI/ooniprobe: maybe/copypsiphon search/for/go
#help: The `make CLI/windows` command builds the ooniprobe and miniooni
#help: command line clients for windows/386 and windows/amd64.
.PHONY: CLI/windows
CLI/windows: search/for/go search/for/mingw-w64 maybe/copypsiphon
./CLI/go-build-windows 386 ./internal/cmd/miniooni
./CLI/go-build-windows 386 ./cmd/ooniprobe
./CLI/go-build-windows amd64 ./internal/cmd/miniooni
./CLI/go-build-windows amd64 ./cmd/ooniprobe
CLI/windows:
go run ./internal/cmd/buildtool windows

#help:
#help: The `make MOBILE/android` command builds the oonimkall library for Android.
Expand Down Expand Up @@ -184,10 +181,6 @@ search/for/java:
@printf "checking for java... "
@command -v java || { echo "not found"; exit 1; }

.PHONY: search/for/mingw-w64
search/for/mingw-w64:
./CLI/check-mingw-w64-version

.PHONY: search/for/xcode
search/for/xcode:
./MOBILE/ios/check-xcode-version
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/buildtool/builddeps.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ func (*buildDeps) PsiphonMaybeCopyConfigFiles() {

// WindowsMingwCheck implements buildtoolmodel.Dependencies
func (*buildDeps) WindowsMingwCheck() {
//windowsMingwCheck() /* TODO(bassosimone) */
windowsMingwCheck()
}
1 change: 1 addition & 0 deletions internal/cmd/buildtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
Short: "Tool for building ooniprobe, miniooni, etc.",
}
root.AddCommand(darwinSubcommand())
root.AddCommand(windowsSubcommand())
logHandler := logx.NewHandlerWithDefaultSettings()
logHandler.Emoji = true
log.Log = &log.Logger{Level: log.InfoLevel, Handler: logHandler}
Expand Down
123 changes: 123 additions & 0 deletions internal/cmd/buildtool/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

//
// Windows build
//

import (
"errors"
"os"
"strings"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/cmd/buildtool/internal/buildtoolmodel"
"github.com/ooni/probe-cli/v3/internal/must"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/shellx"
"github.com/spf13/cobra"
)

// windowsSubcommand returns the windows sucommand.
func windowsSubcommand() *cobra.Command {
return &cobra.Command{
Use: "windows",
Short: "Builds ooniprobe and miniooni for windows",
Run: func(cmd *cobra.Command, args []string) {
windowsBuildAll(&buildDeps{})
},
Args: cobra.NoArgs,
}
}

// windowsBuildAll is the main function of the windows subcommand.
func windowsBuildAll(deps buildtoolmodel.Dependencies) {
deps.PsiphonMaybeCopyConfigFiles()
deps.GolangCheck()
deps.WindowsMingwCheck()
archs := []string{"386", "amd64"}
products := []*product{productMiniooni, productOoniprobe}
for _, arch := range archs {
for _, product := range products {
windowsBuildPackage(deps, arch, product)
}
}
}

// windowsBuildPackage builds the given package for windows
// compiling for the specified architecture.
func windowsBuildPackage(deps buildtoolmodel.Dependencies, goarch string, product *product) {
log.Infof("building %s for windows/%s", product.Pkg, goarch)

argv := runtimex.Try1(shellx.NewArgv("go", "build"))
if deps.PsiphonFilesExist() {
argv.Append("-tags", "ooni_psiphon_config")
}

argv.Append("-ldflags", "-s -w")
argv.Append("-o", product.DestinationPath("windows", goarch))
argv.Append(product.Pkg)

envp := &shellx.Envp{}
switch goarch {
case "amd64":
envp.Append("CC", windowsMingwAmd64Compiler)
case "386":
envp.Append("CC", windowsMingw386Compiler)
default:
panic(errors.New("unsupported windows goarch"))
}

envp.Append("CGO_ENABLED", "1")
envp.Append("GOARCH", goarch)
envp.Append("GOOS", "windows")

config := &shellx.Config{
Logger: log.Log,
Flags: shellx.FlagShowStdoutStderr,
}

runtimex.Try0(shellx.RunEx(config, argv, envp))
}

// windowsMingwExpectedVersion is the expected version of mingw-w64,
// which may be overriden by setting the EXPECTED_MINGW_W64_VERSION
// environment variable before starting the build.
var windowsMingwExpectedVersion = "12.2.0"

// windowsMingwEnvironmentVariable is the name of the environment variable
// that overrides the expected mingw version.
const windowsMingwEnvironmentVariable = "EXPECTED_MINGW_W64_VERSION"

// windowsMingwAmd64Compiler is the amd64 compiler.
const windowsMingwAmd64Compiler = "x86_64-w64-mingw32-gcc"

// windowsMingw386Compiler is the 386 compiler.
const windowsMingw386Compiler = "i686-w64-mingw32-gcc"

// windowsMingwCheck checks we're using the correct mingw version.
func windowsMingwCheck() {
windowsMingwCheckFor(windowsMingwAmd64Compiler)
windowsMingwCheckFor(windowsMingw386Compiler)
}

// windowsMingwCheckFor implements mingwCheck for the given compiler.
func windowsMingwCheckFor(compiler string) {
expected := windowsMingwExpectedVersionGetter()
firstLine := string(must.FirstLineBytes(must.RunOutputQuiet(compiler, "--version")))
v := strings.Split(firstLine, " ")
runtimex.Assert(len(v) == 3, "expected to see exactly three tokens")
if got := v[2]; got != expected {
log.Fatalf("expected mingw %s but got %s", expected, got)
}
log.Infof("using %s %s", compiler, expected)
}

// windowsMingwEexpectedVersionGetter returns the correct expected mingw version.
func windowsMingwExpectedVersionGetter() string {
value := os.Getenv(windowsMingwEnvironmentVariable)
if value == "" {
return windowsMingwExpectedVersion
}
log.Infof("mingw version overriden using %s", windowsMingwEnvironmentVariable)
return value
}
Loading

0 comments on commit 2acefa3

Please sign in to comment.