Adapt for mainline changes in https://github.com/HelenOS/helenos/pull/15,
[harbours.git] / hsct.sh
blobbf93efe40425c2cfb6c52d31fdbe45e99a458385
1 #!/bin/sh
4 # Copyright (c) 2013-2017 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_BUILD_DIR=`pwd`/build
61 HSCT_INCLUDE_DIR=`pwd`/include
62 HSCT_LIB_DIR=`pwd`/libs
63 HSCT_DIST_DIR="`pwd`/dist/"
64 HSCT_ARCHIVE_DIR="`pwd`/archives/"
65 HSCT_CACHE_DIR=`pwd`/helenos
67 # Print short help.
68 # Does not exit the whole script.
69 hsct_usage() {
70 echo "Usage:"
71 echo " $1 action [package]"
72 echo " Action can be one of following:"
73 echo " clean Clean built directory."
74 echo " fetch Fetch sources (e.g. download from homepage)."
75 echo " build Build given package."
76 echo " package Save installable files to allow cleaning."
77 echo " install Install to uspace/dist of HelenOS."
78 echo " archive Create tarball instead of installing."
79 echo " $1 init [/path/to/HelenOS] [profile]"
80 echo " Initialize current directory as coastline build directory".
81 echo " Full path has to be provided to the HelenOS source tree."
82 echo " If profile is specified, forcefully rebuild to specified profile."
83 echo " If no argument is provided, path to HelenOS source tree is"
84 echo " read from the HELENOS_ROOT environment variable."
85 echo " $1 help"
86 echo " Display this help and exit."
89 # Print high-level information message.
90 hsct_info() {
91 echo ">>>" "$@" >&2
94 # Print lower-level information message (additional info after hsct_info).
95 hsct_info2() {
96 echo " ->" "$@" >&2
99 # Print information message from HARBOUR script.
100 msg() {
101 hsct_info "$@"
104 # Print high-level error message.
105 hsct_error() {
106 echo "[hsct]:" "Error:" "$@" >&2
109 # Print additional details to the error message.
110 hsct_error2() {
111 echo "[hsct]:" " -> " "$@" >&2
114 # Run a command but print it first.
115 hsct_run_echo() {
116 echo -n "[hsct]: "
117 for ___i in "$@"; do
118 echo -n "$___i" | sed -e 's#"#\\"#g' -e 's#.*#"&" #'
119 done
120 echo
121 "$@"
124 # Run comman from HARBOUR script and print it as well.
125 run() {
126 hsct_run_echo "$@"
129 hsct_process_harbour_opts() {
130 HSCT_OPTS_NO_DEPENDENCY_BUILDING=false
131 HSCT_OPTS_NO_FILE_DOWNLOADING=false
132 HSCT_HARBOUR_NAME=""
134 while [ "$#" -ne 0 ]; do
135 case "$1" in
136 --no-deps)
137 HSCT_OPTS_NO_DEPENDENCY_BUILDING=true
139 --no-fetch)
140 HSCT_OPTS_NO_FILE_DOWNLOADING=true
142 --*)
143 hsct_error "Unknown option $1."
144 return 1
147 if [ -z "$HSCT_HARBOUR_NAME" ]; then
148 HSCT_HARBOUR_NAME="$1"
149 else
150 hsct_error "Only one package name allowed."
151 return 1
154 esac
155 shift
156 done
158 return 0
161 # Fetch all the specified files in the HARBOUR
162 hsct_fetch() {
163 mkdir -p "$HSCT_SOURCES_DIR"
164 hsct_info "Fetching sources..."
165 for _url in $shipsources; do
166 _filename=`basename "$_url"`
167 if [ "$_filename" = "$_url" ]; then
168 continue
170 if ! [ -r "$HSCT_SOURCES_DIR/$_filename" ]; then
171 if $HSCT_OPTS_NO_FILE_DOWNLOADING; then
172 hsct_error "File $_filename missing, cannot continue."
173 hsct_error2 "Build without --no-fetch."
174 return 1
177 hsct_info2 "Fetching $_filename..."
178 # Remove the file even on Ctrl-C when fetching
179 trap "rm -f \"$HSCT_SOURCES_DIR/$_filename\"; echo" SIGINT SIGQUIT
180 if ! wget $HSCT_WGET_OPTS "$_url" -O "$HSCT_SOURCES_DIR/$_filename"; then
181 rm -f "$HSCT_SOURCES_DIR/$_filename"
182 hsct_error "Failed to fetch $_url."
183 return 1
185 trap - SIGINT SIGQUIT
187 # TODO - check MD5
188 done
189 return 0
192 # Remove the build directory of given package.
193 hsct_clean() {
194 hsct_info "Cleaning build directory..."
195 rm -rf "$HSCT_BUILD_DIR/$shipname/"*
198 # Build the package.
199 hsct_build() {
200 mkdir -p "$HSCT_BUILD_DIR/$shipname"
201 if [ -e "$HSCT_BUILD_DIR/${shipname}.built" ]; then
202 hsct_info "No need to build $shipname."
203 return 0
206 # Check for prerequisities
207 for tug in $shiptugs; do
208 if ! [ -e "$HSCT_BUILD_DIR/${tug}.packaged" ]; then
209 if $HSCT_OPTS_NO_DEPENDENCY_BUILDING; then
210 hsct_error "Dependency $tug not built, cannot continue."
211 hsct_error2 "Build $tug first or run without --no-deps."
212 return 1
214 hsct_info "Need to build $tug first."
215 hsct_info2 "Running $HSCT_HSCT package $tug"
217 $HSCT_HSCT package $tug
218 exit $?
220 if [ $? -ne 0 ]; then
221 hsct_error "Failed to package dependency $tug."
222 hsct_error2 "Cannot continue building $shipname."
223 return 1
225 hsct_info2 "Back from building $tug."
227 done
229 hsct_fetch || return 1
231 for _url in $shipsources; do
232 _filename=`basename "$_url"`
233 if [ "$_filename" = "$_url" ]; then
234 _origin="$HSCT_HOME/$shipname/$_filename"
235 else
236 _origin="$HSCT_SOURCES_DIR/$_filename"
238 ln -srf "$_origin" "$HSCT_BUILD_DIR/$shipname/$_filename"
239 done
242 cd "$HSCT_BUILD_DIR/$shipname/"
243 hsct_info "Building..."
244 set -o errexit
245 build
246 exit $?
248 if [ $? -ne 0 ]; then
249 hsct_error "Build failed!"
250 return 1
252 touch "$HSCT_BUILD_DIR/${shipname}.built"
253 return 0
256 # Pseudo-installation - copy from build directory to "my" directory, copy libraries
257 hsct_package() {
258 mkdir -p "$HSCT_INCLUDE_DIR" || { hsct_error "Failed to create include directory."; return 1; }
259 mkdir -p "$HSCT_LIB_DIR" || { hsct_error "Failed to create library directory."; return 1; }
260 mkdir -p "$HSCT_MY_DIR" || { hsct_error "Failed to create package directory."; return 1; }
262 if [ -e "$HSCT_BUILD_DIR/${shipname}.packaged" ]; then
263 hsct_info "No need to package $shipname."
264 return 0;
267 hsct_build || return 1
270 cd "$HSCT_BUILD_DIR/$shipname/"
271 hsct_info "Packaging..."
272 set -o errexit
273 package
274 exit $?
276 if [ $? -ne 0 ]; then
277 hsct_error "Packaging failed!"
278 return 1
280 touch "$HSCT_BUILD_DIR/${shipname}.packaged"
281 return 0
284 # Install the package to HelenOS source tree (to uspace/overlay).
285 hsct_install() {
286 hsct_package || return 1
288 hsct_info "Installing..."
289 if ls "$HSCT_MY_DIR"/* &>/dev/null; then
290 mkdir -p "$HSCT_OVERLAY"
291 cp -v -r -L "$HSCT_MY_DIR"/* "$HSCT_OVERLAY" || return 1
292 hsct_info2 "Do not forget to rebuild the image."
293 else
294 hsct_info2 "Note: nothing to install."
296 return 0
299 # Create tarball to allow redistribution of the build packages
300 hsct_archive() {
301 hsct_package || return 1
303 hsct_info "Creating the archive..."
304 mkdir -p "$HSCT_ARCHIVE_DIR"
306 set -o errexit
307 cd "$HSCT_DIST_DIR/$shipname"
308 case "$HSCT_FORMAT" in
309 tar.gz)
310 tar czf "$HSCT_ARCHIVE_DIR/$shipname.tar.gz" .
312 tar.xz)
313 tar cJf "$HSCT_ARCHIVE_DIR/$shipname.tar.xz" .
316 hsct_info "Unknown archive_format $HSCT_FORMAT."
317 exit 1
319 esac
321 if [ $? -ne 0 ]; then
322 hsct_error "Archiving failed!"
323 return 1
326 return 0
329 hsct_load_config() {
330 # Defaults
331 HSCT_CONFIG=config.sh
332 HSCT_WGET_OPTS=
333 HSCT_SOURCES_DIR=`pwd`/sources
334 HSCT_FORMAT="tar.xz"
335 HSCT_PARALLELISM=`nproc`
336 HELENOS_ROOT=
338 if [ -e "$HSCT_CONFIG" ]; then
339 . $HSCT_CONFIG
343 # Initialize current directory for coastline building.
344 hsct_init() {
345 hsct_load_config
347 _root_dir=$1
348 profile=$2
350 if [ -z "$_root_dir" ]; then
351 # Try to get HELENOS_ROOT from the environment if not specified.
352 _root_dir="$HELENOS_ROOT"
355 if [ -z "$_root_dir" ]; then
356 hsct_error "HELENOS_ROOT is not set. Either set the environment variable, or specify it on the command line.";
357 return 1
360 _root_dir=`( cd "$_root_dir"; pwd ) 2>/dev/null`
361 if ! [ -e "$_root_dir/HelenOS.config" ]; then
362 hsct_error "$_root_dir does not look like a valid HelenOS directory.";
363 return 1
366 HELENOS_ROOT="$_root_dir"
368 hsct_info "Initializing this build directory."
370 EXPORT_DIR=`pwd`/helenos
371 set -o errexit
372 cd "$HELENOS_ROOT"
373 if [ -z $profile ]; then
374 hsct_info2 "Reusing existing configuration."
375 make -j`nproc` export-posix "EXPORT_DIR=$EXPORT_DIR" HANDS_OFF=y >/dev/null 2>&1
376 else
377 hsct_info2 "Cleaning previous configuration in $PWD."
378 make distclean >/dev/null 2>&1
379 hsct_info2 "Configuring for $profile."
380 make -j`nproc` export-posix "EXPORT_DIR=$EXPORT_DIR" "PROFILE=$profile" HANDS_OFF=y >/dev/null 2>&1
383 if [ $? -ne 0 ]; then
384 hsct_error "Failed to automatically configure HelenOS for profile '$profile'."
385 return 1
388 hsct_info "Creating facade toolchain."
389 mkdir -p facade
390 facade_path=`realpath facade`
393 . helenos/config.rc
395 if [ -z "$HSCT_TARGET" ]; then
396 hsct_error "HSCT_TARGET undefined."
399 cd $HSCT_HOME/facade
400 for x in *; do
401 install -m 755 "$x" "$facade_path/$HSCT_TARGET-$x"
402 done
405 return 0
408 alias leave_script_ok='return 0 2>/dev/null || exit 0'
409 alias leave_script_err='return 1 2>/dev/null || exit 1'
411 hsct_print_vars() {
412 # This is separate from the rest, so that the user can run
413 # eval `path/to/hsct.sh vars` to get these vars in interactive shell.
415 hsct_load_config
416 HELENOS_EXPORT_ROOT="$HSCT_CACHE_DIR"
417 HELENOS_CONFIG="$HSCT_CACHE_DIR/config.rc"
419 if ! [ -e "$HELENOS_CONFIG" ]; then
420 hsct_error "Configuration not found. Maybe you need to run init first?"
423 . $HELENOS_CONFIG
425 echo "export HELENOS_EXPORT_ROOT='$HSCT_CACHE_DIR'"
426 echo "export HSCT_REAL_CC='$HELENOS_TARGET-gcc'"
427 echo "export HSCT_REAL_CXX='$HELENOS_TARGET-g++'"
429 echo "export HSCT_ARFLAGS='$HELENOS_ARFLAGS'"
430 echo "export HSCT_CPPFLAGS='-isystem $HSCT_INCLUDE_DIR $HELENOS_CPPFLAGS'"
431 echo "export HSCT_CFLAGS='$HELENOS_CFLAGS'"
432 echo "export HSCT_CXXFLAGS='$HELENOS_CXXFLAGS'"
433 echo "export HSCT_ASFLAGS='$HELENOS_ASFLAGS'"
434 echo "export HSCT_LDFLAGS='-L $HSCT_LIB_DIR $HELENOS_LDFLAGS'"
435 echo "export HSCT_LDLIBS='$HELENOS_LDLIBS'"
437 target="$HELENOS_ARCH-helenos"
438 cvars="CC=$target-cc CXX=$target-cxx AR=$target-ar AS=$target-as CPP=$target-cpp NM=$target-nm OBJDUMP=$target-objdump OBJCOPY=$target-objcopy STRIP=$target-strip RANLIB=$target-ranlib"
440 echo "export HSCT_CC='$target-cc'"
441 echo "export HSCT_CXX='$target-cxx'"
442 echo "export HSCT_TARGET='$target'"
443 echo "export HSCT_REAL_TARGET='$HELENOS_TARGET'"
444 echo "export HSCT_CONFIGURE_VARS='$cvars'"
445 echo "export HSCT_CONFIGURE_ARGS='--host=$target $cvars'"
447 facade_path=`realpath facade`
448 echo "export PATH='$facade_path:$HELENOS_CROSS_PATH:$PATH'"
451 hsct_pkg() {
452 eval `hsct_print_vars`
454 hsct_load_config
456 HELENOS_CONFIG="$HSCT_CACHE_DIR/config.rc"
458 if ! [ -e "$HELENOS_CONFIG" ]; then
459 hsct_error "Configuration not found. Maybe you need to run init first?"
462 . $HELENOS_CONFIG
464 HSCT_MY_DIR="$HSCT_DIST_DIR/$HSCT_HARBOUR_NAME"
465 HSCT_OVERLAY="$HELENOS_ROOT/uspace/overlay"
466 HSCT_CONFIG_SUB="$HSCT_HOME/config.sub"
468 if ! [ "$HSCT_PARALLELISM" -ge 0 ] 2>/dev/null; then
469 HSCT_PARALLELISM="1"
472 if ! [ -d "$HSCT_HOME/$HSCT_HARBOUR_NAME" ]; then
473 hsct_error "Unknown package $HSCT_HARBOUR_NAME."
474 leave_script_err
477 if ! [ -r "$HSCT_HOME/$HSCT_HARBOUR_NAME/HARBOUR" ]; then
478 hsct_error "HARBOUR file missing." >&2
479 leave_script_err
482 # Source the harbour to get access to the variables and functions
483 . "$HSCT_HOME/$HSCT_HARBOUR_NAME/HARBOUR"
485 if [ "$shipfunnels" -ne "1" ] 2>/dev/null; then
486 shipfunnels="$HSCT_PARALLELISM"
489 case "$HSCT_ACTION" in
490 clean)
491 hsct_clean
493 fetch)
494 hsct_fetch
496 build)
497 hsct_build
499 package)
500 hsct_package
502 install)
503 hsct_install
505 archive)
506 hsct_archive
509 hsct_error "Internal error, we shall not get to this point!"
510 leave_script_err
512 esac
514 return $?
517 HSCT_ACTION="$1"
519 case "$HSCT_ACTION" in
520 clean|fetch|build|package|install|archive)
521 shift
522 if ! hsct_process_harbour_opts "$@"; then
523 leave_script_err
525 if [ -z "$HSCT_HARBOUR_NAME" ]; then
526 hsct_usage "$0"
527 leave_script_err
529 hsct_pkg
530 exit $?
532 vars)
533 hsct_print_vars
534 exit $?
536 init)
537 hsct_init "$2" "$3" "$4"
538 exit $?
540 help|--help|-h|-?)
541 hsct_usage "$0"
542 leave_script_ok
545 hsct_usage "$0"
546 leave_script_err
548 esac