File size: 7,694 Bytes
784ee2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env bash

set -euo pipefail

cd "$(dirname $(readlink -f $0))/../"

baseDir="$(pwd)"
# docker hub organization/user from where to pull/push images
org=openroad

DOCKER_CMD="docker"
noConstantBuildDir=""

_help() {
    cat <<EOF
usage: $0 [CMD] [OPTIONS]

  CMD:
  create                        Create a docker image
  test                          Test the docker image
  push                          Push the docker image to Docker Hub

  OPTIONS:
  -os=OS_NAME                   Choose between ubuntu20.04 and ubuntu22.04 (default).
  -target=TARGET                Choose target for the Docker image:
                                  'dev': os + packages to compile app
                                  'builder': os + packages to compile app +
                                             copy source code and build app
  -threads=N                    Max number of threads to use if compiling.
                                  Default = \$(nproc)
  -tag=TAG                      Use as the image tag. Default is git commit sha.
  -username=USERNAME            Username to loging at the docker registry.
  -password=PASSWORD            Password to loging at the docker registry.
  -ci                           Install CI tools in image
  -dry-run                      Do not push images to the repository
  -push-latest                  Push the latest image to the repository
  -no-constant-build-dir        Do not use constant build directory
  -buildArgs=<ARGS>             Additional build arguments to pass to docker buildx
  -h -help                      Show this message and exits

EOF
    exit "${1:-1}"
}

_setup() {
    case "${os}" in
        "ubuntu20.04")
            osBaseImage="ubuntu:20.04"
            ;;
        "ubuntu22.04")
            osBaseImage="ubuntu:22.04"
            ;;
        *)
            echo "Target OS ${os} not supported" >&2
            _help
            ;;
    esac
    imageName="${IMAGE_NAME_OVERRIDE:-"${org}/flow-${os}-${target}"}"
    if [[ "${tag}" != "" ]]; then
        imageTag="${tag}"
    else
        imageTag=$(./etc/DockerTag.sh -dev)
    fi
    case "${target}" in
        "builder" | "master")
            fromImage="${FROM_IMAGE_OVERRIDE:-"${org}/flow-${os}-dev"}:${imageTag}"
            context="."
            buildArgs+=" --build-arg numThreads=${numThreads}"
            orVersion=$(git -C tools/OpenROAD describe --tags)
            echo "OpenROAD version: ${orVersion}"
            buildArgs+=" --build-arg openroadVersion=${orVersion}"
            ;;
        "dev" )
            fromImage="${FROM_IMAGE_OVERRIDE:-$osBaseImage}"
            cp tools/OpenROAD/etc/DependencyInstaller.sh etc/InstallerOpenROAD.sh
            context="etc"
            local yosys_ver
            yosys_ver=v$(grep 'yosys_ver =' tools/yosys/docs/source/conf.py | awk -F'"' '{print $2}')
            options+=" -yosys-ver=${yosys_ver}"
            buildArgs+=" --build-arg \"options=${options}\" ${noConstantBuildDir}"
            ;;
        *)
            echo "Target ${target} not found" >&2
            _help
            ;;
    esac
    imagePath="${imageName}:${imageTag}"
    buildArgs+=" ${buildArgs} --build-arg fromImage=${fromImage}"
    file="docker/Dockerfile.${target}"
}

_create() {
    echo "Create docker image ${imagePath} using ${file}"
    eval ${DOCKER_CMD} buildx build \
        --file "${file}" \
        --tag "${imagePath}" \
        "${buildArgs}" \
        "${context}"
    rm -f etc/InstallerOpenROAD.sh
}

