1#!/bin/bash -e 2 3NAME=$(basename $0) 4NAME=${NAME%.sh} 5 6# Print available options for this module 7usage_hook() 8{ 9 echo -e "dummy \tbuild dummy" 10 echo -e "A \tbuild dummy A" 11 echo -e "B \tbuild dummy B" 12 echo -e "C \tbuild dummy C" 13 14# Test: 15# ./build.sh shell 16# ./device/rockchip/common/build-hooks/example.sh -h 17# Output: 18# Usage: ./device/rockchip/common/build-hooks/example.sh [OPTIONS] 19# dummy build dummy 20# A build dummy A 21# B build dummy B 22# C build dummy C 23# clean cleanup 24# help usage 25 26 27# Test: 28# ./build.sh -h 29# Output: 30# Usage: build.sh [OPTIONS] 31# Available options: 32# dummy build dummy 33# A build dummy A 34# B build dummy B 35# C build dummy C 36} 37 38# Cleanup hook for "./build.sh cleanall" 39clean_hook() 40{ 41 echo "cleanup $NAME" 42 43# Test: 44# ./build.sh cleanall 45# Output: 46# ... 47# cleanup example 48} 49 50# Build stages: 51# init (preparing SDK configs, etc.) 52# (inherit configs) 53# pre-build (submodule configuring, etc.) 54# build (building, etc.) 55# post-build (firmware packing, etc.) 56# 57# <STAGE>_CMDS: 58# call "<stage>_hook <command> [args]" when commands matched 59# "default" command means unconditional match. 60# args are parsed from command:arg1:arg2... 61 62# Call init_hook() in init stage for "dummy" command and call it again 63# unconditionally 64INIT_CMDS="dummy default" 65init_hook() 66{ 67 echo "init hook for $0 - $@" 68 finish_build init-$NAME $@ 69 70# Test: 71# ./build.sh dummy 72# Output: 73# ... 74# init hook for /XXX/device/rockchip/common/build-hooks/example.sh - dummy 75# Running example.sh - init-example dummy succeeded. 76# init hook for /XXX/device/rockchip/common/build-hooks/example.sh - default 77# Running example.sh - init-example default succeeded. 78} 79 80# Call pre_build_hook() in pre-build stage unconditionally and call it again 81# for "A" command 82PRE_BUILD_CMDS="default A" 83pre_build_hook() 84{ 85 echo "pre-build hook for $0 - $@" 86 finish_build pre-build-$NAME $@ 87 88# Test: 89# ./build.sh A 90# Output: 91# ... 92# pre-build hook for /XXX/device/rockchip/common/build-hooks/example.sh - default 93# Running example.sh - pre-build-example default succeeded. 94# pre-build hook for /XXX/device/rockchip/common/build-hooks/example.sh - A 95# Running example.sh - pre-build-example A succeeded. 96} 97 98# Call build_hook() in build stage for "B" and "C" commands 99BUILD_CMDS="B C" 100build_hook() 101{ 102 echo "build hook for $0 - $@" 103 finish_build build-$NAME $@ 104 105# Test: 106# ./build.sh C B 107# Output: 108# ... 109# build hook for /XXX/device/rockchip/common/build-hooks/example.sh - C 110# Running example.sh - build-example C succeeded. 111# build hook for /XXX/device/rockchip/common/build-hooks/example.sh - B 112# Running example.sh - build-example B succeeded. 113} 114 115# Call post_build_hook() in post-build stage for "A" "C" and "B" commands 116POST_BUILD_CMDS="A C B" 117post_build_hook() 118{ 119 echo "post-build hook for $0 - $@" 120 finish_build post-build-$NAME $@ 121 122# Test: 123# ./build.sh A:arg1:arg2 124# Output: 125# ... 126# post-build hook for /XXX/device/rockchip/common/build-hooks/example.sh - A arg1 arg2 127# Running example.sh - post-build-example A arg1 arg2 succeeded. 128} 129 130# 1: Inherit initial checks 131# Make sure that it's been executed inside of build.sh, or after 132# "./build.sh shell" for developing. 133# 2: Call build hooks when needed 134source "${BUILD_HELPER:-$(dirname "$(realpath "$0")")/../build-hooks/build-helper}" 135 136# Test: 137# ./device/rockchip/common/build-hooks/example.sh 138# Output: 139# /XXX/device/rockchip/common/build-hooks/example.sh.in is not supposed to be executed directly 140 141# Reaching here when executed directly(not from hook triggers) 142 143echo "$0 is executed directly${@:+ with $@}." 144 145# Do something 146 147# Test: 148# ./build.sh shell 149# ./device/rockchip/common/build-hooks/example.sh 150# Output: 151# ... 152# ./device/rockchip/common/build-hooks/example.sh is executed directly. 153