...
1#!/usr/bin/env bash
2# Copyright 2023 The Go Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style
4# license that can be found in the LICENSE file.
5
6# This script copies this directory to golang.org/x/exp/trace.
7# Just point it at a Go commit or a local Go checkout.
8
9set -e
10
11if [ "$#" -ne 1 ]; then
12 echo 'gen.bash expects one argument: a go.googlesource.com/go commit hash to generate the package from or a path to a Go checkout'
13 exit 1
14fi
15
16# Determine the source.
17if [ -d $1 ]; then
18 echo "assuming Go checkout at $1..."
19
20 # Select the Go checkout.
21 GODIR=$1
22else
23 echo "using $1 as a commit hash..."
24
25 # Check out Go.
26 TMP=$(mktemp -d)
27 git -C $TMP clone https://go.googlesource.com/go
28 git -C $TMP/go checkout $1
29 GODIR=$TMP/go
30fi
31
32# Define src and dst.
33SRC=$GODIR/src/internal/trace/v2
34DST=$(dirname $0)
35
36# Copy.
37rsync -av --delete $SRC/ $DST
38rm $DST/mkexp.bash
39
40# Remove the trace_test.go file and the testprogs it invokes.
41# This really tests the tracer, it's not necessary to it bring along
42# The trace tests are also problematic because they fail to run on
43# Go versions before tip. The testprog directory is problematic because
44# of //go:build ignore, so we'd have to complicate the logic below to
45# support it.
46rm $DST/trace_test.go
47rm -r $DST/testdata/testprog
48
49# Remove the oldtrace testdata to avoid checking in new binary files.
50# Remove oldtrace_test.go and internal/oldtrace/parser_test.go because
51# they fail without this data.
52rm -r $DST/internal/oldtrace/testdata
53rm $DST/oldtrace_test.go
54rm $DST/internal/oldtrace/parser_test.go
55
56# Remove mktests.go because its a //go:build ignore file, so it would
57# complicate the logic below. This codebase isn't the source of truth
58# anyway.
59rm $DST/testdata/mktests.go
60
61# Make some packages internal.
62mv $DST/raw $DST/internal/raw
63mv $DST/event $DST/internal/event
64mv $DST/version $DST/internal/version
65mv $DST/testtrace $DST/internal/testtrace
66
67# Move the debug commands out of testdata.
68mv $DST/testdata/cmd $DST/cmd
69
70# Fix up import paths.
71find $DST -name '*.go' | xargs -- sed -i 's/internal\/trace\/v2/golang.org\/x\/exp\/trace/'
72find $DST -name '*.go' | xargs -- sed -i 's/golang.org\/x\/exp\/trace\/raw/golang.org\/x\/exp\/trace\/internal\/raw/'
73find $DST -name '*.go' | xargs -- sed -i 's/golang.org\/x\/exp\/trace\/event/golang.org\/x\/exp\/trace\/internal\/event/'
74find $DST -name '*.go' | xargs -- sed -i 's/golang.org\/x\/exp\/trace\/event\/go122/golang.org\/x\/exp\/trace\/internal\/event\/go122/'
75find $DST -name '*.go' | xargs -- sed -i 's/golang.org\/x\/exp\/trace\/version/golang.org\/x\/exp\/trace\/internal\/version/'
76find $DST -name '*.go' | xargs -- sed -i 's/golang.org\/x\/exp\/trace\/testtrace/golang.org\/x\/exp\/trace\/internal\/testtrace/'
77find $DST -name '*.go' | xargs -- sed -i 's/internal\/txtar/golang.org\/x\/tools\/txtar/'
78
79# Add build tag for Go 1.21 and generated code comment.
80find $DST -name '*.go' | xargs -- sed -i '/LICENSE file./a \
81\
82// Code generated by "gen.bash" from internal/trace/v2; DO NOT EDIT.\n\n//go:build go1.21'
83
84# Format the files.
85find $DST -name '*.go' | xargs -- gofmt -w -s
86
87# Restore known files.
88git checkout gen.bash flightrecorder.go flightrecorder_test.go
View as plain text