-
Notifications
You must be signed in to change notification settings - Fork 51
/
default.nix
231 lines (214 loc) · 8.27 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
{ pkgs ? import ./nix/dep/nixpkgs {}, gitDescribe ? "TEST-dirty", nanoXSdk ? null, ... }:
let
fetchThunk = p:
if builtins.pathExists (p + /git.json)
then pkgs.fetchgit { inherit (builtins.fromJSON (builtins.readFile (p + /git.json))) url rev sha256; }
else if builtins.pathExists (p + /github.json)
then pkgs.fetchFromGitHub { inherit (builtins.fromJSON (builtins.readFile (p + /github.json))) owner repo rev sha256; }
else p;
targets =
{
s = rec {
name = "s";
sdk = fetchThunk ./nix/dep/nanos-secure-sdk;
env = pkgs.callPackage ./nix/bolos-env.nix { clangVersion = 4; };
target = "TARGET_NANOS";
targetId = "0x31100004";
iconHex = pkgs.runCommand "nano-s-icon-hex" {
nativeBuildInputs = [ (pkgs.python.withPackages (ps: [ps.pillow])) ];
} ''
python ${sdk + /icon.py} '${icons/nano-s-tezos.gif}' hexbitmaponly > "$out"
'';
};
x = rec {
name = "x";
sdk = if nanoXSdk == null
then throw "No NanoX SDK"
else assert builtins.typeOf nanoXSdk == "path"; nanoXSdk;
env = pkgs.callPackage ./nix/bolos-env.nix { clangVersion = 7; };
target = "TARGET_NANOX";
targetId = "0x33000004";
iconHex = pkgs.runCommand "${name}-icon-hex" {
nativeBuildInputs = [ (pkgs.python3.withPackages (ps: [ps.pillow])) ];
} ''
python '${sdk + /icon3.py}' --hexbitmaponly '${icons/nano-x-tezos.gif}' > "$out"
'';
};
};
src = pkgs.lib.sources.sourceFilesBySuffices (pkgs.lib.sources.cleanSource ./.) [".c" ".h" ".gif" "Makefile" ".sh" ".json"];
build = bolos:
let
app = bakingApp: pkgs.stdenv.mkDerivation {
name = "ledger-app-tezos-nano-${bolos.name}-${if bakingApp then "baking" else "wallet"}";
inherit src;
postConfigure = ''
PATH="$BOLOS_ENV/clang-arm-fropi/bin:$PATH"
'';
nativeBuildInputs = [
(pkgs.python3.withPackages (ps: [ps.pillow ps.ledgerblue]))
pkgs.jq
];
TARGET = bolos.target;
GIT_DESCRIBE = gitDescribe;
BOLOS_SDK = bolos.sdk;
BOLOS_ENV = bolos.env;
makeFlags = [
"APP=${if bakingApp then "tezos_baking" else "tezos_wallet"}"
];
installPhase = ''
mkdir -p $out
cp -R bin $out
cp -R debug $out
echo
echo ">>>> Application size: <<<<"
size $out/bin/app.elf
'';
};
nvramDataSize = appDir: pkgs.runCommand "nvram-data-size" {} ''
envram_data="$(grep _envram_data '${appDir + /debug/app.map}' | tr -s ' ' | cut -f2 -d' ')"
nvram_data="$(grep _nvram_data '${appDir + /debug/app.map}' | tr -s ' ' | cut -f2 -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}')
target='nano_${bolos.name}'
target_id=${bolos.targetId}
version=$(echo '${gitDescribe}' | cut -f1 -d- | cut -f2 -dv)
icon_hex=$(cat '${bolos.iconHex}')
EOF
'';
walletApp = app false;
bakingApp = app true;
in {
wallet = walletApp;
baking = bakingApp;
release = rec {
wallet = mkRelease "wallet" "Tezos Wallet" walletApp;
baking = mkRelease "baking" "Tezos Baking" bakingApp;
all = pkgs.runCommand "ledger-app-tezos-${bolos.name}.tar.gz" {} ''
mkdir ledger-app-tezos-${bolos.name}
cp -r ${wallet} ledger-app-tezos-${bolos.name}/wallet
# No baking app for Nano X yet
${pkgs.lib.optionalString (bolos.name == "s") ''
cp -r ${baking} ledger-app-tezos-${bolos.name}/baking
''}
install -m a=rx ${./release-installer.sh} ledger-app-tezos-${bolos.name}/install.sh
tar czf $out ledger-app-tezos-${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/scan-build/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 bakingApp: bolos: ((build bolos).${if bakingApp then "baking" else "wallet"}).overrideAttrs (old: {
CCC_ANALYZER_HTML = "${placeholder "out"}";
CCC_ANALYZER_OUTPUT_FORMAT = "html";
CCC_ANALYZER_ANALYSIS = analysisOptions;
CCC_CC = "${bolos.env}/clang-arm-fropi/bin/clang";
CLANG = "${bolos.env}/clang-arm-fropi/bin/clang";
preBuild = ''
mkdir -p $out
'';
makeFlags = old.makeFlags or []
++ [ "CC=${pkgs.clangAnalyzer}/libexec/scan-build/ccc-analyzer" ];
installPhase = ''
{
echo "<html><title>Analyzer Report</title><body><h1>Clang Static Analyzer Results</h1>"
printf "<p>App: <code>${if bakingApp then "tezos_baking" else "tezos_wallet"}</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 {
nano = mkTargets build;
wallet = {
s = nano.s.wallet;
x = nano.x.wallet;
};
baking = {
s = nano.s.baking;
x = nano.x.baking;
};
clangAnalysis = mkTargets (bolos: {
baking = runClangStaticAnalyzer true bolos;
wallet = runClangStaticAnalyzer false 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.env) clang gcc;
inherit (bolos) sdk;
});
}