aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/ci-run.sh
blob: 11eaf5e1e15fece3092b0e4d677c6aebe99b1a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#! /bin/bash

scriptdir=`dirname "$0"`

if [ -n "$GITHUB_EVENT_PATH" ]; then
  export CI=true
fi

. "$scriptdir/ansi_color.sh"
disable_color

print_start() {
  COL="$ANSI_YELLOW"
  if [ "x$2" != "x" ]; then
    COL="$2"
  fi
  printf "${COL}${1}$ANSI_NOCOLOR\n"
}

gstart () {
  print_start "$@"
}
gend () {
  :
}


if [ -n "$CI" ]; then
  echo "INFO: set 'gstart' and 'gend' for CI"
  gstart () {
    printf '::group::'
    print_start "$@"
    SECONDS=0
  }

  gend () {
    duration=$SECONDS
    echo '::endgroup::'
    printf "${ANSI_GRAY}took $(($duration / 60)) min $(($duration % 60)) sec.${ANSI_NOCOLOR}\n"
  }
fi

echo "cliargs: $0 $@"

# Stop in case of error
set -e

ISGPL=false
ISSYNTH=true
USEDOCKER=false

# Transform long options to short ones
for arg in "$@"; do
  shift
  case "$arg" in
      "--color"|"-color")         set -- "$@" "-c";;
      "--docker"|"-docker")       set -- "$@" "-d";;
      "--backend"|"-backend")     set -- "$@" "-b";;
      "--pkg"|"-pkg")             set -- "$@" "-p";;
      "--gplcompat"|"-gplcompat") set -- "$@" "-g";;
      "--no-synth"|"-no-synth")   set -- "$@" "-s";;
    *) set -- "$@" "$arg"
  esac
done
# Parse args
while getopts ":b:p:cdgs" opt; do
  case $opt in
    d) USEDOCKER=true;;
    c) enable_color;;
    b) BACK=$OPTARG ;;
    p) PKG_NAME=$OPTARG;;
    g) ISGPL=true;;
    s) ISSYNTH=false;;
    \?) printf "$ANSI_RED[CI - args] Invalid option: -$OPTARG $ANSI_NOCOLOR\n" >&2
        exit 1 ;;
    :)  printf "$ANSI_RED[CI - args] Option -$OPTARG requires an argument. $ANSI_NOCOLOR\n" >&2
        exit 1 ;;
  esac
done
shift $((OPTIND -1))

#---

#
# Build command options
#

notag() {
  # No tag: use date + commit id
  echo "`git log -1 --date=short --pretty=format:%cd | sed 's/-//g'`-$PKG_SHA"
}

vertag() {
  if expr "$1" : 'v[0-9].*' > /dev/null; then
    # Check version defined in configure.
    cfgver=`grep "^ghdl_version=" configure | sed -e 's/.*"\(.*\)"/\1/'`
    if [ "x$1" != "xv$cfgver" ]; then
      printf "${ANSI_RED}Tag '$1' does not match configure 'ghdl_version' ($cfgver)!${ANSI_NOCOLOR}\n" 1>&2;
      exit 1
    fi
    # Remove leading 'v' in tags in the filenames.
    echo $1 | cut -c2-
  else
    # Regular tag (like snapshots), nothing to change.
    echo "$1"
  fi
}

