Skip to content

Commit

Permalink
all: update tools that manage copyright headers
Browse files Browse the repository at this point in the history
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 tailscale#6865

Signed-off-by: Will Norris <[email protected]>
  • Loading branch information
willnorris committed Jan 27, 2023
1 parent 71029ce commit 947c147
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 98 deletions.
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
BSD 3-Clause License

Copyright (c) 2020 Tailscale & AUTHORS.
All rights reserved.
Copyright (c) 2020 Tailscale Inc & AUTHORS.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
15 changes: 6 additions & 9 deletions cmd/addlicense/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@ import (
)

var (
year = flag.Int("year", 0, "copyright year")
file = flag.String("file", "", "file to modify")
)

func usage() {
fmt.Fprintf(os.Stderr, `
usage: addlicense -year YEAR -file FILE <subcommand args...>
usage: addlicense -file FILE <subcommand args...>
`[1:])

flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
addlicense adds a Tailscale license to the beginning of file,
using year as the copyright year.
addlicense adds a Tailscale license to the beginning of file.
It is intended for use with 'go generate', so it also runs a subcommand,
which presumably creates the file.
Sample usage:
addlicense -year 2021 -file pull_strings.go stringer -type=pull
addlicense -file pull_strings.go stringer -type=pull
`[1:])
os.Exit(2)
}
Expand All @@ -53,7 +51,7 @@ func main() {
check(err)
f, err := os.OpenFile(*file, os.O_TRUNC|os.O_WRONLY, 0644)
check(err)
_, err = fmt.Fprintf(f, license, *year)
_, err = fmt.Fprint(f, license)
check(err)
_, err = f.Write(b)
check(err)
Expand All @@ -69,8 +67,7 @@ func check(err error) {
}

var license = `
// Copyright (c) %d Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
`[1:]
2 changes: 1 addition & 1 deletion cmd/cloner/cloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
w("}")
}
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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func main() {
genView(buf, it, typ, pkg.Types)
}
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)
}
if runCloner {
Expand Down
2 changes: 1 addition & 1 deletion derp/derp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ func (c *sclient) handleFrameSendPacket(ft frameType, fl uint32) error {
// dropReason is why we dropped a DERP frame.
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 (
dropReasonUnknownDest dropReason = iota // unknown destination pubkey
Expand Down
2 changes: 1 addition & 1 deletion net/portmapper/pcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// https://www.rfc-editor.org/rfc/pdfrfc/rfc6887.txt.pdf
// 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

Expand Down
2 changes: 1 addition & 1 deletion net/portmapper/portmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 8 additions & 11 deletions scripts/check_license_headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
check_file() {
got=$1

for year in `seq 2019 2023`; do
want=$(cat <<EOF
// Copyright (c) $year Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
want=$(cat <<EOF
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
EOF
)
if [ "$got" = "$want" ]; then
return 0
fi
done
)
if [ "$got" = "$want" ]; then
return 0
fi
return 1
}

Expand Down Expand Up @@ -50,7 +47,7 @@ for file in $(find $1 -name '*.go' -not -path '*/.git/*'); do
# Generated syscall wrappers
;;
*)
header="$(head -3 $file)"
header="$(head -2 $file)"
if ! check_file "$header"; then
fail=1
echo "${file#$1/} doesn't have the right copyright header:"
Expand Down
5 changes: 2 additions & 3 deletions tstest/integration/gen_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func generate(goos string) {
log.Fatal(err)
}
var out bytes.Buffer
out.WriteString(`// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
out.WriteString(`// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// Code generated by gen_deps.go; DO NOT EDIT.
Expand Down
7 changes: 2 additions & 5 deletions tstest/integration/vms/gen/test_codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ package main

import (
_ "embed"
"fmt"
"log"
"os"
"time"

"github.com/dave/jennifer/jen"
"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -41,9 +39,8 @@ func main() {
}
defer fout.Close()

fmt.Fprintf(fout, "// Copyright (c) %d Tailscale Inc & AUTHORS All rights reserved.\n", time.Now().Year())
fout.WriteString("// Use of this source code is governed by a BSD-style\n")
fout.WriteString("// license that can be found in the LICENSE file.\n")
fout.WriteString("// Copyright (c) Tailscale Inc & AUTHORS\n")
fout.WriteString("// SPDX-License-Identifier: BSD-3-Clause\n")
fout.WriteString("\n")
fout.WriteString("// +build linux\n\n")

Expand Down
68 changes: 6 additions & 62 deletions util/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@
package codegen

import (
"bufio"
"bytes"
"fmt"
"go/ast"
"go/token"
"go/types"
"io"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
"tailscale.com/util/mak"
"tailscale.com/util/must"
)

// LoadTypes returns all named types in pkgName, keyed by their type name.
Expand Down Expand Up @@ -57,9 +52,8 @@ func HasNoClone(structTag string) bool {
return false
}

const copyrightHeader = `// Copyright (c) %d Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
const copyrightHeader = `// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
`

Expand Down Expand Up @@ -109,18 +103,16 @@ func (it *ImportTracker) Write(w io.Writer) {
fmt.Fprintf(w, ")\n\n")
}

func writeHeader(w io.Writer, tool, pkg string, copyrightYear int) {
if copyrightYear != 0 {
fmt.Fprintf(w, copyrightHeader, copyrightYear)
}
func writeHeader(w io.Writer, tool, pkg string) {
fmt.Fprint(w, copyrightHeader)
fmt.Fprintf(w, genAndPackageHeader, tool, pkg)
}

// 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.
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)
writeHeader(buf, tool, pkg.Name, copyrightYear)
writeHeader(buf, tool, pkg.Name)
it.Write(buf)
if _, err := buf.Write(contents.Bytes()); err != nil {
return err
Expand Down Expand Up @@ -271,51 +263,3 @@ func IsViewType(typ types.Type) bool {
}
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
}
2 changes: 1 addition & 1 deletion wgengine/magicsock/magicsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 (
// pingDiscovery means that purpose of a ping was to see if a
// path was valid.
Expand Down

0 comments on commit 947c147

Please sign in to comment.