Skip to content

Commit 6abae1c

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 6abae1c

4 files changed

Lines changed: 312 additions & 26 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: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,35 +71,47 @@
7171
llvm = lrPkgs.llvm_cheri;
7272
};
7373
ftditool-cli = inputs.ftditool.packages.${system}.default;
74-
75-
commonPackages = with pkgs; [
76-
bison
77-
cmake
78-
flex
79-
gnumake
80-
screen
81-
picocom
82-
gtkwave
83-
openfpgaloader
84-
ftditool-cli
85-
openocd
86-
uv
87-
pythonEnv
88-
verible
89-
srecord
90-
d2
91-
dtc
92-
];
74+
cheri-toolchain = pkgs.callPackage ./nix/cheri_toolchain.nix {inherit (lrPkgs) llvm_cheri;};
75+
commonPackages =
76+
(with pkgs; [
77+
bison
78+
cmake
79+
flex
80+
gnumake
81+
picocom
82+
gtkwave
83+
openfpgaloader
84+
ftditool-cli
85+
openocd
86+
uv
87+
pythonEnv
88+
verible
89+
srecord
90+
d2
91+
dtc
92+
autoconf
93+
automake
94+
bc
95+
bmake
96+
byacc
97+
libarchive
98+
libarchive.dev
99+
libelf
100+
libtool
101+
pkg-config
102+
zlib
103+
zlib.dev
104+
])
105+
++ [cheri-toolchain.cheriStdenv.cc];
93106
in {
94107
formatter = pkgs.alejandra;
95108
devShells = rec {
96-
default = cheri;
97-
cheri = pkgs.mkShell {
98-
name = "mocha-cheri";
109+
default = baremetal;
110+
baremetal = pkgs.mkShell {
111+
name = "baremetal";
99112
nativeBuildInputs =
100113
commonPackages
101114
++ (with lrPkgs; [
102-
llvm_cheri
103115
verilator_5_040
104116
]);
105117
buildInputs = with pkgs; [libelf zlib];
@@ -108,8 +120,27 @@
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
};
131+
linux =
132+
(pkgs.buildFHSEnv {
133+
name = "linux";
134+
targetPkgs = pkgs: commonPackages;
135+
136+
profile = ''
137+
export HOSTCC="${pkgs.llvmPackages_21.clang}/bin/clang"
138+
export HOSTCXX="${pkgs.llvmPackages_21.clang}/bin/clang++"
139+
export HOSTLD="${pkgs.llvmPackages_21.lld}/bin/ld.lld"
140+
export LLVM=1
141+
export ARCH=riscv
142+
'';
143+
}).env;
113144
};
114145

115146
apps = {

nix/CrossToolchain.cmake.in

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 -B@llvmCheri@/bin -march=rv64imafdczcherihybrid_zcherilevels -mabi=l64pc128d -mno-relax -Xclang -target-feature -Xclang +cheri-bounded-vararg -Xclang -target-feature -Xclang +cheri-bounded-memarg-caller -Xclang -target-feature -Xclang +cheri-bounded-memarg-callee -isystem @linuxHeadersPurecap@/usr/include -ggdb -gz -Wno-error=unused-command-line-argument -ffreestanding -Werror=implicit-function-declaration -Werror=format -Werror=incompatible-pointer-types -Werror=cheri-capability-misuse -Werror=cheri-bitwise-operations -Werror=pass-failed -Werror=cheri-prototypes -Werror=undefined-internal"
33+
)
34+
35+
set(CHERI_LINKER_FLAGS "${CHERI_COMMON_FLAGS} -fuse-ld=lld")
36+
37+
set(CMAKE_C_FLAGS_INIT "${CHERI_COMMON_FLAGS}")
38+
set(CMAKE_CXX_FLAGS_INIT "${CHERI_COMMON_FLAGS}")
39+
set(CMAKE_ASM_FLAGS_INIT "${CHERI_COMMON_FLAGS}")
40+
set(CMAKE_EXE_LINKER_FLAGS_INIT "${CHERI_LINKER_FLAGS}")
41+
set(CMAKE_SHARED_LINKER_FLAGS_INIT "${CHERI_LINKER_FLAGS}")
42+
set(CMAKE_MODULE_LINKER_FLAGS_INIT "${CHERI_LINKER_FLAGS}")
43+
44+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
45+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
46+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
47+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

nix/cheri_toolchain.nix

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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+
extraBuildCommands = ''
191+
echo "-isystem ${linux-headers-purecap}/usr/include" >> $out/nix-support/cc-cflags
192+
echo "-B${compiler-rt-builtins-purecap}/lib" >> $out/nix-support/cc-cflags
193+
echo "-L${compiler-rt-builtins-purecap}/lib" >> $out/nix-support/cc-ldflags
194+
'';
195+
};
196+
197+
cheriStdenv = pkgsCross.riscv64.stdenv.override {
198+
cc = cheriCC;
199+
allowedRequisites = null;
200+
201+
targetPlatform =
202+
stdenv.targetPlatform
203+
// {
204+
config = "riscv64-linux-musl";
205+
libc = "musl";
206+
};
207+
};
208+
}

0 commit comments

Comments
 (0)