1#!/usr/bin/env nix-shell
2#! nix-shell -p coreutils -p jq -p unzip -i bash
3set -euo pipefail
4
5#
6# A little script to help maintaining this package. It will:
7#
8# - download the specified version of the extension to the store and print its url, packed store path and hash
9# - unpack the extension, bring it to the store and print its store path and hash
10# - fetch its runtimes dependencies from the 'package.json' file using the 'jq' utility, unpack those to the store
11# and print its url store path and hash
12# - patch elf of the binaries that got a nix replacement
13# - bring the patched version to the store
14# - run their '--version' and call 'ldd'
15# - print the version of the runtime deps nix replacements.
16#
17# TODO: Print to a properly formated nix file all the required information to fetch everything (extension + runtime deps).
18# TODO: Print x86 and maybe darwin runtime dependencies.
19#
20
21scriptDir=$(cd "`dirname "$0"`"; pwd)
22echo "scriptDir='$scriptDir'"
23
24extPublisher="vscode"
25extName="cpptools"
26defaultExtVersion="0.16.1"
27extVersion="${1:-$defaultExtVersion}"
28
29echo
30echo "------------- Downloading extension ---------------"
31
32extZipStoreName="${extPublisher}-${extName}.zip"
33extUrl="https://ms-vscode.gallery.vsassets.io/_apis/public/gallery/publisher/ms-vscode/extension/cpptools/${extVersion}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage"
34echo "extUrl='$extUrl'"
35storePathWithSha=$(nix-prefetch-url --name "$extZipStoreName" --print-path "$extUrl" 2> /dev/null)
36
37cpptoolsZipStorePath="$(echo "$storePathWithSha" | tail -n1)"
38cpptoolsZipSha256="$(echo "$storePathWithSha" | head -n1)"
39echo "cpptoolsZipStorePath='$cpptoolsZipStorePath'"
40echo "cpptoolsZipSha256='$cpptoolsZipSha256'"
41
42
43extStoreName="${extPublisher}-${extName}"
44
45
46function rm_tmpdir() {
47 #echo "Removing \`tmpDir='$tmpDir'\`"
48 rm -rf -- "$tmpDir"
49 unset tmpDir
50 trap - INT TERM HUP EXIT
51}
52function make_trapped_tmpdir() {
53 tmpDir=$(mktemp -d)
54 trap rm_tmpdir INT TERM HUP EXIT
55}
56
57echo
58echo "------------- Unpacked extension ---------------"
59
60make_trapped_tmpdir
61unzip -q -d "$tmpDir" "$cpptoolsZipStorePath"
62
63cpptoolsStorePath="$(nix add-to-store -n "$extStoreName" "$tmpDir")"
64cpptoolsSha256="$(nix hash-path --base32 --type sha512 "$cpptoolsStorePath")"
65echo "cpptoolsStorePath='$cpptoolsStorePath'"
66echo "cpptoolsSha256='$cpptoolsSha256'"
67
68rm_tmpdir
69
70storePathWithSha=$(nix-prefetch-url --print-path "file://${cpptoolsStorePath}/extension/package.json" 2> /dev/null)
71
72extPackageJSONStorePath="$(echo "$storePathWithSha" | tail -n1)"
73extPackageJSONSha256="$(echo "$storePathWithSha" | head -n1)"
74echo "extPackageJSONStorePath='$extPackageJSONStorePath'"
75echo "extPackageJSONSha256='$extPackageJSONSha256'"
76
77print_runtime_dep() {
78
79 local outName="$1"
80 local extPackageJSONStorePath="$2"
81 local depDesc="$3"
82
83 local urlRaw=$(cat "$extPackageJSONStorePath" | jq -r --arg desc "$depDesc" '.runtimeDependencies[] | select(.description == $desc) | .url')
84 local url=$(echo $urlRaw | xargs curl -Ls -o /dev/null -w %{url_effective})
85
86 local urlRawVarStr="${outName}_urlRaw='$urlRaw'"
87 local urlVarStr="${outName}_url='$url'"
88 echo "$urlRawVarStr"
89 echo "$urlVarStr"
90
91 local storePathWithSha="$(nix-prefetch-url --unpack --print-path "$url" 2> /dev/null)"
92
93 local storePath="$(echo "$storePathWithSha" | tail -n1)"
94 local sha256="$(echo "$storePathWithSha" | head -n1)"
95
96 local sha256VarStr="${outName}_sha256='$sha256'"
97 local storePathVarStr="${outName}_storePath='$storePath'"
98 echo "$sha256VarStr"
99 echo "$storePathVarStr"
100
101 eval "$urlRawVarStr"
102 eval "$urlVarStr"
103 eval "$sha256VarStr"
104 eval "$storePathVarStr"
105}
106
107echo
108echo "------------- Runtime dependencies ---------------"
109
110print_runtime_dep "langComponentBinaries" "$extPackageJSONStorePath" "C/C++ language components (Linux / x86_64)"
111print_runtime_dep "monoRuntimeBinaries" "$extPackageJSONStorePath" "Mono Runtime (Linux / x86_64)"
112print_runtime_dep "clanFormatBinaries" "$extPackageJSONStorePath" "ClangFormat (Linux / x86_64)"
113
114
115echo
116echo "------------- Runtime deps missing elf deps ---------------"
117
118source "$scriptDir/missing_elf_deps.sh"
119
120echo
121echo "------------- Runtime dep mono ---------------"
122
123make_trapped_tmpdir
124find "$monoRuntimeBinaries_storePath" -mindepth 1 -maxdepth 1 | xargs -d '\n' cp -rp -t "$tmpDir"
125chmod -R a+rwx "$tmpDir"
126
127ls -la "$tmpDir/debugAdapters"
128
129patchelf_mono "$tmpDir/debugAdapters/mono.linux-x86_64"
130
131chmod a+x "$tmpDir/debugAdapters/mono.linux-x86_64"
132ldd "$tmpDir/debugAdapters/mono.linux-x86_64"
133"$tmpDir/debugAdapters/mono.linux-x86_64" --version
134
135monoRuntimeBinariesPatched_storePath="$(nix add-to-store -n "monoRuntimeBinariesPatched" "$tmpDir")"
136echo "monoRuntimeBinariesPatched_storePath='$monoRuntimeBinariesPatched_storePath'"
137
138rm_tmpdir
139
140
141echo
142echo "------------- Runtime dep clang ---------------"
143make_trapped_tmpdir
144find "$clanFormatBinaries_storePath" -mindepth 1 -maxdepth 1 | xargs -d '\n' cp -rp -t "$tmpDir"
145chmod -R a+rwx "$tmpDir"
146
147ls -la "$tmpDir/bin"
148
149patchelf_clangformat "$tmpDir/bin/clang-format"
150
151chmod a+x "$tmpDir/bin/clang-format"
152ldd "$tmpDir/bin/clang-format"
153"$tmpDir/bin/clang-format" --version
154
155
156clanFormatBinariesPatched_storePath="$(nix add-to-store -n "clanFormatBinariesPatched" "$tmpDir")"
157echo "clanFormatBinariesPatched_storePath='$clanFormatBinariesPatched_storePath'"
158
159rm_tmpdir
160
161echo
162echo "------------- Nix mono ---------------"
163print_nix_version_clangtools
164
165echo
166echo "------------- Nix mono ---------------"
167print_nix_version_mono
168