aboutsummaryrefslogtreecommitdiffstats
path: root/build-root/Makefile
blob: a3335236c041c19fc74fd0756a38bfa8f341e262 (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
# Copyright (c) 2017 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

*** Settings ***
| Resource | resources/libraries/robot/performance/performance_setup.robot
| Resource | resources/libraries/robot/crypto/ipsec.robot
| Resource | resources/libraries/robot/overlay/lispgpe.robot
| ...
| Variables | resources/test_data/lisp/performance/lisp_static_adjacency.py
| Force Tags | 3_NODE_SINGLE_LINK_TOPO | PERFTEST | HW_ENV | NDRPDRDISC | IP4FWD
| ... | LISPGPE | IPSEC | IPSECHW | IPSECTRAN | ENCAP | IP4UNRLAY | IP4OVRLAY
| ... | NIC_Intel-XL710
| ...
| Suite Setup | Set up IPSec performance test suite | L3 | Intel-XL710
| ... | HW_cryptodev
| ...
| Suite Teardown | Tear down 3-node performance topology
| ...
| Test Setup | Set up performance test
| ...
| Test Teardown | Tear down performance discovery test | ${min_rate}pps
| ... | ${framesize} | ${traffic_profile}
| ...
| Documentation | *IPv4 IPsec transport mode performance test suite.*
| ...
| ... | *[Top] Network Topologies:* TG-DUT1-DUT2-TG 3-node circular topology\
| ... | with single links between nodes.
| ... | *[Enc] Packet Encapsulations:* Eth-IPv4 on TG-DUTn,\
| ... | Eth-IPv4-IPSec-LISPGPE-IPv4 on DUT1-DUT2
| ... | *[Cfg] DUT configuration:* Each DUT is configured with LISP and IPsec\
| ... | in each direction. IPsec is in transport mode. DUTs get IPv4 traffic\
| ... | from TG, encrypt it and send to another DUT, where packets are\
| ... | decrypted and sent back to TG.
| ... | *[Ver] TG verification:* TG finds and reports throughput NDR (Non Drop\
| ... | Rate) with zero packet loss tolerance or throughput PDR (Partial Drop\
| ... | Rate) with non-zero packet loss tolerance (LT) expressed in number\
| ... | of packets transmitted. NDR is discovered for different\
| ... | number of IPsec tunnels using binary search algorithms with configured\
| ... | starting rate and final step that determines throughput measurement\
| ... | resolution. Test packets are generated by TG on\
| ... | links to DUTs. TG traffic profile contains two L3 flow-groups\
| ... | (flow-group per direction, number of flows per flow-group equals to\
| ... | number of IPSec tunnels) with all packets\
| ... | containing Ethernet header, IPv4 header with IP protocol=61 and\
| ... | static payload. MAC addresses are matching MAC addresses of the TG\
| ... | node interfaces. Incrementing of IP.dst (IPv4 destination address)\
| ... | field is applied to both streams.
| ... | *[Ref] Applicable standard specifications:* RFC6830, RFC4303 and\
| ... | RFC2544.

*** Variables ***
# XL710-DA2 bandwidth limit ~49Gbps/2=24.5Gbps
| ${s_24.5G}= | ${24500000000}
# XL710-DA2 Mpps limit 37.5Mpps/2=18.75Mpps
| ${s_18.75Mpps}= | ${18750000}
| ${dut2_spi}= | ${1000}
| ${dut1_spi}= | ${1001}
| ${ESP_PROTO}= | ${50}
| ${ipsec_overhead}= | ${54}
# Traffic profile:
| ${traffic_profile}= | trex-sl-3n-ethip4-ip4src253

*** Test Cases ***
| tc01-64B-1t1c-ethip4ipsectptlispgpe-ip4base-cbc-sha1-ndrdisc
| | [Documentation]
| | ... | [Cfg] Each DUT is configured with LISP and IPsec in each direction.\
| | ... | IPsec is in transport mode. DUTs are configured with 1 phy\
| | ... | core, 1 receive queue per NIC port.
| | ... | [Ver] Find NDR for 64 Byte frames using binary search start at 40GE\
| | ... | linerate, step 50kpps.
| | ...
| | [Tags] | 64B | 1C | NDRDISC
| | ...
| | # FIXME: Move repeated lines into a keyword.
| | ${framesize}= | Set Variable | ${64}
| | ${min_rate}= | Set Variable | ${50000}
| | ${max_rate}= | Set Variable | ${s_18.75Mpps}
| | ${binary_min}= | Set Variable
@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
# Copyright (c) 2015 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Copyright (c) 2007-2008 Eliot Dresselhaus
#
#  Permission is hereby granted, free of charge, to any person obtaining
#  a copy of this software and associated documentation files (the
#  "Software"), to deal in the Software without restriction, including
#  without limitation the rights to use, copy, modify, merge, publish,
#  distribute, sublicense, and/or sell copies of the Software, and to
#  permit persons to whom the Software is furnished to do so, subject to
#  the following conditions:
#
#  The above copyright notice and this permission notice shall be
#  included in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
#  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
#  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

######################################################################
# Collect makefile fragments
######################################################################

# Scripts require non-POSIX parts of bash
SHELL := /bin/bash

# Where this makefile lives
MU_BUILD_ROOT_DIR = $(shell pwd)
MU_BUILD_NAME = $(shell basename $(MU_BUILD_ROOT_DIR))

# Search path (e.g. multiple directories) where sources are found.
SOURCE_PATH =

# Pick up user's definitions for variables e.g. SOURCE_PATH, etc.
-include build-config.mk

MU_BUILD_ROOT_NAME = $(shell basename $(MU_BUILD_ROOT_DIR))
MU_BUILD_DATA_DIR_NAME = build-data

ABSOLUTE_SOURCE_PATH = $(foreach d,$(SOURCE_PATH),$(shell cd $(d) && pwd))

SOURCE_PATH_BUILD_ROOT_DIRS = $(addsuffix /$(MU_BUILD_NAME),$(ABSOLUTE_SOURCE_PATH))
SOURCE_PATH_BUILD_DATA_DIRS = $(addsuffix /$(MU_BUILD_DATA_DIR_NAME),$(ABSOLUTE_SOURCE_PATH))

# For tools use build-root as source path, otherwise use given source path
FIND_SOURCE_PATH =						\
  $(if $(is_build_tool),					\
    $(SOURCE_PATH_BUILD_ROOT_DIRS) $(MU_BUILD_ROOT_DIR),	\
    $(SOURCE_PATH_BUILD_DATA_DIRS))

# First search given source path, then default to build-root
FULL_SOURCE_PATH = $(SOURCE_PATH_BUILD_DATA_DIRS) $(MU_BUILD_ROOT_DIR)

# Misc functions
is_in_fn = $(strip $(filter $(1),$(2)))
last_fn = $(lastword $1)
chop_fn = $(wordlist 2,$(words $1),x $1)
uniq_fn = $(strip $(if $1,$(call uniq_fn,$(call chop_fn,$1)) \
            $(if $(filter $(call last_fn,$1),$(call chop_fn,$1)),,$(call last_fn,$1))))
ifdef3_fn = $(if $(patsubst undefined,,$(origin $(1))),$(3),$(2))
ifdef_fn = $(call ifdef3_fn,$(1),$(2),$($(1)))

_mu_debug = $(warning "$(1) = $($(1))")

$(foreach d,$(FIND_SOURCE_PATH),					\
  $(eval _mu_package_mk_in_$(d) = $(shell find $(d)/packages/*.mk 2> /dev/null))	\
  $(eval _mu_srcdirs_in_$(d) =						\
    $(shell find $(d)/..						\
      -maxdepth 1							\
      -type d								\
      -and -not -name ".."						\
      -and -not -name $(MU_BUILD_ROOT_NAME)				\
      -and -not -name $(MU_BUILD_DATA_DIR_NAME)))			\
  $(eval _mu_non_package_files_in_$(d) =				\
    $(shell find $(d)/packages						\
      -type f								\
      -and -not -name '*.mk'						\
      -and -not -name '*~' 2> /dev/null))				\
  $(foreach p,$(patsubst %.mk,%,$(notdir $(_mu_package_mk_in_$(d)))),	\
    $(eval _mu_package_dir_$(p) = $(d))					\
    $(eval _mu_package_mk_$(p) = $(d)/packages/$(p).mk)			\
  )									\
  $(foreach p,$(notdir $(_mu_srcdirs_in_$(d))),				\
    $(eval _mu_package_srcdir_$(p) = $(shell cd $(d)/../$(p) && pwd))	\
  )									\
)

# Find root directory for package based on presence of package .mk
# makefile fragment on source path.
_find_build_data_dir_for_package_fn = $(shell			\
  set -eu$(BUILD_DEBUG) ;					\
  for d in $(FIND_SOURCE_PATH) ; do				\
    f="$${d}/packages/$(1).mk" ;				\
    [[ -f $${f} ]] && echo `cd $${d} && pwd` && exit 0 ;	\
  done ;							\
  echo "")
find_build_data_dir_for_package_fn = $(call ifdef_fn,_mu_package_dir_$(1),)

# dir/PACKAGE
_find_source_fn = $(shell				\
  set -eu$(BUILD_DEBUG) ;				\
  d="$(call find_build_data_dir_for_package_fn,$(1))" ;	\
  [[ -n "$${d}" ]] && d="$${d}/../$(1)" ;		\
  echo "$${d}")
find_source_fn = $(call ifdef3_fn,_mu_package_dir_$(1),,$(_mu_package_dir_$(1))/../$(1))

# Find given FILE in source path as build-data/packages/FILE
find_package_file_fn = $(shell				\
  set -eu$(BUILD_DEBUG) ;				\
  d="$(call find_build_data_dir_for_package_fn,$(1))" ;	\
  [[ -n "$${d}" ]] && d="$${d}/packages/$(2)" ;		\
  [[ -f "$${d}" ]] && echo "$${d}")

# Find first FILE in source path with name PATH/build-data/FILE
find_build_data_file_fn = $(shell				\
  set -eu$(BUILD_DEBUG) ;					\
  for d in $(FIND_SOURCE_PATH) ; do				\
    f="$${d}/$(1)" ;						\
    [[ -f $${f} ]] && echo `cd $${d} && pwd`/$(1) && exit 0 ;	\
  done ;							\
  echo "")

######################################################################
# ARCH, PLATFORM
######################################################################

NATIVE_ARCH = $(shell gcc -dumpmachine | sed -e 's/\([a-zA-Z_0-9]*\)-.*/\1/')

# Find all platforms.mk that we can, including those from build-root
$(foreach d,$(FULL_SOURCE_PATH), \
  $(eval -include $(d)/platforms.mk))

# Platform should be defined somewhere by specifying $($(PLATFORM)_arch)
ARCH = $(strip $($(PLATFORM)_arch))
ifeq ($(ARCH),)
  $(error "Unknown platform `$(PLATFORM)'")
endif

# map e.g. ppc7450 -> ppc
BASIC_ARCH = \
   ${shell case '$(ARCH)' in \
      (native) echo $(NATIVE_ARCH) ;; \
      (i*86*) echo i386 ;; \
      (ppc*|powerpc*) echo ppc ;; \
      (*) echo '$(ARCH)' ;; \
     esac }

# x86_64 can be either 32/64.  set BIACH=32 to get 32 bit libraries.
BIARCH = 64

aarch64_libdir = 64
x86_64_libdir = $(BIARCH)
native_libdir = $($(NATIVE_ARCH)_libdir)

# lib or lib64 depending
arch_lib_dir = lib$($(BASIC_ARCH)_libdir)

# OS to configure for.  configure --host will be set to $(ARCH)-$(OS)
# Allow per-platform overrides

OS = $(strip $($(PLATFORM)_os))
ifeq ($(OS),)
  OS = mu-linux
endif

spu_target = spu
native_target =

is_native = $(if $(ARCH:native=),,true)
not_native = $(if $(ARCH:native=),true,)

ARCH_TARGET_tmp = $(call ifdef_fn,$(ARCH)_target,$(ARCH)-$(OS))
TARGET = $(call ifdef_fn,$(PLATFORM)_target,$(ARCH_TARGET_tmp))
TARGET_PREFIX = $(if $(not_native),$(TARGET)-,)

# CPU microarchitecture detection. 
# Either set <platform>_march in build-data/platforms/<platform>.mk,
# or detect and use the build-host instruction set

MARCH = $(strip $($(PLATFORM)_march))
ifeq ($(MARCH),)
  ifneq ($(wildcard $(TOOL_INSTALL_DIR)/bin/$(TARGET)-gcc),)
    TARGET_GCC = $(TOOL_INSTALL_DIR)/bin/$(TARGET)-gcc
  else ifneq ($(wildcard $(MU_BUILD_ROOT_DIR)/tools/bin/$(TARGET)-gcc),)
    TARGET_GCC = $(MU_BUILD_ROOT_DIR)/tools/bin/$(TARGET)-gcc
  endif
  ifneq ($(TARGET_GCC),)
    MARCH = $(shell $(TARGET_GCC) -Q --help=target -march=native | grep march | sed -e 's/.*march=[[:space:]]*//')
  else
    MARCH = native
  endif
else
  ifeq ($(MARCH),nehalem)
    override MARCH = corei7
  else ifeq ($(MARCH),westmere)
    override MARCH = corei7
  else ifeq ($(MARCH),sandybridge)
    override MARCH = corei7-avx
  else ifeq ($(MARCH),ivybridge)
    override MARCH = core-avx-i
  else ifeq ($(MARCH),haswell)
    override MARCH = core-avx2
  endif
endif
export MARCH

MTUNE = $(strip $($(PLATFORM)_mtune))
ifeq ($(MTUNE),)
  MTUNE = generic
endif

######################################################################
# Generic build stuff
######################################################################

# The package we are currently working on
PACKAGE = $*

# Build/install tags.  This lets you have different CFLAGS/CPPFLAGS/LDFLAGS
# for e.g. debug versus optimized compiles.  Each tag has its own set of build/install
# areas.
TAG = 
TAG_PREFIX = $(if $(TAG),$(TAG)-)

# yes you need the space
tag_var_with_added_space_fn = $(if $($(TAG)_TAG_$(1)),$($(TAG)_TAG_$(1)) )

# TAG=debug for debugging
debug_TAG_CFLAGS = -g -O0 -DCLIB_DEBUG -DFORTIFY_SOURCE=2 -march=$(MARCH) \
		   -fstack-protector-all -fPIC
debug_TAG_CXXFLAGS = -g -O0 -DCLIB_DEBUG -DFORTIFY_SOURCE=2 -march=$(MARCH) \
		   -fstack-protector-all -fPIC
debug_TAG_LDFLAGS = -g -O0 -DCLIB_DEBUG -DFORTIFY_SOURCE=2 -march=$(MARCH) \
		   -fstack-protector-all -fPIC

BUILD_PREFIX_package = build-$(TAG_PREFIX)
BUILD_PREFIX_tool = build-tool-$(TAG_PREFIX)
INSTALL_PREFIX = install-$(TAG_PREFIX)
IMAGES_PREFIX = images-$(TAG_PREFIX)

# Whether we are building a tool or not
tool_or_package_fn = $(if $(is_build_tool),tool,package)

# Directory where packages are built & installed
BUILD_DIR = $(MU_BUILD_ROOT_DIR)/$(BUILD_PREFIX_$(call tool_or_package_fn))$(ARCH)
INSTALL_DIR = $(MU_BUILD_ROOT_DIR)/$(INSTALL_PREFIX)$(ARCH)

PLATFORM_IMAGE_DIR = $(MU_BUILD_ROOT_DIR)/$(IMAGES_PREFIX)$(PLATFORM)

# $(call VAR,DEFAULT)
override_var_with_default_fn = $(if $($(1)),$($(1)),$(2))

# $(call if_directory_exists_fn,D1,D2) returns D1 if it exists else D2
define if_directory_exists_fn
$(shell if test -d $(1); then echo $(1); else echo $(2); fi)
endef

# $(call if_file_exists_fn,F1,F2) returns F1 if it exists else F2
define if_file_exists_fn
$(shell if test -f $(1); then echo $(1); else echo $(2); fi)
endef

# Default VAR, package specified override of default PACKAGE_VAR
package_var_fn = $(call override_var_with_default_fn,$(1)_$(2),$(1))

package_build_dir_fn = $(call package_var_fn,$(1),build_dir)

package_install_dir_fn = \
  $(if $(is_build_tool),$(TOOL_INSTALL_DIR),$(INSTALL_DIR)/$(call package_build_dir_fn,$(1)))

PACKAGE_BUILD_DIR = \
  $(BUILD_DIR)/$(call package_build_dir_fn,$(PACKAGE))
PACKAGE_INSTALL_DIR = \
  $(call package_install_dir_fn,$(PACKAGE))

# Tools (gcc, binutils, glibc...) are installed here
TOOL_INSTALL_DIR = $(MU_BUILD_ROOT_DIR)/tools

# Target specific tools go here e.g. mu-build/tools/ppc-mu-linux
TARGET_TOOL_INSTALL_DIR = $(TOOL_INSTALL_DIR)/$(TARGET)

# Set BUILD_DEBUG to vx or x enable shell command tracing.
BUILD_DEBUG =

# Message from build system itself (as opposed to make or shell commands)
build_msg_fn = echo "@@@@ $(1) @@@@"

# Allow CCACHE_DIR to be overridden, e.g. in .../build-root/build-config.mk
ifeq ($(CCACHE_DIR),)
  CCACHE_DIR=$(MU_BUILD_ROOT_DIR)/.ccache
endif

# Always prefer our own tools to those installed on system.
# Note: ccache-bin must be before tool bin.
# 
# Removed LD_LIBRARY_PATH from BUILD_ENV (drb, 10/31/17):  
# export LD_LIBRARY_PATH=$(TOOL_INSTALL_DIR)/lib64:$(TOOL_INSTALL_DIR)/lib
#   Reported to cause trouble. Only of historical interest, since we no longer
#   build a full tool chain from source.

BUILD_ENV =										\
    export CCACHE_DIR=$(CCACHE_DIR) ;					\
    export PATH=$(wildcard /usr/lib*/ccache):$(TOOL_INSTALL_DIR)/bin:$${PATH} ;		\
    $(if $(call configure_var_fn,PATH), export PATH=$${PATH}:$(call configure_var_fn,PATH);,)	\
    export PATH="`echo $${PATH} | sed -e s/[.]://`" ;					\
    $(if $(not_native),export CONFIG_SITE=$(MU_BUILD_ROOT_DIR)/config.site ;,)	\
    set -eu$(BUILD_DEBUG) ;							        \
    set -o pipefail

######################################################################
# Package build generic definitions
######################################################################

package_dir_fn = \
  $(call find_build_data_dir_for_package_fn,$(1))/packages

package_mk_fn = $(call package_dir_fn,$(1))/$(1).mk

# Pick up built-root/pre-package-include.mk for all source directories
$(foreach d,$(SOURCE_PATH_BUILD_ROOT_DIRS),	\
  $(eval -include $(d)/pre-package-include.mk))

$(foreach d,$(addsuffix /packages,$(FIND_SOURCE_PATH)),			\
  $(eval -include $(d)/*.mk)						\
  $(eval ALL_PACKAGES += $(patsubst $(d)/%.mk,%,$(wildcard $(d)/*.mk)))	\
)

# Pick up built-root/post-package-include.mk for all source directories
$(foreach d,$(SOURCE_PATH_BUILD_ROOT_DIRS),	\
  $(eval -include $(d)/post-package-include.mk))

# Linux specific native build tools
NATIVE_TOOLS_LINUX =				\
  e2fsimage					\
  e2fsprogs					\
  fakeroot					\
  jffs2						\
  mkimage					\
  zlib						\
  xz						\
  squashfs

IS_LINUX = $(if $(findstring no,$($(PLATFORM)_uses_linux)),no,yes)

NATIVE_TOOLS_$(IS_LINUX) += $(NATIVE_TOOLS_LINUX)

# only build glibc for linux installs
CROSS_TOOLS_$(IS_LINUX) += glibc gcc

# must be first for bootstrapping
NATIVE_TOOLS = findutils make

# basic tools needed for build system
NATIVE_TOOLS += git automake autoconf libtool texinfo tar

# needed to compile gcc
NATIVE_TOOLS += mpfr gmp mpc

# Tool to sign binaries
NATIVE_TOOLS += sign

# ccache
NATIVE_TOOLS += ccache

# Tools needed on native host to build for platform
NATIVE_TOOLS += $(call ifdef_fn,$(PLATFORM)_native_tools,)

# Tools for cross-compiling from native -> ARCH
CROSS_TOOLS = binutils gcc-bootstrap gdb

# Tools needed on native host to build for platform
CROSS_TOOLS += $(call ifdef_fn,$(PLATFORM)_cross_tools,)

NATIVE_TOOLS += $(NATIVE_TOOLS_yes)
CROSS_TOOLS += $(CROSS_TOOLS_yes)

timestamp_name_fn = .mu_build_$(1)_timestamp
CONFIGURE_TIMESTAMP = $(call timestamp_name_fn,configure)
BUILD_TIMESTAMP = $(call timestamp_name_fn,build)
INSTALL_TIMESTAMP = $(call timestamp_name_fn,install)

TIMESTAMP_DIR = $(PACKAGE_BUILD_DIR)

find_newer_files_fn =						\
  "`for i in $(2) ; do						\
      [[ -f $$i && $$i -nt $(1) ]] && echo "$$i" && exit 0;	\
    done ;							\
    exit 0;`"

find_filter = -not -name '*~'
find_filter += -and -not -path '*/.git*'
find_filter += -and -not -path '*/.svn*'
find_filter += -and -not -path '*/.CVS*'
find_filter += -and -not -path '*/manual/*'
find_filter += -and -not -path '*/autom4te.cache/*'
find_filter += -and -not -path '*/doc/all-cfg.texi'
find_filter += -and -not -path '*/.mu_build_*'

find_newer_filtered_fn =			\
  (! -f $(1)					\
    || -n $(call find_newer_files_fn,$(1),$(3))	\
    || -n "`find -H $(2)			\
	      -type f				\
              -and -newer $(1)			\
	      -and \( $(4) \)			\
              -print -quit`")

find_newer_fn =							\
  $(call find_newer_filtered_fn,$(1),$(2),$(3),$(find_filter))

######################################################################
# Package dependencies
######################################################################

# This must come before %-configure, %-build, %-install pattern rules
# or else dependencies will not work.

package_dependencies_fn =				\
  $(patsubst %-install, %,				\
    $(filter %-install,$($(1)_configure_depend)))

PACKAGE_DEPENDENCIES = $(call package_dependencies_fn,$(PACKAGE))

# package specific configure, build, install dependencies
add_package_dependency_fn = \
  $(if $($(1)_$(2)_depend), \
       $(eval $(1)-$(2) : $($(1)_$(2)_depend)))

$(foreach p,$(ALL_PACKAGES), \
    $(call add_package_dependency_fn,$(p),configure) \
    $(call add_package_dependency_fn,$(p),build) \
    $(call add_package_dependency_fn,$(p),install))

TARGETS_RESPECTING_DEPENDENCIES = image_install wipe diff push-all pull-all find-source

# carry over packages dependencies to image install, wipe, pull-all, push-all
$(foreach p,$(ALL_PACKAGES),							\
  $(if $($(p)_configure_depend),						\
    $(foreach s,$(TARGETS_RESPECTING_DEPENDENCIES),				\
      $(eval $(p)-$(s):								\
	     $(addsuffix -$(s), $(call package_dependencies_fn,$(p)))))))

# recursively resolve dependencies
resolve_dependencies2_fn = $(strip					\
  $(eval __added = $(filter-out $(4),					\
    $(call uniq_fn,							\
      $(foreach l,$(3),							\
       $(call ifdef3_fn,$(l)$(1),,$(call $(2),$($(l)$(1))))		\
      ))))								\
  $(eval __known = $(call uniq_fn,$(4) $(3) $(__added)))		\
  $(if $(__added),							\
    $(call resolve_dependencies2_fn,$(1),$(2),$(__added),$(__known)),	\
    $(__known))								\
)

resolve_dependencies_null_fn = $(1)

resolve_dependencies_fn = $(call resolve_dependencies2_fn,$(1),resolve_dependencies_null_fn,$(2))

######################################################################
# Package configure
######################################################################

# x86_64 can be either 32/64.  set BIACH=32 to get 32 bit libraries.
BIARCH = 64

x86_64_libdir = $(BIARCH)
native_libdir = $($(NATIVE_ARCH)_libdir)

# lib or lib64 depending
arch_lib_dir = lib$($(BASIC_ARCH)_libdir)

# find dynamic linker as absolute path
TOOL_INSTALL_LIB_DIR=$(TOOL_INSTALL_DIR)/$(TARGET)/$(arch_lib_dir)
DYNAMIC_LINKER=${shell cd $(TOOL_INSTALL_LIB_DIR); echo ld*.so.*}

# Pad dynamic linker & rpath so elftool will never have to change ELF section sizes.
# Yes, this is a kludge.
lots_of_slashes_to_pad_names = "/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"

# When PLATFORM != native we *always* use our own versions of GLIBC and dynamic linker
# Allow per-platform overrides
CROSS_LDFLAGS = $(strip $($(PLATFORM)_cross_ldflags))
ifeq ($(CROSS_LDFLAGS),)
  CROSS_LDFLAGS =											\
    -Wl,--dynamic-linker=$(lots_of_slashes_to_pad_names)$(TOOL_INSTALL_LIB_DIR)/$(DYNAMIC_LINKER)	\
    -Wl,-rpath -Wl,$(lots_of_slashes_to_pad_names)$(TOOL_INSTALL_LIB_DIR)
endif

cross_ldflags = $(if $(is_native)$(is_build_tool),,$(CROSS_LDFLAGS) )

# $(call installed_libs_fn,PACKAGE)
# Return install library directory for given package.
# Some packages (e.g. openssl) don't install under lib64; instead they use lib
define installed_lib_fn
$(call if_directory_exists_fn,
  $(call package_install_dir_fn,$(1))/$(arch_lib_dir),
  $(call package_install_dir_fn,$(1))/lib)
endef

# Set -L and rpath to point to dependent libraries previously built by us.
installed_libs_fn =					\
  $(foreach i,$(1),					\
    -L$(call installed_lib_fn,$(i))			\
    -Wl,-rpath -Wl,$(call installed_lib_fn,$(i)))

# As above for include files
installed_include_fn = $(call package_install_dir_fn,$(1))/include

installed_includes_fn = $(foreach i,$(1),-I$(call installed_include_fn,$(i)))

# By default package CPPFLAGS (to set include path -I) and LDFLAGS (to set link path -L)
# point at dependent install directories.
DEFAULT_CPPFLAGS = $(call installed_includes_fn, $(PACKAGE_DEPENDENCIES))
DEFAULT_LDFLAGS = $(call installed_libs_fn, $(PACKAGE_DEPENDENCIES))

configure_var_fn = \
  $(call tag_var_with_added_space_fn,$(1))$(call override_var_with_default_fn,$(PACKAGE)_$(1),$(DEFAULT_$(1)))
configure_ldflags_fn = \
  $(cross_ldflags)$(call configure_var_fn,LDFLAGS)

# Allow packages to override CPPFLAGS, CFLAGS, and LDFLAGS
CONFIGURE_ENV =								\
    $(if $(call configure_var_fn,CPPFLAGS),				\
	 CPPFLAGS="$(CPPFLAGS) $(call configure_var_fn,CPPFLAGS)")	\
    $(if $(call configure_var_fn,CXXFLAGS),				\
	 CXXFLAGS="$(CXXFLAGS) $(call configure_var_fn,CXXFLAGS)")	\
    $(if $(call configure_var_fn,CFLAGS),				\
	 CFLAGS="$(CFLAGS) $(call configure_var_fn,CFLAGS)")		\
    $(if $(call configure_var_fn,CCASFLAGS),				\
	 CCASFLAGS="$(CCASFLAGS) $(call configure_var_fn,CCASFLAGS)")	\
    $(if $(call configure_ldflags_fn),					\
	 LDFLAGS="$(LDFLAGS) $(call configure_ldflags_fn)")		\
    $(if $($(PACKAGE)_configure_env),$($(PACKAGE)_configure_env))

# only partially used now (used in a few .mk files)
ifeq ($(is_build_tool),yes)
prefix     = $(PACKAGE_INSTALL_DIR)
libdir     = $(PACKAGE_INSTALL_DIR)/$(arch_lib_dir)
libexecdir = $(PACKAGE_INSTALL_DIR)/usr/libexec
DESTDIR     = /
else
# Eventually simplify this with no per package DESTDIR or prefix
ppdMacro = $(if $(PER_PACKAGE_DESTDIR),$(call package_build_dir_fn,$(1)))
pppMacro = $(if $(PER_PACKAGE_PREFIX),$(call package_build_dir_fn,$(1)))
prefixMacro     = $($(PLATFORM)_PREFIX_BASE)/$(pppMacro)
prefix = $(call prefixMacro,$(PACKAGE))
libdir     = $($(PLATFORM)_LIBDIR)
libexecdir = $($(PLATFORM)_LIBEXECDIR)
destdirMacro  = $($(PLATFORM)_DESTDIR_BASE)$(ppdMacro)
DESTDIR  = $(call destdirMacro,$(PACKAGE))
endif

configure_package_gnu =						\
  s=$(call find_source_fn,$(PACKAGE_SOURCE))$(PACKAGE_SUBDIR) ;	\
  if [ ! -f $$s/configure ] ; then				\
    autoreconf -i -f $$s ;					\
  fi ;								\
  cd $(PACKAGE_BUILD_DIR) ;					\
  env $(CONFIGURE_ENV)						\
    $$s/configure						\
      $(if $($(PACKAGE)_configure_host_and_target),		\
           $($(PACKAGE)_configure_host_and_target),		\
           $(if $(not_native),--host=$(TARGET),))		\
      $(if $($(PACKAGE)_configure_prefix),			\
           $($(PACKAGE)_configure_prefix),			\
           --libdir=$(PACKAGE_INSTALL_DIR)/$(arch_lib_dir)	\
           --prefix=$(PACKAGE_INSTALL_DIR))			\
      $($(PACKAGE)_configure_args) 				\
      $($(PACKAGE)_configure_args_$(PLATFORM))

configure_package =							\
  $(call build_msg_fn,Configuring $(PACKAGE) in $(PACKAGE_BUILD_DIR)) ;	\
  mkdir -p $(PACKAGE_BUILD_DIR) ;					\
  $(if $($(PACKAGE)_configure),						\
       $($(PACKAGE)_configure),						\
       $(configure_package_gnu))

# Tools (e.g. gcc, binutils, gdb) required a platform to build for
check_platform =								\
  is_tool="$(is_build_tool)" ;							\
  is_cross_package="$(findstring $(PACKAGE),$(CROSS_TOOLS))" ;			\
  is_arch_native="$(if $(subst native,,$(ARCH)),,yes)" ;			\
  if [ "$${is_tool}" == "yes"							\
       -a "$${is_cross_package}" != ""						\
       -a "$${is_arch_native}" != "" ]; then					\
    $(call build_msg_fn,You must specify PLATFORM for building tools) ;		\
    exit 1 ;									\
  fi ;										\
  : check that platform gcc can be found ;					\
  target_gcc=gcc ;								\
  if [ "$${is_arch_native}" != "yes" ] ; then					\
    target_gcc=$(TARGET)-gcc ;							\
  fi ;										\
  if [ "$${is_tool}" != "yes"							\
       -a "$${is_arch_native}" != "yes"						\
       -a ! -x "`which 2> /dev/null $${target_gcc}`" ] ; then			\
    $(call build_msg_fn,							\
	   No cross-compiler found for platform $(PLATFORM) target $(TARGET);	\
	     try make PLATFORM=$(PLATFORM) install-tools) ;			\
    exit 1 ;									\
  fi
    
configure_check_timestamp =						\
  @$(BUILD_ENV) ;							\
  $(check_platform) ;							\
  mkdir -p $(PACKAGE_BUILD_DIR) ;					\
  mkdir -p $(PACKAGE_INSTALL_DIR) ;					\
  conf="$(TIMESTAMP_DIR)/$(CONFIGURE_TIMESTAMP)" ;			\
  dirs="$(call package_mk_fn,$(PACKAGE))				\
	$(wildcard $(call find_source_fn,				\
	             $(PACKAGE_SOURCE))$(PACKAGE_SUBDIR)/configure)	\
       $(MU_BUILD_ROOT_DIR)/config.site" ;				\
  if [[ $(call find_newer_fn, $${conf}, $${dirs}, $?) ]]; then		\
    $(configure_package) ;						\
    touch $${conf} ;							\
  else									\
    $(call build_msg_fn,Configuring $(PACKAGE): nothing to do) ;	\
  fi

.PHONY: %-configure
%-configure: %-find-source
	$(configure_check_timestamp)

######################################################################
# Package build
######################################################################

# /proc/cpuinfo does not exist on platforms without a /proc and on some
# platforms, notably inside containers, it has no content. In those cases
# we assume there's 1 processor; we use 2*ncpu for the -j option.
# NB: GNU Make 4.2 will let us use '$(file </proc/cpuinfo)' to both test
# for file presence and content; for now this will have to do.
ifndef MAKE_PARALLEL_JOBS
MAKE_PARALLEL_JOBS = -j $(if $(shell [ -f /proc/cpuinfo ] && head /proc/cpuinfo), \
	$(shell grep -c ^processor /proc/cpuinfo), 2)
else
MAKE_PARALLEL_JOBS := -j $(MAKE_PARALLEL_JOBS)
endif
MAKE_PARALLEL_FLAGS ?= $(if $($(PACKAGE)_make_parallel_fails),,$(MAKE_PARALLEL_JOBS))

# Make command shorthand for packages & tools.
PACKAGE_MAKE =					\
  $(MAKE)					\
    -C $(PACKAGE_BUILD_DIR)			\
    $($(PACKAGE)_make_args)			\
    $(MAKE_PARALLEL_FLAGS)

build_package =							\
  $(call build_msg_fn,Building $* in $(PACKAGE_BUILD_DIR)) ;	\
  mkdir -p $(PACKAGE_BUILD_DIR) ;				\
  cd $(PACKAGE_BUILD_DIR) ;					\
  $(if $($(PACKAGE)_build),					\
       $($(PACKAGE)_build),					\
       $(PACKAGE_MAKE))

build_check_timestamp =									\
  @$(BUILD_ENV) ;									\
  comp="$(TIMESTAMP_DIR)/$(BUILD_TIMESTAMP)" ;						\
  conf="$(TIMESTAMP_DIR)/$(CONFIGURE_TIMESTAMP)" ;					\
  dirs="$(call find_source_fn,$(PACKAGE_SOURCE))					\
	$($(PACKAGE)_build_timestamp_depends)						\
	$(if $(is_build_tool),,$(addprefix $(INSTALL_DIR)/,$(PACKAGE_DEPENDENCIES)))" ;	\
  if [[ $${conf} -nt $${comp}								\
        || $(call find_newer_fn, $${comp}, $${dirs}, $?) ]]; then			\
    $(build_package) ;									\
    touch $${comp} ;									\
  else											\
    $(call build_msg_fn,Building $(PACKAGE): nothing to do) ;				\
  fi

.PHONY: %-build
%-build: %-configure
	$(build_check_timestamp)

.PHONY: %-rebuild
%-rebuild: %-wipe %-build
	@ :

######################################################################
# Package install
######################################################################

install_package =								\
    : by default, for non-tools, remove any previously installed bits ;		\
    $(if $(is_build_tool)$($(PACKAGE)_keep_instdir),				\
         true,									\
         rm -rf $(PACKAGE_INSTALL_DIR));					\
    mkdir -p $(PACKAGE_INSTALL_DIR) ;						\
    $(if $($(PACKAGE)_pre_install),$($(PACKAGE)_pre_install),true);		\
    $(if $($(PACKAGE)_install),							\
	 $($(PACKAGE)_install),							\
	 $(PACKAGE_MAKE)							\
	    $($(PACKAGE)_install_args)						\
	    install) ;								\
    $(if $($(PACKAGE)_post_install),$($(PACKAGE)_post_install),true)

install_check_timestamp =					\
  @$(BUILD_ENV) ;						\
  inst=$(TIMESTAMP_DIR)/$(INSTALL_TIMESTAMP) ;			\
  dirs="$(PACKAGE_BUILD_DIR)					\
	$($(PACKAGE)_install_dependencies)" ;			\
  if [[ $(call find_newer_fn, $${inst}, $${dirs}, $?) ]]; then	\
    $(call build_msg_fn,Installing $(PACKAGE)) ;		\
    $(install_package) ;					\
    touch $${inst} ;						\
  else								\
    $(call build_msg_fn,Installing $(PACKAGE): nothing to do) ;	\
  fi

.PHONY: %-install
%-install: %-build
	$(install_check_timestamp)

######################################################################
# Source code managment
######################################################################

GIT = git

# Maps package name to source directory root.
# Multiple packages may use a single source tree.
# For example, gcc-bootstrap package shares gcc source.
PACKAGE_SOURCE = $(if $($(PACKAGE)_source),$($(PACKAGE)_source),$(PACKAGE))
PACKAGE_SUBDIR = $(if $($(PACKAGE)_configure_subdir),/$($(PACKAGE)_configure_subdir),)

# Use git to download source if directory is not found
find_source_for_package =									\
  @$(BUILD_ENV) ;										\
  $(call build_msg_fn,Arch for platform '$(PLATFORM)' is $(ARCH)) ;				\
  $(call build_msg_fn,Finding source for $(PACKAGE)) ;						\
  s="$(call find_source_fn,$(PACKAGE_SOURCE))" ;						\
  [[ -z "$${s}" ]]										\
    && $(call build_msg_fn,Package $(PACKAGE) not found with path $(SOURCE_PATH))		\
    && exit 1;											\
  mk="$(call find_build_data_dir_for_package_fn,$(PACKAGE_SOURCE))/packages/$(PACKAGE).mk";	\
  $(call build_msg_fn,Makefile fragment found in $${mk}) ;					\
  if [ ! -d "$${s}" ] ; then									\
    d=`dirname $${mk}` ;									\
    i=`cd $${d}/.. && ($(GIT) config remote.origin.url ||					\
                    awk '/URL/ { print $$2; }' .git/remotes/origin)`;				\
    g=`dirname $${i}` ;										\
    $(call build_msg_fn,Fetching source: $(GIT) clone $${g}/$(PACKAGE_SOURCE) $$s) ;		\
    if ! $(GIT) clone $${g}/$(PACKAGE_SOURCE) $$s; then						\
      $(call build_msg_fn,No source for $(PACKAGE) in $${g});					\
      exit 1;											\
    fi ;											\
    $(call build_msg_fn,Fix file dates in $${g}/$(PACKAGE_SOURCE)) ;					\
    (cd $${s} ; $(MU_BUILD_ROOT_DIR)/autowank --touch) ;					\
  fi ;												\
  s=`cd $${s} && pwd` ;										\
  $(call build_msg_fn,Source found in $${s})

.PHONY: %-find-source
%-find-source:
	$(find_source_for_package)

.PHONY: %-push %-pull %-push-all %-pull-all
%-push %-pull %-push-all %-pull-all:
	@$(BUILD_ENV) ;								\
	push_or_pull=$(patsubst %-all,%,$(subst $(PACKAGE)-,,$@)) ;		\
	$(call build_msg_fn,Git $${push_or_pull} source for $(PACKAGE)) ;	\
	s=$(call find_source_fn,$(PACKAGE_SOURCE)) ;				\
	if [ "x$$s" = "x" ]; then						\
	     $(call build_msg_fn,No source for $(PACKAGE)) ;			\
	     exit 1;								\
	fi ;									\
	cd $$s && $(GIT) $${push_or_pull}

# Pull all packages for platform
.PHONY: pull-all
pull-all:
	@$(BUILD_ENV) ;								\
	$(call build_msg_fn,Git pull build system) ;				\
	for d in $(MU_BUILD_ROOT_DIR)						\
		 $(SOURCE_PATH_BUILD_ROOT_DIRS)					\
	 	 $(SOURCE_PATH_BUILD_DATA_DIRS); do				\
	  $(call build_msg_fn,Git pull $${d}) ;					\
	  pushd $${d} >& /dev/null && $(GIT) pull && popd >& /dev/null ;	\
	done ;									\
	$(call build_msg_fn,Git pull build tools) ;				\
	$(call tool_make_target_fn,pull-all) ;					\
	$(call build_msg_fn,Git pull packages for platform $(PLATFORM)) ;	\
	make PLATFORM=$(PLATFORM) $(patsubst %,%-pull-all,$(ROOT_PACKAGES))

.PHONY: %-diff
%-diff:									
	@$(BUILD_ENV) ;							\
	d=$(call find_source_fn,$(PACKAGE_SOURCE)) ;			\
	$(call build_msg_fn,Git diff $(PACKAGE)) ;			\
	if [ -d $${d}/.git ] ; then 					\
	 cd $${d} && $(GIT) --no-pager diff 2>/dev/null; 		\
        else								\
	 $(call build_msg_fn, $(PACKAGE) not a git directory) ;		\
	fi
            


# generate diffs for everything in source path
.PHONY: diff-all
diff-all:								
	@$(BUILD_ENV) ;							\
	$(call build_msg_fn,Generate diffs) ;				\
	for r in $(ABSOLUTE_SOURCE_PATH); do				\
	  for d in $${r}/* ; do						\
	    if [ -d $${d} ] ; then					\
	      $(call build_msg_fn,Git diff $${d}) ;			\
	      if [ -d $${d}/.git ] ; then				\
		cd $${d} && $(GIT) --no-pager diff 2>/dev/null;		\
	      else							\
		$(call build_msg_fn, $${d} not a git directory) ;	\
	      fi ;							\
	    fi ; 							\
	  done ;							\
	done

######################################################################
# System images
######################################################################

IMAGE_DIR = $(MU_BUILD_ROOT_DIR)/image-$(PLATFORM)

# Reports shared libraries in given directory
find_shared_libs_fn =				\
  find $(1)					\
    -maxdepth 1					\
       -regex '.*/lib[a-z0-9_]+\+?\+?.so'		\
    -o -regex '.*/lib[a-z0-9_]+-[0-9.]+\+?\+?.so'	\
    -o -regex '.*/lib[a-z0-9_]+\+?\+?.so.[0-9.]+'

# By default pick up files from binary directories and /etc.
# Also include shared libraries.
DEFAULT_IMAGE_INCLUDE =					\
  for d in bin sbin libexec				\
           usr/bin usr/sbin usr/libexec			\
           etc; do					\
    [[ -d $$d ]] && echo $$d;				\
  done ;						\
  [[ -d $(arch_lib_dir) ]]				\
    && $(call find_shared_libs_fn,$(arch_lib_dir))

# Define any shell functions needed by install scripts
image_install_functions =			\
  $(foreach p,$(ALL_PACKAGES),			\
    $(if $($(p)_image_install_functions),	\
	 $($(p)_image_install_functions)))

# Should always be over-written by temp dir in %-root-image rule
IMAGE_INSTALL_DIR = $(error you need to set IMAGE_INSTALL_DIR)

image_install_fn =								\
  @$(BUILD_ENV) ;								\
  $(call build_msg_fn,Image-install $(1) for platform $(PLATFORM)) ;		\
  inst_dir=$(IMAGE_INSTALL_DIR) ;						\
  mkdir -p $${inst_dir} ;							\
  cd $(2) ;									\
  : select files to include in image ;						\
  image_include_files="								\
    `$(call ifdef_fn,$(1)_image_include,$(DEFAULT_IMAGE_INCLUDE)) ;		\
     echo "" ;									\
     exit 0 ; `";								\
  : select files regexps to exclude from image ;				\
  image_exclude_files="" ;							\
  if [ ! -z "$($(1)_image_exclude)" ] ; then					\
    image_exclude_files="${image_exclude_files}					\
                         $(patsubst %,--exclude=%,$($(1)_image_exclude))" ;	\
  fi ;										\
  [[ -z "$${image_include_files}" || $${image_include_files} == " " ]]		\
    || tar cf - $${image_include_files} $${image_exclude_files}			\
       | tar xf - -C $${inst_dir} ;						\
  : copy files from copyimg directories on source path if present ;		\
  for build_data_dir in $(SOURCE_PATH_BUILD_DATA_DIRS) ; do			\
    d="$${build_data_dir}/packages/$(1).copyimg" ;				\
    if [ -d "$${d}" ] ; then							\
      env $($(PLATFORM)_copyimg_env)						\
	$(MU_BUILD_ROOT_DIR)/copyimg $${d} $${inst_dir} ;			\
    fi ;									\
  done ;									\
  : run package dependent install script ;					\
  $(if $($(1)_image_install),							\
       $(image_install_functions)						\
       cd $${inst_dir} ;							\
       $($(1)_image_install))

.PHONY: %-image_install
%-image_install: %-install
	$(call image_install_fn,$(PACKAGE),$(PACKAGE_INSTALL_DIR))

basic_system_image_include =					\
  $(call ifdef_fn,$(PLATFORM)_basic_system_image_include, 	\
  echo bin/ldd ;						\
  echo $(arch_lib_dir)/ld*.so* ;				\
  $(call find_shared_libs_fn, $(arch_lib_dir)))

basic_system_image_install =				\
  mkdir -p bin lib mnt proc root sbin sys tmp etc ;	\
  mkdir -p usr usr/{bin,sbin} usr/lib ;			\
  mkdir -p var var/{lib,lock,log,run,tmp} ;		\
  mkdir -p var/lock/subsys var/lib/urandom 

.PHONY: basic_system-image_install
basic_system-image_install: # linuxrc-install
	$(if $(not_native),							\
	     $(call image_install_fn,basic_system,$(TARGET_TOOL_INSTALL_DIR)),)

ROOT_PACKAGES = $(if $($(PLATFORM)_root_packages),$($(PLATFORM)_root_packages),$(default_root_packages))

.PHONY: install-packages
install-packages: $(patsubst %,%-find-source,$(ROOT_PACKAGES))	
	@$(BUILD_ENV) ;							        \
	set -eu$(BUILD_DEBUG) ;							\
	d=$(MU_BUILD_ROOT_DIR)/packages-$(PLATFORM) ;				\
	rm -rf $${d} ;								\
	mkdir -p $${d};								\
	$(MAKE) -C $(MU_BUILD_ROOT_DIR) IMAGE_INSTALL_DIR=$${d}			\
	    $(patsubst %,%-image_install,					\
	      basic_system							\
	      $(ROOT_PACKAGES))	|| exit 1;					\
	$(call build_msg_fn, Relocating ELF executables to run in $${d}) ;	\
	scripts/set-rpath $${d} $${d}/$(arch_lib_dir) ;				\
	: strip symbols from files ;						\
	if [ $${strip_symbols:-no} = 'yes' ] ; then				\
	    $(call build_msg_fn, Stripping symbols from files) ;		\
	    find $${d} -type f							\
		-exec								\
		  $(TARGET_PREFIX)strip						\
		    --strip-unneeded '{}' ';'					\
		    >& /dev/null ;						\
	else									\
	    $(call build_msg_fn, NOT stripping symbols) ;			\
	fi 

# readonly root squashfs image
# Note: $(call build_msg_fn) does not seem to work inside of fakeroot so we use echo
.PHONY: ro-image
$(PLATFORM_IMAGE_DIR)/ro.img ro-image: $(patsubst %,%-find-source,$(ROOT_PACKAGES))
	@$(BUILD_ENV) ;							\
	d=$(PLATFORM_IMAGE_DIR) ;					\
	mkdir -p $$d;							\
	ro_image=$$d/ro.img ;						\
	rm -f $${ro_image} ;						\
	tmp_dir="`mktemp -d $$d/ro-image-XXXXXX`" ;			\
	chmod 0755 $${tmp_dir} ;					\
	cd $${tmp_dir} ;						\
	trap "rm -rf $${tmp_dir}" err ;					\
	fakeroot /bin/bash -c "{					\
	  set -eu$(BUILD_DEBUG) ;					\
	  $(MAKE) -C $(MU_BUILD_ROOT_DIR) IMAGE_INSTALL_DIR=$${tmp_dir}	\
	    $(patsubst %,%-image_install,				\
	      basic_system						\
	      $(ROOT_PACKAGES)) ;					\
	  : make dev directory ;					\
	  $(linuxrc_makedev) ;						\
	  echo @@@@ Relocating ELF executables to run in / @@@@ ;	\
	  scripts/set-rpath /$(arch_lib_dir):/lib ;			\
	  : strip symbols from files ;					\
	  if [ '$${strip_symbols:-yes}' = 'yes' ] ; then		\
	      echo @@@@ Stripping symbols from files @@@@ ;		\
	      find $${tmp_dir} -type f					\
		-exec							\
		  $(TARGET_PREFIX)strip					\
		    --strip-unneeded '{}' ';'				\
		    >& /dev/null ;					\
	  else								\
	      echo @@@@ NOT stripping symbols @@@@ ;			\
	  fi ;								\
	  if [ $${sign_executables:-yes} = 'yes'			\
	       -a -n "$($(PLATFORM)_public_key)" ] ; then		\
	      echo @@@@ Signing executables @@@@ ;			\
	      find $${tmp_dir} -type f					\
		| xargs sign $($(PLATFORM)_public_key)			\
			     $($(PLATFORM)_private_key_passphrase) ;	\
	  fi ;								\
	  : make read-only file system ;				\
	  mksquashfs							\
	    $${tmp_dir} $${ro_image}					\
	    -no-exports -no-progress -no-recovery ;			\
	}" ;								\
	: cleanup tmp directory ;					\
	rm -rf $${tmp_dir}

MKFS_JFFS2_BYTE_ORDER_x86_64 = -l
MKFS_JFFS2_BYTE_ORDER_i686 = -l
MKFS_JFFS2_BYTE_ORDER_ppc = -b
MKFS_JFFS2_BYTE_ORDER_mips = -b
MKFS_JFFS2_BYTE_ORDER_native = $(MKFS_JFFS2_BYTE_ORDER_$(NATIVE_ARCH))

MKFS_JFFS2_SECTOR_SIZE_IN_KBYTES = \
  $(call ifdef_fn,$(PLATFORM)_jffs2_sector_size_in_kbytes,256)

mkfs_fn_jffs2 = mkfs.jffs2				\
  --eraseblock=$(MKFS_JFFS2_SECTOR_SIZE_IN_KBYTES)KiB	\
  --root=$(1) --output=$(2)				\
  $(MKFS_JFFS2_BYTE_ORDER_$(BASIC_ARCH))

# As things stand the actual initrd size parameter
# is set in .../open-repo/build-data/packages/linuxrc.mk.
EXT2_RW_IMAGE_SIZE=notused

mkfs_fn_ext2 = \
  e2fsimage -d $(1) -f $(2) -s $(EXT2_RW_IMAGE_SIZE)

RW_IMAGE_TYPE=jffs2

make_rw_image_fn = \
  $(call mkfs_fn_$(RW_IMAGE_TYPE),$(1),$(2))

rw_image_embed_ro_image_fn =					\
  mkdir -p proc initrd images ro rw union ;			\
  cp $(PLATFORM_IMAGE_DIR)/$(1) images/$(1) ;			\
  md5sum images/$(1) > images/$(1).md5 ;			\
  echo Built by $(LOGNAME) at `date` > images/$(1).stamp ;	\
  mkdir -p changes/$(1)

# make sure RW_IMAGE_TYPE is a type we know how to build
.PHONY: rw-image-check-type
rw-image-check-type:
	@$(BUILD_ENV) ;								\
	if [ -z "$(make_rw_image_fn)" ] ; then					\
	  $(call build_msg_fn,Unknown read/write fs image type;			\
		              try RW_IMAGE_TYPE=ext2 or RW_IMAGE_TYPE=jffs2) ;	\
	  exit 1;								\
	fi

# read write image
.PHONY: rw-image
rw-image: rw-image-check-type ro-image
	@$(BUILD_ENV) ;						\
	d=$(PLATFORM_IMAGE_DIR) ;				\
	mkdir -p $$d ;						\
	rw_image="$$d/rw.$(RW_IMAGE_TYPE)" ;			\
	ro_image="ro.img" ;					\
	rm -f $$rw_image ;					\
	tmp_dir="`mktemp -d $$d/rw-image-XXXXXX`" ;		\
	chmod 0755 $${tmp_dir} ;				\
	cd $${tmp_dir} ;					\
	trap "rm -rf $${tmp_dir}" err ;				\
	fakeroot /bin/bash -c "{				\
	  set -eu$(BUILD_DEBUG) ;				\
	  $(linuxrc_makedev) ;					\
	  $(call rw_image_embed_ro_image_fn,$${ro_image}) ;	\
	  $(call make_rw_image_fn,$${tmp_dir},$${rw_image}) ;	\
	}" ;							\
	: cleanup tmp directory ;				\
	rm -rf $${tmp_dir}

images: linuxrc-install linux-install $(image_extra_dependencies) rw-image
	@$(BUILD_ENV) ;						\
	d=$(PLATFORM_IMAGE_DIR) ;				\
	cd $(BUILD_DIR)/linux-$(PLATFORM) ;			\
	i="" ;							\
	[[ -z $$i && -f bzImage ]] && i=bzImage ;		\
	[[ -z $$i && -f zImage ]] && i=zImage ;			\
	[[ -z $$i && -f linux ]] && i=linux ;			\
	[[ -z $$i && -f vmlinux ]] && i=vmlinux ;		\
	[[ -z $$i ]]						\
	  && $(call build_msg_fn,no linux image to install	\
		in $(BUILD_DIR)/linux-$(PLATFORM))		\
	  && exit 1 ;						\
	cp $$i $$d

######################################################################
# Tool chain build/install
######################################################################

.PHONY: ccache-install
ccache-install:
	$(MAKE) -C $(MU_BUILD_ROOT_DIR)	ccache-build
	mkdir -p $(TOOL_INSTALL_DIR)/ccache-bin
	ln -sf $(MU_BUILD_ROOT_DIR)/build-tool-native/ccache/ccache \
		$(TOOL_INSTALL_DIR)/ccache-bin/$(TARGET_PREFIX)gcc 

TOOL_MAKE = $(MAKE) is_build_tool=yes

tool_make_target_fn = 							\
  $(if $(strip $(NATIVE_TOOLS)),					\
    $(TOOL_MAKE) $(patsubst %,%-$(1),$(NATIVE_TOOLS)) ARCH=native || exit 1 ;) \
  $(TOOL_MAKE) $(patsubst %,%-$(1),$(CROSS_TOOLS))

.PHONY: install-tools
install-tools:
	$(call tool_make_target_fn,install)

.PHONY: bootstrap-tools
bootstrap-tools:
	$(TOOL_MAKE) make-install findutils-install git-install \
	automake-install autoconf-install libtool-install fakeroot-install


######################################################################
# Clean
######################################################################

package_clean_script =							\
  @$(call build_msg_fn, Cleaning $* in $(PACKAGE_INSTALL_DIR)) ;	\
  $(BUILD_ENV) ;							\
  $(if $(is_build_tool),,rm -rf $(PACKAGE_INSTALL_DIR) ;)		\
  rm -rf $(TIMESTAMP_DIR)/$(call timestamp_name_fn,*) ;			\
  $(if $($(PACKAGE)_clean),						\
    $($(PACKAGE)_clean),						\
    $(PACKAGE_MAKE) clean)

.PHONY: %-clean
%-clean:
	$(package_clean_script)

# Wipe e.g. remove build and install directories for packages.
package_wipe_script =											\
  @message=$(if $(is_build_tool),"Wiping build $(PACKAGE)","Wiping build/install $(PACKAGE)") ;		\
  $(call build_msg_fn,$$message) ;									\
  $(BUILD_ENV) ;											\
  rm -rf $(if $(is_build_tool),$(PACKAGE_BUILD_DIR),$(PACKAGE_INSTALL_DIR) $(PACKAGE_BUILD_DIR))

.PHONY: %-wipe
%-wipe:
	$(package_wipe_script)

# Wipe entire build/install area for TAG and PLATFORM
.PHONY: wipe-all
wipe-all:
	@$(call build_msg_fn, Wiping $(BUILD_DIR) $(INSTALL_DIR)) ;	\
	$(BUILD_ENV) ;							\
	rm -rf $(BUILD_DIR) $(INSTALL_DIR)

# Clean everything
distclean:
	rm -rf $(MU_BUILD_ROOT_DIR)/$(BUILD_PREFIX_package)*/
	rm -rf $(MU_BUILD_ROOT_DIR)/$(BUILD_PREFIX_tool)*
	rm -rf $(MU_BUILD_ROOT_DIR)/$(INSTALL_PREFIX)*
	rm -rf $(MU_BUILD_ROOT_DIR)/$(IMAGES_PREFIX)*
	rm -rf $(TOOL_INSTALL_DIR)
	rm -rf $(MU_BUILD_ROOT_DIR)/*.deb
	rm -rf $(MU_BUILD_ROOT_DIR)/*.rpm
	rm -rf $(MU_BUILD_ROOT_DIR)/*.changes
	rm -rf $(MU_BUILD_ROOT_DIR)/python
	if [ -e /usr/bin/dh ];then (cd $(MU_BUILD_ROOT_DIR)/deb/;debian/rules clean); fi
	rm -f $(MU_BUILD_ROOT_DIR)/deb/debian/*.install
	rm -f $(MU_BUILD_ROOT_DIR)/deb/debian/changelog