-
Notifications
You must be signed in to change notification settings - Fork 12
/
default.nix
316 lines (287 loc) · 10.6 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
{ alamgu ? import ./nix/dep/alamgu {}
, gitDescribe ? "TEST-dirty"
, debug ? false
, runTest ? true
}:
let
inherit (alamgu)
pkgs ledgerPkgs
gitignoreNix gitignoreSource
usbtool
speculos;
inherit (pkgs) lib;
nix-thunk = import ./nix/dep/nix-thunk { inherit pkgs; };
sources = nix-thunk.mapSubdirectories nix-thunk.thunkSource ./nix/dep;
patchSDKBinBash = name: sdk: pkgs.stdenv.mkDerivation {
# Replaces SDK's Makefile instances of /bin/bash with Nix's bash
name = name + "_patched_bin_bash";
src = sdk;
dontBuild = true;
installPhase = ''
mkdir -p $out
cp -a $src/. $out
substituteInPlace $out/Makefile.rules_generic --replace /bin/bash "${pkgs.bash}/bin/bash"
'';
};
targets =
{
s = rec {
name = "s";
sdk = patchSDKBinBash "nanos-secure-sdk" (sources.nanos-secure-sdk);
target = "TARGET_NANOS";
targetId = "0x31100004";
test = true;
iconHex = pkgs.runCommand "nano-s-icon-hex" {
nativeBuildInputs = [ (pkgs.python3.withPackages (ps: [ps.pillow])) ];
} ''
python ${sdk + /icon.py} '${icons/nano-s.gif}' hexbitmaponly > "$out"
'';
};
x = rec {
name = "x";
sdk = patchSDKBinBash "ledger-nanox-sdk" (sources.ledger-nanox-sdk);
target = "TARGET_NANOX";
targetId = "0x33000004";
test = false;
iconHex = pkgs.runCommand "${name}-icon-hex" {
nativeBuildInputs = [ (pkgs.python3.withPackages (ps: [ps.pillow])) ];
} ''
python '${sdk + /icon3.py}' --hexbitmaponly '${icons/nano-x.gif}' > "$out"
'';
};
};
gitIgnoredSrc = gitignoreSource ./.;
src0 = lib.sources.cleanSourceWith {
src = gitIgnoredSrc;
filter = p: _: let
p' = baseNameOf p;
srcStr = builtins.toString ./.;
in p' != "glyphs.c" && p' != "glyphs.h"
&& (p == (srcStr + "/Makefile")
|| lib.hasPrefix (srcStr + "/src") p
|| lib.hasPrefix (srcStr + "/glyphs") p
|| lib.hasPrefix (srcStr + "/tests") p
);
};
src = lib.sources.sourceFilesBySuffices src0 [
".c" ".h" ".gif" "Makefile" ".sh" ".json" ".js" ".bats" ".txt" ".der"
];
tests = import ./tests {
inherit pkgs;
nodejs = pkgs.nodejs-16_x;
};
build = bolos:
let
# We want GNU as not LLVM as, which doesn't something completely
# different, or Clang's internal assembler which doesn't take quite the
# same syntax. It also expects a C compiler to not complain about extra
# args, do C pre-processing, etc. We could get clang to use GNU as, but
# we'll just use GCC as they presumably do for this.
#
# Also assembler rule is broken not creating OBJ dir.
setAssembler = ''
export AS=${ledgerPkgs.gccStdenv.cc}/bin/${ledgerPkgs.clangStdenv.cc.targetPrefix}gcc
mkdir -p obj
'';
app = ledgerPkgs.lldClangStdenv.mkDerivation {
name = "ledger-app-avalanche-nano-${bolos.name}";
inherit src;
postConfigure = ''
patchShebangs test.sh
# hack to get around no tests for cross logic
doCheck=${toString (if runTest then bolos.test else false)};
export USE_NIX=1
'';
nativeBuildInputs = [
(pkgs.python3.withPackages (ps: [ps.pillow ps.ledgerblue]))
ledgerPkgs.buildPackages.bats
ledgerPkgs.buildPackages.entr
ledgerPkgs.buildPackages.gdb
ledgerPkgs.buildPackages.jq
ledgerPkgs.buildPackages.libusb
ledgerPkgs.buildPackages.openssl
ledgerPkgs.buildPackages.pkg-config
ledgerPkgs.buildPackages.xxd
speculos.speculos
tests.testScript
# usbtool
];
TARGET = bolos.target;
GIT_DESCRIBE = gitDescribe;
BOLOS_SDK = bolos.sdk;
# note trailing slash
CLANGPATH = "${ledgerPkgs.lldClangStdenv.cc}/bin/";
GCCPATH = "${ledgerPkgs.stdenv.cc}/bin/";
DEBUG=if debug then "1" else "0";
# Do both ways, so nix builds and users running make both work.
preBuild = setAssembler;
shellHook = ''
export USE_NIX=1
'' + setAssembler;
installPhase = ''
mkdir -p $out
cp -R bin $out
cp -R debug $out
echo
echo ">>>> Application size: <<<<"
$SIZE $out/bin/app.elf
'';
checkTarget = "test";
enableParallelBuilding = true;
};
## Note: This has been known to change between sdk upgrades. Make sure to consult
## the $COMMON_LOAD_PARAMS in the Makefile.defines of both SDKs
nvramDataSize = appDir: deviceName:
let mapPath = appDir + /debug/app.map;
in pkgs.runCommand "nvram-data-size" {} ''
nvram_data=0x${ if deviceName == "s"
then "$(grep _nvram_data "+ mapPath + " | tr -s ' ' | cut -f2 -d' ' | cut -f2 -d'x')"
else "$(grep _nvram_data "+ mapPath + " | cut -f1 -d' ')"
}
envram_data=0x${ if deviceName == "s"
then "$(grep _envram_data "+ mapPath + " | tr -s ' ' | cut -f2 -d' '| cut -f2 -d'x')"
else "$(grep _envram_data "+ mapPath + " | cut -f1 -d' ')"
}
echo "$(($envram_data - $nvram_data))" > "$out"
'';
mkRelease = short_name: name: appDir: pkgs.runCommand "${short_name}-nano-${bolos.name}-release-dir" {} ''
mkdir -p "$out"
cp '${appDir + /bin/app.hex}' "$out/app.hex"
cat > "$out/app.manifest" <<EOF
name='${name}'
nvram_size=$(cat '${nvramDataSize appDir bolos.name}')
target='nano_${bolos.name}'
target_id=${bolos.targetId}
version=$(echo '${gitDescribe}' | cut -f1 -d- | cut -f2 -dv)
icon_hex=$(cat '${bolos.iconHex}')
EOF
'';
ledgerApp = app;
in {
inherit app;
release = rec {
app = mkRelease "avalanche" "Avalanche" ledgerApp;
all = pkgs.runCommand "ledger-app-avalanche-${bolos.name}${if debug then "-debug" else ""}.tar.gz" {
nativeBuildInputs = [ (pkgs.python3.withPackages (ps: [ps.pillow ps.ledgerblue])) ];
} ''
mkdir ledger-app-avalanche-${bolos.name}
cp -r ${app} ledger-app-avalanche-${bolos.name}/app
source ${app}/app.manifest
python -m ledgerblue.hashApp \
--hex "${app}/app.hex" \
--targetVersion "" > ledger-app-avalanche-${bolos.name}/code-identifier.txt
install -m a=rx ${./nix/app-installer-impl.sh} ledger-app-avalanche-${bolos.name}/install.sh
tar czf $out ledger-app-avalanche-${bolos.name}/*
'';
};
};
# The package clang-analyzer comes with a perl script `scan-build` that seems
# to get quickly lost with the cross-compiler of the SDK if run by itself.
# So this script reproduces what it does with fewer magic attempts:
# * It prepares the SDK like for a normal build.
# * It intercepts the calls to the compiler with the `CC` make-variable
# (pointing at `.../libexec/ccc-analyzer`).
# * The `CCC_*` variables are used to configure `ccc-analyzer`: output directory
# and which *real* compiler to call after doing the analysis.
# * After the build an `index.html` file is created to point to the individual
# result pages.
#
# See
# https://clang-analyzer.llvm.org/alpha_checks.html#clone_alpha_checkers
# for the list of extra analyzers that are run.
#
runClangStaticAnalyzer =
let
interestingExtrasAnalyzers = [
# "alpha.clone.CloneChecker" # this one is waaay too verbose
"alpha.security.ArrayBound"
"alpha.security.ArrayBoundV2"
"alpha.security.MallocOverflow"
# "alpha.security.MmapWriteExec" # errors as “not found” by ccc-analyzer
"alpha.security.ReturnPtrRange"
"alpha.security.taint.TaintPropagation"
"alpha.deadcode.UnreachableCode"
"alpha.core.CallAndMessageUnInitRefArg"
"alpha.core.CastSize"
"alpha.core.CastToStruct"
"alpha.core.Conversion"
# "alpha.core.FixedAddr" # Seems noisy, and about portability.
"alpha.core.IdenticalExpr"
"alpha.core.PointerArithm"
"alpha.core.PointerSub"
"alpha.core.SizeofPtr"
# "alpha.core.StackAddressAsyncEscape" # Also not found
"alpha.core.TestAfterDivZero"
"alpha.unix.cstring.BufferOverlap"
"alpha.unix.cstring.NotNullTerminated"
"alpha.unix.cstring.OutOfBounds"
];
analysisOptions =
pkgs.lib.strings.concatMapStringsSep
" "
(x: "-analyzer-checker " + x)
interestingExtrasAnalyzers;
in bolos: ((build bolos).app).overrideAttrs (old: {
CCC_ANALYZER_HTML = "${placeholder "out"}";
CCC_ANALYZER_OUTPUT_FORMAT = "html";
CCC_ANALYZER_ANALYSIS = analysisOptions;
preBuild = (old.preBuild or "") + ''
mkdir -p $out
export CCC_CC=$CC
export CCC_CXX=$CXX
'';
makeFlags = old.makeFlags or []
++ [ "CC=${pkgs.clangAnalyzer}/libexec/ccc-analyzer" ];
installPhase = ''
{
echo "<html><title>Analyzer Report</title><body><h1>Clang Static Analyzer Results</h1>"
printf "<p>App: <code>"Avalanche"</code></p>"
printf "<h2>File-results:</h2>"
for html in "$out"/report*.html ; do
echo "<p>"
printf "<code>"
grep BUGFILE "$html" | sed 's/^<!-- [A-Z]* \(.*\) -->$/\1/'
printf "</code>"
printf "<code style=\"color: green\">+"
grep BUGLINE "$html" | sed 's/^<!-- [A-Z]* \(.*\) -->$/\1/'
printf "</code><br/>"
grep BUGDESC "$html" | sed 's/^<!-- [A-Z]* \(.*\) -->$/\1/'
printf " → <a href=\"./%s\">full-report</a>" "$(basename "$html")"
echo "</p>"
done
echo "</body></html>"
} > "$out/index.html"
'';
});
mkTargets = mk: {
s = mk targets.s;
x = mk targets.x;
};
in rec {
inherit
pkgs
usbtool;
nano = mkTargets build;
wallet = {
s = nano.s;
x = nano.x;
};
clangAnalysis = mkTargets (bolos: {
wallet = runClangStaticAnalyzer bolos;
});
env = mkTargets (bolos: {
ide = {
config = {
vscode = pkgs.writeText "vscode-nano-${bolos.name}.code-workspace" (builtins.toJSON {
folders = [ { path = "."; } ];
settings = {
"clangd.path" = pkgs.llvmPackages.clang-unwrapped + /bin/clangd;
};
});
};
};
inherit (bolos) sdk;
});
inherit speculos;
inherit tests;
}