gengoos.mx raw

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