lab.nexedi.com/kirr/go123@v0.0.0-20240207185015-8299741fa871/tracing/internal/xruntime/g_typedef (about)

     1  #!/bin/bash
     2  # g_typedef -- generate type definition of runtime.g
     3  #
     4  # Copyright (C) 2017-2024  Nexedi SA and Contributors.
     5  #                          Kirill Smelkov <kirr@nexedi.com>
     6  #
     7  # This program is free software: you can Use, Study, Modify and Redistribute
     8  # it under the terms of the GNU General Public License version 3, or (at your
     9  # option) any later version, as published by the Free Software Foundation.
    10  #
    11  # You can also Link and Combine this program with other software covered by
    12  # the terms of any of the Free Software licenses or any of the Open Source
    13  # Initiative approved licenses and Convey the resulting work. Corresponding
    14  # source of such a combination shall include the source code for all other
    15  # software used.
    16  #
    17  # This program is distributed WITHOUT ANY WARRANTY; without even the implied
    18  # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    19  #
    20  # See COPYING file for full licensing terms.
    21  # See https://www.nexedi.com/licensing for rationale and options.
    22  
    23  goexec=	# set by main driver to current go compiler
    24  gover=	# go<maj>.<min> version (without .patch)
    25  gomaj=	# go<maj>
    26  gomin=	# <min>
    27  govern=	# e.g. 109 for go1.9
    28  
    29  # goset <goexec> - set <goexec> as current go
    30  goset() {
    31  	goexec=$1
    32  	# go1.1 go1.2 go1.3 go1.4 go1.5 go1.6 go1.7 go1.8 go1.9 go1.10 -> go1.10
    33  	gover=`$goexec list -f '{{ range context.ReleaseTags }} {{ .}}{{end}}' runtime |awk '{print $NF}'`
    34  	IFS=. read gomaj gomin < <(echo "$gover")
    35  	govern=$((${gomaj#go} * 100 + $gomin))
    36  }
    37  
    38  # typedef <type> - print type definition
    39  typedef() {
    40  	$goexec doc -c -u $1 |sed -n -e '
    41  # ignore anything not starting with `type `
    42  /^type /!b
    43  
    44  # if there is opening `{` - loop till closing `}`
    45  /{/{:loop p; n; /^}/!b loop; p; b}
    46  
    47  # else just print this single line
    48  p
    49  '
    50  }
    51  
    52  # typedef_g 	- print <g> & friends definitions
    53  typedef_g() {
    54  	typedef runtime.g
    55  	typedef runtime.stack
    56  	typedef runtime._panic
    57  	typedef runtime._defer
    58  	typedef runtime.gobuf
    59  	typedef runtime.funcval
    60  	#typedef runtime.sudog
    61  	#typedef runtime.hchan
    62  	typedef runtime.timer
    63  	typedef runtime.guintptr
    64  	typedef runtime.puintptr
    65  	typedef runtime.muintptr
    66  	#typedef runtime.m
    67  
    68  	if (( $govern < 109 )); then
    69  		typedef runtime.stkbar
    70  	fi
    71  
    72  	if (( $govern >= 111 )); then
    73  		typedef runtime.waitReason
    74  		typedef runtime.ancestorInfo
    75  	fi
    76  
    77  	if (( $govern >= 119 )); then
    78  		typedef runtime.goroutineProfileStateHolder
    79  	fi
    80  
    81  	if (( $govern >= 121 )); then
    82  		typedef runtime.gTraceState
    83  		typedef runtime.traceTime
    84  	fi
    85  
    86  	if (( $govern >= 122 )); then
    87  		typedef runtime.coro
    88  		typedef runtime.traceSchedResourceState
    89  	fi
    90  }
    91  
    92  # typedef_g_fixed - print adjusted <g> & friends definitions
    93  typedef_g_fixed() {
    94  	typedef_g $1 | \
    95  		sed -e 's/\<sys.Uintreg\>/uintreg/'
    96  
    97  	echo "type uintreg  uint	// FIXME wrong on amd64p32"
    98  	echo "type m struct {}		// FIXME stub"
    99  	echo "type sudog struct {}	// FIXME stub"
   100  
   101  	if (( $govern >= 110 )); then
   102  		echo "type timersBucket struct {}	// FIXME stub"
   103  	fi
   104  }
   105  
   106  
   107  # gen_zruntime	- generate zruntime_g_<gover>.go
   108  _gen_zruntime() {
   109  	echo "// Code generated by g_typedef; DO NOT EDIT."
   110  	echo
   111  	echo "// +build $gover,!$gomaj.$((gomin+1))"
   112  	echo
   113  	echo "package xruntime"
   114  	echo
   115  	echo 'import "unsafe"'
   116  	if (( $govern >= 119 )); then
   117  		echo 'import "sync/atomic"'  # instead of runtime/internal/atomic - for atomic.Uint32
   118  	fi
   119  	echo
   120  	typedef_g_fixed $go
   121  }
   122  gen_zruntime() {
   123  	out="zruntime_g_$gover.go"
   124  	# emit with `any` replaced by `interface{}`.
   125  	# we do this to avoid bumping lang to go1.18 in go.mod, so that go123
   126  	# could still be used with older go compilers.
   127  	_gen_zruntime | \
   128  		gofmt -r 'any -> interface{}' \
   129  		> $out
   130  }
   131  
   132  
   133  # main driver
   134  gov="go18 go19 go1.10 go1.11 go1.12 go1.13 go1.14 go1.15 go1.16 go1.17 go1.18 go1.19 go1.20 go1.21 go1.22"
   135  
   136  for g in $gov; do
   137  	goset $g
   138  	gen_zruntime
   139  done