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