forked from sta523-fa20/lecture_slides
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lec_15.html
605 lines (439 loc) · 13.5 KB
/
lec_15.html
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
595
596
597
598
599
600
601
602
603
604
605
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>make</title>
<meta charset="utf-8" />
<meta name="author" content="Shawn Santo" />
<link rel="stylesheet" href="slides.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# make
## Programming for Statistical Science
### Shawn Santo
---
## Supplementary materials
Full video lecture available in Zoom Cloud Recordings
Additional resources
- [minimal make](http://kbroman.org/minimal_make/) by Karl Broman
- [Why Use Make](https://bost.ocks.org/mike/make/) by Mike Bostock
- GNU make [manual](https://www.gnu.org/software/make/manual/make.html)
- [Make for Windows](http://gnuwin32.sourceforge.net/packages/make.htm)
---
## `make`
- Automatically build software / libraries / documents by specifying
dependencies via a file named `Makefile`
- provide instructions for what you want to build and how it can be built
- Originally created by Stuart Feldman in 1976 at Bell Labs
- Almost universally available (all flavors of UNIX / Linux / OSX)
<br/>
Check for `make` with
```bash
make --version
```
```
#> GNU Make 3.81
#> Copyright (C) 2006 Free Software Foundation, Inc.
#> This is free software; see the source for copying conditions.
#> There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
#> PARTICULAR PURPOSE.
#>
#> This program built for i386-apple-darwin11.3.0
```
---
## `Makefile` structure
```make
target: prerequisite_1 prerequisite_2 ...
recipe
...
...
```
- `target` is the file you want to generate
- `prerequisite_*` are the files the target file depends on
- a recipe is an action that `make` carries out,
commands you run in the terminal
Alternatively,
```make
targetfile: sourcefile
command
...
...
```
---
## `Makefile` structure
A more realistic structure:
```make
target: prerequisite_1 prerequisite_2 ...
recipe
...
...
prerequisite_1: prerequisite_1a prerequisite_1b ...
recipe
...
...
prerequisite_2: prerequisite_2a prerequisite_2b ...
recipe
...
...
```
---
## Example
```make
paper.html: paper.Rmd Fig1/fig.png Fig2/fig.png
Rscript -e "library(rmarkdown);render('paper.Rmd')"
Fig1/fig.png: Fig1/fig.R
cd Fig1; Rscript fig.R
Fig2/fig.png: Fig2/fig.R
cd Fig2; Rscript fig.R
```
<br/>
What are the targets and dependencies?
--
<br/>
The first target is the default goal of what `make` tries to create.
---
## Another example
```make
hd_cov_test_band.o: hd_cov_test_band.c
export PKG_CFLAGS="-fopenmp"
export PKG_LIBS="-lgomp"
R CMD SHLIB hd_cov_test_band.c
clean:
rm hd_cov_test_band.o
rm hd_cov_test_band.so
.PHONY: clean
```
---
## How `make` processes a `Makefile`
1. Once you have a `Makefile` written, type `make` in your terminal.
```make
make
```
2. `make` looks for files named `GNUmakefile`, `makefile`, or
`Makefile`.
3. The `make` program uses the `Makefile` data base and
last-modification times of the files to decide which of the files
need to be updated.
4. For each file that needs to be updated, the recipes are executed.
--
<br/>
```make
hd_cov_test_band.o: hd_cov_test_band.c
export PKG_CFLAGS="-fopenmp"
export PKG_LIBS="-lgomp"
R CMD SHLIB hd_cov_test_band.c
```
---
## Understanding `make`
Consider the `Makefile` below. I run `make`. Later, I change
some code in `Fig2/fig.R` and save the file. What is updated when I run
`make` again?
```make
paper.html: paper.Rmd Fig1/fig.png Fig2/fig.png
Rscript -e "library(rmarkdown);render('paper.Rmd')"
Fig1/fig.png: Fig1/fig.R
cd Fig1;Rscript fig.R
Fig2/fig.png: Fig2/fig.R
cd Fig2;Rscript fig.R
```
--
<br/>
What if I only change some text in `paper.Rmd` and then save the file?
---
## `Makefile` tips
1. Name your file `Makefile`.
2. Use `tab` to add recipes.
3. Use `#` to add comments to your `Makefile`.
4. Split long lines with `\`.
5. Have one target precede each `:`.
6. Remember, recipes are meant to be interpreted by the shell and thus
are written using shell syntax.
7. Use semicolons to specify a sequence of recipes to be executed in a
single shell invocation.
---
class: inverse, center, middle
# `make` Demo
---
class: inverse, center, middle
# Some advanced `make`
---
## Variables
Like R, or other languages, we can define variables.
```make
Fig1/fig.png: Fig1/fig.R
cd Fig1;Rscript fig.R
```
```make
R_OPTS=--no-save --no-restore --no-site-file --no-init-file --no-environ
Fig1/fig.png: Fig1/fig.R
cd Fig1;Rscript $(R_OPTS) fig.R
```
<br/>
- Typically, we use uppercase letters for variable names.
- Refer to a variable's value by `${MY_VARIABLE}` or `$(MY_VARIABLE)`.
- Do not use `:`, `#`, `=`, or a white space in your variable's name.
---
## Built-in variables
| Variable | Description |
|---------:|--------------------------------------------|
| `$@` | the file name of the target |
| `$<` | the name of the first dependency |
| `$^` | the names of all dependencies |
| `$(@D)` | the directory part of the target |
| `$(@F)` | the file part of the target |
| `$(<D)` | the directory part of the first dependency |
| `$(<F)` | the file part of the first dependency |
---
## Pattern rules
Often we want to build several files in the same way. For these cases
we can use `%` as a special wildcard character to match both targets
and dependencies.
Rather than our `Makefile` be
```make
Fig1/fig.png: Fig1/fig.R
cd Fig1; Rscript fig.R
Fig2/fig.png: Fig2/fig.R
cd Fig2; Rscript fig.R
```
we can use built-in variables and patterns to have
```make
Fig%/fig.png: Fig%/fig.R
cd $(<D);Rscript $(<F)
```
- `%` can match any nonempty substring.
- The substring that the `%` matches is called the stem.
- A prerequisite with `%` has the same stem that was
matched by the `%` in the target.
---
## Phony targets
A phony target is one that is not really the name of a file; rather it is just
a name for a recipe to be executed when you make an explicit request. There are
two reasons to use a phony target: to avoid a conflict with a file of the same
name, and to improve performance.
For example,
```make
clean:
rm *.log
```
would remove all `.log` files when `make clean` is run. However, a problem can
arise if we ever have a file named `clean`.
--
To make this more robust we can configure it as
```make
.PHONY: clean
clean:
rm *.log
```
Command `make clean` will remove the log files regardless of whether
a file named `clean` exists.
---
Another common phony target is `all`. Its prerequisites are all
the individual programs we want to build. For example:
```make
.PHONY: all
all: prog1 prog2 prog3
prog1: prog1.o utils.o
cc -o prog1 prog1.o utils.o
prog2: prog2.o
cc -o prog2 prog2.o
prog3: prog3.o sort.o utils.o
cc -o prog3 prog3.o sort.o utils.o
```
<br/>
Use `make` to build all the programs. Or build a subset by specifying
each program's name: `make prog1 prog2`.
---
## Fancy `Makefile`
Our original example:
```make
paper.html: paper.Rmd Fig1/fig.png Fig2/fig.png
Rscript -e "library(rmarkdown);render('paper.Rmd')"
Fig1/fig.png: Fig1/fig.R
cd Fig1;Rscript fig.R
Fig2/fig.png: Fig2/fig.R
cd Fig2;Rscript fig.R
```
--
Update:
```make
all: paper.html
paper.html: paper.Rmd Fig1/fig.png Fig2/fig.png
Rscript -e "library(rmarkdown);render('paper.Rmd')"
Fig%/fig.png: Fig%/fig.R
cd $(<D);Rscript $(<F)
clean:
rm paper.html
rm -f Fig*/*.png
.PHONY: all clean
```
---
## Another fancier `Makefile`
```make
SRC = $(wildcard *.Rmd)
TAR_PDF = $(SRC:.Rmd=.pdf)
TAR_HTML = $(SRC:.Rmd=.html)
all: $(TAR_PDF) $(TAR_HTML)
%.pdf: %.html
Rscript -e "pagedown::chrome_print('$(<F)')"
%.html: %.Rmd
Rscript -e "rmarkdown::render('$(<F)')"
clean:
rm *.pdf
rm *.html
.PHONY: clean all
```
---
## Exercise
Create a `Makefile` for the R project in the
[learn_make](https://github.com/sta523-fa20/learn_make) repository
on GitHub. The target goal should be `learn_make.html`. The below steps will
help guide you in creating `Makefile`.
1. Diagram the dependency structure on paper.
2. First, create a `Makefile` that only knits the Rmd file and
produces the `learn_make.html` file.
3. Next, add rules for the data dependencies.
4. Add phony `clean_html` and `clean_data` targets that delete the html file and
delete the rds files in `data/`, respectively.
5. Revise your `Makefile` with built-in variables or other useful features.
???
```make
learn_make.html: learn_make.Rmd data/ok_tor.rds data/fire_stations.rds data/school_districts.rds
Rscript -e "library(rmarkdown); render('learn_make.Rmd')"
data/ok_tor.rds: R/get_tornadoes.R
Rscript $<
data/fire_stations.rds: R/get_fire_stations.R
Rscript $<
data/school_districts.rds: R/get_school_districts.R
Rscript $<
clean_html:
rm learn_make.html
clean_data:
cd data; rm *.rds
.PHONY: clean_html clean_data
```
---
## References
1. Broman, K. (2020). minimal make. http://kbroman.org/minimal_make/.
2. GNU make. (2020).
https://www.gnu.org/software/make/manual/make.html#toc-An-Introduction-to-Makefiles.
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
(function() {
"use strict"
// Replace <script> tags in slides area to make them executable
var scripts = document.querySelectorAll(
'.remark-slides-area .remark-slide-container script'
);
if (!scripts.length) return;
for (var i = 0; i < scripts.length; i++) {
var s = document.createElement('script');
var code = document.createTextNode(scripts[i].textContent);
s.appendChild(code);
var scriptAttrs = scripts[i].attributes;
for (var j = 0; j < scriptAttrs.length; j++) {
s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
}
scripts[i].parentElement.replaceChild(s, scripts[i]);
}
})();
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};
for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>