github.com/klaytn/klaytn@v1.12.1/utils/build/env.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from internal/build/env.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package build
    22  
    23  import (
    24  	"flag"
    25  	"fmt"
    26  	"os"
    27  	"strings"
    28  )
    29  
    30  var (
    31  	// These flags override values in build env.
    32  	GitCommitFlag   = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
    33  	GitBranchFlag   = flag.String("git-branch", "", `Overrides git branch being built`)
    34  	GitTagFlag      = flag.String("git-tag", "", `Overrides git tag being built`)
    35  	BuildnumFlag    = flag.String("buildnum", "", `Overrides CI build number`)
    36  	PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
    37  	CronJobFlag     = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
    38  )
    39  
    40  // Environment contains metadata provided by the build environment.
    41  type Environment struct {
    42  	Name                    string // name of the environment
    43  	Repo                    string // name of GitHub repo
    44  	Commit, Branch, Tag     string // Git info
    45  	Buildnum                string
    46  	IsPullRequest           bool
    47  	IsCronJob               bool
    48  	IsKlaytnRaceDetectionOn bool
    49  	IsStaticLink            bool
    50  	IsDisabledSymbolTable   bool
    51  }
    52  
    53  func (env Environment) String() string {
    54  	return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t)",
    55  		env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
    56  }
    57  
    58  // Env returns metadata about the current CI environment, falling back to LocalEnv
    59  // if not running on CI.
    60  func Env() Environment {
    61  	switch {
    62  	case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
    63  		return Environment{
    64  			Name:          "travis",
    65  			Repo:          os.Getenv("TRAVIS_REPO_SLUG"),
    66  			Commit:        os.Getenv("TRAVIS_COMMIT"),
    67  			Branch:        os.Getenv("TRAVIS_BRANCH"),
    68  			Tag:           os.Getenv("TRAVIS_TAG"),
    69  			Buildnum:      os.Getenv("TRAVIS_BUILD_NUMBER"),
    70  			IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
    71  			IsCronJob:     os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
    72  		}
    73  	case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
    74  		return Environment{
    75  			Name:          "appveyor",
    76  			Repo:          os.Getenv("APPVEYOR_REPO_NAME"),
    77  			Commit:        os.Getenv("APPVEYOR_REPO_COMMIT"),
    78  			Branch:        os.Getenv("APPVEYOR_REPO_BRANCH"),
    79  			Tag:           os.Getenv("APPVEYOR_REPO_TAG_NAME"),
    80  			Buildnum:      os.Getenv("APPVEYOR_BUILD_NUMBER"),
    81  			IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
    82  			IsCronJob:     os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
    83  		}
    84  	default:
    85  		return LocalEnv()
    86  	}
    87  }
    88  
    89  // LocalEnv returns build environment metadata gathered from git.
    90  func LocalEnv() Environment {
    91  	env := applyEnvFlags(Environment{Name: "local", Repo: "klaytn/klaytn"})
    92  
    93  	if os.Getenv("KLAYTN_RACE_DETECT") == "1" {
    94  		env.IsKlaytnRaceDetectionOn = true
    95  	}
    96  	if os.Getenv("KLAYTN_STATIC_LINK") == "1" {
    97  		env.IsStaticLink = true
    98  	}
    99  	if os.Getenv("KLAYTN_DISABLE_SYMBOL") == "1" {
   100  		env.IsDisabledSymbolTable = true
   101  	}
   102  
   103  	head := readGitFile("HEAD")
   104  	if splits := strings.Split(head, " "); len(splits) == 2 {
   105  		head = splits[1]
   106  	} else if len(splits) == 1 && len(splits[0]) == 40 {
   107  		// HEAD file of detached HEAD contains only commit hash
   108  		env.Commit = splits[0]
   109  		return env
   110  	} else {
   111  		return env
   112  	}
   113  	if env.Commit == "" {
   114  		env.Commit = readGitFile(head)
   115  	}
   116  	if env.Branch == "" {
   117  		if head != "HEAD" {
   118  			env.Branch = strings.TrimPrefix(head, "refs/heads/")
   119  		}
   120  	}
   121  	if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
   122  		env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
   123  	}
   124  	return env
   125  }
   126  
   127  func firstLine(s string) string {
   128  	return strings.Split(s, "\n")[0]
   129  }
   130  
   131  func applyEnvFlags(env Environment) Environment {
   132  	if !flag.Parsed() {
   133  		panic("you need to call flag.Parse before Env or LocalEnv")
   134  	}
   135  	if *GitCommitFlag != "" {
   136  		env.Commit = *GitCommitFlag
   137  	}
   138  	if *GitBranchFlag != "" {
   139  		env.Branch = *GitBranchFlag
   140  	}
   141  	if *GitTagFlag != "" {
   142  		env.Tag = *GitTagFlag
   143  	}
   144  	if *BuildnumFlag != "" {
   145  		env.Buildnum = *BuildnumFlag
   146  	}
   147  	if *PullRequestFlag {
   148  		env.IsPullRequest = true
   149  	}
   150  	if *CronJobFlag {
   151  		env.IsCronJob = true
   152  	}
   153  	return env
   154  }