mirror of https://github.com/restic/restic.git
build.go: Make `-ldflags` compatible to Go 1.5
This change uses the old syntax (-ldflags "-X foo bar") for Go <= 1.4 and the new syntax for (-ldflags "-X foo=bar") for Go 1.5 (without a warning).
This commit is contained in:
parent
b188217e83
commit
dca200c2e9
28
build.go
28
build.go
|
@ -229,6 +229,27 @@ func gitVersion() string {
|
|||
return version
|
||||
}
|
||||
|
||||
// Constants represents a set constants set in the final binary to the given
|
||||
// value.
|
||||
type Constants map[string]string
|
||||
|
||||
// LDFlags returns the string that can be passed to go build's `-ldflags`.
|
||||
func (cs Constants) LDFlags() string {
|
||||
l := make([]string, 0, len(cs))
|
||||
|
||||
if strings.HasPrefix(runtime.Version(), "go1.5") {
|
||||
for k, v := range cs {
|
||||
l = append(l, fmt.Sprintf(`-X "%s=%s"`, k, v))
|
||||
}
|
||||
} else {
|
||||
for k, v := range cs {
|
||||
l = append(l, fmt.Sprintf(`-X %q %q`, k, v))
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(l, " ")
|
||||
}
|
||||
|
||||
func main() {
|
||||
buildTags := []string{}
|
||||
|
||||
|
@ -307,10 +328,13 @@ func main() {
|
|||
}
|
||||
version := getVersion()
|
||||
compileTime := time.Now().Format(timeFormat)
|
||||
ldflags := fmt.Sprintf("-s -X main.compiledAt %q", compileTime)
|
||||
constants := Constants{`main.compiledAt`: compileTime}
|
||||
if version != "" {
|
||||
ldflags = fmt.Sprintf("%s -X main.version %q", ldflags, version)
|
||||
constants["main.version"] = version
|
||||
}
|
||||
ldflags := "-s " + constants.LDFlags()
|
||||
fmt.Printf("ldflags: %s\n", ldflags)
|
||||
|
||||
args := []string{
|
||||
"-tags", strings.Join(buildTags, " "),
|
||||
"-ldflags", ldflags,
|
||||
|
|
Loading…
Reference in New Issue