github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/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  	"strings"
    15  )
    16  
    17  var goarches []string
    18  
    19  func main() {
    20  	data, err := os.ReadFile("../../go/build/syslist.go")
    21  	if err != nil {
    22  		log.Fatal(err)
    23  	}
    24  	const goarchPrefix = `var knownArch = map[string]bool{`
    25  	inGOARCH := false
    26  	for _, line := range strings.Split(string(data), "\n") {
    27  		if strings.HasPrefix(line, goarchPrefix) {
    28  			inGOARCH = true
    29  		} else if inGOARCH && strings.HasPrefix(line, "}") {
    30  			break
    31  		} else if inGOARCH {
    32  			goarch := strings.Fields(line)[0]
    33  			goarch = strings.TrimPrefix(goarch, `"`)
    34  			goarch = strings.TrimSuffix(goarch, `":`)
    35  			goarches = append(goarches, goarch)
    36  		}
    37  	}
    38  
    39  	for _, target := range goarches {
    40  		if target == "amd64p32" {
    41  			continue
    42  		}
    43  		var buf bytes.Buffer
    44  		fmt.Fprintf(&buf, "// Code generated by gengoarch.go using 'go generate'. DO NOT EDIT.\n\n")
    45  		fmt.Fprintf(&buf, "//go:build %s\n\n", target) // must explicitly include target for bootstrapping purposes
    46  		fmt.Fprintf(&buf, "package goarch\n\n")
    47  		fmt.Fprintf(&buf, "const GOARCH = `%s`\n\n", target)
    48  		for _, goarch := range goarches {
    49  			value := 0
    50  			if goarch == target {
    51  				value = 1
    52  			}
    53  			fmt.Fprintf(&buf, "const Is%s = %d\n", strings.Title(goarch), value)
    54  		}
    55  		err := os.WriteFile("zgoarch_"+target+".go", buf.Bytes(), 0666)
    56  		if err != nil {
    57  			log.Fatal(err)
    58  		}
    59  	}
    60  }