Skip to content

Commit

Permalink
Local build system (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-coreum authored Feb 22, 2024
1 parent 4bbf157 commit df3ae80
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 7 deletions.
10 changes: 3 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
.idea/
build/

# Configuration
.idea
.bdjuno
*.toml
*.json
config.yaml

# Coverage
coverage.*
/coverage
/bin
24 changes: 24 additions & 0 deletions bin/bdjuno-builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -e

# go to root dir of repository
pushd "$(dirname "${BASH_SOURCE[0]}")/.." > /dev/null

VERSION=$(git rev-parse --short HEAD)
REPO=$(pwd)
BUILD_BIN="$REPO/bin/.cache/build-$VERSION"

if [ ! -f "$BUILD_BIN" ]; then
rm -f ./bin/.cache/build-*

pushd build > /dev/null
go build -trimpath -o "$BUILD_BIN" ./cmd
popd > /dev/null

"$BUILD_BIN" build/me
fi

popd > /dev/null

exec "$BUILD_BIN" "$@"
47 changes: 47 additions & 0 deletions build/bdjuno/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package bdjuno

import (
"context"
"path/filepath"

"github.com/CoreumFoundation/coreum-tools/pkg/build"
"github.com/CoreumFoundation/crust/build/git"
"github.com/CoreumFoundation/crust/build/golang"
"github.com/CoreumFoundation/crust/build/tools"
)

const (
repoURL = "https://github.com/CoreumFoundation/bdjuno.git"
repoPath = "../bdjuno"
binaryName = "bdjuno"
binaryPath = "bin/" + binaryName
)

// Build builds faucet in docker.
func Build(ctx context.Context, deps build.DepsFunc) error {
return buildBDJuno(ctx, deps, tools.TargetPlatformLinuxLocalArchInDocker)
}

// Tidy runs `go mod tidy` for bdjuno repo.
func Tidy(ctx context.Context, deps build.DepsFunc) error {
return golang.Tidy(ctx, repoPath, deps)
}

// Test run unit tests in bdjuno repo.
func Test(ctx context.Context, deps build.DepsFunc) error {
return golang.Test(ctx, repoPath, deps)
}

func buildBDJuno(ctx context.Context, deps build.DepsFunc, targetPlatform tools.TargetPlatform) error {
deps(ensureRepo)

return golang.Build(ctx, deps, golang.BinaryBuildConfig{
TargetPlatform: targetPlatform,
PackagePath: filepath.Join(repoPath, "cmd", "bdjuno"),
BinOutputPath: filepath.Join("bin", ".cache", binaryName, targetPlatform.String(), "bin", binaryName),
})
}

func ensureRepo(ctx context.Context, deps build.DepsFunc) error {
return git.EnsureRepo(ctx, repoURL)
}
9 changes: 9 additions & 0 deletions build/bdjuno/image/Dockerfile.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM {{ .From }}

# TODO: This is needed because we use psql to load schema. If we do it in go we will be able to use basic image.
RUN apk update && apk add postgresql

ARG TARGETARCH
COPY {{ .Binary }} /{{ .Binary }}

ENTRYPOINT ["/{{ .Binary }}"]
31 changes: 31 additions & 0 deletions build/bdjuno/image/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package image

import (
"bytes"
_ "embed"
"text/template"
)

var (
//go:embed Dockerfile.tmpl
tmpl string
dockerfile = template.Must(template.New("dockerfile").Parse(tmpl))
)

// Data is the structure containing fields required by the template.
type Data struct {
// From is the tag of the base image
From string

// Binary is the name of faucet binary file to copy from build context
Binary string
}

// Execute executes dockerfile template and returns complete dockerfile.
func Execute(data Data) ([]byte, error) {
buf := &bytes.Buffer{}
if err := dockerfile.Execute(buf, data); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
32 changes: 32 additions & 0 deletions build/bdjuno/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package bdjuno

import (
"context"
"path/filepath"

"github.com/CoreumFoundation/bdjuno/build/bdjuno/image"
"github.com/CoreumFoundation/coreum-tools/pkg/build"
"github.com/CoreumFoundation/crust/build/config"
"github.com/CoreumFoundation/crust/build/docker"
"github.com/CoreumFoundation/crust/build/tools"
)

// BuildDockerImage builds docker image of the faucet.
func BuildDockerImage(ctx context.Context, deps build.DepsFunc) error {
deps(Build)

dockerfile, err := image.Execute(image.Data{
From: docker.AlpineImage,
Binary: binaryPath,
})
if err != nil {
return err
}

return docker.BuildImage(ctx, docker.BuildImageConfig{
ContextDir: filepath.Join("bin", ".cache", binaryName, tools.TargetPlatformLinuxLocalArchInDocker.String()),
ImageName: binaryName,
Dockerfile: dockerfile,
Versions: []string{config.ZNetVersion},
})
}
36 changes: 36 additions & 0 deletions build/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"context"
"os"
"path/filepath"

selfBuild "github.com/CoreumFoundation/bdjuno/build"
"github.com/CoreumFoundation/coreum-tools/pkg/build"
"github.com/CoreumFoundation/coreum-tools/pkg/logger"
"github.com/CoreumFoundation/coreum-tools/pkg/must"
"github.com/CoreumFoundation/coreum-tools/pkg/run"
)

func main() {
run.Tool("build", func(ctx context.Context) error {
flags := logger.Flags(logger.ToolDefaultConfig, "build")
if err := flags.Parse(os.Args[1:]); err != nil {
return err
}
exec := build.NewExecutor(selfBuild.Commands)
if build.Autocomplete(exec) {
return nil
}

changeWorkingDir()
return build.Do(ctx, "coreum", flags.Args(), exec)
})
}

// changeWorkingDir sets working dir to the root directory of repository.
func changeWorkingDir() {
must.OK(os.Chdir(filepath.Dir(filepath.Dir(filepath.Dir(must.String(
filepath.EvalSymlinks(must.String(os.Executable())),
))))))
}
27 changes: 27 additions & 0 deletions build/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module github.com/CoreumFoundation/bdjuno/build

go 1.21

require (
github.com/CoreumFoundation/coreum-tools v0.4.1-0.20230627094203-821c6a4eebab
github.com/CoreumFoundation/crust/build v0.0.0-20240221111501-24517d1ac71b
)

require (
github.com/CoreumFoundation/coreum/v4 v4.0.0-20240213123712-d7d6a45ddb8f // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/mod v0.12.0 // indirect
)

require (
github.com/samber/lo v1.39.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
// Make sure to not bump x/exp dependency without cosmos-sdk updated because their breaking change is not compatible
// with cosmos-sdk v0.47.
// Details: https://github.com/cosmos/cosmos-sdk/issues/18415
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
)
82 changes: 82 additions & 0 deletions build/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
github.com/CoreumFoundation/coreum-tools v0.4.1-0.20230627094203-821c6a4eebab h1:XPcSzlQ06tor/py1LkEM9FX70LIeDWB++vLuAO6eAYI=
github.com/CoreumFoundation/coreum-tools v0.4.1-0.20230627094203-821c6a4eebab/go.mod h1:VD93vCHkxYaT/RhOesXTFgd/GQDW54tr0BqGi5JU1c0=
github.com/CoreumFoundation/coreum/v4 v4.0.0-20240213123712-d7d6a45ddb8f h1:a+lBcN3rWvoa/5mvdOQMaUU604/J3h/0rumGnISRglo=
github.com/CoreumFoundation/coreum/v4 v4.0.0-20240213123712-d7d6a45ddb8f/go.mod h1:m17GEOObKO0uMNRSeitPFjjne55MSbzFeTlAONaMGkI=
github.com/CoreumFoundation/crust/build v0.0.0-20240221111501-24517d1ac71b h1:IoQS6FhpOHVKc+xnzNCUYwdIL59CPTgsXKNYlcjVetY=
github.com/CoreumFoundation/crust/build v0.0.0-20240221111501-24517d1ac71b/go.mod h1:7g0+guH6BZLfm8oVJwwgCr8ntzIwJAQo7MC1c5wMFBo=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16 changes: 16 additions & 0 deletions build/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package build

import (
"github.com/CoreumFoundation/bdjuno/build/bdjuno"
"github.com/CoreumFoundation/coreum-tools/pkg/build"
"github.com/CoreumFoundation/crust/build/crust"
)

// Commands is a definition of commands available in build system.
var Commands = map[string]build.CommandFunc{
"build/me": crust.BuildBuilder,
"build": bdjuno.Build,
"images": bdjuno.BuildDockerImage,
"test": bdjuno.Test,
"tidy": bdjuno.Tidy,
}

0 comments on commit df3ae80

Please sign in to comment.