Skip to content

Commit

Permalink
xeus-cling: fix improper linking with LLVM (NixOS#351130)
Browse files Browse the repository at this point in the history
  • Loading branch information
wegank authored Dec 2, 2024
2 parents 89a3c7e + 3f36a68 commit 89bea7b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 48 deletions.
114 changes: 75 additions & 39 deletions pkgs/applications/editors/jupyter-kernels/xeus-cling/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{ callPackage
, cling
, fetchurl
, jq
, makeWrapper
, python3
, stdenv
}:

Expand All @@ -11,48 +14,81 @@
# nix run --impure --expr 'with import <nixpkgs> {}; jupyter.override { definitions = { cpp17 = cpp17-kernel; }; }'

let
xeus-cling = callPackage ./xeus-cling.nix {};

mkDefinition = std:
let
versionSuffix =
if std == "c++11" then " 11"
else if std == "c++14" then " 14"
else if std == "c++17" then " 17"
else if std == "c++17" then " 17"
else if std == "c++2a" then " 2a"
else throw "Unexpected C++ std for cling: ${std}";
in
xeus-cling-unwrapped = callPackage ./xeus-cling.nix {};

xeus-cling = xeus-cling-unwrapped.overrideAttrs (oldAttrs: {
nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [makeWrapper];

# xcpp needs a collection of flags to start up properly, so wrap it by default.
# We'll provide the unwrapped version as a passthru
flags = cling.flags ++ [
"-resource-dir" "${cling.unwrapped}"
"-L" "${cling.unwrapped}/lib"
"-l" "${cling.unwrapped}/lib/cling.so"
];

fixupPhase = ''
runHook preFixup
wrapProgram $out/bin/xcpp --add-flags "$flags"
runHook postFixup
'';

doInstallCheck = true;
installCheckPhase = ''
runHook preCheck
# Smoke check: run a test notebook using Papermill by creating a simple kernelspec
mkdir -p kernels/cpp17
export JUPYTER_PATH="$(pwd)"
cat << EOF > kernels/cpp17/kernel.json
{
displayName = "C++" + versionSuffix;
argv = [
"${xeus-cling}/bin/xcpp"
]
++ cling.flags
++ [
"-resource-dir" "${cling.unwrapped}"
"-L" "${cling.unwrapped}/lib"
"-l" "${cling.unwrapped}/lib/cling.so"
"-std=${std}"
# "-v"
"-f" "{connection_file}"
];
language = "cpp";
logo32 = fetchurl {
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/32px-ISO_C%2B%2B_Logo.svg.png";
hash = "sha256-cr0TB8/j2mkcFhfCkz9F7ZANOuTlWA2OcWtDcXyOjHw=";
};
logo64 = fetchurl {
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/64px-ISO_C%2B%2B_Logo.svg.png";
hash = "sha256-nZtJ4bR7GmQttvqEJC9KejOxphrjjxT36L9yOIITFLk=";
};
};
"argv": ["$out/bin/xcpp", "-std=c++17", "-f", "{connection_file}"],
"language": "cpp17"
}
EOF
${python3.pkgs.papermill}/bin/papermill ${./test.ipynb} out.ipynb
result="$(cat out.ipynb | ${jq}/bin/jq -r '.cells[0].outputs[0].text[0]')"
if [[ "$result" != "Hello world." ]]; then
echo "Kernel test gave '$result'. Expected: 'Hello world.'"
exit 1
fi
runHook postCheck
'';

passthru = (oldAttrs.passthru or {}) // {
unwrapped = xeus-cling-unwrapped;
};
});

mkKernelSpec = std: {
displayName = builtins.replaceStrings ["c++"] ["C++ "] std;
argv = [
"${xeus-cling}/bin/xcpp"
"-std=${std}"
"-f" "{connection_file}"
];
language = "cpp";
logo32 = fetchurl {
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/32px-ISO_C%2B%2B_Logo.svg.png";
hash = "sha256-+TKtwXybKw4oAHfgOsDxvL4ucItPguF76HJHdFTd3s0=";
};
logo64 = fetchurl {
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/ISO_C%2B%2B_Logo.svg/64px-ISO_C%2B%2B_Logo.svg.png";
hash = "sha256-7SjOcSaSPUHIKnjBxMdn+KSjviL69IXhX7eJsacYeGE=";
};
};

