unmake 0.0.25

a makefile linter
Documentation
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
# WARNINGS

`unmake` offers various checks to optimize your makefiles.

Note that `unmake` does not evaluate makefiles, and therefore ignores quirks arising from macro expansions.

Most warnings feature line numbers with an approximate location of the issue in the makefile.

# General

## MISSING_FINAL_EOL

When a text file suddenly reaches End Of File (EOF) without a Line Feed (LF), then the file is said to not feature a final End Of Line (EOL).

UNIX text files expect each line to terminate in a final End Of Line, including the last line.  Omitting a final EOF can cause subtle text processing errors.

### Fail

```make
PKG = curl<EOF>
```

### Pass

```make
PKG = curl<LF>
<EOF>
```

### Mitigation

* Configure [EditorConfig](https://editorconfig.org/) and text editors to apply a final EOL.

## PHONY_TARGET

> Prerequisites of this special target are targets themselves; these targets (known as phony targets) shall be considered always out-of-date when the make utility begins executing. If a phony target’s commands are executed, that phony target shall then be considered up-to-date until the execution of make completes. Subsequent occurrences of .PHONY shall also apply these rules to the additional targets. A .PHONY special target with no prerequisites shall be ignored. If the -t option is specified, phony targets shall not be touched. Phony targets shall not be removed if make receives one of the asynchronous events explicitly described in the ASYNCHRONOUS EVENTS section.

--POSIX 202x Issue 8/D3

Briefly, make assumes that most rule targets are actual filenames. However, conventional targets named `all`, `lint`, `install`, `uninstall`, `publish`, `test*`, or `clean*`, are usually not actual filenames. These are logical targets.

When make is requested to perform these logical, top-level targets, then make needs to know not to apply the usual file-based caching. The way to do this is by declaring `.PHONY:` special rules, whose prerequisites are your logical targets.

You may write logical target declarations as whitespace delimited prerequisites in a single `.PHONY:` rule, or distribute logical target declarations among multiple `.PHONY:` rules.

As well, aggregate targets like `port: cross-compile archive`, that do not have any commands, are usually not actual filenames themselves. Aggregate, commandless targets are also logical targets. Which means that they should also have an entry as a prerequisite in a `.PHONY:` special rule. However, make sometimes infers target trees based on common C filename patterns. So we cannot reliably warn on potentially `.PHONY` worthy empty-command targets until a complete inferred rule system is implemented.

Due to the variance in artifact names, `unmake` cannot automate checking for all possible targets deserving `.PHONY` declarations. Neither `make` nor `unmake` knows this application-specific information. The makefile maintainer should supply this information, and configure any needed `.PHONY` declarations accordingly.

makefile authors may use variables in `.PHONY` prerequisites:

```make
ALLTARGETS!=ls -a
.PHONY: $(ALLTARGETS)
```

The above example effectively treats every target as `.PHONY`, without resorting to boilerplate or breaking POSIX compatibility.

### Fail

```make
all:
	echo "Hello World!"
```

```make
test: test-1 test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

```make
clean:
	-rm -rf bin
```

### Pass

```make
.PHONY: all

all:
	echo "Hello World!"
```

```make
.PHONY: test test-1 test-2

test: test-1 test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

```make
.PHONY: clean

clean:
	-rm -rf bin
```

```make
.PHONY: empty
empty:;
```

```make
.PHONY: port

port: cross-compile archive
```

```make
empty:;
```

```make
port: cross-compile archive
```

If the hypothetical `cross-compile` and `archive` targets are themselves merely logical targets rather than filenames, then they should be declared `.PHONY` as well.

```make
ALLTARGETS!=ls -a *
.PHONY: $(ALLTARGETS)

all: welcome

welcome:
	echo "Hello World!"
```

### Mitigation

* Avoid using make build artifacts named `all`, `test*`, or `clean*`.
* Declare any targets named `all`, `lint`, `install`, `uninstall`, `publish`, `test*`, or `clean*` as `.PHONY`
* Declare command-less rule targets as `.PHONY`
* Note that POSIX usually requires a semicolon (`;`) when declaring rules without commands.
* Note that special targets like `.NOTPARALLEL`, `.PHONY`, `.POSIX`, `.WAIT`, etc., should not themselves be declared as `.PHONY`

## MAKEFILE_PRECEDENCE

> By default, the following files shall be tried in sequence: ./makefile and ./Makefile.

--POSIX 202x Issue 8/D3

Using the lowercase filename `makefile` loads slightly faster than the capitalized filename `Makefile`. The lowercase naming reduces strain on filesystem requests.

### Fail

Makefile:

```make
PKG = curl
```

### Pass

makefile:

```make
PKG = curl
```

### Mitigation

* Rename `Makefile` to `makefile`

## CURDIR_ASSIGNMENT_NOP

> The CURDIR environment variable shall not affect the value of the CURDIR macro unless the -e option is specified. If the -e option is not specified, there is a CURDIR environment variable set, and its value is different from the CURDIR macro value, the environment variable value shall be set to the macro value. If CURDIR is defined in the makefile, present in the MAKEFLAGS environment variable, or specified on the command line, it shall replace the original value of the CURDIR macro in accordance with the logical order described above, but shall not cause make to change its current working directory.

Assignment to `CURDIR` does not actually change the current working directory of the make execution.

### Fail

```make
CURDIR = build
```

### Mitigation

* Avoid assigning the `CURDIR` macro
* Note that some commands offer a built-in way to adjust the current directory, e.g. `tar -C <dir>`
* Promote complex logic to a dedicated script

## WD_NOP

make often resets the working directory across successive commands, and across successive rules. Common commands for changing directories, such as `cd`, `pushd`, and `popd`, may not have the desired effect.

Furthermore, `push` and `popd` are GNU bash extensions to the POSIX sh interpreter standard, and are likely to fail on other machines.

### Fail

```make
all:
	cd foo
```

```make
all:
	pushd foo
```

```make
all:
	popd
```

### Mitigation

* Avoid running makefile commands beginning with `cd`, `pushd`, or `popd`
* Reduce use of shell implementation-specific commands in makefiles
* Note that some commands offer a built-in way to adjust the current directory, e.g. `tar -C <dir>`
* Promote complex logic to a dedicated script

## WAIT_NOP

> When .WAIT appears as a target, it shall have no effect.

`.WAIT` is intended for use as a pseudo-prerequisite marker, in order to customize synchronization logic. `.WAIT` behaves as a useless no operation (NOP) when written as a target.

### Fail

```make
.WAIT:

test: test-1 test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

### Pass

```make
test: test-1 .WAIT test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

```make
test: test-1 test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

### Mitigation

* Use `.WAIT` as an optional pseudo-prerequisite syncronization marker
* Avoid declaring `.WAIT` as a target.

## PHONY_NOP

> A .PHONY special target with no prerequisites shall be ignored.

`.PHONY` with no prerequisites behaves as a useless no operation (NOP). When using the special target `.PHONY` rule, specify at least one prerequisite.

### Fail

```make
.PHONY:

foo: foo.c
	gcc -o foo foo.c
```

### Pass

```make
foo: foo.c
	gcc -o foo foo.c

clean:
	rm -rf bin
```

```make
.PHONY: clean

foo: foo.c
	gcc -o foo foo.c

clean:
	rm -rf bin
```

### Mitigation

* Use `.WAIT` as an optional pseudo-prerequisite syncronization marker
* Avoid declaring `.WAIT` as a target.

## REDUNDANT_NOTPARALLEL_WAIT

The `.WAIT` pseudo-prerequisite disables asynchronous processing between prerequisites of a specific rule.

`.NOTPARALLEL:` disables asyncronous processing for all prerequisites in all rules.

Using both of these special targets simultaneously is unnecessary.

### Fail

```make
.NOTPARALLEL:

test: test-1 .WAIT test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

### Pass

```make
test: test-1 .WAIT test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

```make
.NOTPARALLEL:

test: test-1 test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

```make
test: test-1 test-2

test-1:
	echo "Hello World!"

test-2:
	echo "Hi World!"
```

### Mitigation

* Avoid using `.NOTPARALLEL:` with `.WAIT` redundantly.
* Redundancy of `.WAIT` with `.NOTPARALLEL` is best avoided.

## REDUNDANT_SILENT_AT

At (`@`) elides an individual command from make output. This is useful for reducing log noise.

The `.SILENT` special target also elides commands from make output. If the special rule `.SILENT:` is declared with no prerequisites, then all make commands globally are silenced. If the special rule `.SILENT:` is declared with prerequisite targets, then all commands for those specific targets are silenced.

Using both of these simultaneously is unnecessary.

### Fail

```make
.SILENT:

lint:
	@unmake .
```

```make
.SILENT: lint

lint:
	@unmake .
```

### Pass

```make
.SILENT:

lint:
	unmake .
```

```make
lint:
	@unmake .
```

```make
lint:
	unmake .
```

### Mitigation

* Avoid using at (`@`) with `.SILENT` redundantly.
* Redundancy of `.SILENT` with at (`@`) is best avoided.

## REDUNDANT_IGNORE_MINUS

Hyphen-minus (`-`) continues makefile execution past soft failure exit codes of an individual command. This is useful for implementing cleanup tasks and other idempotent tasks.

The `.IGNORE` special target also continues makefile execution past soft failures. If the special rule `.SILENT:` is declared with prerequisite targets, then exit codes for commands for those specific targets are ignored. However, declaring `.IGNORE:` with no prerequisites is likely to cause subtle build problems.

Using both `-` and `.IGNORE` simultaneously is unnecessary.

If the special rule `.IGNORE:` is declared with no prerequisites, then exit codes of all make commands globally are ignored. Due to more severe issues with `.IGNORE:` declared with no prerequisites, detailed in the `GLOBAL_IGNORE` check, the `REDUNDANT_IGNORE_MINUS` check does not provide an automatic check for redundant `-` with a global `.IGNORE:` declaration.

### Fail

```make
.IGNORE:

clean:
	-rm -rf bin
```

```make
.IGNORE: clean

clean:
	-rm -rf bin
```

### Pass

```make
IGNORE: clean

clean:
	rm -rf bin
```

```make
clean:
	-rm -rf bin
```

```make
clean:
	rm -rf bin
```

### Mitigation

* Note that `.IGNORE:` declared with no prerequisites is likely to cause subtle build problems.
* Avoid using hyphen-minus (`-`) with `.IGNORE` redundantly.
* Redundancy of `.IGNORE` with hyphen-minus (`-`) is best avoided.

## GLOBAL_IGNORE

When the special target rule `.IGNORE:` is declared with no prerequisites, then make ignores exit codes for all make commands, for all rules. This is hazardous, and tends to invite file corruption.

Caution: Avoid using `.IGNORE:` this way. When using the special target `.IGNORE` rule, declare at least one prerequisite.

### Fail

```make
.IGNORE:

foo: foo.c
	gcc -o foo foo.c

clean:
	rm -f foo
```

### Pass

```make
.IGNORE: clean

foo: foo.c
	gcc -o foo foo.c

clean:
	rm -f foo
```

```make
foo: foo.c
	gcc -o foo foo.c

clean:
	-rm -f foo
```

```make
foo: foo.c
	gcc -o foo foo.c

clean:
	rm -f foo
```

### Mitigation

* Avoid using `.IGNORE:` without at least one prerequisite.
* Optionally, apply hyphen-minus (`-`) to individual commands.

## SIMPLIFY_AT / SIMPLIFY_MINUS

Using at (`@`) or hyphen-minus (`-`) command prefixes for several individual commands in a rule can be simplified to a `.SILENT` or `.IGNORE` declaration respectively.

Due to flexibility needs, this warning emits automatically for rules with at least two or more commands, where all of the commands feature the same at (`@`) or hyphen-minus (`-`) prefix. Rules with zero or one command, and rules with mixed command prefixes, may not trigger this warning.

We generally recommend using `.SILENT` / `.IGNORE` over individual at (`@`) / hyphen-minus (`-`).

### Fail

```make
welcome:
	-echo foo
    -echo bar
    -echo baz
```

```make
welcome:
	@echo foo
    @echo bar
    @echo baz
```

### Pass

```make
.IGNORE: welcome

welcome:
	echo foo
    echo bar
    echo baz
```

```make
.SILENT: welcome

welcome:
	echo foo
    echo bar
    echo baz
```

```make
.SILENT:

welcome:
	echo foo
    echo bar
    echo baz
```
### Mitigation

* Use `.SILENT` / `.IGNORE` targets rather than individual at (`@`) / hyphen-minus (`-`) targets.
* Note that `.IGNORE` may have poor behavior without at least one prerequisite.

## IMPLEMENTATION_DEFINED_TARGET

> The interpretation of targets containing the characters '%' and '"' is implementation-defined.

POSIX make has no portable semantic for percent signs (`%`) or double-quotes (`"`) in targets or prerequisites. Using these can vendor lock a makefile onto a specific make implementation, and/or trigger build failures.

### Fail

```make
all: foo%

foo%: foo.c
	gcc -o foo% foo.c
```

```make
all: "foo"

"foo": foo.c
	gcc -o "foo" foo.c
```

### Mitigation

* Avoid percents (`%`) and double-quotes (`"`), in targets and prerequisites.

## COMMAND_COMMENT

When a rule command contains a sharp (`#`), then make forwards the comment to the shell interpreter. This can cause the command to fail in multiline commands. This can cause the command to fail in certain shell interpreters. This increases log noise.

### Fail

```make
foo: foo.c
	#build foo
	gcc -o foo foo.c
```

```make
foo: foo.c
	@#gcc -o foo foo.c
```

```make
foo: foo.c
	-#gcc -o foo foo.c
```

```make
foo: foo.c
	+#gcc -o foo foo.c
```

```make
foo: foo.c
	gcc \
#output file \
		-o foo \
		foo.c
```

### Pass

```make
foo: foo.c
#build foo
	gcc -o foo foo.c
```

```make
#build foo
foo: foo.c
	gcc -o foo foo.c
```

```make
#output file
foo: foo.c
	gcc \
		-o foo \
		foo.c
```

```make
foo: foo.c
	gcc -o foo foo.c
```

```make
#foo: foo.c
#	gcc -o foo foo.c
```

```make
<remove rule>
```

### Mitigation

* Move comments up above multiline commands.
* Move comments to the leftmost column, fully *de*dented.
* Consider removing extraneous lines.

## REPEATED_COMMAND_PREFIX

Supplying the same command prefix multiple times is wasteful.

### Fail

```make
test:
	@@+-+--echo "Hello World!"
```

### Pass

```make
test:
	@+-echo "Hello World!"
```

```make
test:
	echo "Hello World!"
```

(Any combination of `@`, `+`, and `-` is fine as long as none of the prefix types are duplicated.)

### Mitigation

* Remove redundant code.
* Code that is redundant should be removed.

## BLANK_COMMAND

Rule commands consisting of nothing more than at (`@`), plus (`+`), minus (`-`) prefixes, and/or whitespace, can produce spurious results when the essentially empty command is executed. Without any prefixes, blank commands are likely to trigger parse errors.

Blank commands are distinct from blank lines, which normally act as comments.

Blank commands are distinct from rules that are reset to have *no* commands.

### Fail

```make
test:
	@+-
```

### Pass

```make
test:
	@+-echo "Hello World!"
```

```make
test:
	@+-echo "Hello World!"
```

```make
test:;
```

```make
#test:
```

```make
<rule removed>
```

### Mitigation

* Give the command something useful to do.
* Remove extraneous code.

## WHITESPACE_LEADING_COMMAND

After any optional `@`/`+`/`-` prefix modifiers, whitespace leading a command is bad form. In commands, leading whitespace may be a sign of a typo in an earlier multiline instructions.

The earliest whitespace for a command, should consist mainly of the standard one-tab indentation.

Successive lines in a multiline make command commonly may use tabs for visual clarity.

### Fail

```make
foo:
<tab><space>gcc -o foo foo.c
```

```make
foo:
<tab>@+-<space>gcc -o foo foo.c
```

### Pass

```make
foo:
<tab><no space>gcc -o foo foo.c
```

```make
foo:
<tab>@+-<no space>gcc -o foo foo.c
```

```make
foo:
	gcc \
		-o \
		foo \
		foo.c
```

### Mitigation

* Verify multiline instruction syntax in earlier commands.
* Avoid inserting whitespace between `@`/`+`/`-` prefix modifiers and the rest of the command.
* Generally, avoid starting commands with whitespace.
* Consider indenting successive lines in a multiline make command with 1 tab (prerequisites) or 2 tabs (commands), for visual clarity.

## NO_RULES

make generally expects a makefile to define at least one (non-special) rule to provide some action on when running `make`. Excepting include files like `sys.mk` or `*.include.mk`.

### Fail

makefile:

```make
.POSIX:
PKG = curl
```

### Pass

makefile:

```make
.POSIX:
PKG = curl

all:
	apt install -y $(PKG)
```

provision.include.mk:

```make
PKG = curl
```

### Mitigation

* Declare at least one non-special rule in most makefiles.
* Rename include files to `*.include.mk`.

## RULE_ALL

make interprets the first non-special rule as the default rule. Apart from non-special targets, the top-most rule is conventionally named `all`. This helps to avoid confusion and accidents.

Common include files like `sys.mk` and `*.include.mk` may export rules, named `all` or something else. However, the top-most, non-special default rule semantic still applies, so order `include` lines and rule declarations carefully.

### Fail

makefile:

```make
.POSIX:

build:
	echo "Hello World!"
```

### Pass

makefile:

```make
.POSIX:

all:
	echo "Hello World!"
```

Optionally, list subsequent rules as prerequisites for the `all` target.

makefile:

```make
.POSIX:

all: build

build:
	echo "Hello World!"
```

foo.include.mk:

```make
build:
	echo "Hello World!"
```

### Mitigation

* Name the top-most, non-special, default rule `all`.
* Note that the order of `include` lines and rule declarations interacts with the top-most, non-special, default rule.

## STRICT_POSIX

> To receive exactly the behavior described in this section, the user shall ensure that a portable makefile shall:
>
> • Include the special target .POSIX
>
> • Omit any special target reserved for implementations (a leading period followed by uppercase letters) that has not been specified by this section
>
> The behavior of make is unspecified if either or both of these conditions are not met.

It is good form to begin most makefiles with a `.POSIX:` special target rule marker. This marker instructs make implementations to preserve processing semantics as defined in the POSIX standard without alteration. Omitting the marker may result in unknown behavior. So most makefiles benefit from more predictable behavior by leading with `.POSIX:`. You can declare this marker at the very first line of a makefile, or after some blank/comment lines.

However, makefiles named `*.include.mk`, designed for simple text inclusion into other makefiles, should omit the `.POSIX:` marker.

Also, make distributions commonly install a `sys.mk` include file that provides defines a foundational set of macros, include lines, and rules for make implementations. A `.POSIX:` marker may not be necessary for make distribution files. As well, files named like `GNUmakefile`, that are known to be implementation-specific, should not use this marker. But most any POSIX makefile *not* named to indicate its intention as an include file, should feature the `.POSIX:` marker.

### Fail

makefile:

```make
PKG = curl
```

### Pass

makefile:

```make
.POSIX:
PKG = curl
```

provision.include.mk:

```make
PKG = curl
```

GNUmakefile:

```make
PKG = curl
```

Special targets like `.POSIX` and `.PHONY` are important, but they may be elided from other passing examples in this document, for brevity.

### Mitigation

* Declare `.POSIX:` in most makefiles.
* Rename makefiles intended for inclusion to `*.include.mk`.
* Avoid declaring `.POSIX:` in makefiles for specific implementations like `GNUmakefile`.

## RESERVED_TARGET

> Targets and prerequisites consisting of a leading `<period>` followed by the uppercase letters "POSIX" and then any other characters are reserved for future standardization. Targets and prerequisites consisting of a leading `<period>` followed by one or more uppercase letters, that are not described above, are reserved for implementation extensions.

Other than certain special targets, POSIX reserves targets and prerequisites of the form `.(A-Z)`... for either future POSIX use, or for implementation-specific extensions.

Generally, such targets are non-portable. However, the user may have simply mistyped a well-known POSIX special target name. Note that typos may trigger parse errors.

### Fail

```make
.POSIXX:
```

```make
.TEST:
	echo "Hello World!"
```

```make
test: .TEST-UNIT .TEST-INTEGRATION
```

### Pass

```make
.POSIX:
```

```make
foo: foo.c
	gcc -o foo foo.c

test: foo
	./foo
```

```make
test: test-unit test-integration
```

### Mitigation

* Avoid using reserved names in targets or prerequisites.
* Consider pair programming to spot typos.

# Undefined Behavior (UB)

Linter warnings concerning UB level portability issues tend to carry **higher** risk compared to other warnings. This is a consequence of the POSIX standard not specifying any particular error handling (or error detection) semantic for make implementations to follow.

In the case of UB, a makefile may trigger an error message during certain project builds, silently skip processing, corrupt files, segfault, fire missiles, and/or any number of undefined behaviors.

## UB_LATE_POSIX_MARKER

> If it appears as the first non-comment line in the makefile, make shall process the makefile as specified by this section; otherwise, the behavior of make is unspecified.

When the `.POSIX:` rule is used in a makefile, it must be the first thing in the makefile, apart from any blank or commented lines.

Note that common include files like `sys.mk` or `*.include.mk`, should not present `.POSIX:`, as their text necessarily copies as late as some earlier `include` line in an outer makefile.

### Fail

makefile:

```make
PKG = curl
.POSIX:
```

makefile:

```make
.POSIX:
.POSIX:
```

provision.include.mk:

```make
.POSIX:
PKG = curl
```

### Pass

makefile:

```make
.POSIX:
PKG = curl
```

provision.include.mk:

```make
PKG = curl
```

### Mitigation

* Move `.POSIX` to the first non-blank, non-commented line in the makefile.
* Avoid mixing the `.POSIX` target with other targets in a single rule declaration.
* Avoid declaring `.POSIX:` in `sys.mk` or `*.include.mk`.
* Avoid declaring `.POSIX:` multiple times.

## UB_AMBIGUOUS_INCLUDE

> This standard does not specify precedence between macro definition and include directives. Thus, the behavior of:
>
> `include =foo.mk`
>
>is unspecified.

Ambiguous include/macro instructions do not have a clear meaning. The instruction may behave as `include` the path `=foo.mk`, or behave as defining a macro with the name `include` and the value `foo.mk`. Parsing destabilizes.

### Fail

```make
include =foo.mk
```

### Pass

```make
include=foo.mk
```

```make
include foo.mk
```

```make
INCLUDE = include
$(INCLUDE) =foo.mk
```

```make
PTH = =foo.mk
include $(PTH)
```

### Mitigation

* Avoid using equals (`=`) in path names.
* Avoid using lowercase `include` as a macro name.
* Consider removing whitespace between macro names and assignment operators.

## UB_MAKEFLAGS_ASSIGNMENT

> The result of setting MAKEFLAGS in the Makefile is unspecified.

The `MAKEFLAGS` macro is designed as read-only, set aside for make implementations to store command line flags.

POSIX compliant make implementations automatically preserve command line flags with `MAKEFLAGS`.

make implementations *implicitly* forward `MAKEFLAGS` to any child `$(MAKE)` invocations on behalf of the makefile user.

### Fail

```make
MAKEFLAGS = -j

all:
	$(MAKE) $(MAKEFLAGS) foo.mk
```

### Pass

```make
all:
	$(MAKE) foo.mk
```

### Mitigation

* Avoid assigning to the `MAKEFLAGS` macro.
* Move complex logic to a dedicated script.

## UB_SHELL_MACRO

> The value of the SHELL environment variable shall not be used as a macro and shall not be modified by defining the SHELL macro in a makefile or on the command line.

`SHELL` provides low level functionality to make implementation internals. Expanding or assigning this macro is discouraged.

make implementations that use `SHELL`, tend to set useful defaults. Overriding the defaults may produce non-portable, fragile makefiles.

Some implementations do not define `SHELL`. Assigning a value `SHELL` can create an misleading, non-portable impression of makefile behavior.

Due to `unmake` not evaluating macro expansions, expansion of the `SHELL` macro is not implemented as an automatic check.

Some ancient platforms may present `SHELL` with a `cmd[.exe]` interpreter. But even Windows Command Prompt, the Chocolatey GNU make interpreter tends to default to a POSIX compliant `sh` interpreter suitable for use with makefile commands.

### Fail

```make
SHELL = sh
```

```make
all:
	$(SHELL) script.sh
```

```make
all:
	${SHELL} script.sh
```

### Pass

```make
all:
	./script.sh
```

```make
	sh -c "echo $$SHELL"
```

### Mitigation

* Avoid assignments to the `SHELL` makefile macro.
* Treat the `SHELL` makefile macro as a private, internal make macro
* Note that a distinct `SHELL` environment variable may be available to commands, apart from the `SHELL` make macro.
* Move complex shell logic to a dedicated shell script.

## SILENT_INCLUDE

> If, after proceeding as described above, the file still cannot be opened:
>
> * If the word include was prefixed with a <hyphen-minus> character, the file shall be ignored.
>
> * Otherwise, an error shall occur.

Silent includes suppress error handling in the event that an include line is unable to process. Error suppressions like this risk system corruption and overcomplicate troubleshooting.

### Fail

```make
-include foo.mk
```

### Pass

```make
include foo.mk
```

### Mitigation

* Avoid silent include lines.
* Delegate fancy logic into a shell script or a separate build system.

## NONPORTABLE_ASSIGNMENT

The single colon equals (`:=`) macro assignment operator is not portable POSIX. It is an extension that risks different behavior across make implementations.

Instead, select a modern, portable macro assignment operator. POSIX make supports a variety of portable assignment operators with specialized evaluation semantics for assorted use cases.

### Fail

```make
CLIENT := curl --version
```

### Pass

```make
CLIENT ::= curl --version
```

```make
CLIENT :::= curl --version
```

```make
CLIENT ?= curl --version
```

```make
CLIENT != curl --version
```

```make
CLIENT += curl --version
```

```make
CLIENT = curl --version
```

### Mitigation

* Replace nonportable `:=` assignments with modern POSIX equivalents.