Skip to content

Commit

Permalink
fix: improve zig build
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 2, 2024
1 parent f4ad27c commit 549f2e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 10 additions & 3 deletions internal/builders/zig/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zig
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
Expand Down Expand Up @@ -128,9 +129,6 @@ func (b *Builder) WithDefaults(build config.Build) (config.Build, error) {

// Build implements build.Builder.
func (b *Builder) Build(ctx *context.Context, build config.Build, options api.Options) error {
prefix := filepath.Dir(options.Path)
options.Path = filepath.Join(prefix, "bin", options.Name)

t := options.Target.(Target)
a := &artifact.Artifact{
Type: artifact.Binary,
Expand Down Expand Up @@ -160,6 +158,7 @@ func (b *Builder) Build(ctx *context.Context, build config.Build, options api.Op
return err
}

prefix := filepath.Join("zig-out", t.Target)
command := []string{
zigbin,
build.Command,
Expand Down Expand Up @@ -199,6 +198,14 @@ func (b *Builder) Build(ctx *context.Context, build config.Build, options api.Op
log.WithField("cmd", command).Info(s)
}

if err := os.MkdirAll(filepath.Dir(options.Path), 0o755); err != nil {
return err
}
realPath := filepath.Join(build.Dir, prefix, "bin", options.Name)
if err := gio.Copy(realPath, options.Path); err != nil {
return err
}

// TODO: move this to outside builder for both go and zig
modTimestamp, err := tpl.Apply(build.ModTimestamp)
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion internal/builders/zig/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/goreleaser/goreleaser/v2/internal/artifact"
"github.com/goreleaser/goreleaser/v2/internal/testctx"
"github.com/goreleaser/goreleaser/v2/internal/testlib"
api "github.com/goreleaser/goreleaser/v2/pkg/build"
Expand Down Expand Up @@ -149,6 +150,7 @@ func TestBuild(t *testing.T) {
},
Builds: []config.Build{
{
ID: "default",
Dir: "./testdata/proj/",
ModTimestamp: fmt.Sprintf("%d", modTime.Unix()),
BuildDetails: config.BuildDetails{
Expand Down Expand Up @@ -177,7 +179,20 @@ func TestBuild(t *testing.T) {
require.Len(t, bins, 1)

bin := bins[0]
require.Equal(t, filepath.Join(dist, "proj-aarch64-macos", "bin", "proj"), filepath.FromSlash(bin.Path))
require.Equal(t, artifact.Artifact{
Name: "proj",
Path: filepath.Join(dist, "proj-aarch64-macos", "proj"),
Goos: "darwin",
Goarch: "arm64",
Target: "aarch64-macos",
Type: artifact.Binary,
Extra: artifact.Extras{
artifact.ExtraBinary: "proj",
artifact.ExtraBuilder: "zig",
artifact.ExtraExt: "",
artifact.ExtraID: "default",
},
}, *bin)

require.FileExists(t, bin.Path)
fi, err := os.Stat(bin.Path)
Expand Down

0 comments on commit 549f2e8

Please sign in to comment.