all: update tools that manage copyright headers

Update all code generation tools, and those that check for license
headers to use the new standard header.

Also update copyright statement in LICENSE file.

Fixes #6865

Signed-off-by: Will Norris <will@tailscale.com>
pull/7094/head
Will Norris 2 years ago committed by Will Norris
parent 71029cea2d
commit 947c14793a

@ -1,7 +1,6 @@
BSD 3-Clause License BSD 3-Clause License
Copyright (c) 2020 Tailscale & AUTHORS. Copyright (c) 2020 Tailscale Inc & AUTHORS.
All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:

@ -14,26 +14,24 @@ import (
) )
var ( var (
year = flag.Int("year", 0, "copyright year")
file = flag.String("file", "", "file to modify") file = flag.String("file", "", "file to modify")
) )
func usage() { func usage() {
fmt.Fprintf(os.Stderr, ` fmt.Fprintf(os.Stderr, `
usage: addlicense -year YEAR -file FILE <subcommand args...> usage: addlicense -file FILE <subcommand args...>
`[1:]) `[1:])
flag.PrintDefaults() flag.PrintDefaults()
fmt.Fprintf(os.Stderr, ` fmt.Fprintf(os.Stderr, `
addlicense adds a Tailscale license to the beginning of file, addlicense adds a Tailscale license to the beginning of file.
using year as the copyright year.
It is intended for use with 'go generate', so it also runs a subcommand, It is intended for use with 'go generate', so it also runs a subcommand,
which presumably creates the file. which presumably creates the file.
Sample usage: Sample usage:
addlicense -year 2021 -file pull_strings.go stringer -type=pull addlicense -file pull_strings.go stringer -type=pull
`[1:]) `[1:])
os.Exit(2) os.Exit(2)
} }
@ -53,7 +51,7 @@ func main() {
check(err) check(err)
f, err := os.OpenFile(*file, os.O_TRUNC|os.O_WRONLY, 0644) f, err := os.OpenFile(*file, os.O_TRUNC|os.O_WRONLY, 0644)
check(err) check(err)
_, err = fmt.Fprintf(f, license, *year) _, err = fmt.Fprint(f, license)
check(err) check(err)
_, err = f.Write(b) _, err = f.Write(b)
check(err) check(err)
@ -69,8 +67,7 @@ func check(err error) {
} }
var license = ` var license = `
// Copyright (c) %d Tailscale Inc & AUTHORS All rights reserved. // Copyright (c) Tailscale Inc & AUTHORS
// Use of this source code is governed by a BSD-style // SPDX-License-Identifier: BSD-3-Clause
// license that can be found in the LICENSE file.
`[1:] `[1:]

