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  # Finalize DTS_ROOT_SYSTEM_INCLUDE_DIRS.
66  set(DTS_ROOT_SYSTEM_INCLUDE_DIRS)
67  foreach(dts_root ${DTS_ROOT})
68    foreach(dts_root_path
69        include
70        include/zephyr
71        dts/common
72        dts/${ARCH}
73        dts
74        )
75      get_filename_component(full_path ${dts_root}/${dts_root_path} REALPATH)
76      if(EXISTS ${full_path})
77        list(APPEND DTS_ROOT_SYSTEM_INCLUDE_DIRS ${full_path})
78      endif()
79    endforeach()
80  endforeach()
81
82  # Set output variables.
83  set(DTS_ROOT ${DTS_ROOT} PARENT_SCOPE)
84  set(DTS_ROOT_SYSTEM_INCLUDE_DIRS ${DTS_ROOT_SYSTEM_INCLUDE_DIRS} PARENT_SCOPE)
85endfunction()
86
87pre_dt_module_run()
88