buildCmdOpts () {
  BUILD_ARG="$1"

  # Get short commit SHA
  if [ -n "$GITHUB_SHA" ]; then
    GIT_SHA="$GITHUB_SHA"
  fi
  if [ -z "$GIT_SHA" ]; then
    GIT_SHA="`git rev-parse --verify HEAD`"
  fi
  PKG_SHA="`printf $GIT_SHA | cut -c1-10`"

  echo "GITHUB_SHA: $GITHUB_SHA"
  echo "GITHUB_REF: $GITHUB_REF"
  echo "GIT_SHA: $GIT_SHA"

  # Compute package name
  case "$GITHUB_REF" in
    *tags*)
      PKG_TAG="$(vertag "`echo "$GITHUB_REF" | sed 's#^refs/tags/\(.*\)#\1#g'`")"
    ;;
    *heads*|*pull*)
      PKG_TAG="`notag`"
    ;;
    *)
      PKG_TAG="$GITHUB_REF"
    ;;
  esac

  echo "PKG_SHA: $PKG_SHA"
  echo "PKG_TAG: $PKG_TAG"

  # Extract from BUILD_ARG
  IFS='+' read -ra REFS <<< "$BUILD_ARG"
  DDIST=${REFS[0]}  # Linux distro (eg: ubuntuXX, fedoraXX)
  DBACK=${REFS[1]}  # Build/backend (eg: mcode, llvm)

  PKG_NAME="ghdl-${PKG_TAG}-${DDIST}-${DBACK}"
  BUILD_CMD_OPTS="$ENABLECOLOR -b $DBACK"

  if [ "x$ISGPL" = "xtrue" ]; then
      BUILD_CMD_OPTS="$BUILD_CMD_OPTS --gplcompat"
      PKG_NAME="${PKG_NAME}-gplcompat"
      DEXT="-gplcompat"
  fi
  export BUILD_CMD_OPTS="${BUILD_CMD_OPTS} -p $PKG_NAME"

  GHDL_IMAGE_TAG="`echo $BUILD_ARG | sed -e 's/+/-/g'`"
  BUILD_IMAGE_TAG="$GHDL_IMAGE_TAG"

  case $BUILD_ARG in
    *gcc*)
      BUILD_IMAGE_TAG="`echo $GHDL_IMAGE_TAG | sed 's#\(.*\)-gcc.*#\1-gcc#g'`"
    ;;
  esac

  GHDL_IMAGE_TAG="${GHDL_IMAGE_TAG}$DEXT"
}

run_cmd() {
  echo "$@"
  "$@"
}

#
# Build ghdl
#

