github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/builders/golang/targets.go (about)

     1  package golang
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/apex/log"
     7  	"github.com/goreleaser/goreleaser/pkg/config"
     8  )
     9  
    10  type target struct {
    11  	os, arch, arm string
    12  }
    13  
    14  func (t target) String() string {
    15  	if t.arm != "" {
    16  		return fmt.Sprintf("%s_%s_%s", t.os, t.arch, t.arm)
    17  	}
    18  	return fmt.Sprintf("%s_%s", t.os, t.arch)
    19  }
    20  
    21  func matrix(build config.Build) (result []string) {
    22  	// nolint:prealloc
    23  	var targets []target
    24  	for _, target := range allBuildTargets(build) {
    25  		if !valid(target) {
    26  			log.WithField("target", target).
    27  				Debug("skipped invalid build")
    28  			continue
    29  		}
    30  		if ignored(build, target) {
    31  			log.WithField("target", target).
    32  				Debug("skipped ignored build")
    33  			continue
    34  		}
    35  		targets = append(targets, target)
    36  	}
    37  	for _, target := range targets {
    38  		result = append(result, target.String())
    39  	}
    40  	return
    41  }
    42  
    43  func allBuildTargets(build config.Build) (targets []target) {
    44  	for _, goos := range build.Goos {
    45  		for _, goarch := range build.Goarch {
    46  			if goarch == "arm" {
    47  				for _, goarm := range build.Goarm {
    48  					targets = append(targets, target{
    49  						os:   goos,
    50  						arch: goarch,
    51  						arm:  goarm,
    52  					})
    53  				}
    54  				continue
    55  			}
    56  			targets = append(targets, target{
    57  				os:   goos,
    58  				arch: goarch,
    59  			})
    60  		}
    61  	}
    62  	return
    63  }
    64  
    65  // TODO: this could be improved by using a map
    66  // https://github.com/goreleaser/goreleaser/pull/522#discussion_r164245014
    67  func ignored(build config.Build, target target) bool {
    68  	for _, ig := range build.Ignore {
    69  		if ig.Goos != "" && ig.Goos != target.os {
    70  			continue
    71  		}
    72  		if ig.Goarch != "" && ig.Goarch != target.arch {
    73  			continue
    74  		}
    75  		if ig.Goarm != "" && ig.Goarm != target.arm {
    76  			continue
    77  		}
    78  		return true
    79  	}
    80  	return false
    81  }
    82  
    83  func valid(target target) bool {
    84  	var s = target.os + target.arch
    85  	for _, a := range validTargets {
    86  		if a == s {
    87  			return true
    88  		}
    89  	}
    90  	return false
    91  }
    92  
    93  // list from https://golang.org/doc/install/source#environment
    94  var validTargets = []string{
    95  	"androidarm",
    96  	"darwin386",
    97  	"darwinamd64",
    98  	// "darwinarm", - requires admin rights and other ios stuff
    99  	// "darwinarm64", - requires admin rights and other ios stuff
   100  	"dragonflyamd64",
   101  	"freebsd386",
   102  	"freebsdamd64",
   103  	"freebsdarm",
   104  	"linux386",
   105  	"linuxamd64",
   106  	"linuxarm",
   107  	"linuxarm64",
   108  	"linuxppc64",
   109  	"linuxppc64le",
   110  	"linuxmips",
   111  	"linuxmipsle",
   112  	"linuxmips64",
   113  	"linuxmips64le",
   114  	"linuxs390x",
   115  	"netbsd386",
   116  	"netbsdamd64",
   117  	"netbsdarm",
   118  	"openbsd386",
   119  	"openbsdamd64",
   120  	"openbsdarm",
   121  	"plan9386",
   122  	"plan9amd64",
   123  	"solarisamd64",
   124  	"windows386",
   125  	"windowsamd64",
   126  }