time/tzdata: generate zip constant during cmd/dist

We have a make.bash-time generation capability,
so use it to generate the embedded zip file for time/tzdata.
This is one less file to try to review in CLs like CL 455356.

For #22487.
Fixes #43350.

Change-Id: I2fcd0665fa0b1c830baec5fb4cd714483fea25a4
Reviewed-on: https://go-review.googlesource.com/c/go/+/455357
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Russ Cox 2022-12-05 23:39:49 -05:00 committed by Gopher Robot
parent 6bb003d032
commit 627765a861
7 changed files with 27 additions and 6725 deletions

1
.gitignore vendored
View File

@ -40,6 +40,7 @@ _testmain.go
/src/internal/buildcfg/zbootstrap.go
/src/runtime/internal/sys/zversion.go
/src/unicode/maketables
/src/time/tzdata/zzipdata.go
/test.out
/test/garbage/*.out
/test/pass.out

View File

@ -48,9 +48,8 @@ fi
cd zoneinfo
../mkzip ../../zoneinfo.zip
cd ../..
go generate time/tzdata
files="update.bash zoneinfo.zip ../../src/time/tzdata/zipdata.go"
files="update.bash zoneinfo.zip"
modified=true
if git diff --quiet $files; then
modified=false

View File

@ -558,6 +558,9 @@ var deptab = []struct {
{"go/build", []string{
"zcgo.go",
}},
{"time/tzdata", []string{
"zzipdata.go",
}},
}
// depsuffix records the allowed suffixes for source files.
@ -575,6 +578,7 @@ var gentab = []struct {
{"zosarch.go", mkzosarch},
{"zversion.go", mkzversion},
{"zcgo.go", mkzcgo},
{"zzipdata.go", mktzdata},
// not generated anymore, but delete the file if we see it
{"enam.c", nil},
@ -1346,7 +1350,8 @@ func cmdbootstrap() {
timelog("build", "go_bootstrap")
xprintf("Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.\n")
install("runtime") // dependency not visible in sources; also sets up textflag.h
install("runtime") // dependency not visible in sources; also sets up textflag.h
install("time/tzdata") // no dependency in sources; creates generated file
install("cmd/go")
if vflag > 0 {
xprintf("\n")

View File

@ -124,3 +124,20 @@ func mkzcgo(dir, file string) {
writefile(buf.String(), file, writeSkipSame)
}
// mktzdata src/time/tzdata/zzipdata.go:
//
// package tzdata
// const zipdata = "PK..."
func mktzdata(dir, file string) {
zip := readfile(filepath.Join(dir, "../../../lib/time/zoneinfo.zip"))
var buf strings.Builder
fmt.Fprintf(&buf, "// Code generated by go tool dist; DO NOT EDIT.\n")
fmt.Fprintln(&buf)
fmt.Fprintf(&buf, "package tzdata\n")
fmt.Fprintln(&buf)
fmt.Fprintf(&buf, "const zipdata = %q\n", zip)
writefile(buf.String(), file, writeSkipSame)
}

View File

@ -1,77 +0,0 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// This program generates zipdata.go from $GOROOT/lib/time/zoneinfo.zip.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
// header is put at the start of the generated file.
// The string addition avoids this file (generate_zipdata.go) from
// matching the "generated file" regexp.
const header = `// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
` + `// Code generated by generate_zipdata. DO NOT EDIT.
// This file contains an embedded zip archive that contains time zone
// files compiled using the code and data maintained as part of the
// IANA Time Zone Database.
// The IANA asserts that the data is in the public domain.
// For more information, see
// https://www.iana.org/time-zones
// ftp://ftp.iana.org/tz/code/tz-link.html
// https://datatracker.ietf.org/doc/html/rfc6557
package tzdata
const zipdata = `
func main() {
// We should be run in the $GOROOT/src/time/tzdata directory.
data, err := os.ReadFile("../../../lib/time/zoneinfo.zip")
if err != nil {
die("cannot find zoneinfo.zip file: %v", err)
}
of, err := os.Create("zipdata.go")
if err != nil {
die("%v", err)
}
buf := bufio.NewWriter(of)
buf.WriteString(header)
ds := string(data)
i := 0
const chunk = 60
for ; i+chunk < len(data); i += chunk {
if i > 0 {
buf.WriteRune('\t')
}
fmt.Fprintf(buf, "%s +\n", strconv.Quote(ds[i:i+chunk]))
}
fmt.Fprintf(buf, "\t%s\n", strconv.Quote(ds[i:]))
if err := buf.Flush(); err != nil {
die("error writing to zipdata.go: %v", err)
}
if err := of.Close(); err != nil {
die("error closing zipdata.go: %v", err)
}
}
func die(format string, args ...any) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}

View File

@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run generate_zipdata.go
// Package tzdata provides an embedded copy of the timezone database.
// If this package is imported anywhere in the program, then if
// the time package cannot find tzdata files on the system,
@ -67,6 +65,8 @@ func loadFromEmbeddedTZData(name string) (string, error) {
zheader = 0x04034b50
)
// zipdata is provided by zzipdata.go,
// which is generated by cmd/dist during make.bash.
z := zipdata
idx := len(z) - ztailsize

File diff suppressed because it is too large Load Diff