浏览代码

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.
Jakob Borg 5 年之前
父节点
当前提交
d53a2567a4
共有 4 个文件被更改,包括 26 次插入28 次删除
  1. 10 8
      build.go
  2. 11 6
      lib/build/build.go
  3. 0 13
      lib/build/tags_noupgrade.go
  4. 5 1
      lib/build/tags_race.go

+ 10 - 8
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)
 	}

+ 11 - 6
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
 }

+ 0 - 13
lib/build/tags_noupgrade.go

@@ -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")
-}

+ 5 - 1
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"
+	}
 }