2 #===- llvm/utils/docker/build_docker_image.sh ----------------------------===//
4 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 # See https://llvm.org/LICENSE.txt for license information.
6 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 #===----------------------------------------------------------------------===//
16 CMAKE_ENABLED_PROJECTS
=""
18 function show_usage
() {
20 Usage: build_docker_image.sh [options] [-- [cmake_args]...]
24 -h|--help show this help message
26 -s|--source image source dir (i.e. debian10, nvidia-cuda, etc)
27 -d|--docker-repository docker repository for the image
28 -t|--docker-tag docker tag for the image
30 -b|--branch git branch to checkout, i.e. 'main',
33 -r|--revision git revision to checkout
34 -c|--cherrypick revision to cherry-pick. Can be specified multiple times.
35 Cherry-picks are performed in the sorted order using the
37 'git cherry-pick \$rev'.
38 -p|--llvm-project Add the project to a list LLVM_ENABLE_PROJECTS, passed to
40 Can be specified multiple times.
41 -c|--checksums name of a file, containing checksums of llvm checkout.
42 Script will fail if checksums of the checkout do not
45 -i|--install-target name of a cmake install target to build and include in
46 the resulting archive. Can be specified multiple times.
48 Required options: --source and --docker-repository, at least one
51 All options after '--' are passed to CMake invocation.
54 $ build_docker_image.sh -s debian10 -d mydocker/debian10-clang -t latest \
55 -p clang -i install-clang -i install-clang-resource-headers
56 will produce two docker images:
57 mydocker/debian10-clang-build:latest - an intermediate image used to compile
59 mydocker/clang-debian10:latest - a small image with preinstalled clang.
60 Please note that this example produces a not very useful installation, since it
61 doesn't override CMake defaults, which produces a Debug and non-boostrapped
64 To get a 2-stage clang build, you could use this command:
65 $ ./build_docker_image.sh -s debian10 -d mydocker/clang-debian10 -t "latest" \
66 -p clang -i stage2-install-clang -i stage2-install-clang-resource-headers \
68 -DLLVM_TARGETS_TO_BUILD=Native -DCMAKE_BUILD_TYPE=Release \
69 -DBOOTSTRAP_CMAKE_BUILD_TYPE=Release \
70 -DCLANG_ENABLE_BOOTSTRAP=ON \
71 -DCLANG_BOOTSTRAP_TARGETS="install-clang;install-clang-resource-headers"
78 while [[ $# -gt 0 ]]; do
89 -d|
--docker-repository)
91 DOCKER_REPOSITORY
="$1"
99 -r|
--revision|
-c|
--cherrypick|
-b|
--branch)
100 CHECKOUT_ARGS
="$CHECKOUT_ARGS $1 $2"
104 SEEN_INSTALL_TARGET
=1
105 BUILDSCRIPT_ARGS
="$BUILDSCRIPT_ARGS $1 $2"
110 CMAKE_ENABLED_PROJECTS
="$CMAKE_ENABLED_PROJECTS;$PROJ"
120 BUILDSCRIPT_ARGS
="$BUILDSCRIPT_ARGS -- $*"
125 echo "Unknown argument $1"
132 if [ "$CMAKE_ENABLED_PROJECTS" != "" ]; then
133 # Remove the leading ';' character.
134 CMAKE_ENABLED_PROJECTS
="${CMAKE_ENABLED_PROJECTS:1}"
136 if [[ $SEEN_CMAKE_ARGS -eq 0 ]]; then
137 BUILDSCRIPT_ARGS
="$BUILDSCRIPT_ARGS --"
139 BUILDSCRIPT_ARGS
="$BUILDSCRIPT_ARGS -DLLVM_ENABLE_PROJECTS=$CMAKE_ENABLED_PROJECTS"
142 command -v docker
>/dev
/null ||
144 echo "Docker binary cannot be found. Please install Docker to use this script."
148 if [ "$IMAGE_SOURCE" == "" ]; then
149 echo "Required argument missing: --source"
153 if [ "$DOCKER_REPOSITORY" == "" ]; then
154 echo "Required argument missing: --docker-repository"
158 if [ $SEEN_INSTALL_TARGET -eq 0 ]; then
159 echo "Please provide at least one --install-target"
163 SOURCE_DIR
=$
(dirname $0)
164 if [ ! -d "$SOURCE_DIR/$IMAGE_SOURCE" ]; then
165 echo "No sources for '$IMAGE_SOURCE' were found in $SOURCE_DIR"
169 BUILD_DIR
=$
(mktemp
-d)
170 trap "rm -rf $BUILD_DIR" EXIT
171 echo "Using a temporary directory for the build: $BUILD_DIR"
173 cp -r "$SOURCE_DIR/$IMAGE_SOURCE" "$BUILD_DIR/$IMAGE_SOURCE"
174 cp -r "$SOURCE_DIR/scripts" "$BUILD_DIR/scripts"
176 mkdir
"$BUILD_DIR/checksums"
177 if [ "$CHECKSUMS_FILE" != "" ]; then
178 cp "$CHECKSUMS_FILE" "$BUILD_DIR/checksums/checksums.txt"
181 if [ "$DOCKER_TAG" != "" ]; then
182 DOCKER_TAG
=":$DOCKER_TAG"
185 echo "Building ${DOCKER_REPOSITORY}${DOCKER_TAG} from $IMAGE_SOURCE"
186 docker build
-t "${DOCKER_REPOSITORY}${DOCKER_TAG}" \
187 --build-arg "checkout_args=$CHECKOUT_ARGS" \
188 --build-arg "buildscript_args=$BUILDSCRIPT_ARGS" \
189 -f "$BUILD_DIR/$IMAGE_SOURCE/Dockerfile" \