build () {
  rm -f build_ok

  #--- Env

  gstart "[GHDL - build] Environment"
  env
  gend

  #--- GPL: gpl-ize sources

  if [ "$ISGPL" = "true" ]; then
      GPLDIR="${PKG_NAME}.src"
      gstart "[GHDL - build] create GPL source package (${ANSI_CYAN}${GPLDIR}.tgz${ANSI_NOCOLOR})"
      files=`echo *`
      make -f Makefile.in srcdir=. clean-pure-gpl
      mkdir "$GPLDIR"
      cp -pdrl $files "$GPLDIR"
      tar -zcf "${GPLDIR}.tgz" "$GPLDIR"
      CONFIG_OPTS+=' --enable-gplcompat'
      gend
  fi

  #--- Configure

  CDIR=`pwd`
  INSTALL_DIR="$CDIR/install-$BACK"
  mkdir "$INSTALL_DIR"
  mkdir "build-$BACK"
  cd "build-$BACK"

  CONFIG_OPTS+=" --enable-werror"

  if [ "x$ISSYNTH" = "xfalse" ]; then
    CONFIG_OPTS+=" --disable-synth"
  fi

  case "$BACK" in
      gcc*)
          gstart "[GHDL - build] Get gcc sources"
          gccURL="https://codeload.github.com/gcc-mirror/gcc/tar.gz/releases/${BACK}"
          echo "$gccURL"
          mkdir gcc-srcs
          curl -L "$gccURL" | tar -xz -C gcc-srcs --strip-components=1
          cd gcc-srcs
          sed -i.bak s/ftp:/http:/g ./contrib/download_prerequisites
          ./contrib/download_prerequisites
          cd ..
          gend

          gstart "[GHDL - build] Configure ghdl"
          run_cmd ../configure --with-gcc=gcc-srcs $CONFIG_OPTS
          gend
          gstart "[GHDL - build] Copy sources"
          make copy-sources
          mkdir gcc-objs; cd gcc-objs
          gend
          gstart "[GHDL - build] Configure gcc"
          run_cmd ../gcc-srcs/configure --enable-languages=c,vhdl --disable-bootstrap --disable-lto --disable-multilib --disable-libssp --disable-libgomp --disable-libquadmath "`gcc -v 2>&1 | grep -o -- --enable-default-pie`"
          gend
      ;;
      mcode)
          CXX=""
      ;;
      llvm)
          CXX="clang++"
          CONFIG_OPTS+=" --with-llvm-config CXX=$CXX"
      ;;
      llvm-3.5)
          CXX="clang++"
          CONFIG_OPTS+=" --with-llvm-config=llvm-config-3.5 CXX=$CXX"
      ;;
      llvm-*)
          llvmver=`echo $BACK | sed -e "s/llvm-//"`
          CXX="clang++-$llvmver"
          CONFIG_OPTS+=" --with-llvm-config=llvm-config-$llvmver CXX=$CXX"
      ;;
      *)
          printf "$ANSI_RED[GHDL - build] Unknown build $BACK $ANSI_NOCOLOR\n"
          exit 1;;
  esac

  if [ ! "`echo $BACK | grep gcc`" ]; then
      gstart "[GHDL - build] Configure"
      run_cmd ../configure $CONFIG_OPTS
      gend
  fi

  #--- make

  gstart "[GHDL - build] Make"
  make LIB_CFLAGS="$LIB_CFLAGS" OPT_FLAGS="$OPT_FLAGS" -j"$(nproc)"
  gend

  gstart "[GHDL - build] Install"
  make DESTDIR="$INSTALL_DIR" install
  cd ..
  gend

  if [ "`echo $BACK | grep gcc`" ]; then
      gstart "[GHDL - build] Make ghdllib"
      make DESTDIR="$INSTALL_DIR" ghdllib
      gend

      gstart "[GHDL - build] Install ghdllib"
      make DESTDIR="$INSTALL_DIR" install
      cd ..
      gend
  fi

  #--- package

  gstart "[GHDL - build] Create package ${ANSI_DARKCYAN}${PKG_NAME}.tgz"
  tar -zcvf "${PKG_NAME}.tgz" -C "$INSTALL_DIR/usr/local" .
  gend

  #--- build tools versions

  {
      make --version | grep 'Make'
      gnatls --version | grep 'GNATLS'
      gcc --version | grep 'gcc'
      if [ "$CXX" != "" ]; then
          $CXX --version | grep 'clang'
      fi
  } > BUILD_TOOLS

  #---

  printf "$ANSI_GREEN[GHDL - build] SUCCESSFUL${ANSI_NOCOLOR}\n"
  touch build_ok
}

#
# Build ghdl/ghdl image
#

build_img_ghdl() {
    gstart "[DOCKER - build] ghdl/ghdl:${GHDL_IMAGE_TAG}" "$ANSI_BLUE"
    docker build -t ghdl/ghdl:$GHDL_IMAGE_TAG . -f-<<EOF
FROM ghdl/run:$BUILD_IMAGE_TAG
ADD `ls | grep -v '\.src\.' | grep '^ghdl.*\.tgz'` /usr/local
EOF
    gend
}

#
# Full CI run
#

