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

View as plain text