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