github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/internal/sys/gengoos.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  // +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 gooses, 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 (
    26  		goosPrefix   = `const goosList = `
    27  		goarchPrefix = `const goarchList = `
    28  	)
    29  	for _, line := range strings.Split(string(data), "\n") {
    30  		if strings.HasPrefix(line, goosPrefix) {
    31  			text, err := strconv.Unquote(strings.TrimPrefix(line, goosPrefix))
    32  			if err != nil {
    33  				log.Fatalf("parsing goosList: %v", err)
    34  			}
    35  			gooses = strings.Fields(text)
    36  		}
    37  		if strings.HasPrefix(line, goarchPrefix) {
    38  			text, err := strconv.Unquote(strings.TrimPrefix(line, goarchPrefix))
    39  			if err != nil {
    40  				log.Fatalf("parsing goarchList: %v", err)
    41  			}
    42  			goarches = strings.Fields(text)
    43  		}
    44  	}
    45  
    46  	for _, target := range gooses {
    47  		if target == "nacl" {
    48  			continue
    49  		}
    50  		var buf bytes.Buffer
    51  		fmt.Fprintf(&buf, "// Code generated by gengoos.go using 'go generate'. DO NOT EDIT.\n\n")
    52  		if target == "linux" {
    53  			fmt.Fprintf(&buf, "// +build !android\n") // must explicitly exclude android for linux
    54  		}
    55  		if target == "solaris" {
    56  			fmt.Fprintf(&buf, "// +build !illumos\n") // must explicitly exclude illumos for solaris
    57  		}
    58  		if target == "darwin" {
    59  			fmt.Fprintf(&buf, "// +build !ios\n") // must explicitly exclude ios for darwin
    60  		}
    61  		fmt.Fprintf(&buf, "// +build %s\n\n", target) // must explicitly include target for bootstrapping purposes
    62  		fmt.Fprintf(&buf, "package sys\n\n")
    63  		fmt.Fprintf(&buf, "const GOOS = `%s`\n\n", target)
    64  		for _, goos := range gooses {
    65  			value := 0
    66  			if goos == target {
    67  				value = 1
    68  			}
    69  			fmt.Fprintf(&buf, "const Goos%s = %d\n", strings.Title(goos), value)
    70  		}
    71  		err := os.WriteFile("zgoos_"+target+".go", buf.Bytes(), 0666)
    72  		if err != nil {
    73  			log.Fatal(err)
    74  		}
    75  	}
    76  
    77  	for _, target := range goarches {
    78  		if target == "amd64p32" {
    79  			continue
    80  		}
    81  		var buf bytes.Buffer
    82  		fmt.Fprintf(&buf, "// Code generated by gengoos.go using 'go generate'. DO NOT EDIT.\n\n")
    83  		fmt.Fprintf(&buf, "// +build %s\n\n", target) // must explicitly include target for bootstrapping purposes
    84  		fmt.Fprintf(&buf, "package sys\n\n")
    85  		fmt.Fprintf(&buf, "const GOARCH = `%s`\n\n", target)
    86  		for _, goarch := range goarches {
    87  			value := 0
    88  			if goarch == target {
    89  				value = 1
    90  			}
    91  			fmt.Fprintf(&buf, "const Goarch%s = %d\n", strings.Title(goarch), value)
    92  		}
    93  		err := os.WriteFile("zgoarch_"+target+".go", buf.Bytes(), 0666)
    94  		if err != nil {
    95  			log.Fatal(err)
    96  		}
    97  	}
    98  }