|
| 1 | +# Copyright lowRISC contributors. |
| 2 | +# Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +{ |
| 5 | + lib, |
| 6 | + stdenv, |
| 7 | + gnumake, |
| 8 | + cmake, |
| 9 | + rsync, |
| 10 | + fetchFromGitHub, |
| 11 | + llvm_cheri, |
| 12 | + pkgsCross, |
| 13 | +}: let |
| 14 | + CHERI_FLAGS = "rv64imaczcherihybrid_zcherilevels"; |
| 15 | + MABI = "l64pc128"; |
| 16 | +in rec { |
| 17 | + linux-headers-purecap = stdenv.mkDerivation { |
| 18 | + pname = "linux-headers-purecap"; |
| 19 | + version = "6.18"; |
| 20 | + |
| 21 | + src = fetchFromGitHub { |
| 22 | + owner = "CHERI-Alliance"; |
| 23 | + repo = "linux"; |
| 24 | + rev = "af04d488044fa684760d6480f3623e51b46568ca"; |
| 25 | + fetchSubmodules = true; |
| 26 | + hash = "sha256-Gjjs+vV0meFHOAt9i88WMzpFEq8IZ1oXDFVXb7ZuLI8="; |
| 27 | + }; |
| 28 | + |
| 29 | + nativeBuildInputs = [ |
| 30 | + gnumake |
| 31 | + rsync |
| 32 | + ]; |
| 33 | + |
| 34 | + dontConfigure = true; |
| 35 | + dontBuild = true; |
| 36 | + |
| 37 | + installPhase = '' |
| 38 | + runHook preInstall |
| 39 | +
|
| 40 | + make -j$NIX_BUILD_CORES headers_install \ |
| 41 | + ARCH=riscv \ |
| 42 | + INSTALL_HDR_PATH=$out/usr |
| 43 | +
|
| 44 | + runHook postInstall |
| 45 | + ''; |
| 46 | + |
| 47 | + meta = { |
| 48 | + description = "CHERI RISC-V 64 purecap Linux kernel (codasip-cheri-riscv-6.18)"; |
| 49 | + platforms = ["x86_64-linux"]; |
| 50 | + }; |
| 51 | + }; |
| 52 | + |
| 53 | + muslc-linux-riscv64-purecap = stdenv.mkDerivation { |
| 54 | + pname = "muslc-linux-riscv64-purecap"; |
| 55 | + version = "std093"; |
| 56 | + |
| 57 | + src = fetchFromGitHub { |
| 58 | + owner = "CHERI-Alliance"; |
| 59 | + repo = "musl"; |
| 60 | + rev = "12d15cddabcfb3f4f0612730f7147e7cf8c5579f"; |
| 61 | + fetchSubmodules = true; |
| 62 | + hash = "sha256-8cEUnBQSsWEUoe5gLR/3jFO3KwTddJOhDTDnNZgUIXQ="; |
| 63 | + }; |
| 64 | + |
| 65 | + nativeBuildInputs = [ |
| 66 | + gnumake |
| 67 | + llvm_cheri |
| 68 | + ]; |
| 69 | + |
| 70 | + dontConfigure = false; |
| 71 | + |
| 72 | + configurePhase = '' |
| 73 | + runHook preConfigure |
| 74 | +
|
| 75 | + # Dynamically fetch the resource dir |
| 76 | + RESOURCE_DIR=$(${llvm_cheri}/bin/clang -print-resource-dir) |
| 77 | +
|
| 78 | + ./configure \ |
| 79 | + --prefix= \ |
| 80 | + --disable-shared \ |
| 81 | + CC="clang \ |
| 82 | + -target riscv64-linux-musl \ |
| 83 | + -march=${CHERI_FLAGS} \ |
| 84 | + -mabi=${MABI} \ |
| 85 | + -mno-relax \ |
| 86 | + -Xclang -target-feature -Xclang +cheri-bounded-vararg \ |
| 87 | + -Xclang -target-feature -Xclang +cheri-bounded-memarg-caller \ |
| 88 | + -Xclang -target-feature -Xclang +cheri-bounded-memarg-callee \ |
| 89 | + -idirafter $RESOURCE_DIR/include" |
| 90 | +
|
| 91 | + runHook postConfigure |
| 92 | + ''; |
| 93 | + |
| 94 | + installPhase = '' |
| 95 | + runHook preInstall |
| 96 | +
|
| 97 | + make install-headers DESTDIR=$out |
| 98 | + make install-libs DESTDIR=$out |
| 99 | +
|
| 100 | + runHook postInstall |
| 101 | + ''; |
| 102 | + |
| 103 | + dontFixup = true; |
| 104 | + }; |
| 105 | + |
| 106 | + compiler-rt-builtins-purecap = stdenv.mkDerivation { |
| 107 | + pname = "compiler-rt-builtins-purecap"; |
| 108 | + version = "std093"; |
| 109 | + |
| 110 | + src = llvm_cheri.src; |
| 111 | + sourceRoot = "source/compiler-rt/lib/builtins"; |
| 112 | + |
| 113 | + nativeBuildInputs = [ |
| 114 | + cmake |
| 115 | + llvm_cheri |
| 116 | + ]; |
| 117 | + |
| 118 | + llvmCheri = llvm_cheri; |
| 119 | + linuxHeadersPurecap = linux-headers-purecap; |
| 120 | + |
| 121 | + preConfigure = '' |
| 122 | + substituteAll ${./CrossToolchain.cmake.in} CrossToolchain.cmake |
| 123 | + ''; |
| 124 | + |
| 125 | + configurePhase = '' |
| 126 | + runHook preConfigure |
| 127 | +
|
| 128 | + cmake -S . -B build \ |
| 129 | + --toolchain=../CrossToolchain.cmake \ |
| 130 | + -DCMAKE_BUILD_TYPE=RelWithDebInfo \ |
| 131 | + -DLLVM_CONFIG_PATH=NOTFOUND \ |
| 132 | + -DCMAKE_DISABLE_FIND_PACKAGE_LLVM=true \ |
| 133 | + -DCOMPILER_RT_EXCLUDE_ATOMIC_BUILTIN=false \ |
| 134 | + -DCOMPILER_RT_BAREMETAL_BUILD=false \ |
| 135 | + -DCOMPILER_RT_DEFAULT_TARGET_ONLY=true \ |
| 136 | + -DTARGET_TRIPLE="riscv64-linux-musl" \ |
| 137 | + -DCMAKE_SYSROOT="${muslc-linux-riscv64-purecap}" \ |
| 138 | + -DCOMPILER_RT_DEBUG=true \ |
| 139 | + -DCMAKE_C_COMPILER=${llvm_cheri}/bin/clang \ |
| 140 | + -DCMAKE_CXX_COMPILER=${llvm_cheri}/bin/clang++ \ |
| 141 | + -DCMAKE_ASM_COMPILER=${llvm_cheri}/bin/clang \ |
| 142 | + -DCMAKE_C_FLAGS="-march=${CHERI_FLAGS} -mabi=${MABI} -isystem ${linux-headers-purecap}/usr/include" \ |
| 143 | + -DCMAKE_CXX_FLAGS="-march=${CHERI_FLAGS} -mabi=${MABI} -isystem ${linux-headers-purecap}/usr/include" \ |
| 144 | + -DCMAKE_ASM_FLAGS="-march=${CHERI_FLAGS} -mabi=${MABI} -isystem ${linux-headers-purecap}/usr/include" \ |
| 145 | + -DCMAKE_INSTALL_PREFIX="$out" |
| 146 | + ''; |
| 147 | + |
| 148 | + buildPhase = '' |
| 149 | + cmake --build build |
| 150 | + ''; |
| 151 | + |
| 152 | + installPhase = '' |
| 153 | + cmake --build build --target install |
| 154 | + runHook postInstall |
| 155 | + ''; |
| 156 | + |
| 157 | + postInstall = '' |
| 158 | + mkdir -p $out/lib |
| 159 | +
|
| 160 | + ln -fsn $out/lib/linux/libclang_rt.builtins-riscv64.a $out/lib/libclang_rt.builtins-riscv64.a |
| 161 | + ln -fsn $out/lib/linux/libclang_rt.builtins-riscv64.a $out/lib/libgcc.a |
| 162 | +
|
| 163 | + ln -fsn $out/lib/linux/clang_rt.crtbegin-riscv64.o $out/lib/crtbeginT.o |
| 164 | + ln -fsn $out/lib/linux/clang_rt.crtbegin-riscv64.o $out/lib/crtbeginS.o |
| 165 | + ln -fsn $out/lib/linux/clang_rt.crtend-riscv64.o $out/lib/crtend.o |
| 166 | + ln -fsn $out/lib/linux/clang_rt.crtend-riscv64.o $out/lib/crtendS.o |
| 167 | + ''; |
| 168 | + |
| 169 | + dontFixup = true; |
| 170 | + }; |
| 171 | + |
| 172 | + cheriBintools = pkgsCross.riscv64.wrapBintoolsWith { |
| 173 | + bintools = llvm_cheri; |
| 174 | + libc = muslc-linux-riscv64-purecap; |
| 175 | + coreutils = pkgsCross.riscv64.buildPackages.coreutils; |
| 176 | + extraBuildCommands = '' |
| 177 | + for tool in ar as nm objcopy objdump ranlib readelf size strings strip; do |
| 178 | + # Link bare names: llvm-nm -> nm |
| 179 | + if [ -x "$out/bin/$tool" ]; then |
| 180 | + ln -s "$tool" "$out/bin/llvm-$tool" |
| 181 | + fi |
| 182 | + done |
| 183 | + ''; |
| 184 | + }; |
| 185 | + |
| 186 | + cheriCC = pkgsCross.riscv64.wrapCCWith { |
| 187 | + cc = llvm_cheri; |
| 188 | + bintools = cheriBintools; |
| 189 | + libc = muslc-linux-riscv64-purecap; |
| 190 | + coreutils = pkgsCross.riscv64.buildPackages.coreutils; |
| 191 | + extraBuildCommands = '' |
| 192 | + echo "-isystem ${linux-headers-purecap}/usr/include" >> $out/nix-support/cc-cflags |
| 193 | + echo "-B${compiler-rt-builtins-purecap}/lib" >> $out/nix-support/cc-cflags |
| 194 | + echo "-L${compiler-rt-builtins-purecap}/lib" >> $out/nix-support/cc-ldflags |
| 195 | + ''; |
| 196 | + }; |
| 197 | + |
| 198 | + cheriStdenv = pkgsCross.riscv64.stdenv.override { |
| 199 | + cc = cheriCC; |
| 200 | + allowedRequisites = null; |
| 201 | + |
| 202 | + targetPlatform = |
| 203 | + stdenv.targetPlatform |
| 204 | + // { |
| 205 | + config = "riscv64-linux-musl"; |
| 206 | + libc = "musl"; |
| 207 | + }; |
| 208 | + }; |
| 209 | +} |
0 commit comments