1#
2# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6cmake_minimum_required(VERSION 3.8.2)
7include_guard(GLOBAL)
8
9# This path is guaranteed to exist
10find_path(DTS_PATH "sabre.dts" PATHS ${KERNEL_PATH}/tools/dts CMAKE_FIND_ROOT_PATH_BOTH)
11# find a dts file matching platform.dts and put into a cache variable named
12# <platform>_FOUND_DTS
13function(FindDTS var platform)
14    find_file(${platform}_FOUND_DTS ${platform}.dts PATHS ${DTS_PATH} CMAKE_FIND_ROOT_PATH_BOTH)
15    if("${${platform}_FOUND_DTS}}" STREQUAL "${platform}_FOUND_DTS-NOTFOUND")
16        message(WARNING "Could not find default dts file ${PLATFORM.dts}")
17    endif()
18    set(${var} ${${platform}_FOUND_DTS} PARENT_SCOPE)
19endfunction()
20
21# generate a dtb from a dts file using the dtc tool
22function(GenDTB dts_file var)
23    # find the dtc tool
24    find_program(DTC_TOOL dtc)
25    if("${DTC_TOOL}" STREQUAL "DTC_TOOL-NOTFOUND")
26        message(FATAL_ERROR "Cannot find 'dtc' program.")
27    endif()
28    # check the dts we have been given exists
29    if(NOT EXISTS "${dts_file}")
30        message(FATAL_ERROR "Cannot find dts file ${dts_file}")
31    endif()
32    # put all the files into /dtb in the binary dir
33    set(dtb_dir "${CMAKE_BINARY_DIR}/dtb")
34    file(MAKE_DIRECTORY "${dtb_dir}")
35    # generate a file name
36    get_filename_component(FILE_NAME ${dts_file} NAME_WE)
37    set(filename "${dtb_dir}/${FILE_NAME}.dtb")
38    set(${var} ${filename} PARENT_SCOPE)
39    # now add the command to generate the dtb
40    execute_process(
41        COMMAND
42            ${DTC_TOOL} -I dts -O dtb -o ${filename} ${dts_file}
43        OUTPUT_VARIABLE output
44        ERROR_VARIABLE output
45    )
46    if(NOT EXISTS "${filename}")
47        message(FATAL_ERROR "${output}
48            failed to gen ${filename}")
49    endif()
50endfunction()
51