_push() {
    if [[ -z ${username+x} ]] && [[ ${dryRun} != 1 ]]; then
        echo "Missing required -username=<USER> argument"
        _help
    fi
    if [[ -z ${password+x} ]] && [[ ${dryRun} != 1 ]]; then
        echo "Missing required -password=<PASS> argument"
        _help
    fi
    if [[ "${target}" != "dev" ]] && [[ "${target}" != "master" ]]; then
        echo "Target ${target} is not valid candidate for push to Docker Hub." >&2
        _help
    fi

    if [[ "${dryRun}" == 1 ]]; then
        echo "Skipping docker login"
    else
        ${DOCKER_CMD} login --username "${username}" --password "${password}"
    fi

    if [[ "${tag}" == "" ]]; then
        tag=$(./etc/DockerTag.sh -dev)
    fi

    mkdir -p build

    if [[ "${target}" == "dev" ]]; then
        ./etc/DockerHelper.sh create -os=${os} -target=dev -tag=${tag} -ci \
            2>&1 | tee build/create-${os}-dev-${tag}.log

        if [[ "${dryRun}" != 1 ]]; then
            ${DOCKER_CMD} push "${org}/flow-${os}-dev:${tag}"
        fi
    fi

    if [[ "${target}" == "master" ]]; then
        tag=$(./etc/DockerTag.sh -master)
        # Create builder image
        ./etc/DockerHelper.sh create -os=${os} -target=builder \
            2>&1 | tee build/create-${os}-${target}-${tag}.log

        builderTag=${org}/flow-${os}-builder:${imageTag}
        orfsTag=${org}/orfs:${tag}
        echo "Renaming docker image: ${builderTag} -> ${orfsTag}"
        ${DOCKER_CMD} tag ${builderTag} ${orfsTag}

        if [[ "${dryRun}" == 1 ]]; then
            echo "[DRY-RUN] ${DOCKER_CMD} push ${orfsTag}"
            if [[ "${pushLatest}" == 1 ]]; then
                echo "[DRY-RUN] ${DOCKER_CMD} tag ${orfsTag} \"${org}/orfs:latest\""
                echo "[DRY-RUN] ${DOCKER_CMD} push \"${org}/orfs:latest\""
            fi
        else
            ${DOCKER_CMD} push ${orfsTag}

            # Only tag and push as latest if requested
            if [[ "${pushLatest}" == 1 ]]; then
                ${DOCKER_CMD} tag ${orfsTag} "${org}/orfs:latest"
                ${DOCKER_CMD} push "${org}/orfs:latest"
                echo "Tagged and pushed ${org}/orfs:latest"
            fi
        fi
    fi
}

#
# MAIN
#

# script has at least 1 argument, the rule
if [[ $# -lt 1 ]]; then
    echo "Too few arguments" >&2
    _help
fi

if [[ "$1" == "-h" || "$1" == "-help" ]]; then
    _help 0
fi

_rule="_${1}"
shift 1

# check if the rule is exists
if [[ -z $(command -v "${_rule}") ]]; then
    echo "Command ${_rule/_/} not found" >&2
    _help
fi

# default values, can be overwritten by command line arguments
os="ubuntu22.04"
target="dev"
numThreads="-1"
tag=""
buildArgs=""
dryRun=0
pushLatest=0

while [ "$#" -gt 0 ]; do
    case "${1}" in
        -h|-help)
            _help 0
            ;;
        -ci )
            options+=" -ci"
            ;;
        -dry-run )
            dryRun=1
            ;;
        -push-latest )
            pushLatest=1
            ;;
        -os=* )
            os="${1#*=}"
            ;;
        -target=* )
            target="${1#*=}"
            ;;
        -threads=* )
            numThreads="${1#*=}"
            ;;
        -username=* )
            username="${1#*=}"
            ;;
        -password=* )
            password="${1#*=}"
            ;;
        -tag=* )
            tag="${1#*=}"
            ;;
        -buildArgs=* )
            buildArgs="${1#*=}"
            ;;
        -no-constant-build-dir )
            noConstantBuildDir="--build-arg constantBuildDir= "
            ;;
        -os | -target | -threads | -username | -password | -tag )
            echo "${1} requires an argument" >&2
            _help
            ;;
        *)
            echo "unknown option: ${1}" >&2
            _help
            ;;
    esac
    shift 1
done

if [[ "${numThreads}" == "-1" ]]; then
    if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        numThreads=$(nproc --all)
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        numThreads=$(sysctl -n hw.ncpu)
    else
        numThreads=2
        cat << EOF
[WARNING] Unsupported OSTYPE: cannot determine number of host CPUs"
  Defaulting to 2 threads. Use --threads N to use N threads"
EOF
    fi
fi

_setup

"${_rule}"