storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/buildscripts/gen-ldflags.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2015 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package main
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"os/exec"
    26  	"strings"
    27  	"time"
    28  )
    29  
    30  func genLDFlags(version string) string {
    31  	ldflagsStr := "-s -w"
    32  	ldflagsStr += " -X github.com/minio/minio/cmd.Version=" + version
    33  	ldflagsStr += " -X github.com/minio/minio/cmd.ReleaseTag=" + releaseTag(version)
    34  	ldflagsStr += " -X github.com/minio/minio/cmd.CommitID=" + commitID()
    35  	ldflagsStr += " -X github.com/minio/minio/cmd.ShortCommitID=" + commitID()[:12]
    36  	ldflagsStr += " -X github.com/minio/minio/cmd.GOPATH=" + os.Getenv("GOPATH")
    37  	ldflagsStr += " -X github.com/minio/minio/cmd.GOROOT=" + os.Getenv("GOROOT")
    38  	return ldflagsStr
    39  }
    40  
    41  // genReleaseTag prints release tag to the console for easy git tagging.
    42  func releaseTag(version string) string {
    43  	relPrefix := "DEVELOPMENT"
    44  	if prefix := os.Getenv("MINIO_RELEASE"); prefix != "" {
    45  		relPrefix = prefix
    46  	}
    47  
    48  	relSuffix := ""
    49  	if hotfix := os.Getenv("MINIO_HOTFIX"); hotfix != "" {
    50  		relSuffix = hotfix
    51  	}
    52  
    53  	relTag := strings.Replace(version, " ", "-", -1)
    54  	relTag = strings.Replace(relTag, ":", "-", -1)
    55  	relTag = strings.Replace(relTag, ",", "", -1)
    56  	relTag = relPrefix + "." + relTag
    57  
    58  	if relSuffix != "" {
    59  		relTag += "." + relSuffix
    60  	}
    61  
    62  	return relTag
    63  }
    64  
    65  // commitID returns the abbreviated commit-id hash of the last commit.
    66  func commitID() string {
    67  	// git log --format="%h" -n1
    68  	var (
    69  		commit []byte
    70  		e      error
    71  	)
    72  	cmdName := "git"
    73  	cmdArgs := []string{"log", "--format=%H", "-n1"}
    74  	if commit, e = exec.Command(cmdName, cmdArgs...).Output(); e != nil {
    75  		fmt.Fprintln(os.Stderr, "Error generating git commit-id: ", e)
    76  		os.Exit(1)
    77  	}
    78  
    79  	return strings.TrimSpace(string(commit))
    80  }
    81  
    82  func main() {
    83  	var version string
    84  	if len(os.Args) > 1 {
    85  		version = os.Args[1]
    86  	} else {
    87  		version = time.Now().UTC().Format(time.RFC3339)
    88  	}
    89  
    90  	fmt.Println(genLDFlags(version))
    91  }