Home | History | Annotate | Download | only in runtime
      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 	"io/ioutil"
     13 	"log"
     14 	"strconv"
     15 	"strings"
     16 )
     17 
     18 var gooses, goarches []string
     19 
     20 func main() {
     21 	data, err := ioutil.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 %#q: %v", strings.TrimPrefix(line, goosPrefix), 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.Fatal("parsing goarchList: %v", err)
     41 			}
     42 			goarches = strings.Fields(text)
     43 		}
     44 	}
     45 
     46 	for _, target := range gooses {
     47 		var buf bytes.Buffer
     48 		fmt.Fprintf(&buf, "// generated by gengoos.go using 'go generate'\n\n")
     49 		if target == "linux" {
     50 			fmt.Fprintf(&buf, "// +build !android\n\n") // must explicitly exclude android for linux
     51 		}
     52 		fmt.Fprintf(&buf, "package runtime\n\n")
     53 		fmt.Fprintf(&buf, "const theGoos = `%s`\n\n", target)
     54 		for _, goos := range gooses {
     55 			value := 0
     56 			if goos == target {
     57 				value = 1
     58 			}
     59 			fmt.Fprintf(&buf, "const goos_%s = %d\n", goos, value)
     60 		}
     61 		err := ioutil.WriteFile("zgoos_"+target+".go", buf.Bytes(), 0666)
     62 		if err != nil {
     63 			log.Fatal(err)
     64 		}
     65 	}
     66 
     67 	for _, target := range goarches {
     68 		var buf bytes.Buffer
     69 		fmt.Fprintf(&buf, "// generated by gengoos.go using 'go generate'\n\n")
     70 		fmt.Fprintf(&buf, "package runtime\n\n")
     71 		fmt.Fprintf(&buf, "const theGoarch = `%s`\n\n", target)
     72 		for _, goarch := range goarches {
     73 			value := 0
     74 			if goarch == target {
     75 				value = 1
     76 			}
     77 			fmt.Fprintf(&buf, "const goarch_%s = %d\n", goarch, value)
     78 		}
     79 		err := ioutil.WriteFile("zgoarch_"+target+".go", buf.Bytes(), 0666)
     80 		if err != nil {
     81 			log.Fatal(err)
     82 		}
     83 	}
     84 }
     85