diff --git a/printer/goasm.go b/printer/goasm.go index 0d8a12c..66b8485 100644 --- a/printer/goasm.go +++ b/printer/goasm.go @@ -4,6 +4,7 @@ import ( "strconv" "strings" + "github.com/mmcloughlin/avo/buildtags" "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/ir" "github.com/mmcloughlin/avo/operand" @@ -44,8 +45,12 @@ func (p *goasm) header(f *ir.File) { p.Comment(p.cfg.GeneratedWarning()) if len(f.Constraints) > 0 { + constraints, err := buildtags.Format(f.Constraints) + if err != nil { + p.AddError(err) + } p.NL() - p.Printf(f.Constraints.GoString()) + p.Printf(constraints) } if len(f.Includes) > 0 { diff --git a/printer/goasm_test.go b/printer/goasm_test.go index cfae0f0..c974a2f 100644 --- a/printer/goasm_test.go +++ b/printer/goasm_test.go @@ -5,6 +5,7 @@ import ( "github.com/mmcloughlin/avo/attr" "github.com/mmcloughlin/avo/build" + "github.com/mmcloughlin/avo/buildtags" "github.com/mmcloughlin/avo/printer" "github.com/mmcloughlin/avo/reg" ) @@ -73,13 +74,24 @@ func TestConstraints(t *testing.T) { ctx.ConstraintExpr("linux,386 darwin,!cgo") ctx.ConstraintExpr("!noasm") - AssertPrintsLines(t, ctx, printer.NewGoAsm, []string{ + expect := []string{ "// Code generated by avo. DO NOT EDIT.", "", - "// +build linux,386 darwin,!cgo", - "// +build !noasm", - "", - }) + } + if buildtags.GoBuildSyntaxSupported() { + expect = append(expect, + "//go:build ((linux && 386) || (darwin && !cgo)) && !noasm", + ) + } + if buildtags.PlusBuildSyntaxSupported() { + expect = append(expect, + "// +build linux,386 darwin,!cgo", + "// +build !noasm", + ) + } + expect = append(expect, "") + + AssertPrintsLines(t, ctx, printer.NewGoAsm, expect) } func TestAlignmentNoOperands(t *testing.T) { diff --git a/printer/stubs.go b/printer/stubs.go index 171bc62..73c2649 100644 --- a/printer/stubs.go +++ b/printer/stubs.go @@ -1,6 +1,7 @@ package printer import ( + "github.com/mmcloughlin/avo/buildtags" "github.com/mmcloughlin/avo/internal/prnt" "github.com/mmcloughlin/avo/ir" ) @@ -19,8 +20,12 @@ func (s *stubs) Print(f *ir.File) ([]byte, error) { s.Comment(s.cfg.GeneratedWarning()) if len(f.Constraints) > 0 { + constraints, err := buildtags.Format(f.Constraints) + if err != nil { + s.AddError(err) + } s.NL() - s.Printf(f.Constraints.GoString()) + s.Printf(constraints) } s.NL() diff --git a/printer/stubs_test.go b/printer/stubs_test.go index 9666168..8cc0c33 100644 --- a/printer/stubs_test.go +++ b/printer/stubs_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/mmcloughlin/avo/build" + "github.com/mmcloughlin/avo/buildtags" "github.com/mmcloughlin/avo/printer" ) @@ -26,3 +27,32 @@ func TestStubsPragmas(t *testing.T) { "", }) } + +func TestStubsConstraints(t *testing.T) { + ctx := build.NewContext() + ctx.ConstraintExpr("linux darwin") + ctx.ConstraintExpr("amd64 arm64 mips64x ppc64x") + + expect := []string{ + "// Code generated by avo. DO NOT EDIT.", + "", + } + if buildtags.GoBuildSyntaxSupported() { + expect = append(expect, + "//go:build (linux || darwin) && (amd64 || arm64 || mips64x || ppc64x)", + ) + } + if buildtags.PlusBuildSyntaxSupported() { + expect = append(expect, + "// +build linux darwin", + "// +build amd64 arm64 mips64x ppc64x", + ) + } + expect = append(expect, + "", + "package printer", + "", + ) + + AssertPrintsLines(t, ctx, printer.NewStubs, expect) +}