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  execute_process(COMMAND ${WEST}
32    list babblesim_base -f {posixpath}
33    OUTPUT_VARIABLE BSIM_BASE_PATH
34    ERROR_QUIET
35    RESULT_VARIABLE ret_val2)
36  if (NOT (${ret_val1} OR ${ret_val2}))
37    string(STRIP ${BSIM_BASE_PATH} BSIM_COMPONENTS_PATH)
38    get_filename_component(BSIM_OUT_PATH ${BSIM_COMPONENTS_PATH}/.. ABSOLUTE)
39  endif()
40endif()
41
42message(STATUS "Using BSIM from BSIM_COMPONENTS_PATH=${BSIM_COMPONENTS_PATH}\
43 BSIM_OUT_PATH=${BSIM_OUT_PATH}")
44
45if ((NOT DEFINED BSIM_COMPONENTS_PATH) OR (NOT DEFINED BSIM_OUT_PATH))
46  message(FATAL_ERROR "This board requires the BabbleSim simulator. Please either\n\
47  a) Enable the west babblesim group with\n\
48     west config manifest.group-filter +babblesim && west update\n\
49     and build it with\n\
50     cd ${ZEPHYR_BASE}/../tools/bsim\n\
51      make everything -j 8\n\
52     OR\n\
53  b) set the environment variable BSIM_COMPONENTS_PATH to point to your own bsim installation\n\
54     `components/` folder, *and* BSIM_OUT_PATH to point to the folder where the simulator\n\
55     is compiled to.\n\
56     More information can be found in https://babblesim.github.io/folder_structure_and_env.html"
57  )
58endif()
59
60#Many apps cmake files (in and out of tree) expect these environment variables. Lets provide them:
61set(ENV{BSIM_COMPONENTS_PATH} ${BSIM_COMPONENTS_PATH})
62set(ENV{BSIM_OUT_PATH} ${BSIM_OUT_PATH})
63
64# Let's check that it is new enough and built,
65# so we provide better information to users than a compile error:
66
67# Internal function to print a descriptive error message
68# Do NOT use it outside of this module. It uses variables internal to it
69function(bsim_handle_not_built_error)
70  get_filename_component(BSIM_ROOT_PATH ${BSIM_COMPONENTS_PATH}/.. ABSOLUTE)
71  message(FATAL_ERROR "Please ensure you have the latest babblesim and rebuild it."
72          "If you got it from Zephyr's manifest, you can do:\n\
73 west update\n\
74 cd ${BSIM_ROOT_PATH}; make everything -j 8\n"
75  )
76endfunction(bsim_handle_not_built_error)
77
78# Internal function to check that a bsim component is at least the desired version
79# Do NOT use it outside of this module. It uses variables internal to it
80function(bsim_check_build_version bs_comp req_comp_ver)
81  set(version_file ${BSIM_OUT_PATH}/lib/${bs_comp}.version)
82  if (EXISTS ${version_file})
83    file(READ ${version_file} found_version)
84    string(STRIP ${found_version} found_version)
85  else()
86    message(WARNING "BabbleSim was never compiled (${version_file} not found)")
87    bsim_handle_not_built_error()
88  endif()
89
90  if (found_version VERSION_LESS req_comp_ver)
91    message(WARNING
92            "Built ${bs_comp} version = ${found_version} < ${req_comp_ver} (required version)")
93    bsim_handle_not_built_error()
94  endif()
95endfunction(bsim_check_build_version)
96
97bsim_check_build_version(bs_2G4_phy_v1 2.4)
98