refactor(nxp): parameterize i.MX platform builders to reduce duplication

This refactoring reduces code duplication across i.MX93, i.MX8MP, and
i.MX8MQ platforms by extracting common build logic into parameterized
shared builders. This makes it easier to maintain and add new i.MX
platforms while ensuring consistency across all platforms.
This commit is contained in:
Jörg Thalheim
2025-10-29 16:03:39 +01:00
parent 8b405e9c6d
commit 688ee555de
14 changed files with 398 additions and 598 deletions

View File

@@ -0,0 +1,56 @@
# Parameterized Linux kernel builder for i.MX platforms
# This builder is used across i.MX93, i.MX8MP, i.MX8MQ and similar platforms
{ lib, pkgs, ... }@args:
let
inherit (pkgs) buildLinux;
# Import common kernel configuration
kernelConfig = import ../lib/kernel-config.nix;
in
# Platform-specific parameters
{
pname,
version,
src,
defconfig ? "imx_v8_defconfig",
# Optional parameters
extraConfig ? "",
kernelPatches ? [ ],
autoModules ? false,
ignoreConfigErrors ? true,
extraMeta ? { },
}:
let
# Combine common i.MX kernel config with platform-specific config
finalExtraConfig = kernelConfig.imxCommonKernelConfig + extraConfig;
in
buildLinux (
args
// rec {
inherit
version
defconfig
kernelPatches
autoModules
ignoreConfigErrors
;
name = pname;
# modDirVersion needs to be x.y.z, will automatically add .0 if needed
modDirVersion = version;
extraConfig = finalExtraConfig;
inherit src;
meta =
with lib;
{
homepage = "https://github.com/nxp-imx/linux-imx";
license = [ licenses.gpl2Only ];
platforms = [ "aarch64-linux" ];
}
// extraMeta;
}
// (args.argsOverride or { })
)

View File

@@ -0,0 +1,89 @@
# Parameterized OP-TEE OS builder for i.MX platforms
# This builder is used across i.MX93, i.MX8MP, i.MX8MQ and similar platforms
{
lib,
pkgs,
# Platform-specific parameters
pname,
version,
platformFlavor,
src,
# Optional parameters
meta ? { },
}:
let
inherit (pkgs.buildPackages) python3;
toolchain = pkgs.gccStdenv.cc;
binutils = pkgs.gccStdenv.cc.bintools.bintools_bin;
cpp = pkgs.gcc;
# Determine PLATFORM and PLATFORM_FLAVOR from platformFlavor
# Format can be either "imx-mx93evk" (full platform string) or "mx8mpevk" (just flavor, platform is "imx")
# Check if it starts with "imx-" to determine if it's a full platform string or just a flavor
hasFullPlatform = lib.hasPrefix "imx-" platformFlavor;
platform = if hasFullPlatform then platformFlavor else "imx";
flavor = if hasFullPlatform then null else platformFlavor;
in
pkgs.stdenv.mkDerivation {
inherit pname version src;
nativeBuildInputs = [
python3
];
enableParallelBuilding = true;
propagatedBuildInputs = with python3.pkgs; [
pycryptodomex
pyelftools
cryptography
];
# Common postPatch for all i.MX platforms
# This is the major source of code duplication - ~60 lines of identical substitutions
postPatch = ''
# Patch all script shebangs automatically
patchShebangs scripts/
patchShebangs ta/
# Patch toolchain paths in mk/gcc.mk
substituteInPlace mk/gcc.mk \
--replace "\$(CROSS_COMPILE_\$(sm))objcopy" ${binutils}/bin/${toolchain.targetPrefix}objcopy
substituteInPlace mk/gcc.mk \
--replace "\$(CROSS_COMPILE_\$(sm))objdump" ${binutils}/bin/${toolchain.targetPrefix}objdump
substituteInPlace mk/gcc.mk \
--replace "\$(CROSS_COMPILE_\$(sm))nm" ${binutils}/bin/${toolchain.targetPrefix}nm
substituteInPlace mk/gcc.mk \
--replace "\$(CROSS_COMPILE_\$(sm))readelf" ${binutils}/bin/${toolchain.targetPrefix}readelf
substituteInPlace mk/gcc.mk \
--replace "\$(CROSS_COMPILE_\$(sm))ar" ${binutils}/bin/${toolchain.targetPrefix}ar
substituteInPlace mk/gcc.mk \
--replace "\$(CROSS_COMPILE_\$(sm))cpp" ${cpp}/bin/cpp
'';
makeFlags =
[
"PLATFORM=${platform}"
]
++ lib.optionals (!hasFullPlatform) [
"PLATFORM_FLAVOR=${flavor}"
]
++ [
"CFG_ARM64_core=y"
"CFG_TEE_TA_LOG_LEVEL=0"
"CFG_TEE_CORE_LOG_LEVEL=0"
"CROSS_COMPILE=${toolchain}/bin/${toolchain.targetPrefix}"
"CROSS_COMPILE64=${toolchain}/bin/${toolchain.targetPrefix}"
];
installPhase = ''
mkdir -p $out
cp ./out/arm-plat-imx/core/tee-raw.bin $out/tee.bin
'';
meta = {
homepage = "https://github.com/nxp-imx/imx-optee-os";
license = [ lib.licenses.bsd2 ];
platforms = [ "aarch64-linux" ];
} // meta;
}

View File

@@ -0,0 +1,97 @@
# Parameterized U-Boot builder for i.MX platforms
# This builder is used across i.MX93, i.MX8MP, i.MX8MQ and similar platforms
{
lib,
stdenv,
buildPackages,
# Required dependencies
bison,
dtc,
flex,
gnutls,
libuuid,
ncurses,
openssl,
perl,
efitools,
which,
# Platform-specific parameters
pname,
version,
src,
defconfig,
ramdiskAddr,
fdtAddr,
dtbPath,
# Optional parameters
extraConfig ? "",
extraNativeBuildInputs ? [ ],
}:
let
# Import common U-Boot configuration
ubootConfig = import ../lib/uboot-config.nix;
# Generate the common config with platform-specific memory addresses
commonConfig = ubootConfig.imxCommonUbootConfig {
inherit ramdiskAddr fdtAddr;
};
# Combine common config with any platform-specific extra config
finalExtraConfig = commonConfig + extraConfig;
in
stdenv.mkDerivation {
inherit pname version src;
postPatch = ''
patchShebangs tools
patchShebangs scripts
'';
nativeBuildInputs = [
bison
flex
openssl
which
ncurses
libuuid
gnutls
perl
efitools
] ++ extraNativeBuildInputs;
depsBuildBuild = [ buildPackages.stdenv.cc ];
hardeningDisable = [ "all" ];
enableParallelBuilding = true;
makeFlags = [
"DTC=${lib.getExe buildPackages.dtc}"
"CROSS_COMPILE=${stdenv.cc.targetPrefix}"
];
extraConfig = finalExtraConfig;
passAsFile = [ "extraConfig" ];
configurePhase = ''
runHook preConfigure
make ${defconfig}
cat $extraConfigPath >> .config
runHook postConfigure
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp ./u-boot-nodtb.bin $out
cp ./spl/u-boot-spl.bin $out
cp ${dtbPath} $out
cp .config $out
runHook postInstall
'';
dontStrip = true;
}