source: trunk/kBuild/env.sh@ 1513

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

Added a --var mode for quering variables.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1#!/bin/sh
2# $Id: env.sh 1513 2008-04-09 01:52:03Z bird $
3## @file
4# Environment setup script.
5#
6
7#
8# Copyright (c) 2005-2008 knut st. osmundsen <bird-kBuild-spam@anduin.net>
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#
27#set -x
28
29#
30# Check if we're in eval mode or not.
31#
32ERR_REDIR=1
33DBG_REDIR=1
34EVAL_OPT=
35DBG_OPT=
36QUIET_OPT=
37FULL_OPT=
38VAR_OPT=
39while test $# -gt 0;
40do
41 case "$1" in
42 "--debug")
43 DBG_OPT="true"
44 ;;
45 "--quiet")
46 QUIET_OPT="true"
47 ;;
48 "--full")
49 FULL_OPT="true"
50 ;;
51 "--eval")
52 EVAL_OPT="true"
53 ERR_REDIR=2
54 DBG_REDIR=2
55 ;;
56 "--var")
57 shift
58 VAR_OPT="${VAR_OPT} $1"
59 ;;
60 "--help")
61 echo "syntax: $0 [--debug] [--quiet] [--full] [--eval] [--var name] [command [args]]"
62 ## FIXME: long --help screen.
63 exit 1
64 ;;
65 *)
66 break
67 ;;
68 esac
69 shift
70done
71
72
73#
74# Determin the kBuild path from the script location.
75#
76if test -z "$PATH_KBUILD"; then
77 PATH_KBUILD=`dirname "$0"`
78 PATH_KBUILD=`cd "$PATH_KBUILD" ; /bin/pwd`
79fi
80if test ! -f "$PATH_KBUILD/footer.kmk" -o ! -f "$PATH_KBUILD/header.kmk" -o ! -f "$PATH_KBUILD/rules.kmk"; then
81 echo "$0: error: PATH_KBUILD ($PATH_KBUILD) is not pointing to a popluated kBuild directory." 1>&${ERR_REDIR}
82 sleep 1;
83 exit 1;
84fi
85test -n "$DBG_OPT" && echo "dbg: PATH_KBUILD=$PATH_KBUILD" 1>&${DBG_REDIR}
86
87
88#
89# Set default build type.
90#
91if test -z "$BUILD_TYPE"; then
92 BUILD_TYPE=release
93fi
94test -n "$DBG_OPT" && echo "dbg: BUILD_TYPE=$BUILD_TYPE" 1>&${DBG_REDIR}
95
96#
97# Determin the host platform.
98#
99# The CPU isn't important, only the other two are. But, since the cpu,
100# arch and platform (and build type) share a common key space, try make
101# sure any new additions are unique. (See header.kmk, KBUILD_OSES/ARCHES.)
102#
103if test -z "$BUILD_PLATFORM"; then
104 BUILD_PLATFORM=`uname`
105 case "$BUILD_PLATFORM" in
106 linux|Linux|GNU/Linux|LINUX)
107 BUILD_PLATFORM=linux
108 ;;
109
110 os2|OS/2|OS2)
111 BUILD_PLATFORM=os2
112 ;;
113
114 freebsd|FreeBSD|FREEBSD)
115 BUILD_PLATFORM=freebsd
116 ;;
117
118 openbsd|OpenBSD|OPENBSD)
119 BUILD_PLATFORM=openbsd
120 ;;
121
122 netbsd|NetBSD|NETBSD)
123 BUILD_PLATFORM=netbsd
124 ;;
125
126 Darwin|darwin)
127 BUILD_PLATFORM=darwin
128 ;;
129
130 SunOS)
131 BUILD_PLATFORM=solaris
132 ;;
133
134 WindowsNT|CYGWIN_NT-*)
135 BUILD_PLATFORM=win
136 ;;
137
138 *)
139 echo "$0: unknown os $BUILD_PLATFORM" 1>&${ERR_REDIR}
140 sleep 1
141 exit 1
142 ;;
143 esac
144fi
145test -n "$DBG_OPT" && echo "dbg: BUILD_PLATFORM=$BUILD_PLATFORM" 1>&${DBG_REDIR}
146
147if test -z "$BUILD_PLATFORM_ARCH"; then
148 # Try deduce it from the cpu if given.
149 if test -s "$BUILD_PLATFORM_CPU"; then
150 case "$BUILD_PLATFORM_CPU" in
151 i[3456789]86)
152 BUILD_PLATFORM_ARCH='x86'
153 ;;
154 k8|k8l|k9|k10)
155 BUILD_PLATFORM_ARCH='amd64'
156 ;;
157 esac
158 fi
159fi
160if test -z "$BUILD_PLATFORM_ARCH"; then
161 # Use uname -m or isainfo (lots of guesses here, please help clean this up...)
162 if test "$BUILD_PLATFORM" = "solaris"; then
163 BUILD_PLATFORM_ARCH=`isainfo | cut -f 1 -d ' '`
164
165 else
166 BUILD_PLATFORM_ARCH=`uname -m`
167 fi
168 case "$BUILD_PLATFORM_ARCH" in
169 x86_64|AMD64|amd64|k8|k8l|k9|k10)
170 BUILD_PLATFORM_ARCH='amd64'
171 ;;
172 x86|i86pc|ia32|i[3456789]86)
173 BUILD_PLATFORM_ARCH='x86'
174 ;;
175 sparc32|sparc)
176 BUILD_PLATFORM_ARCH='sparc32'
177 ;;
178 sparc64)
179 BUILD_PLATFORM_ARCH='sparc64'
180 ;;
181 s390)
182 BUILD_PLATFORM_ARCH='s390'
183 ;;
184 s390x)
185 BUILD_PLATFORM_ARCH='s390x'
186 ;;
187 ppc32|ppc|powerpc)
188 BUILD_PLATFORM_ARCH='ppc32'
189 ;;
190 ppc64|powerpc64)
191 BUILD_PLATFORM_ARCH='ppc64'
192 ;;
193 mips32|mips)
194 BUILD_PLATFORM_ARCH='mips32'
195 ;;
196 mips64)
197 BUILD_PLATFORM_ARCH='mips64'
198 ;;
199 ia64)
200 BUILD_PLATFORM_ARCH='ia64'
201 ;;
202 #hppa32|hppa|parisc32|parisc)?
203 hppa32|parisc32)
204 BUILD_PLATFORM_ARCH='hppa32'
205 ;;
206 hppa64|parisc64)
207 BUILD_PLATFORM_ARCH='hppa64'
208 ;;
209 arm|armv4l|armv5tel)
210 BUILD_PLATFORM_ARCH='arm'
211 ;;
212 alpha)
213 BUILD_PLATFORM_ARCH='alpha'
214 ;;
215
216 *) echo "$0: unknown cpu/arch - $BUILD_PLATFORM_ARCH" 1>&${ERR_REDIR}
217 sleep 1
218 exit 1
219 ;;
220 esac
221
222fi
223test -n "$DBG_OPT" && echo "dbg: BUILD_PLATFORM_ARCH=$BUILD_PLATFORM_ARCH" 1>&${DBG_REDIR}
224
225if test -z "$BUILD_PLATFORM_CPU"; then
226 BUILD_PLATFORM_CPU="blend"
227fi
228test -n "$DBG_OPT" && echo "dbg: BUILD_PLATFORM_CPU=$BUILD_PLATFORM_CPU" 1>&${DBG_REDIR}
229
230#
231# The target platform.
232# Defaults to the host when not specified.
233#
234if test -z "$BUILD_TARGET"; then
235 BUILD_TARGET="$BUILD_PLATFORM"
236fi
237test -n "$DBG_OPT" && echo "dbg: BUILD_TARGET=$BUILD_TARGET" 1>&${DBG_REDIR}
238
239if test -z "$BUILD_TARGET_ARCH"; then
240 BUILD_TARGET_ARCH="$BUILD_PLATFORM_ARCH"
241fi
242test -n "$DBG_OPT" && echo "dbg: BUILD_TARGET_ARCH=$BUILD_TARGET_ARCH" 1>&${DBG_REDIR}
243
244if test -z "$BUILD_TARGET_CPU"; then
245 if test "$BUILD_TARGET_ARCH" = "$BUILD_PLATFORM_ARCH"; then
246 BUILD_TARGET_CPU="$BUILD_PLATFORM_CPU"
247 else
248 BUILD_TARGET_CPU="blend"
249 fi
250fi
251test -n "$DBG_OPT" && echo "dbg: BUILD_TARGET_CPU=$BUILD_TARGET_CPU" 1>&${DBG_REDIR}
252
253
254# Determin executable extension and path separator.
255_SUFF_EXE=
256_PATH_SEP=":"
257case "$BUILD_PLATFORM" in
258 os2|win|nt)
259 _SUFF_EXE=".exe"
260 _PATH_SEP=";"
261 ;;
262esac
263
264
265#
266# Calc PATH_KBUILD_BIN (but don't export it).
267#
268if test -z "$PATH_KBUILD_BIN"; then
269 PATH_KBUILD_BIN="${PATH_KBUILD}/bin/${BUILD_PLATFORM}.${BUILD_PLATFORM_ARCH}"
270fi
271test -n "$DBG_OPT" && echo "dbg: PATH_KBUILD_BIN=${PATH_KBUILD_BIN} (not exported)" 1>&${DBG_REDIR}
272
273# Make shell. OS/2 and DOS only?
274if test "$BUILD_PLATFORM" = "os2"; then
275 export MAKESHELL="${PATH_KBUILD_BIN}/kmk_ash${_SUFF_EXE}";
276fi
277
278#
279# Add the bin/x.y/ directory to the PATH.
280# NOTE! Once bootstrapped this is the only thing that is actually necessary.
281#
282PATH="${PATH_KBUILD_BIN}${_PATH_SEP}$PATH"
283test -n "$DBG_OPT" && echo "dbg: PATH=$PATH" 1>&${DBG_REDIR}
284
285# Sanity and x bits.
286if test ! -d "${PATH_KBUILD_BIN}/"; then
287 echo "$0: warning: The bin directory for this platform doesn't exists. (${PATH_KBUILD_BIN}/)" 1>&${ERR_REDIR}
288else
289 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;
290 do
291 chmod a+x ${PATH_KBUILD_BIN}/${prog} > /dev/null 2>&1
292 if test ! -f "${PATH_KBUILD_BIN}/${prog}${_SUFF_EXE}"; then
293 echo "$0: warning: The ${prog} program doesn't exist for this platform. (${PATH_KBUILD_BIN}/${prog}${_SUFF_EXE})" 1>&${ERR_REDIR}
294 fi
295 done
296fi
297
298unset _SUFF_EXE
299unset _PATH_SEP
300
301if test -n "${VAR_OPT}"; then
302 # Echo variable values or variable export statements.
303 for var in ${VAR_OPT};
304 do
305 val=
306 case "$var" in
307 PATH)
308 val=$PATH
309 ;;
310 KBUILD_PATH|PATH_KBUILD)
311 val=$PATH_KBUILD
312 ;;
313 KBUILD_BIN_PATH|PATH_KBUILD_BIN)
314 val=$PATH_KBUILD_BIN
315 ;;
316 KBUILD_HOST|BUILD_PLATFORM)
317 val=$BUILD_PLATFORM
318 ;;
319 KBUILD_HOST_ARCH|BUILD_PLATFORM_ARCH)
320 val=$BUILD_PLATFORM_ARCH
321 ;;
322 KBUILD_HOST_CPU|BUILD_PLATFORM_CPU)
323 val=$BUILD_PLATFORM_CPU
324 ;;
325 KBUILD_TARGET|BUILD_TARGET)
326 val=$BUILD_TARGET
327 ;;
328 KBUILD_TARGET_ARCH|BUILD_TARGET_ARCH)
329 val=$BUILD_TARGET_ARCH
330 ;;
331 KBUILD_TARGET_CPU|BUILD_TARGET_CPU)
332 val=$BUILD_TARGET_CPU
333 ;;
334 KBUILD_TYPE|BUILD_TYPE)
335 val=$BUILD_TARGET_CPU
336 ;;
337 *)
338 echo "$0: error: Unknown variable $var specified in --var request." 1>&${ERR_REDIR}
339 sleep 1
340 exit 1
341 ;;
342 esac
343
344 if test -n "$EVAL_OPT"; then
345 echo "export $var=$val"
346 else
347 echo "$var=$val"
348 fi
349 done
350else
351 if test -n "$EVAL_OPT"; then
352 # Echo statements for the shell to evaluate.
353 test -n "$DBG_OPT" && echo "dbg: echoing exported variables" 1>&${DBG_REDIR}
354 echo "export PATH=${PATH}"
355 if test -n "${FULL_OPT}"; then
356 echo "export BUILD_PLATFORM=${BUILD_PLATFORM}"
357 echo "export BUILD_PLATFORM_ARCH=${BUILD_PLATFORM_ARCH}"
358 echo "export BUILD_PLATFORM_CPU=${BUILD_PLATFORM_CPU}"
359 echo "export BUILD_TARGET=${BUILD_TARGET}"
360 echo "export BUILD_TARGET_ARCH=${BUILD_TARGET_ARCH}"
361 echo "export BUILD_TARGET_CPU=${BUILD_TARGET_CPU}"
362 echo "export BUILD_TYPE=${BUILD_TYPE}"
363 echo "export PATH_KBUILD=${PATH_KBUILD}"
364 fi
365 else
366 # Export variables.
367 export PATH
368 if test -n "${FULL_OPT}"; then
369 export PATH_KBUILD
370 export BUILD_TYPE
371 export BUILD_PLATFORM
372 export BUILD_PLATFORM_ARCH
373 export BUILD_PLATFORM_CPU
374 export BUILD_TARGET
375 export BUILD_TARGET_ARCH
376 export BUILD_TARGET_CPU
377 fi
378
379 # Execute command or spawn shell.
380 if test $# -eq 0; then
381 test -z "${QUIET_OPT}" && echo "$0: info: Spawning work shell..." 1>&${ERR_REDIR}
382 if test "$TERM" != 'dumb' -a -n "$BASH"; then
383 export PS1='\[\033[01;32m\]\u@\h \[\033[01;34m\]\W \$ \[\033[00m\]'
384 fi
385 $SHELL -i
386 else
387 test -z "${QUIET_OPT}" && echo "$0: info: Executing command: $*" 1>&${ERR_REDIR}
388 $*
389 fi
390 fi
391fi
392test -n "$DBG_OPT" && echo "dbg: finished" 1>&${DBG_REDIR}
393
Note: See TracBrowser for help on using the repository browser.