|
| 1 | +# develop.nix |
| 2 | +# sets up a development shell in which you can open your editor |
| 3 | +{ |
| 4 | + buildpath ? "build", |
| 5 | + |
| 6 | + # path to your unikernel source (project root with CMakeLists.txt) |
| 7 | + unikernel ? ".", |
| 8 | + |
| 9 | + # optional: path to a local vmrunner checkout (otherwise use includeos.vmrunner) |
| 10 | + vmrunner ? "", |
| 11 | + |
| 12 | + # Enable ccache support. See overlay.nix for details. |
| 13 | + withCcache ? false, |
| 14 | + |
| 15 | + # Enable multicore suport. |
| 16 | + smp ? false, |
| 17 | + |
| 18 | + includeos ? import ./default.nix { inherit withCcache smp; }, |
| 19 | + |
| 20 | + arch ? "x86_64" |
| 21 | +}: |
| 22 | + |
| 23 | +# override stdenv for furhter derivations so they're in sync with includeos patch requirements |
| 24 | +includeos.pkgs.mkShell.override { inherit (includeos) stdenv; } rec { |
| 25 | + vmrunnerPkg = |
| 26 | + if vmrunner == "" then |
| 27 | + includeos.vmrunner |
| 28 | + else |
| 29 | + includeos.pkgs.callPackage (builtins.toPath /. + vmrunner) {}; |
| 30 | + |
| 31 | + # handy tools available in the shell |
| 32 | + packages = [ |
| 33 | + (includeos.pkgs.python3.withPackages (p: [ |
| 34 | + vmrunnerPkg |
| 35 | + ])) |
| 36 | + includeos.pkgs.buildPackages.cmake |
| 37 | + includeos.pkgs.buildPackages.nasm |
| 38 | + includeos.pkgs.qemu |
| 39 | + includeos.pkgs.which |
| 40 | + includeos.pkgs.grub2 |
| 41 | + includeos.pkgs.iputils |
| 42 | + includeos.pkgs.xorriso |
| 43 | + includeos.pkgs.jq |
| 44 | + ]; |
| 45 | + |
| 46 | + # libraries/headers we include against |
| 47 | + buildInputs = [ |
| 48 | + includeos |
| 49 | + includeos.chainloader |
| 50 | + includeos.lest |
| 51 | + includeos.pkgs.openssl |
| 52 | + includeos.pkgs.rapidjson |
| 53 | + ]; |
| 54 | + |
| 55 | + shellHook = '' |
| 56 | + IOS_SRC=${toString ./.} |
| 57 | + if [ ! -d "$IOS_SRC" ]; then |
| 58 | + echo "$unikernel is not a valid directory" >&2 |
| 59 | + return 1 |
| 60 | + fi |
| 61 | +
|
| 62 | + echo "Configuring in: ${buildpath}" |
| 63 | + echo "Source tree: $IOS_SRC" |
| 64 | +
|
| 65 | + # delete old just in case it's dirty |
| 66 | + rm -rf ${buildpath} |
| 67 | + mkdir -p ${buildpath} |
| 68 | +
|
| 69 | + # build includeOS |
| 70 | + cmake -S "$IOS_SRC" -B ${buildpath} \ |
| 71 | + -D CMAKE_EXPORT_COMPILE_COMMANDS=ON \ |
| 72 | + -D ARCH=${arch} \ |
| 73 | + -D CMAKE_MODULE_PATH=${includeos}/cmake |
| 74 | +
|
| 75 | + # procuced by CMake |
| 76 | + CCDB="${buildpath}/compile_commands.json" |
| 77 | +
|
| 78 | + # |
| 79 | + # attempting to use -resource-dir with 'clang++ -print-resource-dir' |
| 80 | + # doesn't work here as we're using -nostdlib/-nostdlibinc |
| 81 | + # |
| 82 | + tmp="$CCDB.clangd.tmp" |
| 83 | + jq \ |
| 84 | + --arg libcxx "${includeos.libraries.libcxx.include}" \ |
| 85 | + --arg libc "${includeos.libraries.libc}" \ |
| 86 | + --arg localsrc "${toString ./.}" \ |
| 87 | + ' |
| 88 | + map(.command |= ( . |
| 89 | + + " -isystem \($libcxx)" |
| 90 | + + " -isystem \($libc)/include" |
| 91 | + | gsub("(?<a>-I)(?<b>/lib/LiveUpdate/include)"; .a + $localsrc + .b) |
| 92 | + )) |
| 93 | + ' "$CCDB" > "$tmp" && mv "$tmp" "$CCDB" |
| 94 | +
|
| 95 | +
|
| 96 | + # most clangd configurations and editors will look in ./build/, but this just makes it easier to find for some niche edge cases |
| 97 | + ln -sfn "${buildpath}/compile_commands.json" "$IOS_SRC/compile_commands.json" |
| 98 | + ''; |
| 99 | +} |
| 100 | + |
0 commit comments