Merge pull request #251898 from ktrinh-anduril/ktrinh/improve-devicetree-infra

authored by Ryan Lahfa and committed by GitHub 5fa3ea86 91b16f57

+71 -20
+47 -19
nixos/modules/hardware/device-tree.nix
··· 66 66 }; 67 67 68 68 filterDTBs = src: if cfg.filter == null 69 - then "${src}/dtbs" 69 + then src 70 70 else 71 71 pkgs.runCommand "dtbs-filtered" {} '' 72 72 mkdir -p $out 73 - cd ${src}/dtbs 73 + cd ${src} 74 74 find . -type f -name '${cfg.filter}' -print0 \ 75 75 | xargs -0 cp -v --no-preserve=mode --target-directory $out --parents 76 76 ''; 77 77 78 - filteredDTBs = filterDTBs cfg.kernelPackage; 79 - 80 - # Compile single Device Tree overlay source 81 - # file (.dts) into its compiled variant (.dtbo) 82 - compileDTS = name: f: pkgs.callPackage({ stdenv, dtc }: stdenv.mkDerivation { 83 - name = "${name}-dtbo"; 84 - 85 - nativeBuildInputs = [ dtc ]; 86 - 87 - buildCommand = '' 88 - $CC -E -nostdinc -I${getDev cfg.kernelPackage}/lib/modules/${cfg.kernelPackage.modDirVersion}/source/scripts/dtc/include-prefixes -undef -D__DTS__ -x assembler-with-cpp ${f} | \ 89 - dtc -I dts -O dtb -@ -o $out 90 - ''; 91 - }) {}; 78 + filteredDTBs = filterDTBs cfg.dtbSource; 92 79 93 80 # Fill in `dtboFile` for each overlay if not set already. 94 81 # Existence of one of these is guarded by assertion below 95 82 withDTBOs = xs: flip map xs (o: o // { dtboFile = 83 + let 84 + includePaths = ["${getDev cfg.kernelPackage}/lib/modules/${cfg.kernelPackage.modDirVersion}/source/scripts/dtc/include-prefixes"] ++ cfg.dtboBuildExtraIncludePaths; 85 + extraPreprocessorFlags = cfg.dtboBuildExtraPreprocessorFlags; 86 + in 96 87 if o.dtboFile == null then 97 - if o.dtsFile != null then compileDTS o.name o.dtsFile 98 - else compileDTS o.name (pkgs.writeText "dts" o.dtsText) 88 + let 89 + dtsFile = if o.dtsFile == null then (pkgs.writeText "dts" o.dtsText) else o.dtsFile; 90 + in 91 + pkgs.deviceTree.compileDTS { 92 + name = "${o.name}-dtbo"; 93 + inherit includePaths extraPreprocessorFlags dtsFile; 94 + } 99 95 else o.dtboFile; } ); 100 96 101 97 in ··· 121 117 example = literalExpression "pkgs.linux_latest"; 122 118 type = types.path; 123 119 description = lib.mdDoc '' 124 - Kernel package containing the base device-tree (.dtb) to boot. Uses 120 + Kernel package where device tree include directory is from. Also used as default source of dtb package to apply overlays to 121 + ''; 122 + }; 123 + 124 + dtboBuildExtraPreprocessorFlags = mkOption { 125 + default = []; 126 + example = literalExpression "[ \"-DMY_DTB_DEFINE\" ]"; 127 + type = types.listOf types.str; 128 + description = lib.mdDoc '' 129 + Additional flags to pass to the preprocessor during dtbo compilations 130 + ''; 131 + }; 132 + 133 + dtboBuildExtraIncludePaths = mkOption { 134 + default = []; 135 + example = literalExpression '' 136 + [ 137 + ./my_custom_include_dir_1 138 + ./custom_include_dir_2 139 + ] 140 + ''; 141 + type = types.listOf types.path; 142 + description = lib.mdDoc '' 143 + Additional include paths that will be passed to the preprocessor when creating the final .dts to compile into .dtbo 144 + ''; 145 + }; 146 + 147 + dtbSource = mkOption { 148 + default = "${cfg.kernelPackage}/dtbs"; 149 + defaultText = literalExpression "\${cfg.kernelPackage}/dtbs"; 150 + type = types.path; 151 + description = lib.mdDoc '' 152 + Path to dtb directory that overlays and other processing will be applied to. Uses 125 153 device trees bundled with the Linux kernel by default. 126 154 ''; 127 155 };
+24 -1
pkgs/os-specific/linux/device-tree/default.nix
··· 1 - { lib, stdenvNoCC, dtc }: 1 + { lib, stdenv, stdenvNoCC, dtc }: 2 2 3 3 with lib; { 4 + # Compile single Device Tree overlay source 5 + # file (.dts) into its compiled variant (.dtb) 6 + compileDTS = ({ 7 + name, 8 + dtsFile, 9 + includePaths ? [], 10 + extraPreprocessorFlags ? [] 11 + }: stdenv.mkDerivation { 12 + inherit name; 13 + 14 + nativeBuildInputs = [ dtc ]; 15 + 16 + buildCommand = 17 + let 18 + includeFlagsStr = lib.concatMapStringsSep " " (includePath: "-I${includePath}") includePaths; 19 + extraPreprocessorFlagsStr = lib.concatStringsSep " " extraPreprocessorFlags; 20 + in 21 + '' 22 + $CC -E -nostdinc ${includeFlagsStr} -undef -D__DTS__ -x assembler-with-cpp ${extraPreprocessorFlagsStr} ${dtsFile} | \ 23 + dtc -I dts -O dtb -@ -o $out 24 + ''; 25 + }); 26 + 4 27 applyOverlays = (base: overlays': stdenvNoCC.mkDerivation { 5 28 name = "device-tree-overlays"; 6 29 nativeBuildInputs = [ dtc ];