Merge libpng branch (thx Esteban Campostrini)
[harbours.git] / hsct.sh
blob252226482c94000d27319c7ba8bdf8a14a98fcbb
1 #!/bin/sh
4 # Copyright (c) 2013 Vojtech Horky
5 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
11 # - Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # - Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # - The name of the author may not be used to endorse or promote products
17 # derived from this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 # Calling harbour functions:
32 # These functions are always called in a subshell to "guard" them a little
33 # bit (variables set by the harbour, cd into package directory).
35 # Notice on usage of set -o errexit (set -e)
36 # We want to use that option for the harbour scripts to get rid of the
37 # "|| return 1" at the end of each line.
38 # Obvious solution is to wrap the call like this:
39 # ( set -o errexit; build ) || { hsct_error "..."; return 1; }
40 # This doesn't work because the whole subshell is then part of a ||
41 # operand and thus the set -e is ignored (even if it is a subshell).
42 # See https://groups.google.com/d/msg/gnu.bash.bug/NCK_0GmIv2M/y6RQF1AWUQkJ
43 # Thus, we need to use the following template to get past this:
44 # ( set -o errexit; build; exit $? );
45 # [ $? -eq 0 ] || { hsct_error "..."; return 1; }
47 # Also notice that we never ever call exit from the top-most shell when
48 # leaving after an error. That is to prevent terminating user shell when
49 # this script is sourced ("env" command). It complicates the error handling
50 # a bit but it is more reliable than trying to guess whether we are running
51 # in a subshell or not.
54 HSCT_HOME=`which -- "$0" 2>/dev/null`
55 # Maybe, we are running Bash
56 [ -z "$HSCT_HOME" ] && HSCT_HOME=`which -- "$BASH_SOURCE" 2>/dev/null`
57 HSCT_HOME=`dirname -- "$HSCT_HOME"`
58 HSCT_HSCT="$HSCT_HOME/hsct.sh"
60 HSCT_CONFIG=hsct.conf
62 HSCT_SOURCES_DIR=`pwd`/sources
63 HSCT_BUILD_DIR=`pwd`/build
64 HSCT_INCLUDE_DIR=`pwd`/include
65 HSCT_LIB_DIR=`pwd`/libs
66 HSCT_DIST_DIR="`pwd`/dist/"
67 HSCT_ARCHIVE_DIR="`pwd`/archives/"
68 HSCT_DISABLED_CFLAGS="-Werror -Werror-implicit-function-declaration"
69 HSCT_CACHE_DIR=`pwd`/helenos
71 # Print short help.
72 # Does not exit the whole script.
73 hsct_usage() {
74 echo "Usage:"
75 echo " $1 action [package]"
76 echo " Action can be one of following:"
77 echo " clean Clean built directory."
78 echo " build Build given package."
79 echo " package Save installable files to allow cleaning."
80 echo " install Install to uspace/dist of HelenOS."
81 echo " archive Create tarball instead of installing."
82 echo " $1 update [rebuild]"
83 echo " Update the cached headers and libraries."
84 echo " If 'rebuild' is specified, HelenOS is forcefully rebuild and"
85 echo " cache is updated afterwards."
86 echo " $1 init /path/to/HelenOS [profile] [build]"
87 echo " Initialize current directory as coastline build directory".
88 echo " Full path has to be provided to the HelenOS source tree."
89 echo " If profile is specified, prepare for that configuration."
90 echo " If 'build' is given, forcefully rebuild to specified profile."
91 echo " $1 help"
92 echo " Display this help and exit."
95 # Print high-level information message.
96 hsct_info() {
97 echo ">>>" "$@" >&2
100 # Print lower-level information message (additional info after hsct_info).
101 hsct_info2() {
102 echo " ->" "$@" >&2
105 # Print information message from HARBOUR script.
106 msg() {
107 hsct_info "$@"
110 # Print high-level error message.
111 hsct_error() {
112 echo "[hsct]:" "Error:" "$@" >&2
115 # Print additional details to the error message.
116 hsct_error2() {
117 echo "[hsct]:" " -> " "$@" >&2
120 # Run a command but print it first.
121 hsct_run_echo() {
122 echo -n "[hsct]: "
123 for ___i in "$@"; do
124 echo -n "$___i" | sed -e 's#"#\\"#g' -e 's#.*#"&" #'
125 done
126 echo
127 "$@"
130 # Run comman from HARBOUR script and print it as well.
131 run() {
132 hsct_run_echo "$@"
135 # Tells whether HelenOS in $HSCT_HELENOS_ROOT is configured.
136 hsct_is_helenos_configured() {
137 [ -e "$HSCT_HELENOS_ROOT/Makefile.config" ]
138 return $?
141 # hsct_get_config CONFIG_FILE variable
142 hsct_get_config() {
143 grep '^[ \t]*'"$2" "$1" \
144 | tail -n 1 \
145 | cut '-d=' -f 2 \
146 | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//'
149 # Fetch all the specified files in the HARBOUR
150 hsct_fetch() {
151 mkdir -p "$HSCT_SOURCES_DIR"
152 hsct_info "Fetching sources..."
153 for _url in $shipsources; do
154 _filename=`basename "$_url"`
155 if [ "$_filename" = "$_url" ]; then
156 continue
158 if ! [ -r "$HSCT_SOURCES_DIR/$_filename" ]; then
159 hsct_info2 "Fetching $_filename..."
160 if ! wget "$_url" -O "$HSCT_SOURCES_DIR/$_filename"; then
161 rm -f "$HSCT_SOURCES_DIR/$_filename"
162 hsct_error "Failed to fetch $_url."
163 return 1
166 # TODO - check MD5
167 done
168 return 0
172 # get_var_from_makefile CC Makefile.common
173 hsct_get_var_from_makefile() {
174 # echo "make -C `dirname "$2"` -f - -s __armagedon_" >&2
176 echo "$3"
177 echo "$4"
178 echo "$5"
179 echo "__genesis__:"
180 echo
181 echo "CONFIG_DEBUG=n"
182 echo include `basename "$2"`
183 echo "CONFIG_DEBUG=n"
184 echo
185 echo "__armagedon__:"
186 echo " echo \$($1)"
187 echo
188 ) | make -C `dirname "$2"` -f - -s __armagedon__
191 # Retrieve variable from uspace/ Makefile.
192 hsct_get_var_from_uspace() {
193 if [ -z "$2" ]; then
194 hsct_get_var_from_makefile "$1" "$HSCT_HELENOS_ROOT/uspace/Makefile.common" "USPACE_PREFIX=$HSCT_HELENOS_ROOT/uspace"
195 else
196 hsct_get_var_from_makefile "$1" "$HSCT_HELENOS_ROOT/uspace/$2" "USPACE_PREFIX=$HSCT_HELENOS_ROOT/uspace" "$3" "$4"
200 # Cache and export a variable.
201 # $1 variable name
202 # $2 value to cache
203 hsct_cache_variable() {
204 export "$1=$2"
206 if [ -n "$3" ]; then
207 echo "#" "$3"
209 echo -n "export $1=\"";
210 echo -n "$2" | sed -e 's#"#\\"#g' -e 's#\\#\\\\#g'
211 echo "\""
212 ) >>"$HSCT_CACHE_DIR/env.sh"
215 # Update the cache - copy headers, libraries etc to this directory.
216 # Does not check that update is possible!
217 hsct_cache_update() {
218 hsct_info "Caching headers, libraries and compile flags"
220 mkdir -p "$HSCT_CACHE_DIR"
222 echo "#!/bin/sh"
223 echo
224 echo "# This is automatically generated file."
225 echo "# All changes will be LOST upon next invocation of hsct.sh"
226 echo
227 ) >"$HSCT_CACHE_DIR/env.sh"
228 chmod +x "$HSCT_CACHE_DIR/env.sh"
232 # Start with binaries
235 hsct_cache_variable HSCT_CC `hsct_get_var_from_uspace CC`
236 hsct_cache_variable HSCT_AS `hsct_get_var_from_uspace AS`
237 hsct_cache_variable HSCT_LD `hsct_get_var_from_uspace LD`
238 hsct_cache_variable HSCT_AR `hsct_get_var_from_uspace AR`
239 hsct_cache_variable HSCT_STRIP `hsct_get_var_from_uspace STRIP`
240 hsct_cache_variable HSCT_OBJCOPY `hsct_get_var_from_uspace OBJCOPY`
241 hsct_cache_variable HSCT_OBJDUMP `hsct_get_var_from_uspace OBJDUMP`
242 # HelenOS do not use ranlib or nm but some applications require it
243 hsct_cache_variable HSCT_RANLIB `echo "$HSCT_AR" | sed 's/-ar$/-ranlib/'`
244 hsct_cache_variable HSCT_NM `echo "$HSCT_AR" | sed 's/-ar$/-nm/'`
247 # Various paths
249 hsct_cache_variable HSTC_HELENOS_ROOT "$HSCT_HELENOS_ROOT"
250 hsct_cache_variable HSCT_INCLUDE_DIR "$HSCT_INCLUDE_DIR"
251 hsct_cache_variable HSCT_LIB_DIR "$HSCT_LIB_DIR"
252 hsct_cache_variable HSCT_MISC_DIR "$HSCT_MISC_DIR"
253 hsct_cache_variable HSCT_APPS_DIR "$HSCT_APPS_DIR"
254 hsct_cache_variable HSCT_CACHE_DIR "$HSCT_CACHE_DIR"
255 hsct_cache_variable HSCT_CACHE_INCLUDE "$HSCT_CACHE_DIR/include"
256 hsct_cache_variable HSCT_CACHE_LIB "$HSCT_CACHE_DIR/lib/other"
259 # Get architecture, convert to target
261 hsct_cache_variable HSCT_UARCH `hsct_get_var_from_uspace UARCH`
262 HSCT_TARGET=""
263 case $HSCT_UARCH in
264 "amd64")
265 HSCT_GNU_TARGET="amd64-linux-gnu"
266 HSCT_HELENOS_TARGET="amd64-helenos"
268 "arm32")
269 HSCT_GNU_TARGET="arm-linux-gnueabi"
270 HSCT_HELENOS_TARGET="arm-helenos-gnueabi"
272 "ia32")
273 HSCT_GNU_TARGET="i686-pc-linux-gnu"
274 HSCT_HELENOS_TARGET="i686-pc-helenos"
276 "ia64")
277 HSCT_GNU_TARGET="ia64-pc-linux-gnu"
278 HSCT_HELENOS_TARGET="ia64-pc-helenos"
280 "mips32")
281 HSCT_GNU_TARGET="mipsel-linux-gnu"
282 HSCT_HELENOS_TARGET="mipsel-helenos"
284 "mips32eb")
285 HSCT_GNU_TARGET="mips-linux-gnu"
286 HSCT_HELENOS_TARGET="mips-helenos"
288 "mips64")
289 HSCT_GNU_TARGET="mips64el-linux-gnu"
290 HSCT_HELENOS_TARGET="mips64el-helenos"
292 "ppc32")
293 HSCT_GNU_TARGET="ppc-linux-gnu"
294 HSCT_HELENOS_TARGET="ppc-helenos"
296 "ppc64")
297 HSCT_GNU_TARGET="ppc64-linux-gnu"
298 HSCT_HELENOS_TARGET="ppc64-helenos"
300 "sparc32")
301 HSCT_GNU_TARGET="sparc-leon3-linux-gnu"
302 HSCT_HELENOS_TARGET="sparc-leon3-helenos"
304 "sparc64")
305 HSCT_GNU_TARGET="sparc64-linux-gnu"
306 HSCT_HELENOS_TARGET="sparc64-helenos"
309 hsct_error 'Unsupported architecture: $(UARCH) =' "'$HSCT_UARCH'."
310 return 1
312 esac
313 hsct_cache_variable HSCT_GNU_TARGET "$HSCT_GNU_TARGET"
314 hsct_cache_variable HSCT_HELENOS_TARGET "$HSCT_HELENOS_TARGET"
315 case `hsct_get_var_from_uspace COMPILER` in
316 gcc_helenos)
317 hsct_cache_variable HSCT_TARGET "$HSCT_HELENOS_TARGET"
320 hsct_cache_variable HSCT_TARGET "$HSCT_GNU_TARGET"
322 esac
326 # Copy the files and update the flags accordingly
328 mkdir -p "$HSCT_CACHE_INCLUDE"
329 mkdir -p "$HSCT_CACHE_DIR/lib"
330 mkdir -p "$HSCT_CACHE_LIB"
332 # Linker script
333 hsct_info2 "Copying linker script and startup object file"
334 _LINKER_SCRIPT=`hsct_get_var_from_uspace LINKER_SCRIPT`
336 set -o errexit
337 _STARTUP_OBJECT=`sed -n 's#.*STARTUP(\([^)]*\)).*#\1#p' <"$_LINKER_SCRIPT" 2>/dev/null`
338 cp "$_STARTUP_OBJECT" "$HSCT_CACHE_DIR/lib/entry.o" || return 1
339 sed "s#$_STARTUP_OBJECT#$HSCT_CACHE_DIR/lib/entry.o#" \
340 <"$_LINKER_SCRIPT" >"$HSCT_CACHE_DIR/link.ld"
342 if [ $? -ne 0 ]; then
343 hsct_error "Failed preparing linker script."
344 return 1
347 _LINKER_SCRIPT="$HSCT_CACHE_DIR/link.ld"
349 # Libraries
350 hsct_info2 "Copying libraries"
351 cp \
352 "$HSTC_HELENOS_ROOT/uspace/lib/c/libc.a" \
353 "$HSTC_HELENOS_ROOT/uspace/lib/math/libmath.a" \
354 "$HSTC_HELENOS_ROOT/uspace/lib/softint/libsoftint.a" \
355 "$HSTC_HELENOS_ROOT/uspace/lib/softfloat/libsoftfloat.a" \
356 "$HSTC_HELENOS_ROOT/uspace/lib/posix/libc4posix.a" \
357 "$HSTC_HELENOS_ROOT/uspace/lib/posix/libposixaslibc.a" \
358 "$HSCT_CACHE_DIR/lib/"
359 if [ $? -ne 0 ]; then
360 hsct_error "Failed copying libraries to cache."
361 return 1
364 # Headers
365 hsct_info2 "Copying headers"
367 set -o errexit
368 cp -L -R "$HSTC_HELENOS_ROOT/uspace/lib/posix/include/posix/" "$HSCT_CACHE_DIR/include/"
369 mkdir -p "$HSCT_CACHE_DIR/include/libc"
370 cp -L -R "$HSTC_HELENOS_ROOT/uspace/lib/c/include/"* "$HSCT_CACHE_DIR/include/libc"
371 cp -L -R "$HSTC_HELENOS_ROOT/abi/include/abi/" "$HSCT_CACHE_DIR/include/"
372 cp -L -R "$HSTC_HELENOS_ROOT/uspace/lib/c/arch/$HSCT_UARCH/include/libarch/" "$HSCT_CACHE_DIR/include/"
373 # We intentionally merge libc and libmath again (as per C standard)
374 cp -L -R "$HSTC_HELENOS_ROOT/uspace/lib/math/include/"* "$HSCT_CACHE_DIR/include/libc"
375 cp -L -R "$HSTC_HELENOS_ROOT/uspace/lib/math/arch/$HSCT_UARCH/include/libarch/" "$HSCT_CACHE_DIR/include/"
376 ln -s -f -n "libc" "$HSCT_CACHE_DIR/include/libmath"
378 if [ $? -ne 0 ]; then
379 hsct_error "Failed copying headers to cache."
380 return 1
383 hsct_info2 "Fixing includes in libc headers"
384 find "$HSCT_CACHE_DIR/include/libc" "$HSCT_CACHE_DIR/include/libarch" -name '*.h' -exec sed \
385 -e 's:#include <:#include <libc/:' \
386 -e 's:#include <libc/libarch/:#include <libarch/:' \
387 -e 's:#include <libc/abi/:#include <abi/:' \
388 -e 's:#include <libc/libc/:#include <libc/:' \
389 -i {} \;
391 # Remember the configuration
392 hsct_info2 "Saving config files"
394 set -o errexit
395 cp "$HSTC_HELENOS_ROOT/config.h" "$HSCT_CACHE_INCLUDE/system_config.h"
396 cp "$HSTC_HELENOS_ROOT/Makefile.config" "$HSCT_CACHE_DIR/Makefile.config"
398 if [ $? -ne 0 ]; then
399 hsct_error "Failed saving config files."
400 return 1
403 # Extra libraries and headers
404 hsct_info2 "Copying extra headers and libraries"
406 set -o errexit
407 # libclui
408 cp -L "$HSTC_HELENOS_ROOT/uspace/lib/clui/libclui.a" "$HSCT_CACHE_LIB"
409 mkdir -p "$HSCT_CACHE_INCLUDE/libclui/"
410 cp -L "$HSTC_HELENOS_ROOT/uspace/lib/clui/tinput.h" "$HSCT_CACHE_INCLUDE/libclui/"
412 if [ $? -ne 0 ]; then
413 hsct_error "Failed copying extra headers and libraries to cache."
414 return 1
418 # Fix the flags
420 hsct_info2 "Fixing compiler flags"
422 # Get the flags
423 _CFLAGS=`hsct_get_var_from_uspace CFLAGS`
424 _LDFLAGS=`hsct_get_var_from_uspace LFLAGS`
426 # CC flags clean-up
427 #_CFLAGS=`echo "$_CFLAGS" | sed 's#-imacros[ \t]*\([^ \t]*/config.h\)#-imacros '"$HSCT_CACHE_DIR"'/include/system_config.h#'`
428 _CFLAGS_OLD="$_CFLAGS"
429 _CFLAGS=""
430 _next_kind="none"
431 for _flag in $_CFLAGS_OLD; do
432 _disabled=false
433 case "$_next_kind" in
434 skip)
435 _disable=true
437 imacro)
438 if echo "$_flag" | grep -q '/config.h$'; then
439 _flag="$HSCT_CACHE_DIR/include/system_config.h"
444 esac
445 _next_kind="none"
447 for _disabled_flag in $HSCT_DISABLED_CFLAGS; do
448 if [ "$_disabled_flag" = "$_flag" ]; then
449 _disabled=true
450 break
452 done
454 if [ "$_flag" = "-L" ]; then
455 _next_kind="skip"
456 _disabled=true
458 if [ "$_flag" = "-I" ]; then
459 _next_kind="skip"
460 _disabled=true
462 if [ "$_flag" = "-imacros" ]; then
463 _next_kind="imacro"
465 if echo "$_flag" | grep -q '^-[IL]'; then
466 _disabled=true
469 if ! $_disabled; then
470 _CFLAGS="$_CFLAGS $_flag"
472 done
474 # LD flags clean-up
475 _LDFLAGS_OLD="$_LDFLAGS"
476 _LDFLAGS=""
477 for _flag in $_LDFLAGS_OLD; do
478 _disabled=false
479 # Get rid of library paths, the files are stored locally
480 if echo "$_flag" | grep -q '^-[L]'; then
481 _disabled=true
483 if ! $_disabled; then
484 _LDFLAGS="$_LDFLAGS $_flag"
486 done
488 # Add paths to cached headers and libraries
489 _LDFLAGS="$_LDFLAGS -L$HSCT_CACHE_DIR/lib -n -T $_LINKER_SCRIPT"
490 _CFLAGS="$_CFLAGS -I$HSCT_CACHE_DIR/include/posix -I$HSCT_CACHE_DIR/include/"
492 # Actually used libraries
493 # The --whole-archive is used to allow correct linking of static libraries
494 # (otherwise, the ordering is crucial and we usally cannot change that in the
495 # application Makefiles).
496 _BASE_LIBS=`hsct_get_var_from_uspace BASE_LIBS | sed 's#[ \t]\+#\n#g' | sed 's#.*/lib\(.*\).a$#\1#' | paste '-sd '`
497 _USE_SOFTFLOAT=""
498 case $_BASE_LIBS in
499 *softfloat*)
500 _USE_SOFTFLOAT="-lsoftfloat"
504 esac
505 case $HSCT_UARCH in
506 arm32)
507 _USE_SOFTFLOAT="-lsoftfloat"
509 sparc64)
510 _USE_SOFTFLOAT="-lsoftfloat"
514 esac
515 _POSIX_LINK_LFLAGS="--whole-archive --start-group -lposixaslibc -lsoftint $_USE_SOFTFLOAT --end-group --no-whole-archive -lc4posix"
517 _LDFLAGS="$_LDFLAGS $_POSIX_LINK_LFLAGS"
519 # The LDFLAGS might be used through CC, thus prefixing with -Wl is required
520 _LDFLAGS_FOR_CC=""
521 for _flag in $_LDFLAGS; do
522 _LDFLAGS_FOR_CC="$_LDFLAGS_FOR_CC -Wl,$_flag"
523 done
525 hsct_cache_variable HSCT_LDFLAGS "$_LDFLAGS"
526 hsct_cache_variable HSCT_LDFLAGS_FOR_CC "$_LDFLAGS_FOR_CC"
527 hsct_cache_variable HSCT_CFLAGS "$_CFLAGS"
530 # Source the env.sh if present otherwise exit with failure.
531 hsct_prepare_env() {
532 if ! [ -e "$HSCT_CACHE_DIR/env.sh" ]; then
533 hsct_error "Cache is not initialized. Maybe HelenOS is not configured?"
534 return 1
537 # Source env.sh to get HSCT_* variables
538 . "$HSCT_CACHE_DIR/env.sh"
540 if [ "$shipfunnels" -gt "$HSCT_PARALLELISM" ] 2>/dev/null; then
541 shipfunnels="$HSCT_PARALLELISM"
542 elif [ "$shipfunnels" -le "$HSCT_PARALLELISM" ] 2>/dev/null; then
543 if [ "$shipfunnels" -le "0" ]; then
544 shipfunnels="$HSCT_PARALLELISM"
546 else
547 shipfunnels="1"
551 # Remove the build directory of given package.
552 hsct_clean() {
553 hsct_info "Cleaning build directory..."
554 rm -rf "$HSCT_BUILD_DIR/$shipname/"*
557 # Decide whether it is possible to update the cache.
558 hsct_can_update_cache() {
559 # If HelenOS is configured, we want to update the cache.
560 # However, if architecture is specified only if the current
561 # configuration is the same.
562 _arch=`hsct_get_config "$HSCT_CONFIG" arch`
563 if [ -z "$_arch" ]; then
564 # Building for any architecture. We update the cache if
565 # HelenOS is configured.
566 hsct_is_helenos_configured
567 return $?
568 else
569 # Update the cache only if HelenOS is configured and the
570 # architecture matches
571 if hsct_is_helenos_configured; then
572 _uarch=`hsct_get_var_from_uspace UARCH`
573 [ "$_uarch" = "$_arch" ]
574 return $?
575 else
576 return 1
581 # Build the package.
582 hsct_build() {
583 mkdir -p "$HSCT_BUILD_DIR/$shipname"
584 if [ -e "$HSCT_BUILD_DIR/${shipname}.built" ]; then
585 hsct_info "No need to build $shipname."
586 return 0
589 if hsct_can_update_cache; then
590 if ! hsct_cache_update; then
591 return 1
595 # Check for prerequisities
596 for tug in $shiptugs; do
597 if ! [ -e "$HSCT_BUILD_DIR/${tug}.packaged" ]; then
598 hsct_info "Need to build $tug first."
599 hsct_info2 "Running $HSCT_HSCT package $tug"
601 $HSCT_HSCT package $tug
602 exit $?
604 if [ $? -ne 0 ]; then
605 hsct_error "Failed to package dependency $tug."
606 hsct_error2 "Cannot continue building $shipname."
607 return 1
609 hsct_info2 "Back from building $tug."
611 done
613 hsct_prepare_env || return 1
615 hsct_fetch || return 1
617 for _url in $shipsources; do
618 _filename=`basename "$_url"`
619 if [ "$_filename" = "$_url" ]; then
620 _origin="$HSCT_HOME/$shipname/$_filename"
621 else
622 _origin="$HSCT_SOURCES_DIR/$_filename"
624 ln -sf "$_origin" "$HSCT_BUILD_DIR/$shipname/$_filename"
625 done
628 cd "$HSCT_BUILD_DIR/$shipname/"
629 hsct_info "Building..."
630 set -o errexit
631 build
632 exit $?
634 if [ $? -ne 0 ]; then
635 hsct_error "Build failed!"
636 return 1
638 touch "$HSCT_BUILD_DIR/${shipname}.built"
639 return 0
642 # Pseudo-installation - copy from build directory to "my" directory, copy libraries
643 hsct_package() {
644 mkdir -p "$HSCT_INCLUDE_DIR" || { hsct_error "Failed to create include directory."; return 1; }
645 mkdir -p "$HSCT_LIB_DIR" || { hsct_error "Failed to create library directory."; return 1; }
646 mkdir -p "$HSCT_MY_DIR" || { hsct_error "Failed to create package directory."; return 1; }
648 if [ -e "$HSCT_BUILD_DIR/${shipname}.packaged" ]; then
649 hsct_info "No need to package $shipname."
650 return 0;
653 hsct_build || return 1
655 hsct_prepare_env || return 1
658 cd "$HSCT_BUILD_DIR/$shipname/"
659 hsct_info "Packaging..."
660 set -o errexit
661 package
662 exit $?
664 if [ $? -ne 0 ]; then
665 hsct_error "Packaging failed!"
666 return 1
668 touch "$HSCT_BUILD_DIR/${shipname}.packaged"
669 return 0
672 # Install the package to HelenOS source tree (to uspace/overlay).
673 hsct_install() {
674 hsct_package || return 1
676 if ! hsct_can_update_cache; then
677 hsct_error "Installation cannot be performed."
678 hsct_error2 "HelenOS is not configured for the proper architecture."
679 return 1
682 hsct_info "Installing..."
683 if ls "$HSCT_MY_DIR"/* &>/dev/null; then
684 cp -v -r -L "$HSCT_MY_DIR"/* "$HSCT_OVERLAY" || return 1
685 hsct_info2 "Do not forget to rebuild the image."
686 else
687 hsct_info2 "Note: nothing to install."
689 return 0
692 # Create tarball to allow redistribution of the build packages
693 hsct_archive() {
694 hsct_package || return 1
696 hsct_info "Creating the tarball..."
697 mkdir -p "$HSCT_ARCHIVE_DIR"
699 set -o errexit
700 cd "$HSCT_DIST_DIR/$shipname"
701 tar cJf "$HSCT_ARCHIVE_DIR/$shipname.tar.xz" .
703 if [ $? -ne 0 ]; then
704 hsct_error "Archiving failed!"
705 return 1
708 return 0
712 # Initialize current directory for coastline building.
713 hsct_init() {
714 if [ -e "$HSCT_CONFIG" ]; then
715 hsct_error "Directory is already initialized ($HSCT_CONFIG exists)."
716 return 1
719 hsct_info "Initializing this build directory."
720 _root_dir=`( cd "$1"; pwd ) 2>/dev/null`
721 if ! [ -e "$_root_dir/HelenOS.config" ]; then
722 hsct_error "$1 does not look like a valid HelenOS directory.";
723 return 1
726 # If no architecture is specified, we would read it from
727 # Makefile.config
728 _uarch=`hsct_get_var_from_uspace UARCH`
729 _machine=`hsct_get_var_from_uspace MACHINE`
730 if [ -z "$2" ]; then
731 if [ -z "$_uarch" ]; then
732 hsct_error "HelenOS is not configured and you haven't specified the architecture";
733 return 1
736 if [ "$3" = "build" ]; then
738 set -o errexit
739 cd "$_root_dir"
740 hsct_info2 "Cleaning previous configuration in $PWD."
741 make distclean >/dev/null 2>&1
742 hsct_info2 "Configuring for $2."
743 make Makefile.config "PROFILE=$2" HANDS_OFF=y >/dev/null
744 hsct_info2 "Building (may take a while)."
745 make >/dev/null 2>&1
747 if [ $? -ne 0 ]; then
748 hsct_error "Failed to automatically configure HelenOS for $2."
749 return 1
751 _uarch=`echo "$2" | cut '-d/' -f 1`
752 _machine=`echo "$2" | cut '-d/' -f 2`
753 else
754 if [ "$_uarch" != "$2" ]; then
755 hsct_error "HelenOS is configured for different architecture (maybe add 'build' parameter?)"
756 return 1
760 hsct_info2 "Generating the configuration file."
761 cat >$HSCT_CONFIG <<EOF_CONFIG
762 root = $_root_dir
763 arch = $_uarch
764 machine = $_machine
765 parallel = 1
766 EOF_CONFIG
767 hsct_cache_update
768 return $?
771 # Update the cache manually.
772 hsct_update() {
773 if [ "$1" = "rebuild" ]; then
774 hsct_info "Rebuilding HelenOS to match local configuration"
776 set -o errexit
777 cd "$HSCT_HELENOS_ROOT"
778 hsct_info2 "Cleaning previous configuration in $PWD."
779 make distclean >/dev/null 2>&1
780 hsct_info2 "Configuring for $HSCT_HELENOS_PROFILE."
781 make Makefile.config \
782 "PROFILE=$HSCT_HELENOS_PROFILE" HANDS_OFF=y \
783 2>&1 >/dev/null | sed '/^Fetching current.*ok$/d'
784 hsct_info2 "Overriding configuration with the stored one."
785 cp "$HSCT_CACHE_DIR/Makefile.config" Makefile.config
786 cp "$HSCT_CACHE_DIR/include/system_config.h" config.h
787 hsct_info2 "Building (may take a while)."
788 make >/dev/null 2>&1
790 if [ $? -ne 0 ]; then
791 hsct_error "Failed to automatically rebuild HelenOS."
792 return 1
794 else
795 if ! hsct_can_update_cache; then
796 return 1
800 hsct_cache_update
801 return $?
804 alias leave_script_ok='return 0 2>/dev/null || exit 0'
805 alias leave_script_err='return 1 2>/dev/null || exit 1'
808 case "$1" in
809 help|--help|-h|-?)
810 hsct_usage "$0"
811 leave_script_ok
813 init)
814 HSCT_HELENOS_ROOT="$2"
815 HSCT_LOAD_CONFIG=false
817 update|clean|build|package|install|archive)
818 HSCT_LOAD_CONFIG=true
821 hsct_usage "$0"
822 leave_script_err
824 esac
827 if $HSCT_LOAD_CONFIG; then
828 if ! [ -e "$HSCT_CONFIG" ]; then
829 hsct_error "Configuration file $HSCT_CONFIG missing."
830 leave_script_err
832 HSCT_HELENOS_ROOT=`hsct_get_config "$HSCT_CONFIG" root`
833 HSCT_HELENOS_ARCH=`hsct_get_config "$HSCT_CONFIG" arch`
834 HSCT_HELENOS_MACHINE=`hsct_get_config "$HSCT_CONFIG" machine`
835 HSCT_PARALLELISM=`hsct_get_config "$HSCT_CONFIG" parallel`
837 if [ -z "$HSCT_HELENOS_ARCH" ]; then
838 hsct_error "I don't know for which architecture you want to build."
839 leave_script_err
842 if [ -z "$HSCT_HELENOS_MACHINE" ]; then
843 HSCT_HELENOS_PROFILE="$HSCT_HELENOS_ARCH"
844 else
845 HSCT_HELENOS_PROFILE="$HSCT_HELENOS_ARCH/$HSCT_HELENOS_MACHINE"
848 if ! [ "$HSCT_PARALLELISM" -ge 0 ] 2>/dev/null; then
849 HSCT_PARALLELISM="1"
853 if [ -z "$HSCT_HELENOS_ROOT" ]; then
854 hsct_error "I don't know where is the HelenOS source root."
855 leave_script_err
858 case "$1" in
859 clean|build|package|install|archive)
860 HSCT_HARBOUR_NAME="$2"
861 if [ -z "$HSCT_HARBOUR_NAME" ]; then
862 hsct_usage "$0"
863 leave_script_err
866 init)
867 hsct_init "$2" "$3" "$4"
868 leave_script_ok
870 update)
871 hsct_update "$2"
872 leave_script_ok
875 hsct_error "Internal error, we shall not get to this point!"
876 leave_script_err
878 esac
881 if ! [ -d "$HSCT_HOME/$HSCT_HARBOUR_NAME" ]; then
882 hsct_error "Unknown package $2"
883 leave_script_err
886 if ! [ -r "$HSCT_HOME/$HSCT_HARBOUR_NAME/HARBOUR" ]; then
887 hsct_error "HARBOUR file missing." >&2
888 leave_script_err
891 HSCT_OVERLAY="$HSCT_HELENOS_ROOT/uspace/overlay"
892 HSCT_MY_DIR="$HSCT_DIST_DIR/$HSCT_HARBOUR_NAME"
894 # Source the harbour to get access to the variables and functions
895 . "$HSCT_HOME/$HSCT_HARBOUR_NAME/HARBOUR"
897 case "$1" in
898 clean)
899 hsct_clean
901 build)
902 hsct_build
904 package)
905 hsct_package
907 install)
908 hsct_install
910 archive)
911 hsct_archive
914 hsct_error "Internal error, we shall not get to this point!"
915 leave_script_err
917 esac
919 if [ $? -eq 0 ]; then
920 leave_script_ok
921 else
922 leave_script_err