1#-------------------------------------------------------------------------------
2# Copyright (c) 2022-2024, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8# Use libfdt path from the environment if defined
9if (DEFINED ENV{TS_LIBFDT_PATH})
10	set(LIBFDT_PATH $ENV{TS_LIBFDT_PATH} CACHE PATH "Location of libfdt" FORCE)
11endif()
12
13# Otherwise clone the dtc repo (which contains libfdt)
14set(DTC_URL "https://github.com/dgibson/dtc" CACHE STRING "dtc repository URL")
15set(DTC_REFSPEC "v1.6.1" CACHE STRING "dtc git refspec")
16set(DTC_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/dtc-src" CACHE PATH "Location of dtc source")
17
18set(GIT_OPTIONS
19	GIT_REPOSITORY ${DTC_URL}
20	GIT_TAG ${DTC_REFSPEC}
21)
22
23include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
24LazyFetch_MakeAvailable(
25	DEP_NAME dtc
26	FETCH_OPTIONS "${GIT_OPTIONS}"
27	SOURCE_DIR ${DTC_SOURCE_DIR}
28)
29
30find_path(LIBFDT_PATH
31	NAMES fdt.c
32	PATHS ${DTC_SOURCE_DIR}/libfdt
33	NO_DEFAULT_PATH
34	REQUIRED
35	DOC "Location of libfdt"
36)
37
38# Add libfdt source files to the target
39if (NOT DEFINED TGT)
40	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
41endif()
42
43target_sources(${TGT} PRIVATE
44	"${LIBFDT_PATH}/fdt.c"
45	"${LIBFDT_PATH}/fdt_ro.c"
46	"${LIBFDT_PATH}/fdt_wip.c"
47	"${LIBFDT_PATH}/fdt_sw.c"
48	"${LIBFDT_PATH}/fdt_rw.c"
49	"${LIBFDT_PATH}/fdt_strerror.c"
50	"${LIBFDT_PATH}/fdt_empty_tree.c"
51	"${LIBFDT_PATH}/fdt_addresses.c"
52	"${LIBFDT_PATH}/fdt_overlay.c"
53	"${LIBFDT_PATH}/fdt_check.c"
54)
55
56target_include_directories(${TGT} PRIVATE ${LIBFDT_PATH})
57