1{ stdenv, vscode-utils
2, fetchurl, unzip
3, mono, writeScript, runtimeShell
4, jq, clang-tools
5, gdbUseFixed ? true, gdb # The gdb default setting will be fixed to specified. Use version from `PATH` otherwise.
6}:
7
8assert gdbUseFixed -> null != gdb;
9
10/*
11 Note that this version of the extension still has some nix specific issues
12 which could not be fixed merely by patching (inside a C# dll).
13
14 In particular, the debugger requires either gnome-terminal or xterm. However
15 instead of looking for the terminal executable in `PATH`, for any linux platform
16 the dll uses an hardcoded path to one of these.
17
18 So, in order for debugging to work properly, you merely need to create symlinks
19 to one of these terminals at the appropriate location.
20
21 The good news is the the utility library is open source and with some effort
22 we could build a patched version ourselves. See:
23
24 <https://github.com/Microsoft/MIEngine/blob/2885386dc7f35e0f1e44827269341e786361f28e/src/MICore/TerminalLauncher.cs#L156>
25
26 Also, the extension should eventually no longer require an external terminal. See:
27
28 <https://github.com/Microsoft/vscode-cpptools/issues/35>
29
30 Once the symbolic link temporary solution taken, everything shoud run smootly.
31*/
32
33let
34 gdbDefaultsTo = if gdbUseFixed then "${gdb}/bin/gdb" else "gdb";
35
36 langComponentBinaries = stdenv.mkDerivation rec {
37 name = "cpptools-language-component-binaries";
38
39 src = fetchurl {
40 # Follow https://go.microsoft.com/fwlink/?linkid=2037608
41 url = "https://download.visualstudio.microsoft.com/download/pr/fd05d7fd-b771-4746-9c54-b5b30afcd82e/1f443716d6156a265bf50cb6e53fa999/bin_linux.zip";
42 sha256 = "198xnq709clibjmd8rrv0haniy2m3qvhn89hg9hpj6lvg9lsr7a4";
43 };
44
45 sourceRoot = name;
46
47 nativeBuildInputs = [ unzip ];
48
49 unpackPhase = ''
50 runHook preUnpack
51 unzip -d $name $src || true
52 runHook postUnpack
53 '';
54
55 installPhase = ''
56 runHook preInstall
57 mkdir -p "$out/bin"
58 cp -a -t "$out/bin" ./bin/*
59 runHook postInstall
60 '';
61
62 postFixup = ''
63 elfInterpreter="$(cat $NIX_CC/nix-support/dynamic-linker)"
64 patchelf --set-interpreter "$elfInterpreter" $out/bin/Microsoft.VSCode.CPP.Extension.linux
65 patchelf --set-interpreter "$elfInterpreter" $out/bin/Microsoft.VSCode.CPP.IntelliSense.Msvc.linux
66 chmod a+x $out/bin/Microsoft.VSCode.CPP.Extension.linux $out/bin/Microsoft.VSCode.CPP.IntelliSense.Msvc.linux
67 '';
68 };
69
70 openDebugAD7Script = writeScript "OpenDebugAD7" ''
71 #!${runtimeShell}
72 BIN_DIR="$(cd "$(dirname "$0")" && pwd -P)"
73 ${if gdbUseFixed
74 then ''
75 export PATH=''${PATH}''${PATH:+:}${gdb}/bin
76 ''
77 else ""}
78 ${mono}/bin/mono $BIN_DIR/bin/OpenDebugAD7.exe $*
79 '';
80in
81
82vscode-utils.buildVscodeMarketplaceExtension {
83 mktplcRef = {
84 name = "cpptools";
85 publisher = "ms-vscode";
86 version = "0.26.3";
87 sha256 = "1rwyvqk3gp5f75x73d33biqvq67xx2vz1lmh3y3ax8kaf9z8jfvr";
88 };
89
90 buildInputs = [
91 jq
92 ];
93
94 postPatch = ''
95 mv ./package.json ./package_ori.json
96
97 # 1. Add activation events so that the extension is functional. This listing is empty when unpacking the extension but is filled at runtime.
98 # 2. Patch `package.json` so that nix's *gdb* is used as default value for `miDebuggerPath`.
99 cat ./package_ori.json | \
100 jq --slurpfile actEvts ${./package-activation-events.json} '(.activationEvents) = $actEvts[0]' | \
101 jq '(.contributes.debuggers[].configurationAttributes | .attach , .launch | .properties.miDebuggerPath | select(. != null) | select(.default == "/usr/bin/gdb") | .default) = "${gdbDefaultsTo}"' > \
102 ./package.json
103
104 # Prevent download/install of extensions
105 touch "./install.lock"
106
107 # Move unused files out of the way.
108 mv ./debugAdapters/bin/OpenDebugAD7.exe.config ./debugAdapters/bin/OpenDebugAD7.exe.config.unused
109
110 # Combining the language component binaries as part of our package.
111 find "${langComponentBinaries}/bin" -mindepth 1 -maxdepth 1 | xargs cp -p -t "./bin"
112
113 # Mono runtimes from nix package (used by generated `OpenDebugAD7`).
114 rm "./debugAdapters/OpenDebugAD7"
115 cp -p "${openDebugAD7Script}" "./debugAdapters/OpenDebugAD7"
116
117 # Clang-format from nix package.
118 mkdir -p "./LLVM"
119 find "${clang-tools}" -mindepth 1 -maxdepth 1 | xargs ln -s -t "./LLVM"
120 '';
121
122 meta = with stdenv.lib; {
123 license = licenses.unfree;
124 maintainers = [ maintainers.jraygauthier ];
125 # A 32 bit linux would also be possible with some effort (specific download of binaries +
126 # patching of the elf files with 32 bit interpreter).
127 platforms = [ "x86_64-linux" ];
128 };
129}