ce6c41fc9aa207f78d07ca74503f6528be46c690
[petitboot] / docker / build-pb
1 #!/usr/bin/env bash
2
3 set -e
4
5 name="$(basename $0)"
6
7 : ${TOP_DIR:="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"}
8
9 source ${TOP_DIR}/docker/builder-include
10
11 usage () {
12         echo "${name} - Builds the petitboot programs using a pb-builder container." >&2
13         echo "Usage: ${name} [flags]" >&2
14         echo "Option flags:" >&2
15         echo "  -c --check    - Run 'make check'." >&2
16         echo "  -d --dry-run  - Do not run docker commands." >&2
17         echo "  -h --help     - Show this help and exit." >&2
18         echo "  -t --tag      - Print Docker tag to stdout and exit." >&2
19         echo "  -v --verbose  - Verbose execution." >&2
20         echo "Environment:" >&2
21         echo "  DOCKER_TAG    - Default: '${DOCKER_TAG}'" >&2
22         echo "Examples:" >&2
23         echo "  ${name} -vc"
24         echo "  ${name} -v"
25 }
26
27 short_opts="cdhtv"
28 long_opts="check,dry-run,help,tag,verbose"
29
30 opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${name}" -- "$@")
31
32 if [ $? != 0 ]; then
33         echo "${name}: ERROR: Internal getopt" >&2 
34         exit 1
35 fi
36
37 eval set -- "${opts}"
38
39 while true ; do
40         case "${1}" in
41         -c | --check)
42                 check=1
43                 shift
44                 ;;
45         -d | --dry-run)
46                 dry_run=1
47                 shift
48                 ;;
49         -h | --help)
50                 usage=1
51                 shift
52                 ;;
53         -t | --tag)
54                 tag=1
55                 shift
56                 ;;
57         -v | --verbose)
58                 set -x
59                 verbose=1
60                 shift
61                 ;;
62         --)
63                 shift
64                 break
65                 ;;
66         *)
67                 echo "${name}: ERROR: Internal opts" >&2 
68                 exit 1
69                 ;;
70         esac
71 done
72
73 if [[ -n "${usage}" ]]; then
74         usage
75         exit 0
76 fi
77
78 if [[ -n "${tag}" ]]; then
79         show_tag
80         exit 0
81 fi
82
83 docker_args="--rm \
84         -u $(id -u):$(id -g) \
85         -v /etc/group:/etc/group:ro \
86         -v /etc/passwd:/etc/passwd:ro \
87         -v ${TOP_DIR}:/opt/pb \
88         -w /opt/pb \
89 "
90
91 if [[ -n "$verbose}" ]]; then
92         bash_debug="-x"
93 fi
94
95 if [[ -n "${check}" ]]; then
96         docker_extra="make check"
97 else
98         docker_extra="true"
99 fi
100
101 cd "${TOP_DIR}"
102 run_cmd "docker run ${docker_args} ${DOCKER_TAG} /bin/bash \
103         -e ${bash_debug} \
104         -c './bootstrap && ./configure --with-ncurses && make && ${docker_extra}'"