Skip to content

Commit bb5601c

Browse files
committed
nix: Add a FHS shell with the toolchain to build linux for mocha
Signed-off-by: Douglas Reis <doreis@lowrisc.org>
1 parent 9173e84 commit bb5601c

5 files changed

Lines changed: 335 additions & 12 deletions

File tree

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@
7171
llvm = lrPkgs.llvm_cheri;
7272
};
7373
ftditool-cli = inputs.ftditool.packages.${system}.default;
74-
74+
cheri-toolchain = pkgs.callPackage ./nix/cheri_toolchain.nix {inherit (lrPkgs) llvm_cheri;};
7575
commonPackages = with pkgs; [
7676
bison
7777
cmake
7878
flex
7979
gnumake
80-
screen
8180
picocom
8281
gtkwave
8382
openfpgaloader
@@ -89,17 +88,30 @@
8988
srecord
9089
d2
9190
dtc
91+
autoconf
92+
automake
93+
bc
94+
bmake
95+
byacc
96+
libarchive
97+
libarchive.dev
98+
libelf
99+
libtool
100+
pkg-config
101+
zlib
102+
zlib.dev
92103
];
93104
in {
94105
formatter = pkgs.alejandra;
95106
devShells = rec {
96-
default = cheri;
97-
cheri = pkgs.mkShell {
98-
name = "mocha-cheri";
107+
default = baremetal;
108+
baremetal = pkgs.mkShell.override {stdenv = cheri-toolchain.cheriStdenv;} {
109+
name = "baremetal";
110+
strictDeps = true;
111+
hardeningDisable = [ "all" ];
99112
nativeBuildInputs =
100113
commonPackages
101114
++ (with lrPkgs; [
102-
llvm_cheri
103115
verilator_5_040
104116
]);
105117
buildInputs = with pkgs; [libelf zlib];
@@ -108,6 +120,12 @@
108120
UV_PYTHON_DOWNLOADS = "never";
109121
# Force uv to use nixpkgs Python interpreter
110122
UV_PYTHON = pythonSet.python.interpreter;
123+
124+
HOSTCC = "${pkgs.llvmPackages_21.clang}/bin/clang";
125+
HOSTCXX = "${pkgs.llvmPackages_21.clang}/bin/clang++";
126+
HOSTLD = "${pkgs.llvmPackages_21.lld}/bin/ld.lld";
127+
LLVM = "1";
128+
ARCH = "riscv";
111129
};
112130
};
113131
};

nix/CrossToolchain.cmake.in

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright lowRISC contributors.
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
set(CMAKE_SYSTEM_NAME Linux)
6+
set(CMAKE_SYSTEM_PROCESSOR riscv64)
7+
8+
set(CMAKE_C_COMPILER "@llvmCheri@/bin/clang")
9+
set(CMAKE_C_COMPILER_TARGET "riscv64-linux-musl")
10+
set(CMAKE_CXX_COMPILER "@llvmCheri@/bin/clang++")
11+
set(CMAKE_CXX_COMPILER_TARGET "riscv64-linux-musl")
12+
set(CMAKE_ASM_COMPILER "@llvmCheri@/bin/clang")
13+
set(CMAKE_ASM_COMPILER_TARGET "riscv64-linux-musl")
14+
15+
set(CMAKE_AR
16+
"@llvmCheri@/bin/llvm-ar"
17+
CACHE FILEPATH "ar")
18+
set(CMAKE_RANLIB
19+
"@llvmCheri@/bin/llvm-ranlib"
20+
CACHE FILEPATH "ranlib")
21+
set(CMAKE_NM
22+
"@llvmCheri@/bin/llvm-nm"
23+
CACHE FILEPATH "nm")
24+
set(CMAKE_STRIP
25+
"@llvmCheri@/bin/llvm-strip"
26+
CACHE FILEPATH "strip")
27+
set(CMAKE_LINKER
28+
"@llvmCheri@/bin/ld.lld"
29+
CACHE FILEPATH "linker")
30+
31+
set(CHERI_COMMON_FLAGS
32+
"-target riscv64-linux-musl \
33+
-B@llvmCheri@/bin \
34+
-march=rv64imaczcherihybrid_zcherilevels \
35+
-mabi=l64pc128 \
36+
-mno-relax \
37+
-Xclang -target-feature -Xclang +cheri-bounded-vararg \
38+
-Xclang -target-feature -Xclang +cheri-bounded-memarg-caller \
39+
-Xclang -target-feature -Xclang +cheri-bounded-memarg-callee \
40+
-isystem @linuxHeadersPurecap@/usr/include \
41+
-ggdb \
42+
-gz \
43+
-Wno-error=unused-command-line-argument \
44+
-ffreestanding \
45+
-Werror=implicit-function-declaration \
46+
-Werror=format \
47+
-Werror=incompatible-pointer-types \
48+
-Werror=cheri-capability-misuse \
49+
-Werror=cheri-bitwise-operations \
50+
-Werror=pass-failed \
51+
-Werror=cheri-prototypes \
52+
-Werror=undefined-internal"
53+
)
54+
55+
set(CHERI_LINKER_FLAGS "${CHERI_COMMON_FLAGS} -fuse-ld=lld")
56+
57+
set(CMAKE_C_FLAGS_INIT "${CHERI_COMMON_FLAGS}")
58+
set(CMAKE_CXX_FLAGS_INIT "${CHERI_COMMON_FLAGS}")
59+
set(CMAKE_ASM_FLAGS_INIT "${CHERI_COMMON_FLAGS}")
60+
set(CMAKE_EXE_LINKER_FLAGS_INIT "${CHERI_LINKER_FLAGS}")
61+
set(CMAKE_SHARED_LINKER_FLAGS_INIT "${CHERI_LINKER_FLAGS}")
62+
set(CMAKE_MODULE_LINKER_FLAGS_INIT "${CHERI_LINKER_FLAGS}")
63+
64+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
65+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
66+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
67+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

nix/cheri_toolchain.nix

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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

Comments
 (0)