Skip to content

Commit

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

See ooni/probe#2401.
  • Loading branch information
bassosimone authored Jan 25, 2023
1 parent 2acefa3 commit 35faf23
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 46 deletions.
35 changes: 0 additions & 35 deletions CLI/go-build-generic

This file was deleted.

2 changes: 1 addition & 1 deletion MONOREPO/w/run-desktop-with-cli.bash
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ arm64)
;;
esac

run ./CLI/go-build-generic ./cmd/ooniprobe
run make CLI/ooniprobe
run mkdir -p ./MONOREPO/repo/probe-desktop/build/probe-cli/${GOOS}_${GOARCH}
run mv -v ooniprobe ./MONOREPO/repo/probe-desktop/build/probe-cli/${GOOS}_${GOARCH}
(
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ CLI/linux-static-arm64: search/for/docker maybe/copypsiphon
#help: The `make CLI/miniooni` command creates a build of miniooni, for the current
#help: system, putting the binary in the top-level directory.
.PHONY: CLI/miniooni
CLI/miniooni: maybe/copypsiphon search/for/go
./CLI/go-build-generic ./internal/cmd/miniooni
CLI/miniooni:
go run ./internal/cmd/buildtool generic miniooni

#help:
#help: The `make CLI/ooniprobe` command creates a build of ooniprobe, for the current
#help: system, putting the binary in the top-level directory.
.PHONY: CLI/ooniprobe
CLI/ooniprobe: maybe/copypsiphon search/for/go
./CLI/go-build-generic ./cmd/ooniprobe
CLI/ooniprobe:
go run ./internal/cmd/buildtool generic ooniprobe

#help:
#help: The `make CLI/windows` command builds the ooniprobe and miniooni
Expand Down
62 changes: 62 additions & 0 deletions internal/cmd/buildtool/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

//
// Generic builder for the current GOOS/GOARCH
//

import (
"runtime"

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

// genericSubcommand returns the generic sucommand.
func genericSubcommand() *cobra.Command {
cmd := &cobra.Command{
Use: "generic",
Short: "Generic Go builder for the current GOOS and GOARCH",
}
cmd.AddCommand(&cobra.Command{
Use: "miniooni",
Short: "Builds miniooni for the current GOOS and GOARCH",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
genericBuildPackage(&buildDeps{}, productMiniooni)
},
})
cmd.AddCommand(&cobra.Command{
Use: "ooniprobe",
Short: "Builds ooniprobe for the current GOOS and GOARCH",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
genericBuildPackage(&buildDeps{}, productOoniprobe)
},
})
return cmd
}

// genericBuildPackage is the generic function for building a package.
func genericBuildPackage(deps buildtoolmodel.Dependencies, product *product) {
deps.PsiphonMaybeCopyConfigFiles()
deps.GolangCheck()

log.Infof("building %s for %s/%s", product.Pkg, runtime.GOOS, runtime.GOARCH)

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

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

runtimex.Try0(shellx.RunEx(config, argv, &shellx.Envp{}))
}
100 changes: 100 additions & 0 deletions internal/cmd/buildtool/generic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/cmd/buildtool/internal/buildtooltest"
"github.com/ooni/probe-cli/v3/internal/shellx/shellxtesting"
)

func TestGenericBuildPackage(t *testing.T) {

// testspec specifies a test case for this test
type testspec struct {
// name is the name of the test case
name string

// product is the product to build
product *product

// hasPsiphon indicates whether we should build with psiphon config
hasPsiphon bool

// expectations contains the commands we expect to see
expect []buildtooltest.ExecExpectations
}

var testcases = []testspec{{
name: "miniooni build with psiphon",
product: productMiniooni,
hasPsiphon: true,
expect: []buildtooltest.ExecExpectations{{
Env: []string{},
Argv: []string{
"go", "build", "-tags", "ooni_psiphon_config",
"-ldflags", "-s -w", "./internal/cmd/miniooni",
},
}},
}, {
name: "miniooni build without psiphon",
product: productMiniooni,
hasPsiphon: false,
expect: []buildtooltest.ExecExpectations{{
Env: []string{},
Argv: []string{
"go", "build", "-ldflags", "-s -w", "./internal/cmd/miniooni",
},
}},
}, {
name: "ooniprobe build with psiphon",
product: productOoniprobe,
hasPsiphon: true,
expect: []buildtooltest.ExecExpectations{{
Env: []string{},
Argv: []string{
"go", "build", "-tags", "ooni_psiphon_config",
"-ldflags", "-s -w", "./cmd/ooniprobe",
},
}},
}, {
name: "ooniprobe build without psiphon",
product: productOoniprobe,
hasPsiphon: false,
expect: []buildtooltest.ExecExpectations{{
Env: []string{},
Argv: []string{
"go", "build", "-ldflags", "-s -w", "./cmd/ooniprobe",
},
}},
}}

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {

cc := &buildtooltest.SimpleCommandCollector{}

deps := &buildtooltest.DependenciesCallCounter{
HasPsiphon: testcase.hasPsiphon,
}

shellxtesting.WithCustomLibrary(cc, func() {
genericBuildPackage(deps, testcase.product)
})

expectCalls := map[string]int{
buildtooltest.TagGolangCheck: 1,
buildtooltest.TagPsiphonMaybeCopyConfigFiles: 1,
buildtooltest.TagPsiphonFilesExist: 1,
}

if diff := cmp.Diff(expectCalls, deps.Counter); diff != "" {
t.Fatal(diff)
}

if err := buildtooltest.CheckManyCommands(cc.Commands, testcase.expect); err != nil {
t.Fatal(err)
}
})
}
}
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(genericSubcommand())
root.AddCommand(windowsSubcommand())
logHandler := logx.NewHandlerWithDefaultSettings()
logHandler.Emoji = true
Expand Down
6 changes: 0 additions & 6 deletions internal/cmd/buildtool/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ var productMiniooni = &product{
Kind: "CLI",
}

// productOohelperd is the oohelperd product.
var productOohelperd = &product{
Pkg: "./internal/cmd/oohelperd",
Kind: "CLI",
}

// productOoniprobe is the ooniprobe product.
var productOoniprobe = &product{
Pkg: "./cmd/ooniprobe",
Expand Down

0 comments on commit 35faf23

Please sign in to comment.