Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1 | # This bbclass implements device tree compliation for user provided device tree |
| 2 | # sources. The compilation of the device tree sources is the same as the kernel |
| 3 | # device tree compilation process, this includes being able to include sources |
| 4 | # from the kernel such as soc dtsi files or header files such as gpio.h. In |
| 5 | # addition to device trees this bbclass also handles compilation of device tree |
| 6 | # overlays. |
| 7 | # |
| 8 | # The output of this class behaves similar to how kernel-devicetree.bbclass |
| 9 | # operates in that the output files are installed into /boot/devicetree. |
| 10 | # However this class on purpose separates the deployed device trees into the |
| 11 | # 'devicetree' subdirectory. This prevents clashes with the kernel-devicetree |
| 12 | # output. Additionally the device trees are populated into the sysroot for |
| 13 | # access via the sysroot from within other recipes. |
| 14 | |
| 15 | SECTION ?= "bsp" |
| 16 | |
| 17 | # The default inclusion of kernel device tree includes and headers means that |
| 18 | # device trees built with them are at least GPLv2 (and in some cases dual |
| 19 | # licensed). Default to GPLv2 if the recipe does not specify a license. |
| 20 | LICENSE ?= "GPLv2" |
| 21 | LIC_FILES_CHKSUM ?= "file://${COMMON_LICENSE_DIR}/GPL-2.0;md5=801f80980d171dd6425610833a22dbe6" |
| 22 | |
| 23 | INHIBIT_DEFAULT_DEPS = "1" |
| 24 | DEPENDS += "dtc-native" |
| 25 | |
| 26 | inherit deploy kernel-arch |
| 27 | |
| 28 | COMPATIBLE_MACHINE ?= "^$" |
| 29 | |
| 30 | PACKAGE_ARCH = "${MACHINE_ARCH}" |
| 31 | |
| 32 | SYSROOT_DIRS += "/boot/devicetree" |
| 33 | FILES_${PN} = "/boot/devicetree/*.dtb /boot/devicetree/*.dtbo" |
| 34 | |
| 35 | S = "${WORKDIR}" |
| 36 | B = "${WORKDIR}/build" |
| 37 | |
| 38 | # Default kernel includes, these represent what are normally used for in-kernel |
| 39 | # sources. |
| 40 | KERNEL_INCLUDE ??= " \ |
| 41 | ${STAGING_KERNEL_DIR}/arch/${ARCH}/boot/dts \ |
| 42 | ${STAGING_KERNEL_DIR}/arch/${ARCH}/boot/dts/* \ |
| 43 | ${STAGING_KERNEL_DIR}/scripts/dtc/include-prefixes \ |
| 44 | " |
| 45 | |
| 46 | DT_INCLUDE[doc] = "Search paths to be made available to both the device tree compiler and preprocessor for inclusion." |
| 47 | DT_INCLUDE ?= "${DT_FILES_PATH} ${KERNEL_INCLUDE}" |
| 48 | DT_FILES_PATH[doc] = "Defaults to source directory, can be used to select dts files that are not in source (e.g. generated)." |
| 49 | DT_FILES_PATH ?= "${S}" |
| 50 | |
| 51 | DT_PADDING_SIZE[doc] = "Size of padding on the device tree blob, used as extra space typically for additional properties during boot." |
| 52 | DT_PADDING_SIZE ??= "0x3000" |
| 53 | DT_RESERVED_MAP[doc] = "Number of reserved map entires." |
| 54 | DT_RESERVED_MAP ??= "8" |
| 55 | DT_BOOT_CPU[doc] = "The boot cpu, defaults to 0" |
| 56 | DT_BOOT_CPU ??= "0" |
| 57 | |
| 58 | DTC_FLAGS ?= "-R ${DT_RESERVED_MAP} -b ${DT_BOOT_CPU}" |
| 59 | DTC_PPFLAGS ?= "-nostdinc -undef -D__DTS__ -x assembler-with-cpp" |
| 60 | DTC_BFLAGS ?= "-p ${DT_PADDING_SIZE}" |
| 61 | DTC_OFLAGS ?= "-p 0 -@ -H epapr" |
| 62 | |
| 63 | python () { |
| 64 | if d.getVar("KERNEL_INCLUDE"): |
| 65 | # auto add dependency on kernel tree, but only if kernel include paths |
| 66 | # are specified. |
| 67 | d.appendVarFlag("do_compile", "depends", " virtual/kernel:do_configure") |
| 68 | } |
| 69 | |
| 70 | def expand_includes(varname, d): |
| 71 | import glob |
| 72 | includes = set() |
| 73 | # expand all includes with glob |
| 74 | for i in (d.getVar(varname) or "").split(): |
| 75 | for g in glob.glob(i): |
| 76 | if os.path.isdir(g): # only add directories to include path |
| 77 | includes.add(g) |
| 78 | return includes |
| 79 | |
| 80 | def devicetree_source_is_overlay(path): |
| 81 | # determine if a dts file is an overlay by checking if it uses "/plugin/;" |
| 82 | with open(path, "r") as f: |
| 83 | for i in f: |
| 84 | if i.startswith("/plugin/;"): |
| 85 | return True |
| 86 | return False |
| 87 | |
| 88 | def devicetree_compile(dtspath, includes, d): |
| 89 | import subprocess |
| 90 | dts = os.path.basename(dtspath) |
| 91 | dtname = os.path.splitext(dts)[0] |
| 92 | bb.note("Processing {0} [{1}]".format(dtname, dts)) |
| 93 | |
| 94 | # preprocess |
| 95 | ppargs = d.getVar("BUILD_CPP").split() |
| 96 | ppargs += (d.getVar("DTC_PPFLAGS") or "").split() |
| 97 | for i in includes: |
| 98 | ppargs.append("-I{0}".format(i)) |
| 99 | ppargs += ["-o", "{0}.pp".format(dts), dtspath] |
| 100 | bb.note("Running {0}".format(" ".join(ppargs))) |
| 101 | subprocess.run(ppargs, check = True) |
| 102 | |
| 103 | # determine if the file is an overlay or not (using the preprocessed file) |
| 104 | isoverlay = devicetree_source_is_overlay("{0}.pp".format(dts)) |
| 105 | |
| 106 | # compile |
| 107 | dtcargs = ["dtc"] + (d.getVar("DTC_FLAGS") or "").split() |
| 108 | if isoverlay: |
| 109 | dtcargs += (d.getVar("DTC_OFLAGS") or "").split() |
| 110 | else: |
| 111 | dtcargs += (d.getVar("DTC_BFLAGS") or "").split() |
| 112 | for i in includes: |
| 113 | dtcargs += ["-i", i] |
| 114 | dtcargs += ["-o", "{0}.{1}".format(dtname, "dtbo" if isoverlay else "dtb")] |
| 115 | dtcargs += ["-I", "dts", "-O", "dtb", "{0}.pp".format(dts)] |
| 116 | bb.note("Running {0}".format(" ".join(dtcargs))) |
| 117 | subprocess.run(dtcargs, check = True) |
| 118 | |
| 119 | python devicetree_do_compile() { |
| 120 | includes = expand_includes("DT_INCLUDE", d) |
| 121 | listpath = d.getVar("DT_FILES_PATH") |
| 122 | for dts in os.listdir(listpath): |
| 123 | if not dts.endswith(".dts"): |
| 124 | continue # skip non-.dts files |
| 125 | dtspath = os.path.join(listpath, dts) |
| 126 | devicetree_compile(dtspath, includes, d) |
| 127 | } |
| 128 | |
| 129 | devicetree_do_install() { |
| 130 | for DTB_FILE in `ls *.dtb *.dtbo`; do |
| 131 | install -Dm 0644 ${B}/${DTB_FILE} ${D}/boot/devicetree/${DTB_FILE} |
| 132 | done |
| 133 | } |
| 134 | |
| 135 | devicetree_do_deploy() { |
| 136 | for DTB_FILE in `ls *.dtb *.dtbo`; do |
| 137 | install -Dm 0644 ${B}/${DTB_FILE} ${DEPLOYDIR}/devicetree/${DTB_FILE} |
| 138 | done |
| 139 | } |
| 140 | addtask deploy before do_build after do_install |
| 141 | |
| 142 | EXPORT_FUNCTIONS do_compile do_install do_deploy |
| 143 | |