1{ stdenv
2, clangStdenv
3, fetchFromGitHub
4, fetchpatch
5, libuuid
6, python3
7, bc
8, llvmPackages_9
9, lib
10, buildPackages
11}:
12
13let
14 pythonEnv = buildPackages.python3.withPackages (ps: [ps.tkinter]);
15
16targetArch = if stdenv.isi686 then
17 "IA32"
18else if stdenv.isx86_64 then
19 "X64"
20else if stdenv.isAarch32 then
21 "ARM"
22else if stdenv.isAarch64 then
23 "AARCH64"
24else
25 throw "Unsupported architecture";
26
27buildStdenv = if stdenv.isDarwin then
28 llvmPackages_9.stdenv
29else
30 stdenv;
31
32buildType = if stdenv.isDarwin then
33 "CLANGPDB"
34 else
35 "GCC5";
36
37edk2 = buildStdenv.mkDerivation {
38 pname = "edk2";
39 version = "202302";
40
41 patches = [
42 # pass targetPrefix as an env var
43 (fetchpatch {
44 url = "https://src.fedoraproject.org/rpms/edk2/raw/08f2354cd280b4ce5a7888aa85cf520e042955c3/f/0021-Tweak-the-tools_def-to-support-cross-compiling.patch";
45 sha256 = "sha256-E1/fiFNVx0aB1kOej2DJ2DlBIs9tAAcxoedym2Zhjxw=";
46 })
47 ];
48
49 # submodules
50 src = fetchFromGitHub {
51 owner = "tianocore";
52 repo = "edk2";
53 rev = "edk2-stable${edk2.version}";
54 fetchSubmodules = true;
55 sha256 = "sha256-KZ5bTdaStO2M1hLPx9LsUSMl9NEiZeYMmFiShxCJqJM=";
56 };
57
58 nativeBuildInputs = [ pythonEnv ];
59 depsBuildBuild = [ buildPackages.stdenv.cc buildPackages.util-linux buildPackages.bash ];
60 strictDeps = true;
61
62 # trick taken from https://src.fedoraproject.org/rpms/edk2/blob/08f2354cd280b4ce5a7888aa85cf520e042955c3/f/edk2.spec#_319
63 ${"GCC5_${targetArch}_PREFIX"}=stdenv.cc.targetPrefix;
64
65 makeFlags = [ "-C BaseTools" ]
66 ++ lib.optionals (stdenv.cc.isClang) [ "CXX=llvm BUILD_AR=ar BUILD_CC=clang BUILD_CXX=clang++ BUILD_AS=clang BUILD_LD=ld" ];
67
68 env.NIX_CFLAGS_COMPILE = "-Wno-return-type" + lib.optionalString (stdenv.cc.isGNU) " -Wno-error=stringop-truncation";
69
70 hardeningDisable = [ "format" "fortify" ];
71
72 installPhase = ''
73 mkdir -vp $out
74 mv -v BaseTools $out
75 mv -v edksetup.sh $out
76 # patchShebangs fails to see these when cross compiling
77 for i in $out/BaseTools/BinWrappers/PosixLike/*; do
78 substituteInPlace $i --replace '/usr/bin/env bash' ${buildPackages.bash}/bin/bash
79 done
80 '';
81
82 enableParallelBuilding = true;
83
84 meta = with lib; {
85 description = "Intel EFI development kit";
86 homepage = "https://github.com/tianocore/tianocore.github.io/wiki/EDK-II/";
87 license = licenses.bsd2;
88 platforms = with platforms; aarch64 ++ arm ++ i686 ++ x86_64;
89 };
90
91 passthru = {
92 mkDerivation = projectDscPath: attrsOrFun: buildStdenv.mkDerivation (finalAttrs:
93 let
94 attrs = lib.toFunction attrsOrFun finalAttrs;
95 in
96 {
97 inherit (edk2) src;
98
99 depsBuildBuild = [ buildPackages.stdenv.cc ] ++ attrs.depsBuildBuild or [];
100 nativeBuildInputs = [ bc pythonEnv ] ++ attrs.nativeBuildInputs or [];
101 strictDeps = true;
102
103 ${"GCC5_${targetArch}_PREFIX"}=stdenv.cc.targetPrefix;
104
105 prePatch = ''
106 rm -rf BaseTools
107 ln -sv ${edk2}/BaseTools BaseTools
108 '';
109
110 configurePhase = ''
111 runHook preConfigure
112 export WORKSPACE="$PWD"
113 . ${edk2}/edksetup.sh BaseTools
114 runHook postConfigure
115 '';
116
117 buildPhase = ''
118 runHook preBuild
119 build -a ${targetArch} -b ${attrs.buildConfig or "RELEASE"} -t ${buildType} -p ${projectDscPath} -n $NIX_BUILD_CORES $buildFlags
120 runHook postBuild
121 '';
122
123 installPhase = ''
124 runHook preInstall
125 mv -v Build/*/* $out
126 runHook postInstall
127 '';
128 } // removeAttrs attrs [ "nativeBuildInputs" "depsBuildBuild" ]);
129 };
130};
131
132in
133
134edk2