From d53a2567a4306e36b3e1e005cd04f0b676404143 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 30 Jul 2020 10:58:43 +0200 Subject: [PATCH] build: Actually set build tags (#6866) Apparently our Tags field depended on having specific files react to tags and add themselves there. This, instead, works for all tags. Also, pass tags to the test command line. --- build.go | 18 ++++++++++-------- lib/build/build.go | 17 +++++++++++------ lib/build/tags_noupgrade.go | 13 ------------- lib/build/tags_race.go | 6 +++++- 4 files changed, 26 insertions(+), 28 deletions(-) delete mode 100644 lib/build/tags_noupgrade.go diff --git a/build.go b/build.go index 57f79d9ab..36a848e92 100644 --- a/build.go +++ b/build.go @@ -297,10 +297,10 @@ func runCommand(cmd string, target target) { build(target, tags) case "test": - test("github.com/syncthing/syncthing/lib/...", "github.com/syncthing/syncthing/cmd/...") + test(strings.Fields(extraTags), "github.com/syncthing/syncthing/lib/...", "github.com/syncthing/syncthing/cmd/...") case "bench": - bench("github.com/syncthing/syncthing/lib/...", "github.com/syncthing/syncthing/cmd/...") + bench(strings.Fields(extraTags), "github.com/syncthing/syncthing/lib/...", "github.com/syncthing/syncthing/cmd/...") case "integration": integration(false) @@ -379,10 +379,11 @@ func parseFlags() { flag.Parse() } -func test(pkgs ...string) { +func test(tags []string, pkgs ...string) { lazyRebuildAssets() - args := []string{"test", "-short", "-timeout", timeout, "-tags", "purego"} + tags = append(tags, "purego") + args := []string{"test", "-short", "-timeout", timeout, "-tags", strings.Join(tags, " ")} if runtime.GOARCH == "amd64" { switch runtime.GOOS { @@ -400,9 +401,9 @@ func test(pkgs ...string) { runPrint(goCmd, append(args, pkgs...)...) } -func bench(pkgs ...string) { +func bench(tags []string, pkgs ...string) { lazyRebuildAssets() - args := append([]string{"test", "-run", "NONE"}, benchArgs()...) + args := append([]string{"test", "-run", "NONE", "-tags", strings.Join(tags, " ")}, benchArgs()...) runPrint(goCmd, append(args, pkgs...)...) } @@ -526,7 +527,7 @@ func appendParameters(args []string, tags []string, pkgs ...string) []string { if !debugBinary { // Regular binaries get version tagged and skip some debug symbols - args = append(args, "-ldflags", ldflags()) + args = append(args, "-ldflags", ldflags(tags)) } else { // -gcflags to disable optimizations and inlining. Skip -ldflags // because `Could not launch program: decoding dwarf section info at @@ -829,13 +830,14 @@ func transifex() { runPrint(goCmd, "run", "../../../../script/transifexdl.go") } -func ldflags() string { +func ldflags(tags []string) string { b := new(strings.Builder) b.WriteString("-w") fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Version=%s", version) fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Stamp=%d", buildStamp()) fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.User=%s", buildUser()) fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Host=%s", buildHost()) + fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Tags=%s", strings.Join(tags, ",")) if v := os.Getenv("EXTRA_LDFLAGS"); v != "" { fmt.Fprintf(b, " %s", v) } diff --git a/lib/build/build.go b/lib/build/build.go index b83967285..5c8d7c336 100644 --- a/lib/build/build.go +++ b/lib/build/build.go @@ -12,6 +12,7 @@ import ( "os" "regexp" "runtime" + "sort" "strconv" "strings" "time" @@ -23,6 +24,7 @@ var ( Host = "unknown" User = "unknown" Stamp = "0" + Tags = "" // Static Codename = "Fermium Flea" @@ -34,9 +36,6 @@ var ( IsBeta bool LongVersion string - // Set by Go build tags - Tags []string - allowedVersionExp = regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\.\d+)*(\+\d+-g[0-9a-f]+)?(-[^\s]+)?$`) envTags = []string{ @@ -88,13 +87,19 @@ func LongVersionFor(program string) string { // This string and date format is essentially part of our external API. Never change it. date := Date.UTC().Format("2006-01-02 15:04:05 MST") v := fmt.Sprintf(`%s %s "%s" (%s %s-%s) %s@%s %s`, program, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date) + + tags := strings.Split(Tags, ",") + if len(tags) == 1 && tags[0] == "" { + tags = tags[:0] + } for _, envVar := range envTags { if os.Getenv(envVar) != "" { - Tags = append(Tags, strings.ToLower(envVar)) + tags = append(tags, strings.ToLower(envVar)) } } - if len(Tags) > 0 { - v = fmt.Sprintf("%s [%s]", v, strings.Join(Tags, ", ")) + if len(tags) > 0 { + sort.Strings(tags) + v = fmt.Sprintf("%s [%s]", v, strings.Join(tags, ", ")) } return v } diff --git a/lib/build/tags_noupgrade.go b/lib/build/tags_noupgrade.go deleted file mode 100644 index 77e5f3b9e..000000000 --- a/lib/build/tags_noupgrade.go +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (C) 2017 The Syncthing Authors. -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this file, -// You can obtain one at https://mozilla.org/MPL/2.0/. - -//+build noupgrade - -package build - -func init() { - Tags = append(Tags, "noupgrade") -} diff --git a/lib/build/tags_race.go b/lib/build/tags_race.go index 706d5606c..4aa471587 100644 --- a/lib/build/tags_race.go +++ b/lib/build/tags_race.go @@ -9,5 +9,9 @@ package build func init() { - Tags = append(Tags, "race") + if Tags == "" { + Tags = "race" + } else { + Tags = Tags + ",race" + } }