···11+{ stdenv, lib, fetchurl, vscode-utils, unzip, dos2unix, mono46, clang-tools, writeScript
22+, gdbUseFixed ? true, gdb # The gdb default setting will be fixed to specified. Use version from `PATH` otherwise.
33+}:
44+55+assert gdbUseFixed -> null != gdb;
66+77+/*
88+ Note that this version of the extension still has some nix specific issues
99+ which could not be fixed merely by patching (inside a C# dll).
1010+1111+ In particular, the debugger requires either gnome-terminal or xterm. However
1212+ instead of looking for the terminal executable in `PATH`, for any linux platform
1313+ the dll uses an hardcoded path to one of these.
1414+1515+ So, in order for debugging to work properly, you merely need to create symlinks
1616+ to one of these terminals at the appropriate location.
1717+1818+ The good news is the the utility library is open source and with some effort
1919+ we could build a patched version ourselves. See:
2020+2121+ <https://github.com/Microsoft/MIEngine/blob/2885386dc7f35e0f1e44827269341e786361f28e/src/MICore/TerminalLauncher.cs#L156>
2222+2323+ Also, the extension should eventually no longer require an external terminal. See:
2424+2525+ <https://github.com/Microsoft/vscode-cpptools/issues/35>
2626+2727+ Once the symbolic link temporary solution taken, everything shoud run smootly.
2828+*/
2929+3030+let
3131+ gdbDefaultsTo = if gdbUseFixed then "${gdb}/bin/gdb" else "gdb";
3232+3333+ langComponentBinaries = stdenv.mkDerivation {
3434+ name = "cpptools-language-component-binaries";
3535+3636+ src = fetchurl {
3737+ url = https://download.visualstudio.microsoft.com/download/pr/11151953/d3cc8b654bffb8a2f3896d101f3c3155/Bin_Linux.zip;
3838+ sha256 = "12qbxsrdc73cqjb84xdck1xafzhfkcyn6bqbpcy1bxxr3b7hxbii";
3939+ };
4040+4141+ buildInputs = [ unzip ];
4242+4343+ patchPhase = ''
4444+ elfInterpreter="${stdenv.glibc.out}/lib/ld-linux-x86-64.so.2"
4545+ patchelf --set-interpreter "$elfInterpreter" ./Microsoft.VSCode.CPP.Extension.linux
4646+ patchelf --set-interpreter "$elfInterpreter" ./Microsoft.VSCode.CPP.IntelliSense.Msvc.linux
4747+ chmod a+x ./Microsoft.VSCode.CPP.Extension.linux ./Microsoft.VSCode.CPP.IntelliSense.Msvc.linux
4848+ '';
4949+5050+ installPhase = ''
5151+ mkdir -p "$out/bin"
5252+ find . -mindepth 1 -maxdepth 1 | xargs cp -a -t "$out/bin"
5353+ '';
5454+ };
5555+5656+ cpptoolsJsonFile = fetchurl {
5757+ url = https://download.visualstudio.microsoft.com/download/pr/11070848/7b97d6724d52cae8377c61bb4601c989/cpptools.json;
5858+ sha256 = "124f091aic92rzbg2vg831y22zr5wi056c1kh775djqs3qv31ja6";
5959+ };
6060+6161+6262+6363+ openDebugAD7Script = writeScript "OpenDebugAD7" ''
6464+ #!${stdenv.shell}
6565+ BIN_DIR="$(cd "$(dirname "$0")" && pwd -P)"
6666+ ${if gdbUseFixed
6767+ then ''
6868+ export PATH=''${PATH}''${PATH:+:}${gdb}/bin
6969+ ''
7070+ else ""}
7171+ ${mono46}/bin/mono $BIN_DIR/bin/OpenDebugAD7.exe $*
7272+ '';
7373+in
7474+7575+vscode-utils.buildVscodeMarketplaceExtension {
7676+ mktplcRef = {
7777+ name = "cpptools";
7878+ publisher = "ms-vscode";
7979+ version = "0.12.3";
8080+ sha256 = "1dcqy54n1w29xhbvxscd41hdrbdwar6g12zx02f6kh2f1kw34z5z";
8181+ };
8282+8383+ buildInputs = [
8484+ dos2unix
8585+ ];
8686+8787+ prePatch = ''
8888+ dos2unix package.json
8989+ '';
9090+9191+ patches = [
9292+ ./vscode-cpptools-0-12-3-package-json.patch
9393+ ];
9494+9595+ postPatch = ''
9696+ # Patch `packages.json` so that nix's *gdb* is used as default value for `miDebuggerPath`.
9797+ substituteInPlace "./package.json" \
9898+ --replace "\"default\": \"/usr/bin/gdb\"" "\"default\": \"${gdbDefaultsTo}\""
9999+100100+ # Prevent download/install of extensions
101101+ touch "./install.lock"
102102+103103+ # Move unused files out of the way.
104104+ mv ./debugAdapters/bin/OpenDebugAD7.exe.config ./debugAdapters/bin/OpenDebugAD7.exe.config.unused
105105+106106+ # Bring the `cpptools.json` file at the root of the package, same as the extension would do.
107107+ cp -p "${cpptoolsJsonFile}" "./cpptools.json"
108108+109109+ # Combining the language component binaries as part of our package.
110110+ find "${langComponentBinaries}/bin" -mindepth 1 -maxdepth 1 | xargs cp -p -t "./bin"
111111+112112+ # Mono runtimes from nix package (used by generated `OpenDebugAD7`).
113113+ rm "./debugAdapters/OpenDebugAD7"
114114+ cp -p "${openDebugAD7Script}" "./debugAdapters/OpenDebugAD7"
115115+116116+ # Clang-format from nix package.
117117+ mkdir -p "./LLVM"
118118+ find "${clang-tools}" -mindepth 1 -maxdepth 1 | xargs ln -s -t "./LLVM"
119119+ '';
120120+121121+ meta = with stdenv.lib; {
122122+ license = licenses.unfree;
123123+ maintainer = [ maintainers.jraygauthier ];
124124+ # A 32 bit linux would also be possible with some effort (specific download of binaries +
125125+ # patching of the elf files with 32 bit interpreter).
126126+ platforms = [ "x86_64-linux" ];
127127+ };
128128+}