buildtags

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2024 License: BSD-3-Clause Imports: 7 Imported by: 8

Documentation

Overview

Package buildtags provides types for representing and manipulating build constraints.

In Go, build constraints are represented as comments in source code together with file naming conventions. For example

// +build linux,386 darwin,!cgo
// +build !purego

Any terms provided in the filename can be thought of as an implicit extra constraint comment line. Collectively, these are referred to as “constraints”. Each line is a “constraint”. Within each constraint the space-separated terms are “options”, and within that the comma-separated items are “terms” which may be negated with at most one exclaimation mark.

These represent a boolean formulae. The constraints are evaluated as the AND of constraint lines; a constraint is evaluated as the OR of its options and an option is evaluated as the AND of its terms. Overall build constraints are a boolean formula that is an AND of ORs of ANDs.

This level of complexity is rarely used in Go programs. Therefore this package aims to provide access to all these layers of nesting if required, but make it easy to forget about for basic use cases too.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format added in v0.3.0

func Format(t ConstraintsConvertable) (string, error)

Format constraints according to the syntax supported by the current Go version.

func GoBuildSyntaxSupported added in v0.3.0

func GoBuildSyntaxSupported() bool

GoBuildSyntaxSupported reports whether the current Go version supports the "//go:build" constraint syntax.

func PlusBuildSyntaxSupported added in v0.3.0

func PlusBuildSyntaxSupported() bool

PlusBuildSyntaxSupported reports whether the current Go version supports the "// +build" constraint syntax.

func SetTags

func SetTags(idents ...string) map[string]bool

SetTags builds a set where the given list of identifiers are true.

Types

type Constraint

type Constraint []Option

Constraint represents the OR of a list of Options.

func Any

func Any(opts ...OptionConvertable) Constraint

Any builds a Constraint that will be true if any of its options are true.

func ParseConstraint

func ParseConstraint(expr string) (Constraint, error)

ParseConstraint parses a space-separated list of options.

Example
package main

import (
	"fmt"

	"github.com/mmcloughlin/avo/buildtags"
)

func main() {
	c, err := buildtags.ParseConstraint("a,!b c")
	fmt.Print(c.GoString())
	fmt.Println(err)
}
Output:

// +build a,!b c
<nil>

func (Constraint) Evaluate

func (c Constraint) Evaluate(v map[string]bool) bool

Evaluate the boolean formula represented by c under the given assignment of tag values. This is the OR of the values of the constituent Options.

func (Constraint) GoString

func (c Constraint) GoString() string

GoString represents the Constraint as one +build comment line.

func (Constraint) ToConstraint

func (c Constraint) ToConstraint() Constraint

ToConstraint returns c.

func (Constraint) ToConstraints

func (c Constraint) ToConstraints() Constraints

ToConstraints returns the list of constraints containing just c.

func (Constraint) Validate

func (c Constraint) Validate() error

Validate validates the constraint.

type ConstraintConvertable

type ConstraintConvertable interface {
	ToConstraint() Constraint
}

ConstraintConvertable can be converted to a Constraint.

type Constraints

type Constraints []Constraint

Constraints represents the AND of a list of Constraint lines.

func And

And builds Constraints that will be true if all of its constraints are true.

func (Constraints) Evaluate

func (cs Constraints) Evaluate(v map[string]bool) bool

Evaluate the boolean formula represented by cs under the given assignment of tag values. This is the AND of the values of the constituent Constraints.

func (Constraints) GoString

func (cs Constraints) GoString() string

GoString represents Constraints as +build comment lines.

func (Constraints) ToConstraints

func (cs Constraints) ToConstraints() Constraints

ToConstraints returns cs.

func (Constraints) Validate

func (cs Constraints) Validate() error

Validate validates the constraints set.

type ConstraintsConvertable

type ConstraintsConvertable interface {
	ToConstraints() Constraints
}

ConstraintsConvertable can be converted to a Constraints object.

type Interface

type Interface interface {
	ConstraintsConvertable
	fmt.GoStringer
	Evaluate(v map[string]bool) bool
	Validate() error
}

Interface represents a build constraint.

type Option

type Option []Term

Option represents the AND of a list of Terms.

func Opt

func Opt(terms ...Term) Option

Opt builds an Option from the list of Terms.

func ParseOption

func ParseOption(expr string) (Option, error)

ParseOption parses a comma-separated list of terms.

func (Option) Evaluate

func (o Option) Evaluate(v map[string]bool) bool

Evaluate the boolean formula represented by o under the given assignment of tag values. This is the AND of the values of the constituent Terms.

func (Option) GoString

func (o Option) GoString() string

GoString represents the Option as a comma-separated list of terms.

func (Option) ToConstraint

func (o Option) ToConstraint() Constraint

ToConstraint returns a Constraint containing just this option.

func (Option) ToConstraints

func (o Option) ToConstraints() Constraints

ToConstraints returns Constraints containing just this option.

func (Option) ToOption

func (o Option) ToOption() Option

ToOption returns o.

func (Option) Validate

func (o Option) Validate() error

Validate validates o.

type OptionConvertable

type OptionConvertable interface {
	ToOption() Option
}

OptionConvertable can be converted to an Option.

type Term

type Term string

Term is an atomic term in a build constraint: an identifier or its negation.

func Not

func Not(ident string) Term

Not returns a term for the negation of ident.

func (Term) Evaluate

func (t Term) Evaluate(v map[string]bool) bool

Evaluate the term under the given set of identifier values.

func (Term) GoString

func (t Term) GoString() string

GoString returns t.

func (Term) IsNegated

func (t Term) IsNegated() bool

IsNegated reports whether t is the negation of an identifier.

func (Term) Name

func (t Term) Name() string

Name returns the identifier for this term.

func (Term) ToConstraint

func (t Term) ToConstraint() Constraint

ToConstraint returns a Constraint containing just this term.

func (Term) ToConstraints

func (t Term) ToConstraints() Constraints

ToConstraints returns Constraints containing just this term.

func (Term) ToOption

func (t Term) ToOption() Option

ToOption returns an Option containing just this term.

func (Term) Validate

func (t Term) Validate() error

Validate the term.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL