Source file src/cmd/dist/buildruntime.go

     1  // Copyright 2012 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  package main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  /*
    15   * Helpers for building runtime.
    16   */
    17  
    18  // mkzversion writes zversion.go:
    19  //
    20  //	package sys
    21  //
    22  //	const StackGuardMultiplier = <multiplier value>
    23  //
    24  func mkzversion(dir, file string) {
    25  	var buf bytes.Buffer
    26  	fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
    27  	fmt.Fprintln(&buf)
    28  	fmt.Fprintf(&buf, "package sys\n")
    29  	fmt.Fprintln(&buf)
    30  	fmt.Fprintf(&buf, "const StackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault())
    31  
    32  	writefile(buf.String(), file, writeSkipSame)
    33  }
    34  
    35  // mkbuildcfg writes internal/buildcfg/zbootstrap.go:
    36  //
    37  //	package buildcfg
    38  //
    39  //	const defaultGOROOT = <goroot>
    40  //	const defaultGO386 = <go386>
    41  //	...
    42  //	const defaultGOOS = runtime.GOOS
    43  //	const defaultGOARCH = runtime.GOARCH
    44  //
    45  // The use of runtime.GOOS and runtime.GOARCH makes sure that
    46  // a cross-compiled compiler expects to compile for its own target
    47  // system. That is, if on a Mac you do:
    48  //
    49  //	GOOS=linux GOARCH=ppc64 go build cmd/compile
    50  //
    51  // the resulting compiler will default to generating linux/ppc64 object files.
    52  // This is more useful than having it default to generating objects for the
    53  // original target (in this example, a Mac).
    54  func mkbuildcfg(file string) {
    55  	var buf bytes.Buffer
    56  	fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
    57  	fmt.Fprintln(&buf)
    58  	fmt.Fprintf(&buf, "package buildcfg\n")
    59  	fmt.Fprintln(&buf)
    60  	fmt.Fprintf(&buf, "import \"runtime\"\n")
    61  	fmt.Fprintln(&buf)
    62  	fmt.Fprintf(&buf, "const defaultGO386 = `%s`\n", go386)
    63  	fmt.Fprintf(&buf, "const defaultGOAMD64 = `%s`\n", goamd64)
    64  	fmt.Fprintf(&buf, "const defaultGOARM = `%s`\n", goarm)
    65  	fmt.Fprintf(&buf, "const defaultGOMIPS = `%s`\n", gomips)
    66  	fmt.Fprintf(&buf, "const defaultGOMIPS64 = `%s`\n", gomips64)
    67  	fmt.Fprintf(&buf, "const defaultGOPPC64 = `%s`\n", goppc64)
    68  	fmt.Fprintf(&buf, "const defaultGOEXPERIMENT = `%s`\n", goexperiment)
    69  	fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled)
    70  	fmt.Fprintf(&buf, "const defaultGO_LDSO = `%s`\n", defaultldso)
    71  	fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion())
    72  	fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n")
    73  	fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n")
    74  
    75  	writefile(buf.String(), file, writeSkipSame)
    76  }
    77  
    78  // mkobjabi writes cmd/internal/objabi/zbootstrap.go:
    79  //
    80  //	package objabi
    81  //
    82  //	const stackGuardMultiplierDefault = <multiplier value>
    83  //
    84  func mkobjabi(file string) {
    85  	var buf bytes.Buffer
    86  	fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
    87  	fmt.Fprintln(&buf)
    88  	fmt.Fprintf(&buf, "package objabi\n")
    89  	fmt.Fprintln(&buf)
    90  	fmt.Fprintf(&buf, "const stackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault())
    91  
    92  	writefile(buf.String(), file, writeSkipSame)
    93  }
    94  
    95  // stackGuardMultiplierDefault returns a multiplier to apply to the default
    96  // stack guard size. Larger multipliers are used for non-optimized
    97  // builds that have larger stack frames.
    98  func stackGuardMultiplierDefault() int {
    99  	for _, s := range strings.Split(os.Getenv("GO_GCFLAGS"), " ") {
   100  		if s == "-N" {
   101  			return 2
   102  		}
   103  	}
   104  	return 1
   105  }
   106  

View as plain text