Hog Hog2025.2
Init.sh
Go to the documentation of this file.
1 #!/usr/bin/env bash
2 # Copyright 2018-2025 The University of Birmingham
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 ## @file Init.sh
17 # shellcheck source=Other/CommonFunctions.sh
18 . $(dirname "$0")/Other/CommonFunctions.sh
19 
20 ## @fn help_message
21 #
22 # @brief Prints an help message
23 #
24 # The help message contains both the options available for the first line of the tcl, both the command usage
25 # This function uses echo to print to screen
26 #
27 # @param[in] $1 the invoked command
28 #
29 function help_message() {
30  echo
31  echo " Hog - Initialise repository"
32  echo " ---------------------------"
33  echo " Initialise your Hog-handled firmware repository"
34  echo " - (optional) Compile questasim/modelsim/riviera libraries (if questasim executable is found)"
35  echo " - (optional) Create vivado projects (if vivado executable is found)"
36  echo
37  exit 0
38 }
39 
40 ## @fn init
41 #
42 # @brief main function, initialize the repository by compiling simulation libraries and creating projects
43 #
44 #
45 function init() {
46 
47  local OLD_DIR
48  OLD_DIR=$(pwd)
49  local DIR
50  DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
51 
52  if [ "$1" == "-h" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ] || [ "$1" == "-H" ]; then
53  help_message "$0"
54  fi
55 
56  cd "${DIR}" || exit
57 
58  COMPILER_FOUND=false
59 
60  ##! The script checks if Vivado is installed and set up on the shell.
61  ##! NOTE that these checks are performed using 'command -v '
62  if [ "$(command -v vivado)" ]; then
63  COMPILER_FOUND=true
64  local VIVADO
65  VIVADO=$(command -v vivado)
66  ##! If Vivado is installed it checks if vsim command is defined (Questasim or Modelsim is installed and set-up in the shell).
67  ##! NOTE that these checks are performed using 'command -v '
68  echo
69  ##! Ask the user if he wants to compile the simulation libraries
70  ##! NOTE use read to grab user input
71  ##! NOTE if the user input contains Y or y then is accepted as yes
72  read -p " Do you want to compile the simulation libraries for Vivado (this might take some time)? " -n 1 -r
73  echo
74  if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
75  echo "Please select the target simulator"
76  printf "1. Questa Advanced Simulator \n2. ModelSim Simulator \n3. Riviera-PRO Simulator \n4. Incisive Enterprise Simulator (IES) \n5. Xcelium Parallel Simulator \n6. Verilog Compiler Simulator (VCS)"
77  read -p " " -n 1 -r
78  echo
79  if [ "${REPLY}" -le 6 ] && [ "${REPLY}" -gt 0 ]; then
80  case $REPLY in
81  1)
82  SIMULATOR=questa
83  ;;
84  2)
85  SIMULATOR=modelsim
86  ;;
87  3)
88  SIMULATOR=riviera
89  ;;
90  4)
91  SIMULATOR=ies
92  ;;
93  5)
94  SIMULATOR=xcelium
95  ;;
96  6)
97  SIMULATOR=vcs
98  ;;
99  esac
100 
101  echo "Where do you want the simulation libraries to be saved? (skip for default: SimulationLib)"
102  read -r SIMDIR
103  echo
104  if [[ $SIMDIR == "" ]]; then
105  SIMDIR="SimulationLib"
106  fi
107 
108  Msg Info "Compiling $SIMULATOR libraries into $SIMDIR..."
109  "${VIVADO}" -mode batch -notrace -source ./Tcl/utils/compile_simlib.tcl -tclargs -simulator $SIMULATOR -output_dir $SIMDIR
110  rm -f ./Tcl/.cxl.*
111  rm -f ./Tcl/compile_simlib.log
112  rm -f ./Tcl/*.ini
113  else
114  echo "Chosen simulator is not valid. No simulation libraries will be compiled..."
115  fi
116 
117  fi
118  fi
119 
120  # Repeat compilation using Quartus
121  if [ "$(command -v quartus_sh)" ]; then
122  COMPILER_FOUND=true
123  local QUARTUS
124  QUARTUS=$(command -v quartus_sh)
125  ##! If Quartus is installed it checks if vsim command is defined (Questasim or Modelsim is installed and set-up in the shell).
126  ##! NOTE that these checks are performed using 'command -v '
127  if [ "$(command -v vsim)" ]; then
128  echo
129  ##! If Questasim or Modelsim is installed ask user if he wants to compile
130  ##! NOTE use read to grab user input
131  ##! NOTE if the user input contains Y or y then is accepted as yes
132  read -p " Do you want to compile Questasim libraries for Quartus (this might take some time)? " -n 1 -r
133  echo
134  if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
135  Msg Info "Compiling Questasim libraries into SimulationLib..."
136  mkdir -p "${OLD_DIR}/SimulationLib_quartus/verilog/"
137  mkdir -p "${OLD_DIR}/SimulationLib_quartus/vhdl/"
138  "${QUARTUS}" --simlib_comp -suppress_messages -tool questasim -language verilog -family all -directory "${OLD_DIR}/SimulationLib_quartus/verilog/"
139  "${QUARTUS}" --simlib_comp -suppress_messages -tool questasim -language vhdl -family all -directory "${OLD_DIR}/SimulationLib_quartus/vhdl/"
140  else
141  read -p " Do you want to compile Modelsim libraries for Quartus (this might take some time)? " -n 1 -r
142  echo
143  if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
144  Msg Info "Compiling Modelsim libraries into SimulationLib..."
145  mkdir -p "${OLD_DIR}/SimulationLib_quartus/verilog/"
146  mkdir -p "${OLD_DIR}/SimulationLib_quartus/vhdl/"
147  "${QUARTUS}" --simlib_comp -suppress_messages -tool modelsim -language verilog -family all -directory "${OLD_DIR}/SimulationLib_quartus/verilog/"
148  "${QUARTUS}" --simlib_comp -suppress_messages -tool modelsim -language vhdl -family all -directory "${OLD_DIR}/SimulationLib_quartus/vhdl/"
149  fi
150  fi
151  else
152  Msg Warning "No modelsim executable found, will not compile libraries"
153  fi
154  fi
155 
156  if ! $COMPILER_FOUND; then
157  Msg Warning "No Vivado or Quartus executable found!"
158  fi
159 
160  ##! Scan for existing Vivado projects and ask user to automatically create listFiles
161  ##! NOTE projects already in Projects directory have already a Hog structure, ignore them
162  ##! NOTE use read to grab user input
163  ##! NOTE if the user input contains Y or y then is accepted as yes
164 
165  Vivado_prjs=$(find "$DIR/.." -path "$DIR/../Projects" -prune -false -o -name "*.xpr")
166 
167  for Vivado_prj in $Vivado_prjs; do
168  echo
169  Vivado_prj_base=$(basename "$Vivado_prj")
170  read -p " Found existing Vivado project $Vivado_prj_base. Do you want to convert it to a Hog compatible project? (creates listfiles and hog.conf) " -n 1 -r
171  echo # (optional) move to a new line
172  if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
173  Force=""
174  if [ -d "$DIR/../Top/${Vivado_prj_base%.*}" ]; then
175  read -p " Directory \"Top/${Vivado_prj_base%.*}\" exists. Do you want to overwrite it? " -n 1 -r
176  echo # (optional) move to a new line
177  if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
178  Force=" -force "
179  else
180  continue
181  fi
182  fi
183  vivado -mode batch -notrace -source $DIR/Tcl/utils/check_list_files.tcl $Vivado_prj -tclargs -recreate $Force -recreate_conf
184  fi
185  done
186 
187  ##! Ask user if he wants to create all projects in the repository
188  ##! NOTE use read to grab user input
189  ##! NOTE if the user input contains Y or y then is accepted as yes
190  echo
191  read -p " Do you want to create projects now (can be done later with CreateProject.sh)? " -n 1 -r
192  echo # (optional) move to a new line
193  if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
194  cd ..
195  proj=$(search_projects Top)
196  Msg Info "Creating projects for: $proj..."
197  for f in $proj; do
198  Msg Info "Creating Vivado project: $f..."
199  ./Hog/Do C "${f}"
200  done
201  fi
202 
203  ##! Ask user if he wants to add custom Vivado GUI button to automatically update listfiles
204  ##! NOTE use read to grab user input
205  ##! NOTE if the user input contains Y or y then is accepted as yes
206  if [ "$(command -v vivado)" ]; then
207  echo
208  read -p " Do you want to add four buttons to the Vivado GUI to check and update the list files and the project hog.conf file automatically? " -n 1 -r
209  echo
210  echo
211  if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
212  vivado -mode batch -notrace -source $DIR/Tcl/utils/add_hog_custom_button.tcl
213  fi
214  fi
215 
216  ##! Check if hog tags tag exist, and if not ask user if they want to create v0.0.1
217  cd "$DIR/.." || exit
218  if git describe --match "v*.*.*" >/dev/null 2>&1; then
219  Msg Info "Repository contains Hog-compatible tags."
220  else
221  read -p " Your repository does not have Hog-compatible tags, do you wish to create an initial tag v0.0.1 now?" -n 1 -r
222  echo # (optional) move to a new line
223  if [[ "${REPLY}" =~ ^[Yy]$ ]]; then
224  git tag v0.0.1 -m "First Hog tag"
225  fi
226  fi
227 
228  Msg Info "All done."
229  cd "${OLD_DIR}" || exit
230 }
231 
232 
233 
234 function HogInitFunc(){
235  # init $@
236  # echo "HogInitFunc ($*)"
237  init "$@"
238  exit 0
239 }
240 
241 if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
242  print_hog "$(dirname "$0")"
243  init "$@"
244 fi
245