1# Copyright 2018 Oticon A/S
2# SPDX-License-Identifier: Apache-2.0
3
4source ${ZEPHYR_BASE}/tests/bsim/sh_common.source
5
6: "${BSIM_COMPONENTS_PATH:?BSIM_COMPONENTS_PATH must be defined}"
7
8WORK_DIR="${WORK_DIR:-${ZEPHYR_BASE}/bsim_out}"
9
10function print_error_info(){
11  echo -e "\033[0;31mFailure building ${app} ${conf_file} for ${BOARD}\033[0m\n\
12  You can rebuild it with\n\
13  "${cmake_cmd[@]@Q}" && ninja ${ninja_args}"
14}
15
16function _compile(){
17  : "${app:?app must be defined}"
18
19  local app_root="${app_root:-${ZEPHYR_BASE}}"
20  local BOARD_ROOT="${BOARD_ROOT:-${ZEPHYR_BASE}}"
21  local conf_file="${conf_file:-prj.conf}"
22  local extra_conf_file="${extra_conf_file:-""}"
23  local conf_overlay="${conf_overlay:-""}"
24  local snippet="${snippet:-""}"
25
26  default_cmake_args=(-DCONFIG_COVERAGE=y -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
27    -DCONFIG_COMPILER_WARNINGS_AS_ERRORS=y -DCONFIG_ASSERT=y)
28  local cmake_args=(${cmake_args:-"${default_cmake_args[@]}"})
29  local cmake_extra_args=(${cmake_extra_args:-""})
30  local ninja_args="${ninja_args:-""}"
31  local cc_flags="${cc_flags:-""}"
32
33  if [ "${conf_overlay}" ]; then
34    overlay_basename="${conf_overlay##*/}"
35    overlay_file="${overlay_basename//;/_}"
36    local exe_basename="${exe_name:-bs_${BOARD}_${app}_${conf_file}_${overlay_file}}"
37  else
38    local exe_basename="${exe_name:-bs_${BOARD}_${app}_${conf_file}}"
39  fi
40
41  local exe_basename=$(echo ${exe_basename} | tr \"/\\.\; _ )
42  local exe_name=${BSIM_OUT_PATH}/bin/$exe_basename
43  local map_file_name=${exe_name}.Tsymbols
44
45  local this_dir=${WORK_DIR}/${app}/${exe_basename}
46  local app_absolute=$(realpath "${app_root}/${app}")
47  if [[ "${app_absolute}" != "${app_root}"* ]]; then
48    this_dir=${WORK_DIR}/${app_absolute}/${exe_basename}
49  fi
50
51  local modules_arg="${ZEPHYR_MODULES:+-DZEPHYR_MODULES=${ZEPHYR_MODULES}}"
52
53  echo "Building $exe_name"
54
55  local ret=0
56
57  local cmake_cmd=(cmake -GNinja -DBOARD_ROOT=${BOARD_ROOT} -DBOARD=${BOARD})
58  if [ $conf_file != "prj.conf" ]; then
59    local cmake_cmd+=( -DCONF_FILE=${conf_file})
60  fi
61  orifs="$IFS"; IFS=
62  local cmake_cmd+=( -DOVERLAY_CONFIG="${conf_overlay}" \
63            -DEXTRA_CONF_FILE="${extra_conf_file}" \
64            -DSNIPPET="${snippet}" \
65            ${modules_arg} \
66            "${cmake_args[@]}" ${cc_flags:+-DCMAKE_C_FLAGS=${cc_flags}} "${cmake_extra_args[@]}")
67  if [ -v sysbuild ]; then
68    local cmake_cmd+=( -DAPP_DIR=${app_root}/${app} ${ZEPHYR_BASE}/share/sysbuild/)
69  else
70    local cmake_cmd+=( ${app_root}/${app})
71  fi
72  IFS="$orifs"
73
74  # Set INCR_BUILD when calling to only do an incremental build
75  if [ ! -v INCR_BUILD ] || [ ! -d "${this_dir}" ]; then
76      [ -d "${this_dir}" ] && rm "${this_dir}" -rf
77      mkdir -p "${this_dir}" && cd "${this_dir}"
78      "${cmake_cmd[@]}" &> cmake.out || \
79      { ret="$?"; print_error_info ; cat cmake.out && return $ret; }
80  else
81      cd "${this_dir}"
82  fi
83  ninja ${ninja_args} &> ninja.out || \
84  { ret="$?"; print_error_info ; cat ninja.out && return $ret; }
85  cp ${this_dir}/zephyr/zephyr.exe ${exe_name}
86
87  nm ${exe_name} | grep -v " [U|w] " | sort | cut -d" " -f1,3 > ${map_file_name}
88  sed -i "1i $(wc -l ${map_file_name} | cut -d" " -f1)" ${map_file_name}
89}
90
91function compile(){
92  run_in_background _compile
93}
94