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