1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (c) 2021, 2023 Nordic Semiconductor ASA
4
5include_guard(GLOBAL)
6include(extensions)
7
8# Finalize the value of DTS_ROOT, so we know where all our
9# DTS files, bindings, and vendor prefixes are.
10#
11# Outcome:
12# The following variables will be defined when this CMake module completes:
13#
14# - DTS_ROOT: a deduplicated list of places where devicetree
15#   implementation files (like bindings, vendor prefixes, etc.) are
16#   found
17# - DTS_ROOT_SYSTEM_INCLUDE_DIRS: set to "PATH1 PATH2 ...",
18#   with one path per potential location where C preprocessor #includes
19#   may be found for devicetree files
20#
21# Required variables:
22# None.
23#
24# Optional variables:
25# - APPLICATION_SOURCE_DIR: path to app (added to DTS_ROOT)
26# - BOARD_DIR: directory containing the board definition (added to DTS_ROOT)
27# - DTS_ROOT: initial contents may be populated here
28# - ZEPHYR_BASE: path to zephyr repository (added to DTS_ROOT)
29# - SHIELD_DIRS: paths to shield definitions (added to DTS_ROOT)
30
31# Using a function avoids polluting the parent scope unnecessarily.
32function(pre_dt_module_run)
33  # Convert relative paths to absolute paths relative to the application
34  # source directory.
35  zephyr_file(APPLICATION_ROOT DTS_ROOT)
36
37  # DTS_ROOT always includes the application directory, the board
38  # directory, shield directories, and ZEPHYR_BASE.
39  list(APPEND
40    DTS_ROOT
41    ${APPLICATION_SOURCE_DIR}
42    ${BOARD_DIR}
43    ${SHIELD_DIRS}
44    ${ZEPHYR_BASE}
45    )
46
47  # Convert the directories in DTS_ROOT to absolute paths without
48  # symlinks.
49  #
50  # DTS directories can come from multiple places. Some places, like a
51  # user's CMakeLists.txt can preserve symbolic links. Others, like
52  # scripts/zephyr_module.py --settings-out resolve them.
53  unset(real_dts_root)
54  foreach(dts_dir ${DTS_ROOT})
55    file(REAL_PATH ${dts_dir} real_dts_dir)
56    list(APPEND real_dts_root ${real_dts_dir})
57  endforeach()
58  set(DTS_ROOT ${real_dts_root})
59
60  # Finalize DTS_ROOT.
61  list(REMOVE_DUPLICATES DTS_ROOT)
62
63  if(HWMv1)
64    set(arch_include dts/${ARCH})
65  else()
66    foreach(arch ${ARCH_V2_NAME_LIST})
67      list(APPEND arch_include dts/${arch})
68    endforeach()
69  endif()
70
71  # Finalize DTS_ROOT_SYSTEM_INCLUDE_DIRS.
72  set(DTS_ROOT_SYSTEM_INCLUDE_DIRS)
73  foreach(dts_root ${DTS_ROOT})
74    foreach(dts_root_path
75        include
76        include/zephyr
77        dts/common
78        ${arch_include}
79        dts
80        )
81      get_filename_component(full_path ${dts_root}/${dts_root_path} REALPATH)
82      if(EXISTS ${full_path})
83        list(APPEND DTS_ROOT_SYSTEM_INCLUDE_DIRS ${full_path})
84      endif()
85    endforeach()
86  endforeach()
87
88  # Set output variables.
89  set(DTS_ROOT ${DTS_ROOT} PARENT_SCOPE)
90  set(DTS_ROOT_SYSTEM_INCLUDE_DIRS ${DTS_ROOT_SYSTEM_INCLUDE_DIRS} PARENT_SCOPE)
91endfunction()
92
93pre_dt_module_run()
94