| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | if [ -z "${CI_QUIET}" ]; |
| 4 | then |
| 5 | set -x |
| 6 | fi |
| 7 | set -e |
| 8 | |
| 9 | # default value for NJOBS |
| 10 | : "${NJOBS:=1}" |
| 11 | export NJOBS |
| 12 | |
| 13 | . "$(dirname "${BASH_SOURCE[0]}")/../ci-env.sh" |
| 14 | |
| 15 | # Where we download and build external developments |
| 16 | CI_BUILD_DIR="$PWD/_build_ci" |
| 17 | |
| 18 | declare -A overlays |
| 19 | |
| 20 | # overlay <project> <giturl> <ref> <prnumber> [<prbranch>] |
| 21 | # creates an overlay for project using a given url and branch which is |
| 22 | # active for prnumber or prbranch. prbranch defaults to ref. |
| 23 | function overlay() |
| 24 | { |
| 25 | local project=$1 |
| 26 | local ov_url=$2 |
| 27 | local ov_ref=$3 |
| 28 | local ov_prnumber=$4 |
| 29 | local ov_prbranch=$5 |
| 30 | : "${ov_prbranch:=$ov_ref}" |
| 31 | |
| 32 | if [ "$CI_PULL_REQUEST" = "$ov_prnumber" ] || [ "$CI_BRANCH" = "$ov_prbranch" ]; then |
| 33 | if ! is_in_projects "$project"; then |
| 34 | echo "Error: $1 is not a known project which can be overlayed" |
| 35 | exit 1 |
| 36 | fi |
| 37 | |
| 38 | overlays[${project}_URL]=$ov_url |
| 39 | overlays[${project}_REF]=$ov_ref |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | set +x |
| 44 | . "$(dirname "${BASH_SOURCE[0]}")/../ci-basic-overlay.sh" |
| 45 | |
| 46 | for overlay in "$(dirname "${BASH_SOURCE[0]}")"/../user-overlays/*.sh; do |
| 47 | # shellcheck source=/dev/null |
| 48 | # the directoy can be empty |
| 49 | if [ -e "${overlay}" ]; then . "${overlay}"; fi |
| 50 | done |
| 51 | |
| 52 | if [ -z "${CI_QUIET}" ]; |
| 53 | then |
| 54 | set -x |
| 55 | fi |
| 56 | |
| 57 | # [git_download <project> [<destination>]] will download <project> and |
| 58 | # unpack it in <destination> (if given; default: |
| 59 | # $CI_BUILD_DIR/<project>) if the folder does not exist already; if it |
| 60 | # does, it will do nothing except print a warning (this can be useful |
| 61 | # when building locally). |
| 62 | # Note: when there is an overlay, $WITH_SUBMODULES is set to 1 or $CI |
| 63 | # is unset or empty (local build), it uses git clone to perform the |
| 64 | # download. |
| 65 | git_download() |
| 66 | { |
| 67 | local project=$1 |
| 68 | local dest="${2:-$CI_BUILD_DIR/$project}" |
| 69 | |
| 70 | local giturl_var="${project}_CI_GITURL" |
| 71 | local giturl="${!giturl_var}" |
| 72 | local ref_var="${project}_CI_REF" |
| 73 | local ref="${!ref_var}" |
| 74 | local parent_project_var="${project}_CI_PARENT_PROJECT" |
| 75 | local parent_project="${!parent_project_var}" |
| 76 | local submodule_folder_var="${project}_CI_SUBMODULE_FOLDER" |
| 77 | local submodule_folder="${!submodule_folder_var}" |
| 78 | |
| 79 | local ov_url=${overlays[${project}_URL]} |
| 80 | local ov_ref=${overlays[${project}_REF]} |
| 81 | |
| 82 | local dest_prefix="$(dirname "$dest")/" |
| 83 | if [ "${CI}${USE_CI_DIRECTORY_STRUCTURE}" = "" ]; then |
| 84 | # we can reuse the parent project download when not on CI |
| 85 | local parent_project_dest="$CI_BUILD_DIR/$parent_project" |
| 86 | # we use relative symlinks so they are relocatable |
| 87 | local parent_project_relative_dest="${parent_project_dest#$dest_prefix}" |
| 88 | else |
| 89 | # on CI, we need to ensure that there's no overlap in directory tree |
| 90 | # between sibling jobs, since otherwise they will scribble over |
| 91 | # each others .v timestamps and result in duplicated builds |
| 92 | local parent_project_dest="${dest}-PARENT-${parent_project}" |
| 93 | # we use relative symlinks so they are relocatable |
| 94 | local parent_project_relative_dest="${parent_project_dest#$dest_prefix}" |
| 95 | fi |
| 96 | |
| 97 | if [ -d "$dest" ]; then |
| 98 | echo "Warning: download and unpacking of $project skipped because $dest already exists." |
| 99 | elif [[ $ov_url ]] || [ "$WITH_SUBMODULES" = "1" ] || [ "$CI" = "" ] || [ -n "${parent_project}" ]; then |
| 100 | if [ -n "${parent_project}" ]; then |
| 101 | # if there is a parent project, we first download the parent |
| 102 | # project then symlink the submodule_folder to dest; this allows |
| 103 | # project CI scripts to be transparent w.r.t. whether or not the |
| 104 | # project is cloned from a submodule / submodule_folder. |
| 105 | if [ ! -d "${parent_project_dest}" ]; then |
| 106 | WITH_SUBMODULES=1 git_download "${parent_project}" "${parent_project_dest}" |
| 107 | fi |
| 108 | # now we can create the symlinks |
| 109 | ln -s "${parent_project_relative_dest}/${submodule_folder}" "$dest" |
| 110 | pushd "$dest" |
| 111 | ref="$(git rev-parse HEAD)" |
| 112 | else |
| 113 | # if $ref isn't a branch (eg it's a commit hash or a tag) git clone -b will fail |
| 114 | # in that case do a full git clone |
| 115 | git clone "$giturl" "$dest" -b "$ref" --depth 10 || git clone "$giturl" "$dest" |
| 116 | pushd "$dest" |
| 117 | git checkout "$ref" |
| 118 | fi |
| 119 | git log -n 1 |
| 120 | if [[ $ov_url ]]; then |
| 121 | # In CI we merge into the upstream branch to stay synchronized |
| 122 | # Locally checkout the overlay and rebase on upstream |
| 123 | # We act differently because merging is what will happen when the PR is merged |
| 124 | # but rebasing produces something that is nicer to edit |
| 125 | if [[ $CI ]]; then |
| 126 | git -c pull.rebase=false -c [email protected] -c user.name=Nobody \ |
| 127 | pull --no-edit --no-ff "$ov_url" "$ov_ref" |
| 128 | git log -n 1 HEAD^2 || true # no merge commit if the overlay was merged upstream |
| 129 | git log -n 1 |
| 130 | else |
| 131 | git remote add -t "$ov_ref" -f overlay "$ov_url" |
| 132 | git checkout -b "$ov_ref" overlay/"$ov_ref" |
| 133 | git rebase "$ref" |
| 134 | git log -n 1 |
| 135 | fi |
| 136 | fi |
| 137 | if [ "$WITH_SUBMODULES" = 1 ]; then |
| 138 | git submodule update --init --recursive --depth 10 |
| 139 | fi |
| 140 | popd |
| 141 | else # When possible, we download tarballs to reduce bandwidth and latency |
| 142 | local archiveurl_var="${project}_CI_ARCHIVEURL" |
| 143 | local archiveurl="${!archiveurl_var}" |
| 144 | mkdir -p "$dest" |
| 145 | pushd "$dest" |
| 146 | local commit |
| 147 | commit=$(git ls-remote "$giturl" "refs/heads/$ref" | cut -f 1) |
| 148 | if [[ "$commit" == "" ]]; then |
| 149 | # $ref must have been a tag or hash, not a branch |
| 150 | commit="$ref" |
| 151 | fi |
| 152 | wget "$archiveurl/$commit.tar.gz" |
| 153 | tar xfz "$commit.tar.gz" --strip-components=1 |
| 154 | rm -f "$commit.tar.gz" |
| 155 | popd |
| 156 | fi |
| 157 | } |
| 158 | |
| 159 | make() |
| 160 | { |
| 161 | # +x: add x only if defined |
| 162 | if [ -z "${MAKEFLAGS+x}" ] && [ -n "${NJOBS}" ]; |
| 163 | then |
| 164 | # Not submake and parallel make requested |
| 165 | command make -j "$NJOBS" "$@" |
| 166 | else |
| 167 | command make "$@" |
| 168 | fi |
| 169 | } |
| 170 | |
| 171 | # run make -k; make again if it failed so that the failing file comes last |
| 172 | # makes it easier to find the error messages in the CI log |
| 173 | function make_full() { |
| 174 | if ! make -k "$@"; then make -k "$@"; exit 1; fi |
| 175 | } |