github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/buildscripts/gen-ldflags.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // Copyright (c) 2015-2021 MinIO, Inc.
     5  //
     6  // This file is part of MinIO Object Storage stack
     7  //
     8  // This program is free software: you can redistribute it and/or modify
     9  // it under the terms of the GNU Affero General Public License as published by
    10  // the Free Software Foundation, either version 3 of the License, or
    11  // (at your option) any later version.
    12  //
    13  // This program is distributed in the hope that it will be useful
    14  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  // GNU Affero General Public License for more details.
    17  //
    18  // You should have received a copy of the GNU Affero General Public License
    19  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20  
    21  package main
    22  
    23  import (
    24  	"fmt"
    25  	"os"
    26  	"os/exec"
    27  	"strconv"
    28  	"strings"
    29  	"time"
    30  )
    31  
    32  func genLDFlags(version string) string {
    33  	releaseTag, date := releaseTag(version)
    34  	copyrightYear := strconv.Itoa(date.Year())
    35  	ldflagsStr := "-s -w"
    36  	ldflagsStr += " -X github.com/minio/minio/cmd.Version=" + version
    37  	ldflagsStr += " -X github.com/minio/minio/cmd.CopyrightYear=" + copyrightYear
    38  	ldflagsStr += " -X github.com/minio/minio/cmd.ReleaseTag=" + releaseTag
    39  	ldflagsStr += " -X github.com/minio/minio/cmd.CommitID=" + commitID()
    40  	ldflagsStr += " -X github.com/minio/minio/cmd.ShortCommitID=" + commitID()[:12]
    41  	ldflagsStr += " -X github.com/minio/minio/cmd.GOPATH=" + os.Getenv("GOPATH")
    42  	ldflagsStr += " -X github.com/minio/minio/cmd.GOROOT=" + os.Getenv("GOROOT")
    43  	return ldflagsStr
    44  }
    45  
    46  // genReleaseTag prints release tag to the console for easy git tagging.
    47  func releaseTag(version string) (string, time.Time) {
    48  	relPrefix := "DEVELOPMENT"
    49  	if prefix := os.Getenv("MINIO_RELEASE"); prefix != "" {
    50  		relPrefix = prefix
    51  	}
    52  
    53  	relSuffix := ""
    54  	if hotfix := os.Getenv("MINIO_HOTFIX"); hotfix != "" {
    55  		relSuffix = hotfix
    56  	}
    57  
    58  	relTag := strings.Replace(version, " ", "-", -1)
    59  	relTag = strings.Replace(relTag, ":", "-", -1)
    60  	t, err := time.Parse("2006-01-02T15-04-05Z", relTag)
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	relTag = strings.Replace(relTag, ",", "", -1)
    65  	relTag = relPrefix + "." + relTag
    66  	if relSuffix != "" {
    67  		relTag += "." + relSuffix
    68  	}
    69  
    70  	return relTag, t
    71  }
    72  
    73  // commitID returns the abbreviated commit-id hash of the last commit.
    74  func commitID() string {
    75  	// git log --format="%H" -n1
    76  	var (
    77  		commit []byte
    78  		err    error
    79  	)
    80  	cmdName := "git"
    81  	cmdArgs := []string{"log", "--format=%H", "-n1"}
    82  	if commit, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil {
    83  		fmt.Fprintln(os.Stderr, "Error generating git commit-id: ", err)
    84  		os.Exit(1)
    85  	}
    86  
    87  	return strings.TrimSpace(string(commit))
    88  }
    89  
    90  func commitTime() time.Time {
    91  	// git log --format=%cD -n1
    92  	var (
    93  		commitUnix []byte
    94  		err        error
    95  	)
    96  	cmdName := "git"
    97  	cmdArgs := []string{"log", "--format=%cI", "-n1"}
    98  	if commitUnix, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil {
    99  		fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err)
   100  		os.Exit(1)
   101  	}
   102  
   103  	t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(commitUnix)))
   104  	if err != nil {
   105  		fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err)
   106  		os.Exit(1)
   107  	}
   108  
   109  	return t.UTC()
   110  }
   111  
   112  func main() {
   113  	var version string
   114  	if len(os.Args) > 1 {
   115  		version = os.Args[1]
   116  	} else {
   117  		version = commitTime().Format(time.RFC3339)
   118  	}
   119  
   120  	fmt.Println(genLDFlags(version))
   121  }