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