in

{
cpp11-kernel = mkDefinition "c++11";
cpp14-kernel = mkDefinition "c++14";
cpp17-kernel = mkDefinition "c++17";
cpp2a-kernel = mkDefinition "c++2a";
cpp11-kernel = mkKernelSpec "c++11";
cpp14-kernel = mkKernelSpec "c++14";
cpp17-kernel = mkKernelSpec "c++17";
cpp2a-kernel = mkKernelSpec "c++2a";

inherit xeus-cling;
}
24 changes: 24 additions & 0 deletions pkgs/applications/editors/jupyter-kernels/xeus-cling/test.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "574ed398-7bfe-4a34-a7dd-9fa85535aed2",
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>\n",
"std::cout << \"Hello world.\";"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++ 17",
"language": "cpp",
"name": "cpp17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
16 changes: 9 additions & 7 deletions pkgs/by-name/cl/cling/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ let
sparseCheckout = ["clang"];
};

llvm = llvmPackages_13.llvm.override { enableSharedLibraries = false; };

unwrapped = stdenv.mkDerivation rec {
pname = "cling-unwrapped";
version = "1.0";
Expand Down Expand Up @@ -72,12 +74,12 @@ let
strictDeps = true;

cmakeFlags = [
"-DLLVM_BINARY_DIR=${llvmPackages_13.llvm.out}"
"-DLLVM_CONFIG=${llvmPackages_13.llvm.dev}/bin/llvm-config"
"-DLLVM_LIBRARY_DIR=${llvmPackages_13.llvm.lib}/lib"
"-DLLVM_MAIN_INCLUDE_DIR=${llvmPackages_13.llvm.dev}/include"
"-DLLVM_TABLEGEN_EXE=${llvmPackages_13.llvm.out}/bin/llvm-tblgen"
"-DLLVM_TOOLS_BINARY_DIR=${llvmPackages_13.llvm.out}/bin"
"-DLLVM_BINARY_DIR=${llvm.out}"
"-DLLVM_CONFIG=${llvm.dev}/bin/llvm-config"
"-DLLVM_LIBRARY_DIR=${llvm.lib}/lib"
"-DLLVM_MAIN_INCLUDE_DIR=${llvm.dev}/include"
"-DLLVM_TABLEGEN_EXE=${llvm.out}/bin/llvm-tblgen"
"-DLLVM_TOOLS_BINARY_DIR=${llvm.out}/bin"
"-DLLVM_BUILD_TOOLS=Off"
"-DLLVM_TOOL_CLING_BUILD=ON"

Expand Down Expand Up @@ -139,7 +141,7 @@ let
"-nostdinc"
"-nostdinc++"

"-resource-dir" "${llvmPackages_13.llvm.lib}/lib"
"-resource-dir" "${llvm.lib}/lib"

"-isystem" "${lib.getLib unwrapped}/lib/clang/${llvmPackages_13.clang.version}/include"
]
Expand Down
3 changes: 1 addition & 2 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7232,8 +7232,7 @@ with pkgs;
};

inherit (callPackage ../applications/editors/jupyter-kernels/xeus-cling { })
cpp11-kernel cpp14-kernel cpp17-kernel cpp2a-kernel;
xeus-cling = callPackage ../applications/editors/jupyter-kernels/xeus-cling/xeus-cling.nix { };
cpp11-kernel cpp14-kernel cpp17-kernel cpp2a-kernel xeus-cling;

clojure = callPackage ../development/interpreters/clojure {
# set this to an LTS version of java
Expand Down

0 comments on commit 89bea7b

Please sign in to comment.