1# Testsuite environment
2#
3# This file defines the shell functions to analyse a file, elaborate or run
4# a design. There are version for expected success and expected failure.
5#
6# Every test should source and use this file.
7
8# Note: the executable must be set in the GHDL environment variable
9
10# User defined variables (can be set to run the testsuite in some
11#  configuration, such as optimization or debugging):
12# GHDL_STD_FLAGS
13#
14# Testbench flags.  Can be used to run the whole testsuite under a specific
15#  flag (like -O, -g).  Should not be set in testsuite.sh files.
16#  GHDL_FLAGS
17
18#GHDL=ghdl
19RM=rm
20LN=ln
21
22# Exit in case of failure in shell scripts.
23set -e
24
25# Define colors
26ANSI_NOCOLOR="\033[0m"
27ANSI_RED="\033[31m"
28ANSI_BLUE="\033[34m"
29ANSI_GREEN="\033[32m"
30# Optionally disable colors
31if [ -z "$ENABLECOLOR" ]; then unset ANSI_NOCOLOR ANSI_RED ANSI_BLUE ANSI_GREEN; fi
32
33if [ x"$GHDL" = x ]; then
34  echo "error: GHDL environment variable is not defined"
35  exit 4
36fi
37
38PYTHON=${PYTHON:-python3}
39
40# Analyze files (no error expected)
41analyze ()
42{
43  echo "analyze $@"
44  "$GHDL" -a $GHDL_STD_FLAGS $GHDL_FLAGS "$@"
45}
46
47# Analyze files (failure expected)
48analyze_failure ()
49{
50  echo "try to analyze $@"
51  # for arg in $@; do echo "arg: $arg"; done
52  if ! "$GHDL" -a --expect-failure $GHDL_STD_FLAGS $GHDL_FLAGS "$@" ; then
53    echo "Failure expected"
54    return 1
55  fi
56}
57
58# Elaborate a design (no error expected)
59# Note: somewhat deprecated, use elab_simulate instead.
60elab ()
61{
62  echo "elaborate $@"
63  "$GHDL" -e $GHDL_STD_FLAGS $GHDL_FLAGS $@
64}
65
66# Elaborate a design (failure expected)
67# Note: somewhat deprecated, use elab_simulate_failure instead.
68elab_failure ()
69{
70  echo "elaborate (failure expected) $@"
71  "$GHDL" -e --expect-failure $GHDL_STD_FLAGS $GHDL_FLAGS $@
72}
73
74# Simulate a design (no error expected)
75# Note: somewhat deprecated, use elab_simulate instead.
76simulate ()
77{
78  echo "simulate $@ ($GHDL_FLAGS $@)" >&2
79  "$GHDL" -r $GHDL_STD_FLAGS $GHDL_FLAGS "$@"
80  #./$@
81}
82
83# Simulate a design (failure expected)
84# Note: somewhat deprecated, use elab_simulate_failure instead.
85simulate_failure ()
86{
87  echo "simulate (failure expected) $@" >&2
88  "$GHDL" -r $GHDL_STD_FLAGS $GHDL_FLAGS $@ --expect-failure
89  #./$@
90}
91
92# Elaborate and simulate a design (no error expected)
93elab_simulate ()
94{
95  echo "elaborate and simulate $@"
96  "$GHDL" --elab-run $GHDL_STD_FLAGS $GHDL_FLAGS $@
97}
98
99# Elaborate and simulate a design (failure expected)
100elab_simulate_failure ()
101{
102  echo "elaborate and simulate (failure expected) $@"
103  "$GHDL" --elab-run $GHDL_STD_FLAGS $GHDL_FLAGS $@ --expect-failure
104}
105
106synth()
107{
108  echo "Synthesis of $@" >&2
109  "$GHDL" --synth $GHDL_STD_FLAGS $GHDL_FLAGS $@
110}
111
112synth_failure ()
113{
114  echo "try to synthesize $@"
115  # for arg in $@; do echo "arg: $arg"; done
116  if ! "$GHDL" --synth --expect-failure $GHDL_STD_FLAGS $GHDL_FLAGS "$@" ; then
117    echo "Failure expected"
118    return 1
119  fi
120}
121
122# Synthesis of a single file
123synth_only()
124{
125  synth $1.vhdl -e > syn_$1.vhdl
126}
127
128# Synthesis of a single file and analyze the result
129synth_analyze()
130{
131  synth $1.vhdl -e > syn_$1.vhdl
132  analyze syn_$1.vhdl
133}
134
135# Analyze and test $1
136# Then synthesize and test the result
137synth_tb()
138{
139  t=$1
140
141  analyze $t.vhdl tb_$t.vhdl
142  elab_simulate tb_$t
143  clean
144
145  synth $t.vhdl -e $t > syn_$t.vhdl
146  analyze syn_$t.vhdl tb_$t.vhdl
147  elab_simulate tb_$t --ieee-asserts=disable-at-0 --assert-level=error
148  clean
149}
150
151# Check if a C compiler is installed on this system
152c_compiler_is_available ()
153{
154  if [ -z $CC ]; then
155    CC="gcc"
156  fi
157  which $CC
158}
159
160# Check if a feature is present
161ghdl_has_feature ()
162{
163  "$GHDL" -r $GHDL_STD_FLAGS $GHDL_FLAGS $1 --has-feature=$2
164}
165
166ghdl_is_interpretation ()
167{
168  "$GHDL" --version | grep -q interpretation
169}
170
171# Run a program
172run ()
173{
174  echo "run $@"
175  eval $@
176}
177
178# Run a program (failure expected)
179run_failure ()
180{
181  echo "run (failure expected) $@"
182  if eval $@; then
183    echo "failure expected";
184    false;
185  fi
186}
187
188# Be sure vpi can be used
189add_vpi_path()
190{
191  if [ "$OS" = "Windows_NT" ]; then
192    # Need to put the directory containing libghdlvpi.dll in the path.
193    vpi_lib=`$GHDL --vpi-library-dir-unix`
194    echo vpi_lib: $vpi_lib
195    PATH="$PATH:$vpi_lib"
196  fi
197}
198
199# Clean the environment
200clean ()
201{
202  if [ $# -eq 0 ]; then
203    echo "Remove work library"
204    "$GHDL" --remove $GHDL_STD_FLAGS
205  else
206    case "$1" in
207      --std=*)
208        echo "Remove work library"
209        "$GHDL" --remove $1 ;;
210      *)
211        echo "Remove $1 library"
212        "$GHDL" --remove $GHDL_STD_FLAGS --work=$1 ;;
213    esac
214  fi
215}
216