github.com/google/cadvisor@v0.49.1/build/build.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2015 Google Inc. All rights reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -e
    18  
    19  export GOOS=${GOOS:-$(go env GOOS)}
    20  export GOARCH=${GOARCH:-$(go env GOARCH)}
    21  export CGO_ENABLED=${GO_CGO_ENABLED:-"1"}
    22  GO_FLAGS=${GO_FLAGS:-"-tags=netgo"}    # Extra go flags to use in the build.
    23  BUILD_USER=${BUILD_USER:-"${USER}@${HOSTNAME}"}
    24  BUILD_DATE=${BUILD_DATE:-$( date +%Y%m%d-%H:%M:%S )}
    25  VERBOSE=${VERBOSE:-}
    26  OUTPUT_NAME_WITH_ARCH=${OUTPUT_NAME_WITH_ARCH:-"false"}
    27  
    28  repo_path="github.com/google/cadvisor"
    29  
    30  version=${VERSION:-$( git describe --tags --dirty --abbrev=14 | sed -E 's/-([0-9]+)-g/.\1+/' )}
    31  revision=$( git rev-parse --short HEAD 2> /dev/null || echo 'unknown' )
    32  branch=$( git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown' )
    33  go_version=$( go version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/' )
    34  
    35  
    36  # go 1.4 requires ldflags format to be "-X key value", not "-X key=value"
    37  ldseparator="="
    38  if [ "${go_version:0:3}" = "1.4" ]; then
    39  	ldseparator=" "
    40  fi
    41  
    42  ldflags="
    43    -X ${repo_path}/version.Version${ldseparator}${version}
    44    -X ${repo_path}/version.Revision${ldseparator}${revision}
    45    -X ${repo_path}/version.Branch${ldseparator}${branch}
    46    -X ${repo_path}/version.BuildUser${ldseparator}${BUILD_USER}
    47    -X ${repo_path}/version.BuildDate${ldseparator}${BUILD_DATE}
    48    -X ${repo_path}/version.GoVersion${ldseparator}${go_version}"
    49  
    50  echo ">> building cadvisor"
    51  
    52  if [ -n "$VERBOSE" ]; then
    53    echo "Building with -ldflags $ldflags"
    54  fi
    55  
    56  mkdir -p "$PWD/_output"
    57  output_file="$PWD/_output/cadvisor"
    58  if [ "${OUTPUT_NAME_WITH_ARCH}" = "true" ] ; then
    59    output_file="${output_file}-${version}-${GOOS}-${GOARCH}"
    60  fi
    61  
    62  # Since github.com/google/cadvisor/cmd is a submodule, we must build from inside that directory
    63  pushd cmd > /dev/null
    64    go build ${GO_FLAGS} -ldflags "${ldflags}" -o "${output_file}" "${repo_path}/cmd"
    65  popd > /dev/null
    66  
    67  exit 0