Source file src/internal/goos/gengoos.go

     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 gooses []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 goosPrefix = `var knownOS = map[string]bool{`
    25  	inGOOS := false
    26  	for _, line := range strings.Split(string(data), "\n") {
    27  		if strings.HasPrefix(line, goosPrefix) {
    28  			inGOOS = true
    29  		} else if inGOOS && strings.HasPrefix(line, "}") {
    30  			break
    31  		} else if inGOOS {
    32  			goos := strings.Fields(line)[0]
    33  			goos = strings.TrimPrefix(goos, `"`)
    34  			goos = strings.TrimSuffix(goos, `":`)
    35  			gooses = append(gooses, goos)
    36  		}
    37  	}
    38  
    39  	for _, target := range gooses {
    40  		if target == "nacl" {
    41  			continue
    42  		}
    43  		var tags []string
    44  		if target == "linux" {
    45  			tags = append(tags, "!android") // must explicitly exclude android for linux
    46  		}
    47  		if target == "solaris" {
    48  			tags = append(tags, "!illumos") // must explicitly exclude illumos for solaris
    49  		}
    50  		if target == "darwin" {
    51  			tags = append(tags, "!ios") // must explicitly exclude ios for darwin
    52  		}
    53  		tags = append(tags, target) // must explicitly include target for bootstrapping purposes
    54  		var buf bytes.Buffer
    55  		fmt.Fprintf(&buf, "// Code generated by gengoos.go using 'go generate'. DO NOT EDIT.\n\n")
    56  		fmt.Fprintf(&buf, "//go:build %s\n\n", strings.Join(tags, " && "))
    57  		fmt.Fprintf(&buf, "package goos\n\n")
    58  		fmt.Fprintf(&buf, "const GOOS = `%s`\n\n", target)
    59  		for _, goos := range gooses {
    60  			value := 0
    61  			if goos == target {
    62  				value = 1
    63  			}
    64  			fmt.Fprintf(&buf, "const Is%s = %d\n", strings.Title(goos), value)
    65  		}
    66  		err := os.WriteFile("zgoos_"+target+".go", buf.Bytes(), 0666)
    67  		if err != nil {
    68  			log.Fatal(err)
    69  		}
    70  	}
    71  }
    72  

View as plain text