@ -79,7 +79,7 @@ func main() {
w("}") w("}")
} }
cloneOutput := pkg.Name + "_clone.go" cloneOutput := pkg.Name + "_clone.go"
if err := codegen.WritePackageFile("tailscale.com/cmd/cloner", pkg, cloneOutput, codegen.CopyrightYear("."), it, buf); err != nil { if err := codegen.WritePackageFile("tailscale.com/cmd/cloner", pkg, cloneOutput, it, buf); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

@ -383,7 +383,7 @@ func main() {
genView(buf, it, typ, pkg.Types) genView(buf, it, typ, pkg.Types)
} }
out := pkg.Name + "_view.go" out := pkg.Name + "_view.go"
if err := codegen.WritePackageFile("tailscale/cmd/viewer", pkg, out, codegen.CopyrightYear("."), it, buf); err != nil { if err := codegen.WritePackageFile("tailscale/cmd/viewer", pkg, out, it, buf); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if runCloner { if runCloner {

@ -955,7 +955,7 @@ func (c *sclient) handleFrameSendPacket(ft frameType, fl uint32) error {
// dropReason is why we dropped a DERP frame. // dropReason is why we dropped a DERP frame.
type dropReason int type dropReason int
//go:generate go run tailscale.com/cmd/addlicense -year 2021 -file dropreason_string.go go run golang.org/x/tools/cmd/stringer -type=dropReason -trimprefix=dropReason //go:generate go run tailscale.com/cmd/addlicense -file dropreason_string.go go run golang.org/x/tools/cmd/stringer -type=dropReason -trimprefix=dropReason
const ( const (
dropReasonUnknownDest dropReason = iota // unknown destination pubkey dropReasonUnknownDest dropReason = iota // unknown destination pubkey

@ -17,7 +17,7 @@ import (
// https://www.rfc-editor.org/rfc/pdfrfc/rfc6887.txt.pdf // https://www.rfc-editor.org/rfc/pdfrfc/rfc6887.txt.pdf
// https://tools.ietf.org/html/rfc6887 // https://tools.ietf.org/html/rfc6887
//go:generate go run tailscale.com/cmd/addlicense -year 2021 -file pcpresultcode_string.go go run golang.org/x/tools/cmd/stringer -type=pcpResultCode -trimprefix=pcpCode //go:generate go run tailscale.com/cmd/addlicense -file pcpresultcode_string.go go run golang.org/x/tools/cmd/stringer -type=pcpResultCode -trimprefix=pcpCode
type pcpResultCode uint8 type pcpResultCode uint8

@ -586,7 +586,7 @@ func (c *Client) createOrGetMapping(ctx context.Context) (external netip.AddrPor
} }
} }
//go:generate go run tailscale.com/cmd/addlicense -year 2021 -file pmpresultcode_string.go go run golang.org/x/tools/cmd/stringer -type=pmpResultCode -trimprefix=pmpCode //go:generate go run tailscale.com/cmd/addlicense -file pmpresultcode_string.go go run golang.org/x/tools/cmd/stringer -type=pmpResultCode -trimprefix=pmpCode
type pmpResultCode uint16 type pmpResultCode uint16

@ -9,17 +9,14 @@
check_file() { check_file() {
got=$1 got=$1
for year in `seq 2019 2023`; do want=$(cat <<EOF
want=$(cat <<EOF // Copyright (c) Tailscale Inc & AUTHORS
// Copyright (c) $year Tailscale Inc & AUTHORS All rights reserved. // SPDX-License-Identifier: BSD-3-Clause
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
EOF EOF
) )
if [ "$got" = "$want" ]; then if [ "$got" = "$want" ]; then
return 0 return 0
fi fi
done
return 1 return 1
} }
@ -50,7 +47,7 @@ for file in $(find $1 -name '*.go' -not -path '*/.git/*'); do
# Generated syscall wrappers # Generated syscall wrappers
;; ;;
*) *)
header="$(head -3 $file)" header="$(head -2 $file)"
if ! check_file "$header"; then if ! check_file "$header"; then
fail=1 fail=1
echo "${file#$1/} doesn't have the right copyright header:" echo "${file#$1/} doesn't have the right copyright header:"

@ -35,9 +35,8 @@ func generate(goos string) {
log.Fatal(err) log.Fatal(err)
} }
var out bytes.Buffer var out bytes.Buffer
out.WriteString(`// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. out.WriteString(`// Copyright (c) Tailscale Inc & AUTHORS
// Use of this source code is governed by a BSD-style // SPDX-License-Identifier: BSD-3-Clause
// license that can be found in the LICENSE file.
// Code generated by gen_deps.go; DO NOT EDIT. // Code generated by gen_deps.go; DO NOT EDIT.

@ -7,10 +7,8 @@ package main
import ( import (
_ "embed" _ "embed"
"fmt"
"log" "log"
"os" "os"
"time"
"github.com/dave/jennifer/jen" "github.com/dave/jennifer/jen"
"github.com/iancoleman/strcase" "github.com/iancoleman/strcase"
@ -41,9 +39,8 @@ func main() {
} }
defer fout.Close() defer fout.Close()
fmt.Fprintf(fout, "// Copyright (c) %d Tailscale Inc & AUTHORS All rights reserved.\n", time.Now().Year()) fout.WriteString("// Copyright (c) Tailscale Inc & AUTHORS\n")
fout.WriteString("// Use of this source code is governed by a BSD-style\n") fout.WriteString("// SPDX-License-Identifier: BSD-3-Clause\n")
fout.WriteString("// license that can be found in the LICENSE file.\n")
fout.WriteString("\n") fout.WriteString("\n")
fout.WriteString("// +build linux\n\n") fout.WriteString("// +build linux\n\n")

@ -5,7 +5,6 @@
package codegen package codegen
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"go/ast" "go/ast"
@ -13,16 +12,12 @@ import (
"go/types" "go/types"
"io" "io"
"os" "os"
"path/filepath"
"reflect" "reflect"
"regexp"
"strconv"
"strings" "strings"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
"golang.org/x/tools/imports" "golang.org/x/tools/imports"
"tailscale.com/util/mak" "tailscale.com/util/mak"
"tailscale.com/util/must"
) )
// LoadTypes returns all named types in pkgName, keyed by their type name. // LoadTypes returns all named types in pkgName, keyed by their type name.
@ -57,9 +52,8 @@ func HasNoClone(structTag string) bool {
return false return false
} }
const copyrightHeader = `// Copyright (c) %d Tailscale Inc & AUTHORS All rights reserved. const copyrightHeader = `// Copyright (c) Tailscale Inc & AUTHORS
// Use of this source code is governed by a BSD-style // SPDX-License-Identifier: BSD-3-Clause
// license that can be found in the LICENSE file.
` `
@ -109,18 +103,16 @@ func (it *ImportTracker) Write(w io.Writer) {
fmt.Fprintf(w, ")\n\n") fmt.Fprintf(w, ")\n\n")
} }
func writeHeader(w io.Writer, tool, pkg string, copyrightYear int) { func writeHeader(w io.Writer, tool, pkg string) {
if copyrightYear != 0 { fmt.Fprint(w, copyrightHeader)
fmt.Fprintf(w, copyrightHeader, copyrightYear)
}
fmt.Fprintf(w, genAndPackageHeader, tool, pkg) fmt.Fprintf(w, genAndPackageHeader, tool, pkg)
} }
// WritePackageFile adds a file with the provided imports and contents to package. // WritePackageFile adds a file with the provided imports and contents to package.
// The tool param is used to identify the tool that generated package file. // The tool param is used to identify the tool that generated package file.
func WritePackageFile(tool string, pkg *packages.Package, path string, copyrightYear int, it *ImportTracker, contents *bytes.Buffer) error { func WritePackageFile(tool string, pkg *packages.Package, path string, it *ImportTracker, contents *bytes.Buffer) error {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
writeHeader(buf, tool, pkg.Name, copyrightYear) writeHeader(buf, tool, pkg.Name)
it.Write(buf) it.Write(buf)
if _, err := buf.Write(contents.Bytes()); err != nil { if _, err := buf.Write(contents.Bytes()); err != nil {
return err return err
@ -271,51 +263,3 @@ func IsViewType(typ types.Type) bool {
} }
return t.Field(0).Name() == "ж" return t.Field(0).Name() == "ж"
} }
// CopyrightYear reports the greatest copyright year in non-generated *.go files
// in the current directory, for use in the copyright line of generated code.
//
// It panics on I/O error, as it's assumed this is only being used by "go
// generate" or GitHub actions.
//
// TODO(bradfitz,dgentry): determine what heuristic to use for all this: latest
// year, earliest, none? don't list years at all? IANAL. Get advice of others.
// For now we just want to unbreak the tree. See Issue 6865.
func CopyrightYear(dir string) (year int) {
files, err := os.ReadDir(dir)
if err != nil {
panic(err)
}
rxYear := regexp.MustCompile(`^// Copyright \(c\) (20\d{2}) `)
rxGenerated := regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.$`)
Files:
for _, f := range files {
name := f.Name()
if !f.Type().IsRegular() ||
strings.HasPrefix(name, ".") || // includes emacs noise
!strings.HasSuffix(name, ".go") ||
strings.HasSuffix(name, "_clone.go") ||
strings.HasSuffix(name, "_view.go") ||
strings.HasSuffix(name, "_test.go") {
continue
}
src, err := os.ReadFile(filepath.Join(dir, name))
if err != nil {
panic(err)
}
bs := bufio.NewScanner(bytes.NewReader(src))
for bs.Scan() {
line := bs.Bytes()
if m := rxYear.FindSubmatch(line); m != nil {
if y := must.Get(strconv.Atoi(string(m[1]))); y > year {
year = y
}
continue
}
if rxGenerated.Match(line) {
continue Files
}
}
}
return year
}

@ -4061,7 +4061,7 @@ func (de *endpoint) sendDiscoPing(ep netip.AddrPort, discoKey key.DiscoPublic, t
// discoPingPurpose is the reason why a discovery ping message was sent. // discoPingPurpose is the reason why a discovery ping message was sent.
type discoPingPurpose int type discoPingPurpose int
//go:generate go run tailscale.com/cmd/addlicense -year 2020 -file discopingpurpose_string.go go run golang.org/x/tools/cmd/stringer -type=discoPingPurpose -trimprefix=ping //go:generate go run tailscale.com/cmd/addlicense -file discopingpurpose_string.go go run golang.org/x/tools/cmd/stringer -type=discoPingPurpose -trimprefix=ping
const ( const (
// pingDiscovery means that purpose of a ping was to see if a // pingDiscovery means that purpose of a ping was to see if a
// path was valid. // path was valid.

Loading…
Cancel
Save