1#!/bin/bash
2#===- libcxx/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#===----------------------------------------------------------------------===//
10set -e
11
12IMAGE_SOURCE=""
13DOCKER_REPOSITORY=""
14DOCKER_TAG=""
15
16function show_usage() {
17  cat << EOF
18Usage: build_docker_image.sh [options] [-- [cmake_args]...]
19
20Available options:
21  General:
22    -h|--help               show this help message
23  Docker-specific:
24    -s|--source             image source dir (i.e. debian8, nvidia-cuda, etc)
25    -d|--docker-repository  docker repository for the image
26    -t|--docker-tag         docker tag for the image
27
28Required options: --source and --docker-repository.
29
30For example, running:
31$ build_docker_image.sh -s debian9 -d mydocker/debian9-clang -t latest
32will produce two docker images:
33    mydocker/debian9-clang-build:latest - an intermediate image used to compile
34      clang.
35    mydocker/clang-debian9:latest       - a small image with preinstalled clang.
36Please note that this example produces a not very useful installation, since it
37doesn't override CMake defaults, which produces a Debug and non-boostrapped
38version of clang.
39EOF
40}
41
42while [[ $# -gt 0 ]]; do
43  case "$1" in
44    -h|--help)
45      show_usage
46      exit 0
47      ;;
48    -s|--source)
49      shift
50      IMAGE_SOURCE="$1"
51      shift
52      ;;
53    -d|--docker-repository)
54      shift
55      DOCKER_REPOSITORY="$1"
56      shift
57      ;;
58    -t|--docker-tag)
59      shift
60      DOCKER_TAG="$1"
61      shift
62      ;;
63    *)
64      echo "Unknown argument $1"
65      exit 1
66      ;;
67  esac
68done
69
70
71command -v docker >/dev/null ||
72  {
73    echo "Docker binary cannot be found. Please install Docker to use this script."
74    exit 1
75  }
76
77if [ "$IMAGE_SOURCE" == "" ]; then
78  echo "Required argument missing: --source"
79  exit 1
80fi
81
82if [ "$DOCKER_REPOSITORY" == "" ]; then
83  echo "Required argument missing: --docker-repository"
84  exit 1
85fi
86
87SOURCE_DIR=$(dirname $0)
88if [ ! -d "$SOURCE_DIR/$IMAGE_SOURCE" ]; then
89  echo "No sources for '$IMAGE_SOURCE' were found in $SOURCE_DIR"
90  exit 1
91fi
92
93BUILD_DIR=$(mktemp -d)
94trap "rm -rf $BUILD_DIR" EXIT
95echo "Using a temporary directory for the build: $BUILD_DIR"
96
97cp -r "$SOURCE_DIR/$IMAGE_SOURCE" "$BUILD_DIR/$IMAGE_SOURCE"
98cp -r "$SOURCE_DIR/scripts" "$BUILD_DIR/scripts"
99
100
101if [ "$DOCKER_TAG" != "" ]; then
102  DOCKER_TAG=":$DOCKER_TAG"
103fi
104
105echo "Building ${DOCKER_REPOSITORY}${DOCKER_TAG} from $IMAGE_SOURCE"
106docker build -t "${DOCKER_REPOSITORY}${DOCKER_TAG}" \
107  -f "$BUILD_DIR/$IMAGE_SOURCE/Dockerfile" \
108  "$BUILD_DIR"
109echo "Done"
110