Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install protobuf tools #15

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
me "github.com/sei-protocol/build"
"github.com/sei-protocol/build/pkg/tools/git"
"github.com/sei-protocol/build/pkg/tools/golang"
_ "github.com/sei-protocol/build/pkg/tools/protobuf"
"github.com/sei-protocol/build/pkg/tools/rust"
)

Expand Down
98 changes: 98 additions & 0 deletions pkg/tools/golang/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ package golang

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

"github.com/outofforest/build"
"github.com/outofforest/libexec"
"github.com/outofforest/logger"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/sei-protocol/build/pkg/tools"
)
Expand All @@ -15,6 +22,7 @@ const (
)

var t = []tools.Tool{
// https://go.dev/dl/
tools.BinaryTool{
Name: Go,
Version: "1.22.4",
Expand Down Expand Up @@ -45,6 +53,7 @@ var t = []tools.Tool{
},
},
},

// https://github.com/golangci/golangci-lint/releases/
tools.BinaryTool{
Name: GolangCI,
Expand Down Expand Up @@ -75,6 +84,95 @@ var t = []tools.Tool{
},
}

// GoPackageTool is the tool installed using go install command.
type GoPackageTool struct {
Name tools.Name
Version string
Package string
}

// GetName returns the name of the tool.
func (gpt GoPackageTool) GetName() tools.Name {
return gpt.Name
}

// GetVersion returns the version of the tool.
func (gpt GoPackageTool) GetVersion() string {
return gpt.Version
}

// IsCompatible tells if tool is defined for the platform.
func (gpt GoPackageTool) IsCompatible(platform tools.Platform) (bool, error) {
golang, err := tools.Get(Go)
if err != nil {
return false, err
}
return golang.IsCompatible(platform)
}

// Ensure ensures that tool is installed.
func (gpt GoPackageTool) Ensure(ctx context.Context, platform tools.Platform) error {
binName := filepath.Base(gpt.Package)
downloadDir := tools.ToolDownloadDir(ctx, platform, gpt)
dst := filepath.Join("bin", binName)

//nolint:nestif // complexity comes from trivial error-handling ifs.
if tools.ShouldReinstall(ctx, platform, gpt, dst, binName) {
if err := tools.Ensure(ctx, Go, platform); err != nil {
return errors.Wrapf(err, "ensuring go failed")
}

cmd := exec.Command(tools.Bin(ctx, "bin/go", platform), "install", gpt.Package+"@"+gpt.Version)
cmd.Env = append(env(ctx), "GOBIN="+downloadDir)

if err := libexec.Exec(ctx, cmd); err != nil {
return err
}

srcPath := filepath.Join(downloadDir, binName)

binChecksum, err := tools.Checksum(srcPath)
if err != nil {
return err
}

linksDir := tools.ToolLinksDir(ctx, platform, gpt)
dstPath := filepath.Join(linksDir, dst)
dstPathChecksum := dstPath + ":" + binChecksum

if err := os.Remove(dstPath); err != nil && !os.IsNotExist(err) {
panic(err)
}
if err := os.Remove(dstPathChecksum); err != nil && !os.IsNotExist(err) {
return errors.WithStack(err)
}

if err := os.MkdirAll(filepath.Dir(dstPath), 0o700); err != nil {
return errors.WithStack(err)
}
if err := os.Chmod(srcPath, 0o700); err != nil {
return errors.WithStack(err)
}
srcLinkPath, err := filepath.Rel(filepath.Dir(dstPathChecksum), filepath.Join(downloadDir, binName))
if err != nil {
return errors.WithStack(err)
}
if err := os.Symlink(srcLinkPath, dstPathChecksum); err != nil {
return errors.WithStack(err)
}
if err := os.Symlink(filepath.Base(dstPathChecksum), dstPath); err != nil {
return errors.WithStack(err)
}
if _, err := filepath.EvalSymlinks(dstPath); err != nil {
return errors.WithStack(err)
}

logger.Get(ctx).Info("Binary installed to path", zap.String("path", dstPath))
}

return tools.LinkFiles(ctx, platform, gpt, []string{dst})
}

// EnsureGo ensures that go is available.
func EnsureGo(ctx context.Context, _ build.DepsFunc) error {
return tools.Ensure(ctx, Go, tools.PlatformLocal)
Expand Down
81 changes: 81 additions & 0 deletions pkg/tools/protobuf/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package protobuf

import (
"context"

"github.com/outofforest/build"

"github.com/sei-protocol/build/pkg/tools"
"github.com/sei-protocol/build/pkg/tools/golang"
)

// Tool names.
const (
Protoc tools.Name = "protoc"
ProtocGenGo tools.Name = "protoc-gen-go"
ProtocGenGoGRPC tools.Name = "protoc-gen-go-grpc"
)

var t = []tools.Tool{
// https://github.com/protocolbuffers/protobuf/releases
tools.BinaryTool{
Name: Protoc,
Version: "v25.0",
Sources: tools.Sources{
tools.PlatformLinuxAMD64: {
URL: "https://github.com/protocolbuffers/protobuf/releases/download/v25.0/protoc-25.0-linux-x86_64.zip",
Hash: "sha256:d26c4efe0eae3066bb560625b33b8fc427f55bd35b16f246b7932dc851554e67",
Links: map[string]string{
"bin/protoc": "bin/protoc",
},
},
tools.PlatformDarwinAMD64: {
URL: "https://github.com/protocolbuffers/protobuf/releases/download/v25.0/protoc-25.0-osx-x86_64.zip",
Hash: "sha256:15eefb30ba913e8dc4dd21d2ccb34ce04a2b33124f7d9460e5fd815a5d6459e3",
Links: map[string]string{
"bin/protoc": "bin/protoc",
},
},
tools.PlatformDarwinARM64: {
URL: "https://github.com/protocolbuffers/protobuf/releases/download/v25.0/protoc-25.0-osx-aarch_64.zip",
Hash: "sha256:76a997df5dacc0608e880a8e9069acaec961828a47bde16c06116ed2e570588b",
Links: map[string]string{
"bin/protoc": "bin/protoc",
},
},
},
},

// https://github.com/protocolbuffers/protobuf-go
golang.GoPackageTool{
Name: ProtocGenGo,
Version: "v1.34.2",
Package: "google.golang.org/protobuf/cmd/protoc-gen-go",
},

// https://github.com/grpc/grpc-go/releases
golang.GoPackageTool{
Name: ProtocGenGoGRPC,
Version: "v1.5.1",
Package: "google.golang.org/grpc/cmd/protoc-gen-go-grpc",
},
}

// EnsureProtoc ensures that protoc is available.
func EnsureProtoc(ctx context.Context, _ build.DepsFunc) error {
return tools.Ensure(ctx, Protoc, tools.PlatformLocal)
}

// EnsureProtocGenGo ensures that protoc-gen-go is available.
func EnsureProtocGenGo(ctx context.Context, _ build.DepsFunc) error {
return tools.Ensure(ctx, ProtocGenGo, tools.PlatformLocal)
}

// EnsureProtocGenGoGRPC ensures that protoc-gen-go is available.
func EnsureProtocGenGoGRPC(ctx context.Context, _ build.DepsFunc) error {
return tools.Ensure(ctx, ProtocGenGoGRPC, tools.PlatformLocal)
}

func init() {
tools.Add(t...)
}