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
25  default_cmake_args=(-DCONFIG_COVERAGE=y -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
26    -DCONFIG_COMPILER_WARNINGS_AS_ERRORS=y -DCONFIG_ASSERT=y)
27  local cmake_args=(${cmake_args:-"${default_cmake_args[@]}"})
28  local cmake_extra_args="${cmake_extra_args:-""}"
29  local ninja_args="${ninja_args:-""}"
30  local cc_flags="${cc_flags:-""}"
31
32  if [ "${conf_overlay}" ]; then
33    overlay_basename="${conf_overlay##*/}"
34    overlay_file="${overlay_basename//;/_}"
35    local exe_basename="${exe_name:-bs_${BOARD}_${app}_${conf_file}_${overlay_file}}"
36  else
37    local exe_basename="${exe_name:-bs_${BOARD}_${app}_${conf_file}}"
38  fi
39
40  local exe_basename=$(echo ${exe_basename} | tr \"/\\.\; _ )
41  local exe_name=${BSIM_OUT_PATH}/bin/$exe_basename
42  local map_file_name=${exe_name}.Tsymbols
43
44  local this_dir=${WORK_DIR}/${app}/${exe_basename}
45
46  local modules_arg="${ZEPHYR_MODULES:+-DZEPHYR_MODULES=${ZEPHYR_MODULES}}"
47
48  echo "Building $exe_name"
49
50  local ret=0
51
52  local cmake_cmd=(cmake -GNinja -DBOARD_ROOT=${BOARD_ROOT} -DBOARD=${BOARD})
53  if [ $conf_file != "prj.conf" ]; then
54    local cmake_cmd+=( -DCONF_FILE=${conf_file})
55  fi
56  orifs="$IFS"; IFS=
57  local cmake_cmd+=( -DOVERLAY_CONFIG="${conf_overlay}" \
58            -DEXTRA_CONF_FILE="${extra_conf_file}" \
59            ${modules_arg} \
60            "${cmake_args[@]}" ${cc_flags:+-DCMAKE_C_FLAGS=${cc_flags}} ${cmake_extra_args})
61  if [ -v sysbuild ]; then
62    local cmake_cmd+=( -DAPP_DIR=${app_root}/${app} ${ZEPHYR_BASE}/share/sysbuild/)
63  else
64    local cmake_cmd+=( ${app_root}/${app})
65  fi
66  IFS="$orifs"
67
68  # Set INCR_BUILD when calling to only do an incremental build
69  if [ ! -v INCR_BUILD ] || [ ! -d "${this_dir}" ]; then
70      [ -d "${this_dir}" ] && rm "${this_dir}" -rf
71      mkdir -p "${this_dir}" && cd "${this_dir}"
72      "${cmake_cmd[@]}" &> cmake.out || \
73      { ret="$?"; print_error_info ; cat cmake.out && return $ret; }
74  else
75      cd "${this_dir}"
76  fi
77  ninja ${ninja_args} &> ninja.out || \
78  { ret="$?"; print_error_info ; cat ninja.out && return $ret; }
79  cp ${this_dir}/zephyr/zephyr.exe ${exe_name}
80
81  nm ${exe_name} | grep -v " [U|w] " | sort | cut -d" " -f1,3 > ${map_file_name}
82  sed -i "1i $(wc -l ${map_file_name} | cut -d" " -f1)" ${map_file_name}
83}
84
85function compile(){
86  run_in_background _compile
87}
88