Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / integrations / docker / images / chip-cirque-device-base / build.sh
1 #!/usr/bin/env bash
2
3 #
4 # Copyright (c) 2020 Project CHIP Authors
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 # build.sh   - utility for building (and optionally) tagging and pushing
20 #               the a Docker image
21 #
22 # This script expects to find a Dockerfile next to $0, so symlink
23 #  in an image name directory is the expected use case.
24
25 me=$(basename "$0")
26 cd "$(dirname "$0")"
27
28 ORG=${DOCKER_BUILD_ORG:-connectedhomeip}
29
30 # directory name is
31 IMAGE=${DOCKER_BUILD_IMAGE:-$(basename "$(pwd)")}
32
33 # version
34 VERSION=${DOCKER_BUILD_VERSION:-$(cat version)}
35
36 [[ ${*/--help//} != "${*}" ]] && {
37     set +x
38     echo "Usage: $me <OPTIONS>
39
40   Build and (optionally tag as latest, push) a docker image from Dockerfile in CWD
41
42   Options:
43    --try-download  try to download latest image from dockerhub and skip whole
44                    build procedure if the expected image version is downloaded.
45    --no-cache      passed as a docker build argument
46    --latest        update latest to the current built (or downloaded) version (\"$VERSION\")
47    --push          push image(s) to docker.io (requires docker login for \"$ORG\")
48    --help          get this message
49
50 "
51     exit 0
52 }
53
54 die() {
55     echo "$me: *** ERROR: $*"
56     exit 1
57 }
58
59 set -ex
60
61 [[ -n $VERSION ]] || die "version cannot be empty"
62
63 if [[ ${*/--try-download//} != "${*}" ]]; then
64     docker pull "$ORG"/"$IMAGE":"$VERSION"
65     if [[ $? -eq 0 ]]; then
66         # tag it as latest for this version, note: this should only be used on CI
67         [[ ${*/--latest//} != "${*}" ]] && {
68             docker tag "$ORG"/"$IMAGE":"$VERSION" "$ORG"/"$IMAGE":latest
69         }
70         exit 0
71     fi
72 fi
73
74 # go find and build any CHIP images this image is "FROM"
75 awk -F/ '/^FROM connectedhomeip/ {print $2}' Dockerfile | while read -r dep; do
76     dep=${dep%:*}
77     (cd "../$dep" && ./build.sh "$@")
78 done
79
80 BUILD_ARGS=()
81 if [[ ${*/--no-cache//} != "${*}" ]]; then
82     BUILD_ARGS+=(--no-cache)
83 fi
84
85 SOURCE=${BASH_SOURCE[0]}
86 SOURCE_DIR=$(cd "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)
87 REPO_DIR="$SOURCE_DIR/../../../../"
88
89 # The image build will clone its own ot-br-posix checkout due to limitations of git submodule.
90 # Using the same ot-br-posix version as chip
91 OT_BR_POSIX=$REPO_DIR/third_party/ot-br-posix/repo
92 OT_BR_POSIX_CHECKOUT=$(cd "$REPO_DIR" && git rev-parse :third_party/ot-br-posix/repo)
93
94 docker build -t "$ORG/$IMAGE:$VERSION" -f "$SOURCE_DIR/Dockerfile" "${BUILD_ARGS[@]}" --build-arg OT_BR_POSIX_CHECKOUT="$OT_BR_POSIX_CHECKOUT" "$SOURCE_DIR"
95
96 [[ ${*/--latest//} != "${*}" ]] && {
97     docker tag "$ORG"/"$IMAGE":"$VERSION" "$ORG"/"$IMAGE":latest
98 }
99
100 [[ ${*/--push//} != "${*}" ]] && {
101     docker push "$ORG"/"$IMAGE":"$VERSION"
102     [[ ${*/--latest//} != "${*}" ]] && {
103         docker push "$ORG"/"$IMAGE":latest
104     }
105 }
106
107 exit 0