-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDeviceTree.cmake
81 lines (70 loc) · 3.07 KB
/
DeviceTree.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
##############################################################################
# #
# Copyright (C) 2024 MachineWare GmbH #
# All Rights Reserved #
# #
# This is work is licensed under the terms described in the LICENSE file #
# found in the root directory of this source tree. #
# #
##############################################################################
# This will preprocess and compile a devicetree using cmake, syntax is:
#
# compile_dts(<name> SOURCE <source> [INCLUDES <dirs>] [DEFINES <defines>])
#
# Example:
# compile_dts(mytree.dtb SOURCE src/mytree.dts INCLUDES kernel/headers DEFINES FOO=BAR)
# install(${CMAKE_CURRENT_BINARY_DIR}/mytree.dtb DESTINATION firmware/)
function(compile_dts output)
cmake_parse_arguments(DTS "" "SOURCE" "INCLUDES;DEFINES" ${ARGN})
if(NOT output)
message(FATAL_ERROR "OUTPUT argument is required")
endif()
if(NOT DTS_SOURCE)
message(FATAL_ERROR "SOURCE argument is required")
endif()
if(NOT IS_ABSOLUTE DTS_SOURCE)
set(DTS_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/${DTS_SOURCE})
endif()
find_program(DTC_COMPILER NAMES dtc dtc.exe)
if(NOT DTC_COMPILER)
message(FATAL_ERROR "DTC compiler not found. Please install dtc.")
endif()
# collect define flags
set(define_flags "")
foreach(def IN LISTS DTS_DEFINES)
list(APPEND define_flags -D${def})
endforeach()
# collect include directories
set(include_flags "")
foreach(dir IN LISTS DTS_INCLUDES)
if(NOT IS_ABSOLUTE dir)
set(dir "${CMAKE_CURRENT_SOURCE_DIR}/${dir}")
endif()
list(APPEND include_flags -I${dir})
endforeach()
# collect all device tree includes
set(include_files "")
foreach(dir IN LISTS DTS_INCLUDES)
file(GLOB_RECURSE headers "${dir}/*.dtsi")
list(APPEND include_files ${headers})
endforeach()
# find a compiler we can use for preprocessing
if(MSVC)
set(preprocessor ${CMAKE_C_COMPILER})
set(preprocessor_flags /P /EP /TC /D__DTS__ ${include_flags} ${define_flags})
set(preprocessor_output "${output}.i")
else()
set(preprocessor ${CMAKE_C_COMPILER})
set(preprocessor_flags -E -nostdinc -undef -D__DTS__ -x assembler-with-cpp ${include_flags} ${define_flags})
set(preprocessor_output "${output}.pp")
endif()
add_custom_command(
OUTPUT ${output}
COMMAND ${preprocessor} ${preprocessor_flags} ${DTS_SOURCE} -o ${preprocessor_output}
COMMAND ${DTC_COMPILER} -I dts -O dtb -o ${output} ${preprocessor_output}
DEPENDS ${DTS_SOURCE} ${include_files}
COMMENT "Compiling devicetree ${output}"
VERBATIM
)
add_custom_target(${output}_target ALL DEPENDS ${output})
endfunction()