Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/dist/buildruntime.go

Documentation: cmd/dist

     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 defaultGOARM = `%s`\n", goarm)
    64  	fmt.Fprintf(&buf, "const defaultGOMIPS = `%s`\n", gomips)
    65  	fmt.Fprintf(&buf, "const defaultGOMIPS64 = `%s`\n", gomips64)
    66  	fmt.Fprintf(&buf, "const defaultGOPPC64 = `%s`\n", goppc64)
    67  	fmt.Fprintf(&buf, "const defaultGOEXPERIMENT = `%s`\n", goexperiment)
    68  	fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled)
    69  	fmt.Fprintf(&buf, "const defaultGO_LDSO = `%s`\n", defaultldso)
    70  	fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion())
    71  	fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n")
    72  	fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n")
    73  
    74  	writefile(buf.String(), file, writeSkipSame)
    75  }
    76  
    77  // mkobjabi writes cmd/internal/objabi/zbootstrap.go:
    78  //
    79  //	package objabi
    80  //
    81  //	const stackGuardMultiplierDefault = <multiplier value>
    82  //
    83  func mkobjabi(file string) {
    84  	var buf bytes.Buffer
    85  	fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
    86  	fmt.Fprintln(&buf)
    87  	fmt.Fprintf(&buf, "package objabi\n")
    88  	fmt.Fprintln(&buf)
    89  	fmt.Fprintf(&buf, "const stackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault())
    90  
    91  	writefile(buf.String(), file, writeSkipSame)
    92  }
    93  
    94  // stackGuardMultiplierDefault returns a multiplier to apply to the default
    95  // stack guard size. Larger multipliers are used for non-optimized
    96  // builds that have larger stack frames.
    97  func stackGuardMultiplierDefault() int {
    98  	for _, s := range strings.Split(os.Getenv("GO_GCFLAGS"), " ") {
    99  		if s == "-N" {
   100  			return 2
   101  		}
   102  	}
   103  	return 1
   104  }
   105  

View as plain text