ci_run () {
  if [ "x$TASK" = "x" ]; then
    if [ "x$1" = "x" ]; then
      printf "${ANSI_RED}TASK not defined${ANSI_NOCOLOR}\n"
      exit 1
    else
      TASK="$1"
    fi
  fi

  gstart "[CI] git fetch --unshallow" "$ANSI_BLUE"
  # The command 'git describe' (used for version) needs the history. Get it.
  # But the following command fails if the repository is complete.
  git fetch --unshallow || true
  gend

  # Get build command options
  gstart "[CI] Get build command options" "$ANSI_BLUE"
  buildCmdOpts "$TASK"
  echo "build cmd: $BUILD_CMD_OPTS"
  gend

  # Build

  RUN="docker run --rm -t -e CI -v `pwd`:/work -w /work"
  if [ "x$IS_MACOS" = "xtrue" ]; then
      export CPATH="$CPATH:$(xcrun --show-sdk-path)/usr/include"
      export PATH="$PATH:$(brew --prefix llvm)/bin"
      CC=clang bash -c "${scriptdir}/ci-run.sh $BUILD_CMD_OPTS build"
  else
      # Assume linux

      if [ "$USEDOCKER" = "true" ]; then
	  gstart "[CI] Docker pull ghdl/build:$BUILD_IMAGE_TAG" "$ANSI_BLUE"
	  docker pull ghdl/build:$BUILD_IMAGE_TAG
	  gend

	  printf "$ANSI_BLUE[CI] Build ghdl in docker image ghdl/build:$BUILD_IMAGE_TAG\n"
	  $RUN \
              -e GHDL_VER_DESC="$(git describe --dirty)" \
              -e GHDL_VER_REF="$(git rev-parse --abbrev-ref HEAD)@${BUILD_IMAGE_TAG}" \
              -e GHDL_VER_HASH="$(git rev-parse HEAD)" \
              -e CONFIG_OPTS="$CONFIG_OPTS" \
              ghdl/build:"$BUILD_IMAGE_TAG" \
              bash -c "${scriptdir}/ci-run.sh $BUILD_CMD_OPTS build"
      else
	  sudo apt-get update -qq
	  sudo apt-get -y install --no-install-recommends gnat zlib1g-dev
	  bash -c "${scriptdir}/ci-run.sh $BUILD_CMD_OPTS build"
      fi
  fi

  if [ ! -f build_ok ]; then
      printf "${ANSI_RED}[GHDL - build] FAILED${ANSI_NOCOLOR}\n"
      exit 1
  fi

  # Test

  if [ "x$IS_MACOS" = "xtrue" ]; then
      pip3 install -r testsuite/requirements.txt
      CC=clang \
      PATH="$PATH:$(pwd)/install-$(echo "$TASK" | cut -d+ -f2)/usr/local/bin" \
      ./testsuite/testsuite.sh
  else
      if [ "$USEDOCKER" = "true" ]; then
	  # Build ghdl/ghdl:$GHDL_IMAGE_TAG image
	  build_img_ghdl
      fi

      tests="sanity"

      case "$GHDL_IMAGE_TAG" in
          *ubuntu*|*buster*|*bullseye*)
	      if [ "$USEDOCKER" = "true" ]; then
		  GHDL_TEST_IMAGE="test:$GHDL_IMAGE_TAG-py"
		  gstart "[CI] Docker build $GHDL_TEST_IMAGE" "$ANSI_BLUE"
		  docker build -t "$GHDL_TEST_IMAGE" . -f- <<-EOF
# syntax=docker/dockerfile:experimental
FROM ghdl/ghdl:$GHDL_IMAGE_TAG
RUN apt update -qq && apt install -y python3 python3-pip
RUN --mount=type=bind,src=./,target=/tmp/ghdl/ \
  pip3 install -r /tmp/ghdl/testsuite/requirements.txt
EOF
		  gend
	      else
		  pip3 install -r testsuite/requirements.txt
	      fi
              tests+=" pyunit"
              ;;
          *)
              GHDL_TEST_IMAGE="ghdl/ghdl:$GHDL_IMAGE_TAG"
              ;;
      esac

      if [ -d testsuite/gna ]; then
        tests+=" gna"
      fi

      tests+=" vests"

      if [ "x$ISSYNTH" = "xtrue" ]; then
        tests+=" synth"
      fi

      tests+=" vpi vhpi"

      if [ "$USEDOCKER" = "true" ]; then
	  # Run tests in docker container
	  $RUN "$GHDL_TEST_IMAGE" bash -c "GHDL=ghdl ./testsuite/testsuite.sh $tests"
      else
	  PATH="$PATH:$(pwd)/install-$(echo "$TASK" | cut -d+ -f2)/usr/local/bin" \
	      ./testsuite/testsuite.sh $tests
      fi
  fi

  if [ ! -f testsuite/test_ok ]; then
      printf "${ANSI_RED}[GHDL - test] FAILED${ANSI_NOCOLOR}\n"
      exit 1
  fi
}

#---

echo "command: $0 $@"

unset IS_MACOS
if [ "$GITHUB_OS" = "macOS" ]; then
  IS_MACOS="true"
fi

case "$1" in
  build)
    build
  ;;
  *)
    ci_run
  ;;
esac