Skip to content

Commit

Permalink
buildtags: go:build support (#208)
Browse files Browse the repository at this point in the history
Implements formatting of constraints according to the current Go version
supported syntax. This is achieved by delegating to go/format and extracting
out the resulting comments.

Also provides functions to query for constraint syntax support, which are
mainly intended for writing version-dependent tests.

Updates #183
  • Loading branch information
mmcloughlin authored Oct 29, 2021
1 parent a548039 commit 55d98cc
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
45 changes: 45 additions & 0 deletions buildtags/syntax.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package buildtags

import (
"bufio"
"bytes"
"fmt"
"go/format"
"strings"
)

// PlusBuildSyntaxSupported reports whether the current Go version supports the
// "// +build" constraint syntax.
func PlusBuildSyntaxSupported() bool { return plusbuild }

// GoBuildSyntaxSupported reports whether the current Go version supports the
// "//go:build" constraint syntax.
func GoBuildSyntaxSupported() bool { return gobuild }

// Format constraints according to the syntax supported by the current Go version.
func Format(t ConstraintsConvertable) (string, error) {
// Print build tags to minimal Go source that can be passed to go/format.
src := t.ToConstraints().GoString() + "\npackage stub"

// Format them.
formatted, err := format.Source([]byte(src))
if err != nil {
return "", fmt.Errorf("format build constraints: %w", err)
}

// Extract the comment lines.
buf := bytes.NewReader(formatted)
scanner := bufio.NewScanner(buf)
output := ""
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "//") {
output += line + "\n"
}
}
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("parse formatted build constraints: %w", err)
}

return output, nil
}
9 changes: 9 additions & 0 deletions buildtags/syntax_go116.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !go1.17
// +build !go1.17

package buildtags

const (
plusbuild = true
gobuild = false
)
9 changes: 9 additions & 0 deletions buildtags/syntax_go117.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build go1.17 && !go1.18
// +build go1.17,!go1.18

package buildtags

const (
plusbuild = true
gobuild = true
)
9 changes: 9 additions & 0 deletions buildtags/syntax_go118.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build go1.18
// +build go1.18

package buildtags

const (
plusbuild = false
gobuild = true
)
71 changes: 71 additions & 0 deletions buildtags/syntax_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package buildtags

import (
"bytes"
"fmt"
"testing"
)

func TestFormat(t *testing.T) {
cases := []struct {
PlusBuild []string
GoBuild string
}{
{
PlusBuild: []string{"amd64"},
GoBuild: "amd64",
},
{
PlusBuild: []string{
"linux darwin",
"amd64 arm64 mips64x ppc64x",
},
GoBuild: "(linux || darwin) && (amd64 || arm64 || mips64x || ppc64x)",
},
{
PlusBuild: []string{"!linux,!darwin !amd64,!arm64,!mips64x,!ppc64x"},
GoBuild: "(!linux && !darwin) || (!amd64 && !arm64 && !mips64x && !ppc64x)",
},
{
PlusBuild: []string{
"linux,386 darwin,!cgo",
"!noasm",
},
GoBuild: "((linux && 386) || (darwin && !cgo)) && !noasm",
},
}

for _, c := range cases {
// Parse constraints.
var cs Constraints
for _, expr := range c.PlusBuild {
constraint, err := ParseConstraint(expr)
if err != nil {
t.Fatal(err)
}
cs = append(cs, constraint)
}

// Build expected output.
var buf bytes.Buffer
if GoBuildSyntaxSupported() {
fmt.Fprintf(&buf, "//go:build %s\n", c.GoBuild)
}
if PlusBuildSyntaxSupported() {
for _, expr := range c.PlusBuild {
fmt.Fprintf(&buf, "// +build %s\n", expr)
}
}
expect := buf.String()

// Format and check.
got, err := Format(cs)
if err != nil {
t.Fatal(err)
}

if got != expect {
t.Errorf("got=\n%s\nexpect=\n%s\n", got, expect)
}
}
}

0 comments on commit 55d98cc

Please sign in to comment.