1# Copyright (c) 2023 Nordic Semiconductor ASA
2# SPDX-License-Identifier: Apache-2.0
3
4# FindBabbleSim module for locating BabbleSim
5#
6# The module defines the following variables:
7#
8# 'BSIM_COMPONENTS_PATH'
9# Path to the BabbleSim components source folder
10#
11# 'BSIM_OUT_PATH'
12# Path to the BabbleSim build output root path (under which libraries and binaries) are kept
13#
14# We first try to find it via the environment variables BSIM_OUT_PATH and BSIM_COMPONENTS_PATH.
15# If these are not set, as a fallback we attempt to find it through west, in case the user
16# fetched babblesim using the manifest.
17# Note that what we find through the environment variables is meant to have precedence.
18#
19# If BabbleSim cannot be found we error right away with a message trying to guide users
20
21zephyr_get(BSIM_COMPONENTS_PATH)
22zephyr_get(BSIM_OUT_PATH)
23
24if((DEFINED WEST) AND (NOT DEFINED BSIM_COMPONENTS_PATH) AND (NOT DEFINED BSIM_OUT_PATH))
25  # Let's ask west for the bsim_project existence and its path
26  execute_process(COMMAND ${WEST}
27    status babblesim_base
28    OUTPUT_QUIET
29    ERROR_QUIET
30    RESULT_VARIABLE ret_val1
31  )
32  execute_process(COMMAND ${WEST}
33    list babblesim_base -f {posixpath}
34    OUTPUT_VARIABLE BSIM_BASE_PATH
35    ERROR_QUIET
36    RESULT_VARIABLE ret_val2
37  )
38  if(NOT (${ret_val1} OR ${ret_val2}))
39    string(STRIP ${BSIM_BASE_PATH} BSIM_COMPONENTS_PATH)
40    get_filename_component(BSIM_OUT_PATH ${BSIM_COMPONENTS_PATH}/.. ABSOLUTE)
41  endif()
42endif()
43
44message(STATUS "Using BSIM from BSIM_COMPONENTS_PATH=${BSIM_COMPONENTS_PATH} "
45  "BSIM_OUT_PATH=${BSIM_OUT_PATH}")
46
47if((NOT DEFINED BSIM_COMPONENTS_PATH) OR (NOT DEFINED BSIM_OUT_PATH))
48  message(FATAL_ERROR
49    "This board requires the BabbleSim simulator. Please either\n"
50    " a) Enable the west babblesim group with\n"
51    "    west config manifest.group-filter +babblesim && west update\n"
52    "    and build it with\n"
53    "    cd ${ZEPHYR_BASE}/../tools/bsim\n"
54    "     make everything -j 8\n"
55    "    OR\n"
56    " b) set the environment variable BSIM_COMPONENTS_PATH to point to your own bsim installation\n"
57    "    `components/` folder, *and* BSIM_OUT_PATH to point to the folder where the simulator\n"
58    "    is compiled to.\n"
59    "    More information can be found in https://babblesim.github.io/folder_structure_and_env.html"
60  )
61endif()
62
63# Many apps cmake files (in and out of tree) expect these environment variables. Lets provide them:
64set(ENV{BSIM_COMPONENTS_PATH} ${BSIM_COMPONENTS_PATH})
65set(ENV{BSIM_OUT_PATH} ${BSIM_OUT_PATH})
66
67# Let's check that it is new enough and built,
68# so we provide better information to users than a compile error:
69
70# Internal function to print a descriptive error message
71# Do NOT use it outside of this module. It uses variables internal to it
72function(bsim_handle_not_built_error)
73  get_filename_component(BSIM_ROOT_PATH ${BSIM_COMPONENTS_PATH}/.. ABSOLUTE)
74  message(FATAL_ERROR
75    "Please ensure you have the latest babblesim and rebuild it.\n"
76    "If you got it from Zephyr's manifest, you can do:\n"
77    " west update\n"
78    " cd ${BSIM_ROOT_PATH}; make everything -j 8"
79  )
80endfunction(bsim_handle_not_built_error)
81
82# Internal function to check that a bsim component is at least the desired version
83# Do NOT use it outside of this module. It uses variables internal to it
84function(bsim_check_build_version bs_comp req_comp_ver)
85  set(version_file ${BSIM_OUT_PATH}/lib/${bs_comp}.version)
86  if(EXISTS ${version_file})
87    file(READ ${version_file} found_version)
88    string(STRIP ${found_version} found_version)
89  else()
90    message(WARNING "BabbleSim was never compiled (${version_file} not found)")
91    bsim_handle_not_built_error()
92  endif()
93
94  if(found_version VERSION_LESS req_comp_ver)
95    message(WARNING "Built ${bs_comp} version = ${found_version} < ${req_comp_ver} "
96      "(required version)")
97    bsim_handle_not_built_error()
98  endif()
99endfunction(bsim_check_build_version)
100
101bsim_check_build_version(bs_2G4_phy_v1 2.4)
102