]> gitweb @ CieloNegro.org - autobuild.git/blob - Build
initial commit
[autobuild.git] / Build
1 #!/usr/bin/env bash
2 # -----------------------------------------------------------------------------
3 # Build automation script designed to work with autotools.
4 # -----------------------------------------------------------------------------
5 set -e
6
7 # -----------------------------------------------------------------------------
8 # Functions used by Build.rc
9 #
10 declare prefix
11 function setPrefix () {
12     if (( $# != 1 )); then
13         echo "Usage: $0 PREFIX" >&2
14         return 1
15     fi
16     prefix="$1"
17 }
18
19 function setPath () {
20     if (( $# == 0 )); then
21         echo "Usage: $0 VAR [PATH, ...]" >&2
22         return 1
23     fi
24
25     local -r var="$1"
26     local paths=("${@:2}")
27
28     case ${#paths[@]} in
29         0)
30             unset $var;;
31         1)
32             export $var="$paths";;
33         *)
34             export $var="${paths[0]}"
35             local path
36             for path in "${paths[@]:1}"; do
37                 local val=$(eval echo \$$var)
38                 export $var="$val:$path"
39             done
40     esac
41
42     # Special case for PATH: we prepend "$prefix/bin" to it.
43     if [[ $var = "PATH" ]]; then
44         PATH="$prefix/bin:$PATH"
45     fi
46 }
47
48 declare -a configArgs
49 function setConfigArgs () {
50     configArgs=( \
51         --prefix="$prefix" \
52         PATH="$PATH" \
53         "$@" \
54         )
55 }
56
57 declare buildTarget
58 function setBuildTarget () {
59     if (( $# != 1 )); then
60         echo "Usage: $0 TARGET" >&2
61         return 1
62     fi
63     buildTarget="$1"
64 }
65
66 declare docDirectory
67 function setDocDirectory () {
68     if (( $# != 1 )); then
69         echo "Usage: $0 DIRECTORY" >&2
70         return 1
71     fi
72     docDirectory="$1"
73 }
74
75 declare docTarget
76 function setDocTarget () {
77     if (( $# != 1 )); then
78         echo "Usage: $0 TARGET" >&2
79         return 1
80     fi
81     docTarget="$1"
82 }
83
84 # -----------------------------------------------------------------------------
85 # Setting variables
86 #
87 setPrefix "/usr/local"
88 setPath PATH "/usr/bin" "/bin"
89 setConfigArgs
90 setBuildTarget "all"
91 setDocDirectory "."
92 setDocTarget "all"
93
94 if [ -f "Build.rc" ]; then
95     . "Build.rc"
96 fi
97
98 # -----------------------------------------------------------------------------
99 # Main
100 #
101 function run () {
102     if (( $# == 0 )); then
103         echo "run: Usage: run CMD [ARG...]" >&2
104         return 1
105     fi
106
107     if [[ -t 1 ]]; then
108         # Bold + Green
109         echo -ne "\e[1;32m"
110         echo "$@"
111         echo -ne "\e[0m"
112     else
113         echo "$@"
114     fi
115
116     $@
117 }
118
119 function detectNumCPUs () {
120     if sysctl -n "hw.ncpu" 2>/dev/null; then
121         # This works for most BSDs.
122         :
123     elif grep -qF processor /proc/cpuinfo 2>/dev/null; then
124         # Linux sucks...
125         grep -cF processor /proc/cpuinfo
126     else
127         echo "WARNING: I don't know how to detect the number of processors on this platform." >&2
128         echo 1
129     fi
130 }
131
132 function runMake () {
133     run gmake -w -j $(detectNumCPUs) $@
134 }
135
136 function configure () {
137     if [[ ! -f configure.ac && ! -f configure.in ]]; then
138         echo "ERROR: Neither configure.ac nor configure.in exists in the current directory." >&2
139         return 1
140     fi
141
142     if [[ ! -f configure ]]; then
143         run autoreconf -v -i -f
144     fi
145
146     if [[ ! -f "_build/Makefile" ]]; then
147         run mkdir -p _build
148         run pushd _build
149         run ../configure "${configArgs[@]}"
150         run popd
151     fi
152 }
153
154 function build () {
155     configure
156     runMake -C "_build" $buildTarget
157 }
158
159 function doc () {
160     configure
161     runMake -C "_build/$docDirectory" $docTarget
162 }
163
164 function check () {
165     build
166     runMake -C "_build" check
167 }
168
169 function clean () {
170     run rm -rf "_build"
171 }
172
173 function dist () {
174     configure
175     runMake -C "_build" dist
176 }
177
178 function install () {
179     build
180     runMake -C "_build" install
181 }
182
183 case "$1" in
184     ""|"build") build  ;;
185     "doc"     ) doc    ;;
186     "check"   ) check  ;;
187     "clean"   ) clean  ;;
188     "dist"    ) dist   ;;
189     "install" ) install;;
190     *)
191         echo "Usage: $0 [build | doc | check | clean | dist | install]" >&2
192         exit 1
193 esac