source: trunk/kBuild/env.sh@ 1521

Last change on this file since 1521 was 1517, checked in by bird, 17 years ago

Implemented the new environment / global variable policy.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
RevLine 
[1510]1#!/bin/sh
[224]2# $Id: env.sh 1517 2008-04-09 22:25:57Z bird $
3## @file
4# Environment setup script.
5#
[1511]6
7#
[1507]8# Copyright (c) 2005-2008 knut st. osmundsen <bird-kBuild-spam@anduin.net>
[224]9#
10# This file is part of kBuild.
11#
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.
16#
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.
21#
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
25#
26#
[1067]27#set -x
[224]28
[992]29#
[1510]30# Check if we're in eval mode or not.
31#
32ERR_REDIR=1
33DBG_REDIR=1
34EVAL_OPT=
35DBG_OPT=
36QUIET_OPT=
[1512]37FULL_OPT=
[1517]38LEGACY_OPT="true"
[1513]39VAR_OPT=
[1517]40VALUE_ONLY_OPT=
[1512]41while test $# -gt 0;
42do
43 case "$1" in
44 "--debug")
45 DBG_OPT="true"
46 ;;
[1517]47 "--no-debug")
48 DBG_OPT=
49 ;;
[1512]50 "--quiet")
51 QUIET_OPT="true"
52 ;;
[1517]53 "--verbose")
54 QUIET_OPT=
55 ;;
[1512]56 "--full")
57 FULL_OPT="true"
58 ;;
[1517]59 "--normal")
60 FULL_OPT=
61 ;;
62 "--legacy")
63 LEGACY_OPT="true"
64 ;;
65 "--no-legacy")
66 LEGACY_OPT=
67 ;;
[1512]68 "--eval")
69 EVAL_OPT="true"
70 ERR_REDIR=2
71 DBG_REDIR=2
72 ;;
[1513]73 "--var")
74 shift
75 VAR_OPT="${VAR_OPT} $1"
[1517]76 ERR_REDIR=2
77 DBG_REDIR=2
[1513]78 ;;
[1517]79 "--value-only")
80 VALUE_ONLY_OPT="true"
81 ;;
82 "--name-and-value")
83 VALUE_ONLY_OPT=
84 ;;
[1512]85 "--help")
[1517]86 echo "kBuild Environment Setup Script, v0.1.3"
87 echo ""
88 echo "syntax: $0 [options] [command [args]]"
89 echo " or: $0 [options] --var <varname>"
90 echo " or: $0 [options] --eval"
91 echo " or: $0 [options] --eval --var <varname>"
92 echo ""
93 echo "The first form will execute the command, or if no command is givne start"
94 echo "an interactive shell."
95 echo "The second form will print the specfified variable(s)."
96 echo "The third form will print all exported variables suitable for bourne shell"
97 echo "evalutation."
98 echo "The forth form will only print the specified variable(s)."
99 echo ""
100 echo "Options:"
101 echo " --debug, --no-debug"
102 echo " Controls debug output. Default: --no-debug"
103 echo " --quiet, --verbose"
104 echo " Controls informational output. Default: --verbose"
105 echo " --full, --normal"
106 echo " Controls the variable set. Default: --normal"
107 echo " --legacy, --no-legacy"
108 echo " Include legacy variables in result. Default: --legacy"
109 echo " --value-only, --name-and-value"
110 echo " Controls what the result of a --var query. Default: --name-and-value"
111 echo ""
[1512]112 exit 1
113 ;;
114 *)
115 break
116 ;;
117 esac
[1510]118 shift
[1512]119done
[1510]120
121
122#
[1517]123# Deal with legacy environment variables.
[992]124#
[1517]125if test -n "$PATH_KBUILD"; then
126 if test -n "$KBUILD_PATH" -a "$KBUILD_PATH" != "$PATH_KBUILD"; then
127 echo "$0: error: KBUILD_PATH ($KBUILD_PATH) and PATH_KBUILD ($PATH_KBUILD) disagree." 1>&${ERR_REDIR}
128 sleep 1
129 exit 1
130 fi
131 KBUILD_PATH=$PATH_KBUILD
[224]132fi
[1517]133if test -n "$PATH_KBUILD_BIN"; then
134 if test -n "$KBUILD_BIN_PATH" -a "$KBUILD_BIN_PATH" != "$PATH_KBUILD_BIN"; then
135 echo "$0: error: KBUILD_BIN_PATH ($KBUILD_BIN_PATH) and PATH_KBUILD_BIN ($PATH_KBUILD_BIN) disagree." 1>&${ERR_REDIR}
136 sleep 1
137 exit 1
138 fi
139 KBUILD_BIN_PATH=$PATH_KBUILD_BIN
[224]140fi
141
[1517]142if test -n "$BUILD_TYPE"; then
143 if test -n "$KBUILD_TYPE" -a "$KBUILD_TYPE" != "$BUILD_TYPE"; then
144 echo "$0: error: KBUILD_TYPE ($KBUILD_TYPE) and BUILD_TYPE ($BUILD_TYPE) disagree." 1>&${ERR_REDIR}
145 sleep 1
146 exit 1
147 fi
148 KBUILD_TYPE=$BUILD_TYPE
149fi
[1076]150
[1517]151if test -n "$BUILD_PLATFORM"; then
152 if test -n "$KBUILD_HOST" -a "$KBUILD_HOST" != "$BUILD_PLATFORM"; then
153 echo "$0: error: KBUILD_HOST ($KBUILD_HOST) and BUILD_PLATFORM ($BUILD_PLATFORM) disagree." 1>&${ERR_REDIR}
154 sleep 1
155 exit 1
156 fi
157 KBUILD_HOST=$BUILD_PLATFORM
158fi
159if test -n "$BUILD_PLATFORM_ARCH"; then
160 if test -n "$KBUILD_HOST_ARCH" -a "$KBUILD_HOST_ARCH" != "$BUILD_PLATFORM_ARCH"; then
161 echo "$0: error: KBUILD_HOST_ARCH ($KBUILD_HOST_ARCH) and BUILD_PLATFORM_ARCH ($BUILD_PLATFORM_ARCH) disagree." 1>&${ERR_REDIR}
162 sleep 1
163 exit 1
164 fi
165 KBUILD_HOST_ARCH=$BUILD_PLATFORM_ARCH
166fi
167if test -n "$BUILD_PLATFORM_CPU"; then
168 if test -n "$KBUILD_HOST_CPU" -a "$KBUILD_HOST_CPU" != "$BUILD_PLATFORM_CPU"; then
169 echo "$0: error: KBUILD_HOST_CPU ($KBUILD_HOST_CPU) and BUILD_PLATFORM_CPU ($BUILD_PLATFORM_CPU) disagree." 1>&${ERR_REDIR}
170 sleep 1
171 exit 1
172 fi
173 KBUILD_HOST_CPU=$BUILD_PLATFORM_CPU
174fi
175
176if test -n "$BUILD_TARGET"; then
177 if test -n "$KBUILD_TARGET" -a "$KBUILD_TARGET" != "$BUILD_TARGET"; then
178 echo "$0: error: KBUILD_TARGET ($KBUILD_TARGET) and BUILD_TARGET ($BUILD_TARGET) disagree." 1>&${ERR_REDIR}
179 sleep 1
180 exit 1
181 fi
182 KBUILD_TARGET=$BUILD_TARGET
183fi
184if test -n "$BUILD_TARGET_ARCH"; then
185 if test -n "$KBUILD_TARGET_ARCH" -a "$KBUILD_TARGET_ARCH" != "$BUILD_TARGET_ARCH"; then
186 echo "$0: error: KBUILD_TARGET_ARCH ($KBUILD_TARGET_ARCH) and BUILD_TARGET_ARCH ($BUILD_TARGET_ARCH) disagree." 1>&${ERR_REDIR}
187 sleep 1
188 exit 1
189 fi
190 KBUILD_TARGET_ARCH=$BUILD_TARGET_ARCH
191fi
192if test -n "$BUILD_TARGET_CPU"; then
193 if test -n "$KBUILD_TARGET_CPU" -a "$KBUILD_TARGET_CPU" != "$BUILD_TARGET_CPU"; then
194 echo "$0: error: KBUILD_TARGET_CPU ($KBUILD_TARGET_CPU) and BUILD_TARGET_CPU ($BUILD_TARGET_CPU) disagree." 1>&${ERR_REDIR}
195 sleep 1
196 exit 1
197 fi
198 KBUILD_TARGET_CPU=$BUILD_TARGET_CPU
199fi
200
201
[992]202#
203# Set default build type.
204#
[1517]205if test -z "$KBUILD_TYPE"; then
206 KBUILD_TYPE=release
[224]207fi
[1517]208test -n "$DBG_OPT" && echo "dbg: KBUILD_TYPE=$KBUILD_TYPE" 1>&${DBG_REDIR}
[224]209
[992]210#
211# Determin the host platform.
212#
213# The CPU isn't important, only the other two are. But, since the cpu,
214# arch and platform (and build type) share a common key space, try make
215# sure any new additions are unique. (See header.kmk, KBUILD_OSES/ARCHES.)
216#
[1517]217if test -z "$KBUILD_HOST"; then
218 KBUILD_HOST=`uname`
219 case "$KBUILD_HOST" in
[994]220 linux|Linux|GNU/Linux|LINUX)
[1517]221 KBUILD_HOST=linux
[994]222 ;;
223
224 os2|OS/2|OS2)
[1517]225 KBUILD_HOST=os2
[994]226 ;;
227
228 freebsd|FreeBSD|FREEBSD)
[1517]229 KBUILD_HOST=freebsd
[994]230 ;;
231
232 openbsd|OpenBSD|OPENBSD)
[1517]233 KBUILD_HOST=openbsd
[994]234 ;;
235
236 netbsd|NetBSD|NETBSD)
[1517]237 KBUILD_HOST=netbsd
[994]238 ;;
239
240 Darwin|darwin)
[1517]241 KBUILD_HOST=darwin
[994]242 ;;
243
244 SunOS)
[1517]245 KBUILD_HOST=solaris
[994]246 ;;
247
248 WindowsNT|CYGWIN_NT-*)
[1517]249 KBUILD_HOST=win
[994]250 ;;
251
252 *)
[1517]253 echo "$0: unknown os $KBUILD_HOST" 1>&${ERR_REDIR}
[994]254 sleep 1
255 exit 1
256 ;;
257 esac
258fi
[1517]259test -n "$DBG_OPT" && echo "dbg: KBUILD_HOST=$KBUILD_HOST" 1>&${DBG_REDIR}
[994]260
[1517]261if test -z "$KBUILD_HOST_ARCH"; then
[992]262 # Try deduce it from the cpu if given.
[1517]263 if test -n "$KBUILD_HOST_CPU"; then
264 case "$KBUILD_HOST_CPU" in
[992]265 i[3456789]86)
[1517]266 KBUILD_HOST_ARCH='x86'
[992]267 ;;
268 k8|k8l|k9|k10)
[1517]269 KBUILD_HOST_ARCH='amd64'
[992]270 ;;
271 esac
272 fi
[224]273fi
[1517]274if test -z "$KBUILD_HOST_ARCH"; then
[1156]275 # Use uname -m or isainfo (lots of guesses here, please help clean this up...)
[1517]276 if test "$KBUILD_HOST" = "solaris"; then
277 KBUILD_HOST_ARCH=`isainfo | cut -f 1 -d ' '`
[1244]278
[1156]279 else
[1517]280 KBUILD_HOST_ARCH=`uname -m`
[1156]281 fi
[1517]282 case "$KBUILD_HOST_ARCH" in
[992]283 x86_64|AMD64|amd64|k8|k8l|k9|k10)
[1517]284 KBUILD_HOST_ARCH='amd64'
[992]285 ;;
286 x86|i86pc|ia32|i[3456789]86)
[1517]287 KBUILD_HOST_ARCH='x86'
[224]288 ;;
[992]289 sparc32|sparc)
[1517]290 KBUILD_HOST_ARCH='sparc32'
[299]291 ;;
[992]292 sparc64)
[1517]293 KBUILD_HOST_ARCH='sparc64'
[992]294 ;;
295 s390)
[1517]296 KBUILD_HOST_ARCH='s390'
[992]297 ;;
[1380]298 s390x)
[1517]299 KBUILD_HOST_ARCH='s390x'
[1380]300 ;;
[992]301 ppc32|ppc|powerpc)
[1517]302 KBUILD_HOST_ARCH='ppc32'
[992]303 ;;
304 ppc64|powerpc64)
[1517]305 KBUILD_HOST_ARCH='ppc64'
[992]306 ;;
307 mips32|mips)
[1517]308 KBUILD_HOST_ARCH='mips32'
[992]309 ;;
310 mips64)
[1517]311 KBUILD_HOST_ARCH='mips64'
[992]312 ;;
313 ia64)
[1517]314 KBUILD_HOST_ARCH='ia64'
[992]315 ;;
316 #hppa32|hppa|parisc32|parisc)?
317 hppa32|parisc32)
[1517]318 KBUILD_HOST_ARCH='hppa32'
[992]319 ;;
320 hppa64|parisc64)
[1517]321 KBUILD_HOST_ARCH='hppa64'
[992]322 ;;
[1380]323 arm|armv4l|armv5tel)
[1517]324 KBUILD_HOST_ARCH='arm'
[992]325 ;;
326 alpha)
[1517]327 KBUILD_HOST_ARCH='alpha'
[992]328 ;;
329
[1517]330 *) echo "$0: unknown cpu/arch - $KBUILD_HOST_ARCH" 1>&${ERR_REDIR}
[224]331 sleep 1
332 exit 1
333 ;;
334 esac
335
336fi
[1517]337test -n "$DBG_OPT" && echo "dbg: KBUILD_HOST_ARCH=$KBUILD_HOST_ARCH" 1>&${DBG_REDIR}
[224]338
[1517]339if test -z "$KBUILD_HOST_CPU"; then
340 KBUILD_HOST_CPU="blend"
[992]341fi
[1517]342test -n "$DBG_OPT" && echo "dbg: KBUILD_HOST_CPU=$KBUILD_HOST_CPU" 1>&${DBG_REDIR}
[224]343
[992]344#
345# The target platform.
346# Defaults to the host when not specified.
347#
[1517]348if test -z "$KBUILD_TARGET"; then
349 KBUILD_TARGET="$KBUILD_HOST"
[224]350fi
[1517]351test -n "$DBG_OPT" && echo "dbg: KBUILD_TARGET=$KBUILD_TARGET" 1>&${DBG_REDIR}
[224]352
[1517]353if test -z "$KBUILD_TARGET_ARCH"; then
354 KBUILD_TARGET_ARCH="$KBUILD_HOST_ARCH"
[224]355fi
[1517]356test -n "$DBG_OPT" && echo "dbg: KBUILD_TARGET_ARCH=$KBUILD_TARGET_ARCH" 1>&${DBG_REDIR}
[224]357
[1517]358if test -z "$KBUILD_TARGET_CPU"; then
359 if test "$KBUILD_TARGET_ARCH" = "$KBUILD_HOST_ARCH"; then
360 KBUILD_TARGET_CPU="$KBUILD_HOST_CPU"
[994]361 else
[1517]362 KBUILD_TARGET_CPU="blend"
[994]363 fi
[224]364fi
[1517]365test -n "$DBG_OPT" && echo "dbg: KBUILD_TARGET_CPU=$KBUILD_TARGET_CPU" 1>&${DBG_REDIR}
[224]366
[1517]367#
[224]368# Determin executable extension and path separator.
[1517]369#
[224]370_SUFF_EXE=
371_PATH_SEP=":"
[1517]372case "$KBUILD_HOST" in
[992]373 os2|win|nt)
[224]374 _SUFF_EXE=".exe"
375 _PATH_SEP=";"
376 ;;
377esac
378
[1076]379#
[1517]380# Determin KBUILD_PATH from the script location and calc KBUILD_BIN_PATH from there.
[1076]381#
[1517]382if test -z "$KBUILD_PATH"; then
383 KBUILD_PATH=`dirname "$0"`
384 KBUILD_PATH=`cd "$KBUILD_PATH" ; /bin/pwd`
[1076]385fi
[1517]386if test ! -f "$KBUILD_PATH/footer.kmk" -o ! -f "$KBUILD_PATH/header.kmk" -o ! -f "$KBUILD_PATH/rules.kmk"; then
387 echo "$0: error: KBUILD_PATH ($KBUILD_PATH) is not pointing to a popluated kBuild directory." 1>&${ERR_REDIR}
388 sleep 1
389 exit 1
390fi
391test -n "$DBG_OPT" && echo "dbg: KBUILD_PATH=$KBUILD_PATH" 1>&${DBG_REDIR}
[1076]392
[1517]393if test -z "$KBUILD_BIN_PATH"; then
394 KBUILD_BIN_PATH="${KBUILD_PATH}/bin/${KBUILD_HOST}.${KBUILD_HOST_ARCH}"
[1510]395fi
[1517]396test -n "$DBG_OPT" && echo "dbg: KBUILD_BIN_PATH=${KBUILD_BIN_PATH}" 1>&${DBG_REDIR}
[224]397
[992]398#
[1517]399# Make shell - OS/2 only.
400# Remove this!
401#
402if test "$KBUILD_HOST" = "os2"; then
403 export MAKESHELL="${KBUILD_BIN_PATH}/kmk_ash${_SUFF_EXE}";
404fi
405
406#
[992]407# Add the bin/x.y/ directory to the PATH.
408# NOTE! Once bootstrapped this is the only thing that is actually necessary.
409#
[1517]410PATH="${KBUILD_BIN_PATH}${_PATH_SEP}$PATH"
[1510]411test -n "$DBG_OPT" && echo "dbg: PATH=$PATH" 1>&${DBG_REDIR}
[224]412
[1517]413#
[224]414# Sanity and x bits.
[1517]415#
416if test ! -d "${KBUILD_BIN_PATH}/"; then
417 echo "$0: warning: The bin directory for this platform doesn't exists. (${KBUILD_BIN_PATH}/)" 1>&${ERR_REDIR}
[224]418else
[731]419 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;
[224]420 do
[1517]421 chmod a+x ${KBUILD_BIN_PATH}/${prog} > /dev/null 2>&1
422 if test ! -f "${KBUILD_BIN_PATH}/${prog}${_SUFF_EXE}"; then
423 echo "$0: warning: The ${prog} program doesn't exist for this platform. (${KBUILD_BIN_PATH}/${prog}${_SUFF_EXE})" 1>&${ERR_REDIR}
[224]424 fi
425 done
426fi
427
[1517]428#
429# The environment is in place, now take the requested action.
430#
[1513]431if test -n "${VAR_OPT}"; then
432 # Echo variable values or variable export statements.
433 for var in ${VAR_OPT};
434 do
435 val=
436 case "$var" in
437 PATH)
438 val=$PATH
439 ;;
[1517]440 KBUILD_PATH)
441 val=$KBUILD_PATH
[1513]442 ;;
[1517]443 KBUILD_BIN_PATH)
444 val=$KBUILD_BIN_PATH
[1513]445 ;;
[1517]446 KBUILD_HOST)
447 val=$KBUILD_HOST
[1513]448 ;;
[1517]449 KBUILD_HOST_ARCH)
450 val=$KBUILD_HOST_ARCH
[1513]451 ;;
[1517]452 KBUILD_HOST_CPU)
453 val=$KBUILD_HOST_CPU
[1513]454 ;;
[1517]455 KBUILD_TARGET)
456 val=$KBUILD_TARGET
[1513]457 ;;
[1517]458 KBUILD_TARGET_ARCH)
459 val=$KBUILD_TARGET_ARCH
[1513]460 ;;
[1517]461 KBUILD_TARGET_CPU)
462 val=$KBUILD_TARGET_CPU
[1513]463 ;;
[1517]464 KBUILD_TYPE)
465 val=$KBUILD_TYPE
[1513]466 ;;
467 *)
468 echo "$0: error: Unknown variable $var specified in --var request." 1>&${ERR_REDIR}
469 sleep 1
470 exit 1
471 ;;
472 esac
[1512]473
[1513]474 if test -n "$EVAL_OPT"; then
475 echo "export $var=$val"
476 else
[1517]477 if test -n "$VALUE_ONLY_OPT"; then
478 echo "$val"
479 else
480 echo "$var=$val"
481 fi
[1510]482 fi
[1513]483 done
484else
485 if test -n "$EVAL_OPT"; then
486 # Echo statements for the shell to evaluate.
487 test -n "$DBG_OPT" && echo "dbg: echoing exported variables" 1>&${DBG_REDIR}
488 echo "export PATH=${PATH}"
489 if test -n "${FULL_OPT}"; then
[1517]490 echo "export KBUILD_PATH=${KBUILD_PATH}"
491 echo "export KBUILD_TYPE=${KBUILD_TYPE}"
492 echo "export KBUILD_HOST=${KBUILD_HOST}"
493 echo "export KBUILD_HOST_ARCH=${KBUILD_HOST_ARCH}"
494 echo "export KBUILD_HOST_CPU=${KBUILD_HOST_CPU}"
495 echo "export KBUILD_TARGET=${KBUILD_TARGET}"
496 echo "export KBUILD_TARGET_ARCH=${KBUILD_TARGET_ARCH}"
497 echo "export KBUILD_TARGET_CPU=${KBUILD_TARGET_CPU}"
498
499 if test -n "${LEGACY_OPT}"; then
500 echo "export PATH_KBUILD=${KBUILD_PATH}"
501 echo "export BUILD_TYPE=${KBUILD_TYPE}"
502 echo "export BUILD_PLATFORM=${KBUILD_HOST}"
503 echo "export BUILD_PLATFORM_ARCH=${KBUILD_HOST_ARCH}"
504 echo "export BUILD_PLATFORM_CPU=${KBUILD_HOST_CPU}"
505 echo "export BUILD_TARGET=${KBUILD_TARGET}"
506 echo "export BUILD_TARGET_ARCH=${KBUILD_TARGET_ARCH}"
507 echo "export BUILD_TARGET_CPU=${KBUILD_TARGET_CPU}"
508 fi
[1513]509 fi
[1510]510 else
[1513]511 # Export variables.
512 export PATH
513 if test -n "${FULL_OPT}"; then
[1517]514 export KBUILD_PATH
515 export KBUILD_TYPE
516 export KBUILD_HOST
517 export KBUILD_HOST_ARCH
518 export KBUILD_HOST_CPU
519 export KBUILD_TARGET
520 export KBUILD_TARGET_ARCH
521 export KBUILD_TARGET_CPU
522
523 if test -n "${LEGACY_OPT}"; then
524 export PATH_KBUILD=$KBUILD_PATH
525 export BUILD_TYPE=$KBUILD_TYPE
526 export BUILD_PLATFORM=$KBUILD_HOST
527 export BUILD_PLATFORM_ARCH=$KBUILD_HOST_ARCH
528 export BUILD_PLATFORM_CPU=$KBUILD_HOST_CPU
529 export BUILD_TARGET=$KBUILD_TARGET
530 export BUILD_TARGET_ARCH=$KBUILD_TARGET_ARCH
531 export BUILD_TARGET_CPU=$KBUILD_TARGET_CPU
532 fi
[1513]533 fi
534
535 # Execute command or spawn shell.
536 if test $# -eq 0; then
537 test -z "${QUIET_OPT}" && echo "$0: info: Spawning work shell..." 1>&${ERR_REDIR}
538 if test "$TERM" != 'dumb' -a -n "$BASH"; then
539 export PS1='\[\033[01;32m\]\u@\h \[\033[01;34m\]\W \$ \[\033[00m\]'
540 fi
541 $SHELL -i
542 else
543 test -z "${QUIET_OPT}" && echo "$0: info: Executing command: $*" 1>&${ERR_REDIR}
544 $*
545 fi
[224]546 fi
547fi
[1513]548test -n "$DBG_OPT" && echo "dbg: finished" 1>&${DBG_REDIR}
[731]549
Note: See TracBrowser for help on using the repository browser.