···1+{ stdenv, lib, fetchurl, vscode-utils, unzip, dos2unix, mono46, clang-tools, writeScript
2+, gdbUseFixed ? true, gdb # The gdb default setting will be fixed to specified. Use version from `PATH` otherwise.
3+}:
4+5+assert 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+30+let
31+ gdbDefaultsTo = if gdbUseFixed then "${gdb}/bin/gdb" else "gdb";
32+33+ langComponentBinaries = stdenv.mkDerivation {
34+ name = "cpptools-language-component-binaries";
35+36+ src = fetchurl {
37+ url = https://download.visualstudio.microsoft.com/download/pr/11151953/d3cc8b654bffb8a2f3896d101f3c3155/Bin_Linux.zip;
38+ sha256 = "12qbxsrdc73cqjb84xdck1xafzhfkcyn6bqbpcy1bxxr3b7hxbii";
39+ };
40+41+ buildInputs = [ unzip ];
42+43+ patchPhase = ''
44+ elfInterpreter="${stdenv.glibc.out}/lib/ld-linux-x86-64.so.2"
45+ patchelf --set-interpreter "$elfInterpreter" ./Microsoft.VSCode.CPP.Extension.linux
46+ patchelf --set-interpreter "$elfInterpreter" ./Microsoft.VSCode.CPP.IntelliSense.Msvc.linux
47+ chmod a+x ./Microsoft.VSCode.CPP.Extension.linux ./Microsoft.VSCode.CPP.IntelliSense.Msvc.linux
48+ '';
49+50+ installPhase = ''
51+ mkdir -p "$out/bin"
52+ find . -mindepth 1 -maxdepth 1 | xargs cp -a -t "$out/bin"
53+ '';
54+ };
55+56+ cpptoolsJsonFile = fetchurl {
57+ url = https://download.visualstudio.microsoft.com/download/pr/11070848/7b97d6724d52cae8377c61bb4601c989/cpptools.json;
58+ sha256 = "124f091aic92rzbg2vg831y22zr5wi056c1kh775djqs3qv31ja6";
59+ };
60+61+62+63+ openDebugAD7Script = writeScript "OpenDebugAD7" ''
64+ #!${stdenv.shell}
65+ BIN_DIR="$(cd "$(dirname "$0")" && pwd -P)"
66+ ${if gdbUseFixed
67+ then ''
68+ export PATH=''${PATH}''${PATH:+:}${gdb}/bin
69+ ''
70+ else ""}
71+ ${mono46}/bin/mono $BIN_DIR/bin/OpenDebugAD7.exe $*
72+ '';
73+in
74+75+vscode-utils.buildVscodeMarketplaceExtension {
76+ mktplcRef = {
77+ name = "cpptools";
78+ publisher = "ms-vscode";
79+ version = "0.12.3";
80+ sha256 = "1dcqy54n1w29xhbvxscd41hdrbdwar6g12zx02f6kh2f1kw34z5z";
81+ };
82+83+ buildInputs = [
84+ dos2unix
85+ ];
86+87+ prePatch = ''
88+ dos2unix package.json
89+ '';
90+91+ patches = [
92+ ./vscode-cpptools-0-12-3-package-json.patch
93+ ];
94+95+ postPatch = ''
96+ # Patch `packages.json` so that nix's *gdb* is used as default value for `miDebuggerPath`.
97+ substituteInPlace "./package.json" \
98+ --replace "\"default\": \"/usr/bin/gdb\"" "\"default\": \"${gdbDefaultsTo}\""
99+100+ # Prevent download/install of extensions
101+ touch "./install.lock"
102+103+ # Move unused files out of the way.
104+ mv ./debugAdapters/bin/OpenDebugAD7.exe.config ./debugAdapters/bin/OpenDebugAD7.exe.config.unused
105+106+ # Bring the `cpptools.json` file at the root of the package, same as the extension would do.
107+ cp -p "${cpptoolsJsonFile}" "./cpptools.json"
108+109+ # Combining the language component binaries as part of our package.
110+ find "${langComponentBinaries}/bin" -mindepth 1 -maxdepth 1 | xargs cp -p -t "./bin"
111+112+ # Mono runtimes from nix package (used by generated `OpenDebugAD7`).
113+ rm "./debugAdapters/OpenDebugAD7"
114+ cp -p "${openDebugAD7Script}" "./debugAdapters/OpenDebugAD7"
115+116+ # Clang-format from nix package.
117+ mkdir -p "./LLVM"
118+ find "${clang-tools}" -mindepth 1 -maxdepth 1 | xargs ln -s -t "./LLVM"
119+ '';
120+121+ meta = with stdenv.lib; {
122+ license = licenses.unfree;
123+ maintainer = [ maintainers.jraygauthier ];
124+ # A 32 bit linux would also be possible with some effort (specific download of binaries +
125+ # patching of the elf files with 32 bit interpreter).
126+ platforms = [ "x86_64-linux" ];
127+ };
128+}