]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - utils/docker/build_docker_image.sh
Vendor import of llvm trunk r321414:
[FreeBSD/FreeBSD.git] / utils / docker / build_docker_image.sh
1 #!/bin/bash
2 #===- llvm/utils/docker/build_docker_image.sh ----------------------------===//
3 #
4 #                     The LLVM Compiler Infrastructure
5 #
6 # This file is distributed under the University of Illinois Open Source
7 # License. See LICENSE.TXT for details.
8 #
9 #===----------------------------------------------------------------------===//
10 set -e
11
12 IMAGE_SOURCE=""
13 DOCKER_REPOSITORY=""
14 DOCKER_TAG=""
15 BUILDSCRIPT_ARGS=""
16
17 function show_usage() {
18   cat << EOF
19 Usage: build_docker_image.sh [options] [-- [cmake_args]...]
20
21 Available options:
22   General:
23     -h|--help               show this help message
24   Docker-specific:
25     -s|--source             image source dir (i.e. debian8, nvidia-cuda, etc)
26     -d|--docker-repository  docker repository for the image
27     -t|--docker-tag         docker tag for the image
28   LLVM-specific:
29     -b|--branch         svn branch to checkout, i.e. 'trunk',
30                         'branches/release_40'
31                         (default: 'trunk')
32     -r|--revision       svn revision to checkout
33     -c|--cherrypick     revision to cherry-pick. Can be specified multiple times.
34                         Cherry-picks are performed in the sorted order using the
35                         following command:
36                         'svn patch <(svn diff -c \$rev)'.
37     -p|--llvm-project   name of an svn project to checkout. Will also add the
38                         project to a list LLVM_ENABLE_PROJECTS, passed to CMake.
39                         For clang, please use 'clang', not 'cfe'.
40                         Project 'llvm' is always included and ignored, if
41                         specified.
42                         Can be specified multiple times.
43     -i|--install-target name of a cmake install target to build and include in
44                         the resulting archive. Can be specified multiple times.
45     -c|--checksums      name of a file, containing checksums of llvm checkout.
46                         Script will fail if checksums of the checkout do not
47                         match.
48
49 Required options: --source and --docker-repository, at least one
50   --install-target.
51
52 All options after '--' are passed to CMake invocation.
53
54 For example, running:
55 $ build_docker_image.sh -s debian8 -d mydocker/debian8-clang -t latest \ 
56   -p clang -i install-clang -i install-clang-headers
57 will produce two docker images:
58     mydocker/debian8-clang-build:latest - an intermediate image used to compile
59       clang.
60     mydocker/clang-debian8:latest       - a small image with preinstalled clang.
61 Please note that this example produces a not very useful installation, since it
62 doesn't override CMake defaults, which produces a Debug and non-boostrapped
63 version of clang.
64
65 To get a 2-stage clang build, you could use this command:
66 $ ./build_docker_image.sh -s debian8 -d mydocker/clang-debian8 -t "latest" \ 
67     -p clang -i stage2-install-clang -i stage2-install-clang-headers \ 
68     -- \ 
69     -DLLVM_TARGETS_TO_BUILD=Native -DCMAKE_BUILD_TYPE=Release \ 
70     -DBOOTSTRAP_CMAKE_BUILD_TYPE=Release \ 
71     -DCLANG_ENABLE_BOOTSTRAP=ON \ 
72     -DCLANG_BOOTSTRAP_TARGETS="install-clang;install-clang-headers"
73 EOF
74 }
75
76 CHECKSUMS_FILE=""
77 SEEN_INSTALL_TARGET=0
78 while [[ $# -gt 0 ]]; do
79   case "$1" in
80     -h|--help)
81       show_usage
82       exit 0
83       ;;
84     -s|--source)
85       shift
86       IMAGE_SOURCE="$1"
87       shift
88       ;;
89     -d|--docker-repository)
90       shift
91       DOCKER_REPOSITORY="$1"
92       shift
93       ;;
94     -t|--docker-tag)
95       shift
96       DOCKER_TAG="$1"
97       shift
98       ;;
99     -i|--install-target|-r|--revision|-c|-cherrypick|-b|--branch|-p|--llvm-project)
100       if [ "$1" == "-i" ] || [ "$1" == "--install-target" ]; then
101         SEEN_INSTALL_TARGET=1
102       fi
103       BUILDSCRIPT_ARGS="$BUILDSCRIPT_ARGS $1 $2"
104       shift 2
105       ;;
106     -c|--checksums)
107       shift
108       CHECKSUMS_FILE="$1"
109       shift
110       ;;
111     --)
112       shift
113       BUILDSCRIPT_ARGS="$BUILDSCRIPT_ARGS -- $*"
114       shift $#
115       ;;
116     *)
117       echo "Unknown argument $1"
118       exit 1
119       ;;
120   esac
121 done
122
123 command -v docker >/dev/null ||
124   {
125     echo "Docker binary cannot be found. Please install Docker to use this script."
126     exit 1
127   }
128
129 if [ "$IMAGE_SOURCE" == "" ]; then
130   echo "Required argument missing: --source"
131   exit 1
132 fi
133
134 if [ "$DOCKER_REPOSITORY" == "" ]; then
135   echo "Required argument missing: --docker-repository"
136   exit 1
137 fi
138
139 if [ $SEEN_INSTALL_TARGET -eq 0 ]; then
140   echo "Please provide at least one --install-target"
141   exit 1
142 fi
143
144 SOURCE_DIR=$(dirname $0)
145 if [ ! -d "$SOURCE_DIR/$IMAGE_SOURCE" ]; then
146   echo "No sources for '$IMAGE_SOURCE' were found in $SOURCE_DIR"
147   exit 1
148 fi
149
150 BUILD_DIR=$(mktemp -d)
151 trap "rm -rf $BUILD_DIR" EXIT
152 echo "Using a temporary directory for the build: $BUILD_DIR"
153
154 cp -r "$SOURCE_DIR/$IMAGE_SOURCE" "$BUILD_DIR/$IMAGE_SOURCE"
155 cp -r "$SOURCE_DIR/scripts" "$BUILD_DIR/scripts"
156
157 mkdir "$BUILD_DIR/checksums"
158 if [ "$CHECKSUMS_FILE" != "" ]; then
159   cp "$CHECKSUMS_FILE" "$BUILD_DIR/checksums/checksums.txt"
160 fi
161
162 if [ "$DOCKER_TAG" != "" ]; then
163   DOCKER_TAG=":$DOCKER_TAG"
164 fi
165
166 echo "Building from $IMAGE_SOURCE"
167 echo "Building $DOCKER_REPOSITORY-build$DOCKER_TAG"
168 docker build -t "$DOCKER_REPOSITORY-build$DOCKER_TAG" \
169   --build-arg "buildscript_args=$BUILDSCRIPT_ARGS" \
170   -f "$BUILD_DIR/$IMAGE_SOURCE/build/Dockerfile" \
171   "$BUILD_DIR"
172
173 echo "Copying clang installation to release image sources"
174 docker run -v "$BUILD_DIR/$IMAGE_SOURCE:/workspace" "$DOCKER_REPOSITORY-build$DOCKER_TAG" \
175   cp /tmp/clang.tar.gz /workspace/release
176
177 echo "Building release image"
178 docker build -t "${DOCKER_REPOSITORY}${DOCKER_TAG}" \
179   "$BUILD_DIR/$IMAGE_SOURCE/release"
180
181 echo "Done"