0.1.5: Backported r2355: footer.kmk: Make INTERMEDIATES + keyword work. Fixes #89.
[kbuild-mirror.git] / kBuild / env.sh
blob0ab6097fc29bfc55e08fb1f0bfbb8ff92797c031
1 #!/bin/sh
2 # $Id$
3 ## @file
4 # Environment setup script.
8 # Copyright (c) 2005-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
10 # This file is part of kBuild.
12 # kBuild is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
17 # kBuild is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with kBuild; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #set -x
30 # Check if we're in eval mode or not.
32 ERR_REDIR=1
33 DBG_REDIR=1
34 EVAL_OPT=
35 EVAL_EXPORT="export "
36 DBG_OPT=
37 QUIET_OPT=
38 FULL_OPT=
39 FULL_WITH_BIN_OPT=
40 LEGACY_OPT=
41 VAR_OPT=
42 VALUE_ONLY_OPT=
43 EXP_TYPE_OPT=
44 while test $# -gt 0;
46 case "$1" in
47 "--debug-script")
48 DBG_OPT="true"
50 "--no-debug-script")
51 DBG_OPT=
53 "--quiet")
54 QUIET_OPT="true"
56 "--verbose")
57 QUIET_OPT=
59 "--full")
60 FULL_OPT="true"
62 "--full-with-bin")
63 FULL_OPT="true"
64 FULL_WITH_BIN_OPT="true"
66 "--normal")
67 FULL_OPT=
69 "--legacy")
70 LEGACY_OPT="true"
72 "--no-legacy")
73 LEGACY_OPT=
75 "--eval")
76 EVAL_OPT="true"
77 ERR_REDIR=2
78 DBG_REDIR=2
80 "--set")
81 EVAL_OPT="true"
82 EVAL_EXPORT=""
83 ERR_REDIR=2
84 DBG_REDIR=2
86 "--var")
87 shift
88 VAR_OPT="${VAR_OPT} $1"
89 ERR_REDIR=2
90 DBG_REDIR=2
92 "--value-only")
93 VALUE_ONLY_OPT="true"
95 "--name-and-value")
96 VALUE_ONLY_OPT=
98 "--release")
99 EXP_TYPE_OPT=1
100 KBUILD_TYPE=release
101 BUILD_TYPE=
103 "--debug")
104 EXP_TYPE_OPT=1
105 KBUILD_TYPE=debug
106 BUILD_TYPE=
108 "--profile")
109 EXP_TYPE_OPT=1
110 KBUILD_TYPE=profile
111 BUILD_TYPE=
114 "--help")
115 echo "kBuild Environment Setup Script, v0.2.0-pre"
116 echo ""
117 echo "syntax: $0 [options] [command [args]]"
118 echo " or: $0 [options] --var <varname>"
119 echo " or: $0 [options] --eval"
120 echo " or: $0 [options] --eval --var <varname>"
121 echo ""
122 echo "The first form will execute the command, or if no command is given start"
123 echo "an interactive shell."
124 echo "The second form will print the specfified variable(s)."
125 echo "The third form will print all exported variables suitable for bourne shell"
126 echo "evaluation."
127 echo "The forth form will only print the specified variable(s)."
128 echo ""
129 echo "Options:"
130 echo " --debug, --release, --profile"
131 echo " Alternative way of specifying KBUILD_TYPE."
132 echo " --debug-script, --no-debug-script"
133 echo " Controls debug output. Default: --no-debug-script"
134 echo " --quiet, --verbose"
135 echo " Controls informational output. Default: --verbose"
136 echo " --full, --full-with-bin, --normal"
137 echo " Controls the variable set. Default: --normal"
138 echo " --legacy, --no-legacy"
139 echo " Include legacy variables in result. Default: --no-legacy"
140 echo " --value-only, --name-and-value"
141 echo " Controls what the result of a --var query. Default: --name-and-value"
142 echo " --set, --export"
143 echo " Whether --eval explicitly export the variables. --set is useful for"
144 echo " getting a list of environment vars for a commandline, while --eval"
145 echo ' is useful for eval `env.sh`. Default: --export'
146 echo ""
147 exit 1
150 break
152 esac
153 shift
154 done
158 # Deal with legacy environment variables.
160 if test -n "$PATH_KBUILD"; then
161 if test -n "$KBUILD_PATH" -a "$KBUILD_PATH" != "$PATH_KBUILD"; then
162 echo "$0: error: KBUILD_PATH ($KBUILD_PATH) and PATH_KBUILD ($PATH_KBUILD) disagree." 1>&${ERR_REDIR}
163 sleep 1
164 exit 1
166 KBUILD_PATH=$PATH_KBUILD
168 if test -n "$PATH_KBUILD_BIN"; then
169 if test -n "$KBUILD_BIN_PATH" -a "$KBUILD_BIN_PATH" != "$PATH_KBUILD_BIN"; then
170 echo "$0: error: KBUILD_BIN_PATH ($KBUILD_BIN_PATH) and PATH_KBUILD_BIN ($PATH_KBUILD_BIN) disagree." 1>&${ERR_REDIR}
171 sleep 1
172 exit 1
174 KBUILD_BIN_PATH=$PATH_KBUILD_BIN
177 if test -n "$BUILD_TYPE"; then
178 if test -n "$KBUILD_TYPE" -a "$KBUILD_TYPE" != "$BUILD_TYPE"; then
179 echo "$0: error: KBUILD_TYPE ($KBUILD_TYPE) and BUILD_TYPE ($BUILD_TYPE) disagree." 1>&${ERR_REDIR}
180 sleep 1
181 exit 1
183 KBUILD_TYPE=$BUILD_TYPE
186 if test -n "$BUILD_PLATFORM"; then
187 if test -n "$KBUILD_HOST" -a "$KBUILD_HOST" != "$BUILD_PLATFORM"; then
188 echo "$0: error: KBUILD_HOST ($KBUILD_HOST) and BUILD_PLATFORM ($BUILD_PLATFORM) disagree." 1>&${ERR_REDIR}
189 sleep 1
190 exit 1
192 KBUILD_HOST=$BUILD_PLATFORM
194 if test -n "$BUILD_PLATFORM_ARCH"; then
195 if test -n "$KBUILD_HOST_ARCH" -a "$KBUILD_HOST_ARCH" != "$BUILD_PLATFORM_ARCH"; then
196 echo "$0: error: KBUILD_HOST_ARCH ($KBUILD_HOST_ARCH) and BUILD_PLATFORM_ARCH ($BUILD_PLATFORM_ARCH) disagree." 1>&${ERR_REDIR}
197 sleep 1
198 exit 1
200 KBUILD_HOST_ARCH=$BUILD_PLATFORM_ARCH
202 if test -n "$BUILD_PLATFORM_CPU"; then
203 if test -n "$KBUILD_HOST_CPU" -a "$KBUILD_HOST_CPU" != "$BUILD_PLATFORM_CPU"; then
204 echo "$0: error: KBUILD_HOST_CPU ($KBUILD_HOST_CPU) and BUILD_PLATFORM_CPU ($BUILD_PLATFORM_CPU) disagree." 1>&${ERR_REDIR}
205 sleep 1
206 exit 1
208 KBUILD_HOST_CPU=$BUILD_PLATFORM_CPU
211 if test -n "$BUILD_TARGET"; then
212 if test -n "$KBUILD_TARGET" -a "$KBUILD_TARGET" != "$BUILD_TARGET"; then
213 echo "$0: error: KBUILD_TARGET ($KBUILD_TARGET) and BUILD_TARGET ($BUILD_TARGET) disagree." 1>&${ERR_REDIR}
214 sleep 1
215 exit 1
217 KBUILD_TARGET=$BUILD_TARGET
219 if test -n "$BUILD_TARGET_ARCH"; then
220 if test -n "$KBUILD_TARGET_ARCH" -a "$KBUILD_TARGET_ARCH" != "$BUILD_TARGET_ARCH"; then
221 echo "$0: error: KBUILD_TARGET_ARCH ($KBUILD_TARGET_ARCH) and BUILD_TARGET_ARCH ($BUILD_TARGET_ARCH) disagree." 1>&${ERR_REDIR}
222 sleep 1
223 exit 1
225 KBUILD_TARGET_ARCH=$BUILD_TARGET_ARCH
227 if test -n "$BUILD_TARGET_CPU"; then
228 if test -n "$KBUILD_TARGET_CPU" -a "$KBUILD_TARGET_CPU" != "$BUILD_TARGET_CPU"; then
229 echo "$0: error: KBUILD_TARGET_CPU ($KBUILD_TARGET_CPU) and BUILD_TARGET_CPU ($BUILD_TARGET_CPU) disagree." 1>&${ERR_REDIR}
230 sleep 1
231 exit 1
233 KBUILD_TARGET_CPU=$BUILD_TARGET_CPU
238 # Set default build type.
240 if test -z "$KBUILD_TYPE"; then
241 KBUILD_TYPE=release
243 test -n "$DBG_OPT" && echo "dbg: KBUILD_TYPE=$KBUILD_TYPE" 1>&${DBG_REDIR}
246 # Determin the host platform.
248 # The CPU isn't important, only the other two are. But, since the cpu,
249 # arch and platform (and build type) share a common key space, try make
250 # sure any new additions are unique. (See header.kmk, KBUILD_OSES/ARCHES.)
252 if test -z "$KBUILD_HOST"; then
253 KBUILD_HOST=`uname`
254 case "$KBUILD_HOST" in
255 Darwin|darwin)
256 KBUILD_HOST=darwin
259 DragonFly)
260 KBUILD_HOST=dragonfly
263 freebsd|FreeBSD|FREEBSD)
264 KBUILD_HOST=freebsd
267 linux|Linux|GNU/Linux|LINUX)
268 KBUILD_HOST=linux
271 netbsd|NetBSD|NETBSD)
272 KBUILD_HOST=netbsd
275 openbsd|OpenBSD|OPENBSD)
276 KBUILD_HOST=openbsd
279 os2|OS/2|OS2)
280 KBUILD_HOST=os2
283 SunOS)
284 KBUILD_HOST=solaris
287 WindowsNT|CYGWIN_NT-*)
288 KBUILD_HOST=win
292 echo "$0: unknown os $KBUILD_HOST" 1>&${ERR_REDIR}
293 sleep 1
294 exit 1
296 esac
298 test -n "$DBG_OPT" && echo "dbg: KBUILD_HOST=$KBUILD_HOST" 1>&${DBG_REDIR}
300 if test -z "$KBUILD_HOST_ARCH"; then
301 # Try deduce it from the cpu if given.
302 if test -n "$KBUILD_HOST_CPU"; then
303 case "$KBUILD_HOST_CPU" in
304 i[3456789]86)
305 KBUILD_HOST_ARCH='x86'
307 k8|k8l|k9|k10)
308 KBUILD_HOST_ARCH='amd64'
310 esac
313 if test -z "$KBUILD_HOST_ARCH"; then
314 # Use uname -m or isainfo (lots of guesses here, please help clean this up...)
315 if test "$KBUILD_HOST" = "solaris"; then
316 KBUILD_HOST_ARCH=`isainfo | cut -f 1 -d ' '`
318 else
319 KBUILD_HOST_ARCH=`uname -m`
321 case "$KBUILD_HOST_ARCH" in
322 x86_64|AMD64|amd64|k8|k8l|k9|k10)
323 KBUILD_HOST_ARCH='amd64'
325 x86|i86pc|ia32|i[3456789]86)
326 KBUILD_HOST_ARCH='x86'
328 sparc32|sparc|sparcv8|sparcv7|sparcv8e)
329 KBUILD_HOST_ARCH='sparc32'
331 sparc64|sparcv9)
332 KBUILD_HOST_ARCH='sparc64'
334 s390)
335 KBUILD_HOST_ARCH='s390'
337 s390x)
338 KBUILD_HOST_ARCH='s390x'
340 ppc32|ppc|powerpc)
341 KBUILD_HOST_ARCH='ppc32'
343 ppc64|powerpc64)
344 KBUILD_HOST_ARCH='ppc64'
346 mips32|mips)
347 KBUILD_HOST_ARCH='mips32'
349 mips64)
350 KBUILD_HOST_ARCH='mips64'
352 ia64)
353 KBUILD_HOST_ARCH='ia64'
355 hppa32|parisc32|parisc)
356 KBUILD_HOST_ARCH='hppa32'
358 hppa64|parisc64)
359 KBUILD_HOST_ARCH='hppa64'
361 arm|armv4l|armv5tel|armv5tejl)
362 KBUILD_HOST_ARCH='arm'
364 alpha)
365 KBUILD_HOST_ARCH='alpha'
368 *) echo "$0: unknown cpu/arch - $KBUILD_HOST_ARCH" 1>&${ERR_REDIR}
369 sleep 1
370 exit 1
372 esac
375 test -n "$DBG_OPT" && echo "dbg: KBUILD_HOST_ARCH=$KBUILD_HOST_ARCH" 1>&${DBG_REDIR}
377 if test -z "$KBUILD_HOST_CPU"; then
378 KBUILD_HOST_CPU="blend"
380 test -n "$DBG_OPT" && echo "dbg: KBUILD_HOST_CPU=$KBUILD_HOST_CPU" 1>&${DBG_REDIR}
383 # The target platform.
384 # Defaults to the host when not specified.
386 if test -z "$KBUILD_TARGET"; then
387 KBUILD_TARGET="$KBUILD_HOST"
389 test -n "$DBG_OPT" && echo "dbg: KBUILD_TARGET=$KBUILD_TARGET" 1>&${DBG_REDIR}
391 if test -z "$KBUILD_TARGET_ARCH"; then
392 KBUILD_TARGET_ARCH="$KBUILD_HOST_ARCH"
394 test -n "$DBG_OPT" && echo "dbg: KBUILD_TARGET_ARCH=$KBUILD_TARGET_ARCH" 1>&${DBG_REDIR}
396 if test -z "$KBUILD_TARGET_CPU"; then
397 if test "$KBUILD_TARGET_ARCH" = "$KBUILD_HOST_ARCH"; then
398 KBUILD_TARGET_CPU="$KBUILD_HOST_CPU"
399 else
400 KBUILD_TARGET_CPU="blend"
403 test -n "$DBG_OPT" && echo "dbg: KBUILD_TARGET_CPU=$KBUILD_TARGET_CPU" 1>&${DBG_REDIR}
406 # Determin executable extension and path separator.
408 _SUFF_EXE=
409 _PATH_SEP=":"
410 case "$KBUILD_HOST" in
411 os2|win|nt)
412 _SUFF_EXE=".exe"
413 _PATH_SEP=";"
415 esac
418 # Determin KBUILD_PATH from the script location and calc KBUILD_BIN_PATH from there.
420 if test -z "$KBUILD_PATH"; then
421 KBUILD_PATH=`dirname "$0"`
422 KBUILD_PATH=`cd "$KBUILD_PATH" ; /bin/pwd`
424 if test ! -f "$KBUILD_PATH/footer.kmk" -o ! -f "$KBUILD_PATH/header.kmk" -o ! -f "$KBUILD_PATH/rules.kmk"; then
425 echo "$0: error: KBUILD_PATH ($KBUILD_PATH) is not pointing to a popluated kBuild directory." 1>&${ERR_REDIR}
426 sleep 1
427 exit 1
429 test -n "$DBG_OPT" && echo "dbg: KBUILD_PATH=$KBUILD_PATH" 1>&${DBG_REDIR}
431 if test -z "$KBUILD_BIN_PATH"; then
432 KBUILD_BIN_PATH="${KBUILD_PATH}/bin/${KBUILD_HOST}.${KBUILD_HOST_ARCH}"
434 test -n "$DBG_OPT" && echo "dbg: KBUILD_BIN_PATH=${KBUILD_BIN_PATH}" 1>&${DBG_REDIR}
437 # Add the bin/x.y/ directory to the PATH.
438 # NOTE! Once bootstrapped this is the only thing that is actually necessary.
440 PATH="${KBUILD_BIN_PATH}${_PATH_SEP}$PATH"
441 test -n "$DBG_OPT" && echo "dbg: PATH=$PATH" 1>&${DBG_REDIR}
444 # Sanity and x bits.
446 if test ! -d "${KBUILD_BIN_PATH}/"; then
447 echo "$0: warning: The bin directory for this platform doesn't exist. (${KBUILD_BIN_PATH}/)" 1>&${ERR_REDIR}
448 else
449 for prog in kmk kDepPre kDepIDB kmk_append kmk_ash kmk_cat kmk_cp kmk_echo kmk_install kmk_ln kmk_mkdir kmk_mv kmk_rm kmk_rmdir kmk_sed;
451 chmod a+x ${KBUILD_BIN_PATH}/${prog} > /dev/null 2>&1
452 if test ! -f "${KBUILD_BIN_PATH}/${prog}${_SUFF_EXE}"; then
453 echo "$0: warning: The ${prog} program doesn't exist for this platform. (${KBUILD_BIN_PATH}/${prog}${_SUFF_EXE})" 1>&${ERR_REDIR}
455 done
459 # The environment is in place, now take the requested action.
461 MY_RC=0
462 if test -n "${VAR_OPT}"; then
463 # Echo variable values or variable export statements.
464 for var in ${VAR_OPT};
466 val=
467 case "$var" in
468 PATH)
469 val=$PATH
471 KBUILD_PATH)
472 val=$KBUILD_PATH
474 KBUILD_BIN_PATH)
475 val=$KBUILD_BIN_PATH
477 KBUILD_HOST)
478 val=$KBUILD_HOST
480 KBUILD_HOST_ARCH)
481 val=$KBUILD_HOST_ARCH
483 KBUILD_HOST_CPU)
484 val=$KBUILD_HOST_CPU
486 KBUILD_TARGET)
487 val=$KBUILD_TARGET
489 KBUILD_TARGET_ARCH)
490 val=$KBUILD_TARGET_ARCH
492 KBUILD_TARGET_CPU)
493 val=$KBUILD_TARGET_CPU
495 KBUILD_TYPE)
496 val=$KBUILD_TYPE
499 echo "$0: error: Unknown variable $var specified in --var request." 1>&${ERR_REDIR}
500 sleep 1
501 exit 1
503 esac
505 if test -n "$EVAL_OPT"; then
506 echo "${EVAL_EXPORT} $var=$val"
507 else
508 if test -n "$VALUE_ONLY_OPT"; then
509 echo "$val"
510 else
511 echo "$var=$val"
514 done
515 else
516 if test -n "$EVAL_OPT"; then
517 # Echo statements for the shell to evaluate.
518 test -n "$DBG_OPT" && echo "dbg: echoing exported variables" 1>&${DBG_REDIR}
519 echo "${EVAL_EXPORT} PATH=${PATH}"
520 test -n "${FULL_OPT}" -o "${EXP_TYPE_OPT}" && echo "${EVAL_EXPORT} KBUILD_TYPE=${KBUILD_TYPE}"
521 if test -n "${FULL_OPT}"; then
522 echo "${EVAL_EXPORT} KBUILD_PATH=${KBUILD_PATH}"
523 if test -n "{FULL_WITH_BIN_OPT}"; then
524 echo "${EVAL_EXPORT} KBUILD_BIN_PATH=${KBUILD_BIN_PATH}"
526 echo "${EVAL_EXPORT} KBUILD_HOST=${KBUILD_HOST}"
527 echo "${EVAL_EXPORT} KBUILD_HOST_ARCH=${KBUILD_HOST_ARCH}"
528 echo "${EVAL_EXPORT} KBUILD_HOST_CPU=${KBUILD_HOST_CPU}"
529 echo "${EVAL_EXPORT} KBUILD_TARGET=${KBUILD_TARGET}"
530 echo "${EVAL_EXPORT} KBUILD_TARGET_ARCH=${KBUILD_TARGET_ARCH}"
531 echo "${EVAL_EXPORT} KBUILD_TARGET_CPU=${KBUILD_TARGET_CPU}"
533 if test -n "${LEGACY_OPT}"; then
534 echo "${EVAL_EXPORT} PATH_KBUILD=${KBUILD_PATH}"
535 if test -n "${FULL_WITH_BIN_OPT}"; then
536 echo "${EVAL_EXPORT} PATH_KBUILD_BIN=${KBUILD_PATH_BIN}"
538 echo "${EVAL_EXPORT} BUILD_TYPE=${KBUILD_TYPE}"
539 echo "${EVAL_EXPORT} BUILD_PLATFORM=${KBUILD_HOST}"
540 echo "${EVAL_EXPORT} BUILD_PLATFORM_ARCH=${KBUILD_HOST_ARCH}"
541 echo "${EVAL_EXPORT} BUILD_PLATFORM_CPU=${KBUILD_HOST_CPU}"
542 echo "${EVAL_EXPORT} BUILD_TARGET=${KBUILD_TARGET}"
543 echo "${EVAL_EXPORT} BUILD_TARGET_ARCH=${KBUILD_TARGET_ARCH}"
544 echo "${EVAL_EXPORT} BUILD_TARGET_CPU=${KBUILD_TARGET_CPU}"
547 else
548 # Export variables.
549 export PATH
550 test -n "${FULL_OPT}" -o "${EXP_TYPE_OPT}" && export KBUILD_TYPE
551 if test -n "${FULL_OPT}"; then
552 export KBUILD_PATH
553 if test -n "${FULL_WITH_BIN_OPT}"; then
554 export KBUILD_BIN_PATH
556 export KBUILD_HOST
557 export KBUILD_HOST_ARCH
558 export KBUILD_HOST_CPU
559 export KBUILD_TARGET
560 export KBUILD_TARGET_ARCH
561 export KBUILD_TARGET_CPU
563 if test -n "${LEGACY_OPT}"; then
564 export PATH_KBUILD=$KBUILD_PATH
565 if test -n "${FULL_WITH_BIN_OPT}"; then
566 export PATH_KBUILD_BIN=$KBUILD_BIN_PATH
568 export BUILD_TYPE=$KBUILD_TYPE
569 export BUILD_PLATFORM=$KBUILD_HOST
570 export BUILD_PLATFORM_ARCH=$KBUILD_HOST_ARCH
571 export BUILD_PLATFORM_CPU=$KBUILD_HOST_CPU
572 export BUILD_TARGET=$KBUILD_TARGET
573 export BUILD_TARGET_ARCH=$KBUILD_TARGET_ARCH
574 export BUILD_TARGET_CPU=$KBUILD_TARGET_CPU
578 # Execute command or spawn shell.
579 if test $# -eq 0; then
580 test -z "${QUIET_OPT}" && echo "$0: info: Spawning work shell..." 1>&${ERR_REDIR}
581 if test "$TERM" != 'dumb' -a -n "$BASH"; then
582 export PS1='\[\033[01;32m\]\u@\h \[\033[01;34m\]\W \$ \[\033[00m\]'
584 $SHELL -i
585 MY_RC=$?
586 else
587 test -z "${QUIET_OPT}" && echo "$0: info: Executing command: $*" 1>&${ERR_REDIR}
589 MY_RC=$?
590 test -z "${QUIET_OPT}" -a "$MY_RC" -ne 0 && echo "$0: info: rc=$MY_RC: $*" 1>&${ERR_REDIR}
594 test -n "$DBG_OPT" && echo "dbg: finished (rc=$MY_RC)" 1>&${DBG_REDIR}
595 exit $MY_RC