github.com/elijahmorg/goternal@v1.18.0/goarch/gengoarch.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"log"
    13  	"os"
    14  	"strconv"
    15  	"strings"
    16  )
    17  
    18  var goarches []string
    19  
    20  func main() {
    21  	data, err := os.ReadFile("../../go/build/syslist.go")
    22  	if err != nil {
    23  		log.Fatal(err)
    24  	}
    25  	const goarchPrefix = `const goarchList = `
    26  	for _, line := range strings.Split(string(data), "\n") {
    27  		if strings.HasPrefix(line, goarchPrefix) {
    28  			text, err := strconv.Unquote(strings.TrimPrefix(line, goarchPrefix))
    29  			if err != nil {
    30  				log.Fatalf("parsing goarchList: %v", err)
    31  			}
    32  			goarches = strings.Fields(text)
    33  		}
    34  	}
    35  
    36  	for _, target := range goarches {
    37  		if target == "amd64p32" {
    38  			continue
    39  		}
    40  		var buf bytes.Buffer
    41  		fmt.Fprintf(&buf, "// Code generated by gengoarch.go using 'go generate'. DO NOT EDIT.\n\n")
    42  		fmt.Fprintf(&buf, "//go:build %s\n", target) // must explicitly include target for bootstrapping purposes
    43  		fmt.Fprintf(&buf, "package goarch\n\n")
    44  		fmt.Fprintf(&buf, "const GOARCH = `%s`\n\n", target)
    45  		for _, goarch := range goarches {
    46  			value := 0
    47  			if goarch == target {
    48  				value = 1
    49  			}
    50  			fmt.Fprintf(&buf, "const Is%s = %d\n", strings.Title(goarch), value)
    51  		}
    52  		err := os.WriteFile("zgoarch_"+target+".go", buf.Bytes(), 0666)
    53  		if err != nil {
    54  			log.Fatal(err)
    55  		}
    56  	}
    57  }