1d87fb421447ddce0cdb399d81d99afd03a370db
[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 "  -i --interactive        - Run an interactive pb-builder container." >&2
19         echo "  -o --configure-opts     - Extra arguments for configure." >&2
20         echo "  -t --tag                - Print Docker tag to stdout and exit." >&2
21         echo "  -v --verbose            - Verbose execution." >&2
22         echo "Environment:" >&2
23         echo "  DOCKER_TAG              - Default: '${DOCKER_TAG}'" >&2
24         echo "  CFLAGS                  - Default: '${CFLAGS}'" >&2
25         echo "  LDFLAGS                 - Default: '${LDFLAGS}'" >&2
26         echo "Examples:" >&2
27         echo "  ${name} -vc"
28         echo "  ${name} -v"
29 }
30
31 short_opts="cdhio:tv"
32 long_opts="check,dry-run,help,interactive,configure-opts:,tag,verbose"
33
34 opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${name}" -- "$@")
35
36 if [ $? != 0 ]; then
37         echo "${name}: ERROR: Internal getopt" >&2 
38         exit 1
39 fi
40
41 eval set -- "${opts}"
42
43 while true ; do
44         case "${1}" in
45         -c | --check)
46                 check=1
47                 shift
48                 ;;
49         -d | --dry-run)
50                 dry_run=1
51                 shift
52                 ;;
53         -h | --help)
54                 usage=1
55                 shift
56                 ;;
57         -i | --interactive)
58                 interactive=1
59                 shift
60                 ;;
61         -o | --configure-opts)
62                 shift
63                 configure_opts=${1}
64                 shift
65                 ;;
66         -t | --tag)
67                 tag=1
68                 shift
69                 ;;
70         -v | --verbose)
71                 set -x
72                 verbose=1
73                 shift
74                 ;;
75         --)
76                 shift
77                 break
78                 ;;
79         *)
80                 echo "${name}: ERROR: Internal opts" >&2 
81                 exit 1
82                 ;;
83         esac
84 done
85
86 if [[ -n "${usage}" ]]; then
87         usage
88         exit 0
89 fi
90
91 if [[ -n "${tag}" ]]; then
92         show_tag
93         exit 0
94 fi
95
96 docker_base_args="\
97         --rm \
98         -v /etc/group:/etc/group:ro \
99         -v /etc/passwd:/etc/passwd:ro \
100         -v ${TOP_DIR}:/opt/pb \
101         -w /opt/pb \
102 "
103
104 docker_user_args="-u $(id -u):$(id -g)"
105
106 if [[ -n "${interactive}" ]]; then
107         cd "${TOP_DIR}"
108         docker_args="${docker_base_args} --privileged"
109         run_cmd "docker run -it ${docker_args} ${DOCKER_TAG} /bin/bash"
110         exit
111 fi
112
113 if [[ -n "${verbose}" ]]; then
114         bash_debug="-x"
115 fi
116
117 if [[ -n "${check}" ]]; then
118         docker_extra="make check"
119 else
120         docker_extra="true"
121 fi
122
123 flags="CFLAGS=${CFLAGS} LDFLAGS=${LDFLAGS}"
124
125 cd "${TOP_DIR}"
126 docker_args="${docker_base_args} ${docker_user_args}"
127 run_cmd "docker run ${docker_args} ${DOCKER_TAG} /bin/bash \
128         -e ${bash_debug} \
129         -c './bootstrap && ${flags} ./configure ${configure_opts[@]} && make && ${docker_extra}'"