github.com/metux/go-metabuild@v0.0.0-20240118143255-d9ed5ab697f9/util/compiler/base/machine.go (about)

     1  package base
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type Machine struct {
     9  	Arch   string
    10  	Vendor string
    11  	Kernel string
    12  	System string
    13  	Valid  bool
    14  }
    15  
    16  func (mach Machine) String() string {
    17  	if !mach.Valid {
    18  		return "unknown"
    19  	}
    20  	if mach.Vendor == "" {
    21  		if mach.System == "" {
    22  			return fmt.Sprintf("%s-%s", mach.Arch, mach.Kernel)
    23  		}
    24  		return fmt.Sprintf("%s-%s-%s", mach.Arch, mach.Kernel, mach.System)
    25  	}
    26  	if mach.System == "" {
    27  		return fmt.Sprintf("%s-%s-%s", mach.Arch, mach.Vendor, mach.Kernel)
    28  	}
    29  	return fmt.Sprintf("%s-%s-%s-%s", mach.Arch, mach.Vendor, mach.Kernel, mach.System)
    30  }
    31  
    32  func ParseMachine(mach string) Machine {
    33  	s := strings.Split(strings.TrimSpace(mach), "-")
    34  	if len(s) == 0 || s[0] == "" {
    35  		return Machine{}
    36  	}
    37  	if len(s) == 3 {
    38  		// no vendor field
    39  		if s[1] == "linux" {
    40  			return Machine{Arch: s[0], Kernel: s[1], System: s[2], Valid: true}
    41  		}
    42  		// FIXME: are there other special cases ?
    43  		return Machine{Arch: s[0], Vendor: s[1], Kernel: s[2], Valid: true}
    44  	}
    45  	return Machine{Arch: s[0], Vendor: s[1], Kernel: s[2], System: s[3], Valid: true}
    46  }