aboutsummaryrefslogtreecommitdiffstats
path: root/dist/ci-run.sh
blob: 70e6b6d449c278790dd09dbaaef92d6c01835a95 (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
466
467
468
469
470
471
472
473
474
#! /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 "$TRAVIS" ]; then
  echo "INFO: set 'gstart' and 'gend' for TRAVIS"
  # This is a trimmed down copy of https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/bash/*
  travis_time_start() {
    # `date +%N` returns the date in nanoseconds. It is used as a replacement for $RANDOM, which is only available in bash.
    travis_timer_id=`date +%N`
    travis_start_time=`travis_nanoseconds`
    echo "travis_time:start:$travis_timer_id"
  }
  travis_time_finish() {
    travis_end_time=`travis_nanoseconds`
    local duration=$(($travis_end_time-$travis_start_time))
    echo "travis_time:end:$travis_timer_id:start=$travis_start_time,finish=$travis_end_time,duration=$duration"
  }

  if [ "$TRAVIS_OS_NAME" = "osx" ]; then
    travis_nanoseconds() {
      date -u '+%s000000000'
    }
  else
    travis_nanoseconds() {
      date -u '+%s%N'
    }
  fi

  gstart () {
    echo "travis_fold:start:group"
    travis_time_start
    print_start "$@"
  }

  gend () {
    travis_time_finish
    echo "travis_fold:end:group"
  }
else
  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
fi

echo "cliargs: $0 $@"

# Stop in case of error
set -e

ISGPL=false
ISSYNTH=true

# Transform long options to short ones
for arg in "$@"; do
  shift
  case "$arg" in
      "--color"|"-color")       set -- "$@" "-c";;
      "--backend"|"-backend")   set -- "$@" "-b";;
      "--pkg"|"-pkg")           set -- "$@" "-p";;
      "--gpl"|"-gpl")           set -- "$@" "-g";;
      "--no-synth"|"-no-synth") set -- "$@" "-s";;
    *) set -- "$@" "$arg"
  esac
done
# Parse args
while getopts ":b:p:cgs" opt; do
  case $opt in
    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 "$TRAVIS_COMMIT" ]; then
    GIT_SHA="$TRAVIS_COMMIT"
  fi
  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 "TRAVIS_COMMIT: $TRAVIS_COMMIT"
  echo "TRAVIS_TAG: $TRAVIS_TAG"
  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`"
    ;;
    "")
      if [ -z "$TRAVIS_TAG" ]; then
        PKG_TAG="`notag`"
      else
        PKG_TAG="`vertag "$TRAVIS_TAG"`"
      fi
    ;;
    *)
      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 --gpl"
      PKG_NAME="${PKG_NAME}-gpl"
      DEXT="-gpl"
  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"
      gend
  fi

  #--- Configure

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

  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"
  set +e
  make LIB_CFLAGS="$LIB_CFLAGS" OPT_FLAGS="$OPT_FLAGS" -j`nproc` 2>make_err.log
  tail -1000 make_err.log
  set -e
  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 -e TRAVIS -v `pwd`:/work -w /work"
  if [ "x$IS_MACOS" = "xtrue" ]; then
      export CPATH="$CPATH:`xcrun --show-sdk-path`/usr/include"
      CC=clang \
      CONFIG_OPTS="--disable-libghdl" \
      bash -c "${scriptdir}/ci-run.sh $BUILD_CMD_OPTS build"
  else
      # Assume linux

      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_DESC="$(git describe --dirty)@${BUILD_IMAGE_TAG}" -e CONFIG_OPTS="$CONFIG_OPTS" "ghdl/build:$BUILD_IMAGE_TAG" bash -c "${scriptdir}/ci-run.sh $BUILD_CMD_OPTS build"
  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
      CC=clang \
      prefix="`cd ./install-mcode; pwd`/usr/local" \
      ./testsuite/testsuite.sh sanity pyunit gna vests vpi
  else
      # Build ghdl/ghdl:$GHDL_IMAGE_TAG image
      build_img_ghdl
      case "$GHDL_IMAGE_TAG" in
        *ubuntu*)
          GHDL_TEST_IMAGE="test:$GHDL_IMAGE_TAG-py"
          docker build -t "$GHDL_TEST_IMAGE" - <<-EOF
FROM ghdl/ghdl:$GHDL_IMAGE_TAG
RUN apt update -qq && apt install -y python3
EOF
        ;;
        *)
          GHDL_TEST_IMAGE="ghdl/ghdl:$GHDL_IMAGE_TAG"
        ;;
      esac
      # Run test in docker container
      tests="sanity pyunit"
      if [ "x$ISGPL" != "xtrue" ]; then
        tests="$tests gna"
      fi
      tests="$tests vests"
      if [ "x$ISSYNTH" = "xtrue" ]; then
        tests="$tests synth"
      fi
      tests="$tests vpi"
      $RUN "$GHDL_TEST_IMAGE" bash -c "GHDL=ghdl ./testsuite/testsuite.sh $tests"
  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" ] || [ "$TRAVIS_OS_NAME" = "osx" ]; then
  IS_MACOS="true"
fi

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