| 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 | |
| 9 | set -e |
| 10 | |
| 11 | if [ "$#" -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 |
| 14 | fi |
| 15 | |
| 16 | # Determine the source. |
| 17 | if [ -d $1 ]; then |
| 18 | echo "assuming Go checkout at $1..." |
| 19 | |
| 20 | # Select the Go checkout. |
| 21 | GODIR=$1 |
| 22 | else |
| 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 |
| 30 | fi |
| 31 | |
| 32 | # Define src and dst. |
| 33 | SRC=$GODIR/src |
| 34 | DST=$(dirname $0) |
| 35 | |
| 36 | # Copy. |
| 37 | find "$DST" -mindepth 1 -not -name "$(basename $0)" -not -name "flightrecorder*.go" -exec rm -rf {} + # cleanup, except gen.bash and flight recorder |
| 38 | rsync -av $SRC/internal/trace/ $DST |
| 39 | rsync -av $SRC/internal/diff $DST/internal |
| 40 | |
| 41 | # Remove the trace_test.go file and the testprogs it invokes. |
| 42 | # This really tests the tracer, it's not necessary to it bring along |
| 43 | # The trace tests are also problematic because they fail to run on |
| 44 | # Go versions before tip. The testprog directory is problematic because |
| 45 | # of //go:build ignore, so we'd have to complicate the logic below to |
| 46 | # support it. |
| 47 | rm $DST/trace_test.go |
| 48 | rm -r $DST/testdata/testprog |
| 49 | |
| 50 | # Remove the tracev1 testdata to avoid checking in new binary files. |
| 51 | # Remove tracev1_test.go and internal/tracev1/parser_test.go because |
| 52 | # they fail without this data. |
| 53 | rm -r $DST/internal/tracev1/testdata |
| 54 | rm $DST/tracev1_test.go |
| 55 | rm $DST/internal/tracev1/parser_test.go |
| 56 | |
| 57 | # Remove files that are only pertinent to cmd/trace. |
| 58 | rm $DST/export_test.go |
| 59 | rm $DST/gc*.go |
| 60 | rm $DST/mud*.go |
| 61 | rm $DST/summary*.go |
| 62 | rm -r $DST/traceviewer |
| 63 | |
| 64 | # Remove mktests.go because its a //go:build ignore file, so it would |
| 65 | # complicate the logic below. This codebase isn't the source of truth |
| 66 | # anyway. |
| 67 | rm $DST/testdata/mktests.go |
| 68 | |
| 69 | # Make some packages internal. |
| 70 | mv $DST/raw $DST/internal/raw |
| 71 | mv $DST/tracev2 $DST/internal/tracev2 |
| 72 | mv $DST/version $DST/internal/version |
| 73 | mv $DST/testtrace $DST/internal/testtrace |
| 74 | |
| 75 | # Fix up import paths. |
| 76 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's internal/trace golang.org/x/exp/trace ' |
| 77 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's golang.org/x/exp/trace/raw golang.org/x/exp/trace/internal/raw ' |
| 78 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's golang.org/x/exp/trace/tracev2 golang.org/x/exp/trace/internal/tracev2 ' |
| 79 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's golang.org/x/exp/trace/version golang.org/x/exp/trace/internal/version ' |
| 80 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's golang.org/x/exp/trace/testtrace golang.org/x/exp/trace/internal/testtrace ' |
| 81 | # Some tests rely on the following, relatively small internal packages |
| 82 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's internal/diff golang.org/x/exp/trace/internal/diff ' |
| 83 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's internal/txtar golang.org/x/tools/txtar ' |
| 84 | # Right now the only thing used from internal/testenv is testenv.Builder, which |
| 85 | # we can just inline |
| 86 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e '/"internal\/testenv"/d' |
| 87 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's testenv.Builder() os.Getenv("GO_BUILDER_NAME") g' |
| 88 | # Some of the tests rely on testing.(*T).ArtifactDir, which is a Go 1.26+ |
| 89 | # feature. For now we use TempDir. Typically we should only see these tests |
| 90 | # fail if they were already broken in the main repository anyway. |
| 91 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e 's t.ArtifactDir t.TempDir ' |
| 92 | |
| 93 | # Add build tag for Go 1.23 and generated code comment. |
| 94 | find $DST -name '*.go' | xargs -- sed -i'.tmp' -e '/LICENSE file./a \ |
| 95 | \ |
| 96 | // Code generated by "gen.bash" from internal/trace; DO NOT EDIT.\ |
| 97 | \ |
| 98 | //go:build go1.23' |
| 99 | |
| 100 | # Format the files. |
| 101 | find $DST -name '*.go' | xargs -- gofmt -w -s |
| 102 | |
| 103 | # Delete sed backups |
| 104 | find $DST -name '*.go.tmp' -delete |