-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrecipes.html
More file actions
1206 lines (754 loc) · 63.3 KB
/
recipes.html
File metadata and controls
1206 lines (754 loc) · 63.3 KB
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
<!DOCTYPE HTML>
<html lang="ko" >
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-161001174-4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-161001174-4');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Recipes | Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="./DAG.html" />
<link rel="prev" href="./static_pattern_rules.html" />
</head>
<body>
<div class="book"
data-level="5.4"
data-chapter-title="Recipes"
data-filepath="recipes.md"
data-basepath="."
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="bug_reports.html">
<a href="./bug_reports.html">
<i class="fa fa-check"></i>
<b>1.</b>
Bug reports
</a>
</li>
<li class="chapter " data-level="2" data-path="variables_and_functions.html">
<a href="./variables_and_functions.html">
<i class="fa fa-check"></i>
<b>2.</b>
Variables and Functions
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="multiline_variables.html">
<a href="./multiline_variables.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Multi-Line Variables
</a>
</li>
<li class="chapter " data-level="2.2" data-path="target_specific_variables.html">
<a href="./target_specific_variables.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Target-Specific Variables
</a>
</li>
<li class="chapter " data-level="2.3" data-path="automatic_variables.html">
<a href="./automatic_variables.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Automatic Variables
</a>
</li>
<li class="chapter " data-level="2.4" data-path="substitution_references.html">
<a href="./substitution_references.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Substitution References
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="special_variables.html">
<a href="./special_variables.html">
<i class="fa fa-check"></i>
<b>3.</b>
Special Variables
</a>
</li>
<li class="chapter " data-level="4" data-path="directives.html">
<a href="./directives.html">
<i class="fa fa-check"></i>
<b>4.</b>
Directives
</a>
<ul class="articles">
<li class="chapter " data-level="4.1" data-path="conditional_directives.html">
<a href="./conditional_directives.html">
<i class="fa fa-check"></i>
<b>4.1.</b>
Conditional Directives
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5" data-path="rules.html">
<a href="./rules.html">
<i class="fa fa-check"></i>
<b>5.</b>
Rules
</a>
<ul class="articles">
<li class="chapter " data-level="5.1" data-path="double_colon_rules.html">
<a href="./double_colon_rules.html">
<i class="fa fa-check"></i>
<b>5.1.</b>
Double-Colon Rules
</a>
</li>
<li class="chapter " data-level="5.2" data-path="pattern_rules.html">
<a href="./pattern_rules.html">
<i class="fa fa-check"></i>
<b>5.2.</b>
Pattern Rules
</a>
</li>
<li class="chapter " data-level="5.3" data-path="static_pattern_rules.html">
<a href="./static_pattern_rules.html">
<i class="fa fa-check"></i>
<b>5.3.</b>
Static Pattern Rules
</a>
</li>
<li class="chapter active" data-level="5.4" data-path="recipes.html">
<a href="./recipes.html">
<i class="fa fa-check"></i>
<b>5.4.</b>
Recipes
</a>
</li>
<li class="chapter " data-level="5.5" data-path="DAG.html">
<a href="./DAG.html">
<i class="fa fa-check"></i>
<b>5.5.</b>
DAG
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6" data-path="special_targets.html">
<a href="./special_targets.html">
<i class="fa fa-check"></i>
<b>6.</b>
Special Targets
</a>
</li>
<li class="chapter " data-level="7" data-path="recursive_make.html">
<a href="./recursive_make.html">
<i class="fa fa-check"></i>
<b>7.</b>
Recursive Make
</a>
</li>
<li class="chapter " data-level="8" data-path="include.html">
<a href="./include.html">
<i class="fa fa-check"></i>
<b>8.</b>
Include
</a>
</li>
<li class="chapter " data-level="9" data-path="one_DAG.html">
<a href="./one_DAG.html">
<i class="fa fa-check"></i>
<b>9.</b>
One DAG
</a>
</li>
<li class="chapter " data-level="10" data-path="parallel_execution.html">
<a href="./parallel_execution.html">
<i class="fa fa-check"></i>
<b>10.</b>
Parallel Execution
</a>
</li>
<li class="chapter " data-level="11" data-path="builtin_functions.html">
<a href="./builtin_functions.html">
<i class="fa fa-check"></i>
<b>11.</b>
Built-in Functions
</a>
<ul class="articles">
<li class="chapter " data-level="11.1" data-path="builtin_functions/text_functions.html">
<a href="./builtin_functions/text_functions.html">
<i class="fa fa-check"></i>
<b>11.1.</b>
Text Functions
</a>
</li>
<li class="chapter " data-level="11.2" data-path="builtin_functions/file_name_functions.html">
<a href="./builtin_functions/file_name_functions.html">
<i class="fa fa-check"></i>
<b>11.2.</b>
File Name Functions
</a>
</li>
<li class="chapter " data-level="11.3" data-path="builtin_functions/conditional_functions.html">
<a href="./builtin_functions/conditional_functions.html">
<i class="fa fa-check"></i>
<b>11.3.</b>
Conditional Functions
</a>
</li>
<li class="chapter " data-level="11.4" data-path="builtin_functions/foreach_function.html">
<a href="./builtin_functions/foreach_function.html">
<i class="fa fa-check"></i>
<b>11.4.</b>
Foreach Function
</a>
</li>
<li class="chapter " data-level="11.5" data-path="builtin_functions/file_function.html">
<a href="./builtin_functions/file_function.html">
<i class="fa fa-check"></i>
<b>11.5.</b>
File Function
</a>
</li>
<li class="chapter " data-level="11.6" data-path="builtin_functions/call_function.html">
<a href="./builtin_functions/call_function.html">
<i class="fa fa-check"></i>
<b>11.6.</b>
Call Function
</a>
</li>
<li class="chapter " data-level="11.7" data-path="builtin_functions/eval_function.html">
<a href="./builtin_functions/eval_function.html">
<i class="fa fa-check"></i>
<b>11.7.</b>
Eval Function
</a>
</li>
<li class="chapter " data-level="11.8" data-path="builtin_functions/value_function.html">
<a href="./builtin_functions/value_function.html">
<i class="fa fa-check"></i>
<b>11.8.</b>
Value Function
</a>
</li>
<li class="chapter " data-level="11.9" data-path="builtin_functions/origin_function.html">
<a href="./builtin_functions/origin_function.html">
<i class="fa fa-check"></i>
<b>11.9.</b>
Origin Function
</a>
</li>
<li class="chapter " data-level="11.10" data-path="builtin_functions/flavor_function.html">
<a href="./builtin_functions/flavor_function.html">
<i class="fa fa-check"></i>
<b>11.10.</b>
Flavor Function
</a>
</li>
<li class="chapter " data-level="11.11" data-path="builtin_functions/make_control_functions.html">
<a href="./builtin_functions/make_control_functions.html">
<i class="fa fa-check"></i>
<b>11.11.</b>
Make Control Functions
</a>
</li>
<li class="chapter " data-level="11.12" data-path="builtin_functions/shell_function.html">
<a href="./builtin_functions/shell_function.html">
<i class="fa fa-check"></i>
<b>11.12.</b>
Shell Function
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="12" data-path="command_line_options.html">
<a href="./command_line_options.html">
<i class="fa fa-check"></i>
<b>12.</b>
Command-line Options
</a>
</li>
<li class="chapter " data-level="13" data-path="debugging.html">
<a href="./debugging.html">
<i class="fa fa-check"></i>
<b>13.</b>
Debugging
</a>
</li>
<li class="chapter " data-level="14" data-path="tips.html">
<a href="./tips.html">
<i class="fa fa-check"></i>
<b>14.</b>
Tips
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Introduction</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="recipes">Recipes</h1>
<p>shell 명령 실행에 의해 실제 작업 처리가 일어나는 곳이 rule 의 recipe 영역입니다.
그 외 global 영역에서 처리되는 변수, 함수, 대입 연산과 사용자가 정의한 rule 들은
recipe 가 실행되기 전에 준비 역할을 하는 셈이죠.
어떤 rule 들이 실행돼야 하는지 그리고 어떤 순서대로 실행돼야 하는지가 결정된 후에
recipe 영역에 있는 shell 명령들이 실행됩니다.</p>
<p>recipe 는 기본적으로 tab 문자를 이용해 구분합니다.
makefile 에 tab 문자로 시작하는 임의의 단어를 입력한 후에 실행해보면
recipe 라인으로 인식하는 것을 볼 수 있습니다.</p>
<pre><code class="lang-makefile">sh$ cat Makefile
hello recipe <span class="hljs-comment"># 앞의 공백은 tab 문자</span>
<span class="hljs-comment">##### 실행 결과 #####</span>
sh$ make <span class="hljs-comment"># 타겟라인 없이 recipe 가 시작되어 오류 발생</span>
Makefile:2: *** recipe commences before first target. Stop.
</code></pre>
<p>recipe 도 라인 단위입니다.
이 말은 라인 단위로 <code>sh -c</code> 에 의해 명령이 실행된다는 의미입니다.
따라서 recipe 를 shell script 파일 작성하듯이 작성하면 안됩니다.
왜냐하면 다음 라인은 다른 PID 갖는 <code>sh -c</code> 이 되므로 앞서 설정한 shell 변수값을 사용할 수 없기 때문입니다.
다음을 보면 COUNT 변수값이 첫 라인에서는 정상적으로 표시되지만 나머지 라인에서는
표시되지 않는 것을 볼 수 있습니다.</p>
<pre><code class="lang-makefile">foo :
@COUNT=100; echo shell PID: $$$$, COUNT: $$COUNT
@echo shell PID: $$$$, COUNT: $$COUNT
@echo shell PID: $$$$, COUNT: $$COUNT
<span class="hljs-comment">######### 실행 결과 #########</span>
sh$ make
shell PID: 10790, COUNT: 100
shell PID: 10791, COUNT: <span class="hljs-comment"># 각 라인 별로 PID 값이 다르다</span>
shell PID: 10792, COUNT:
</code></pre>
<p>이렇게 recipe 에서 라인 단위를 사용하는 이유는 해당 라인의 <code>sh -c</code> 가 오류로 종료할 경우
make 실행을 종료하기 위해서입니다.
타겟 파일을 만드는 작업 중에 하나의 라인에서라도 오류가 발생하면 안되기 때문입니다.
만약에 하나의 <code>sh -c</code> 에서 모두 shell 명령을 실행하고 싶으면
<code>&&</code> 나 <code>;</code> 를 이용해서 연이어서 작성하고 라인이 길어질 경우는
newline 을 escape 해서 하나의 라인으로 만들어야 합니다.</p>
<pre><code class="lang-makefile"><span class="hljs-comment"># newline 을 escape 하여 하나의 라인이 되므로 tab 문자는 처음에만 입력해도 된다.</span>
foo :
@COUNT=100; echo shell PID: $$$$, COUNT: $$COUNT; \
echo shell PID: $$$$, COUNT: $$COUNT; \
echo shell PID: $$$$, COUNT: $$COUNT
<span class="hljs-comment">######### 실행 결과 #########</span>
shell PID: 10909, COUNT: 100
shell PID: 10909, COUNT: 100
shell PID: 10909, COUNT: 100
----------------------------------------------------------
<span class="hljs-constant">LIST</span> = one two three
<span class="hljs-title">all:</span>
@for i in $(LIST); do \ <span class="hljs-comment"># 모두 하나의 라인이 되므로 명령을 작성할 때는 </span>
echo $$i; \ <span class="hljs-comment"># ';' 문자를 사용해야 한다.</span>
done
<span class="hljs-comment">######### 실행 결과 #########</span>
one
two
three
</code></pre>
<h4 id="rule-context-와-comment">Rule context 와 comment</h4>
<p>recipe 작성 중에는 make comment 라인이 와도 되고 공백 라인이 와도 됩니다.
하지만 comment 라인이 tab 으로 시작하면 그것은 make comment 가 아니라 shell comment 가 됩니다.
recipe 작성 중간에 tab 으로 시작하지 않는 make 구문 ( 대입 연산, 타겟 라인, script )
이 오면 그것은 해당 rule context 의 종료가 됩니다.</p>
<blockquote>
<p>newline 을 escape 하여 하나의 라인으로 작성할 경우는 comment 도 공백 라인도 안됩니다.</p>
</blockquote>
<pre><code class="lang-makefile">foo :
@echo aaaaa
<span class="hljs-comment"># make comment ..... # 중간에 make comment 라인이 와도 된다.</span>
<span class="hljs-comment"># shell comment 111 .... # tab 으로 시작할 경우는 shell comment 라인이 된다.</span>
@<span class="hljs-comment"># shell comment 222 ....</span>
@echo bbbbb
<span class="hljs-comment"># 공백 라인이 와도 된다.</span>
@echo ccccc
<span class="hljs-comment">###### 실행 결과 ######</span>
aaaaa
<span class="hljs-comment"># shell comment 111 ....</span>
bbbbb
ccccc
</code></pre>
<p>recipe 에서 사용되는 함수는 마찬가지로 tab 으로 시작해야 합니다.
그렇지 않으면 해당 rule context 가 종료되는 결과가 발생하고
함수는 global 영역에 포함됩니다.</p>
<pre><code class="lang-makefile"><span class="hljs-constant">AA</span> := 100
foo :
@echo target $@ : $(AA)
$(if $(filter 100,$(AA)),$(eval AA := 200)) <span class="hljs-comment"># tab 으로 시작해야 한다.</span>
<span class="hljs-comment">######## 실행 결과 ########</span>
target foo : 200
</code></pre>
<p>rule 의 recipe 작성을 마치고 다른 make 구문을 작성할 때는
앞에 tab 문자가 오지 않도록 주의해야 됩니다.
그렇지 않으면 앞선 rule context 에 연이어 shell 명령문으로 인식됩니다.</p>
<pre><code class="lang-makefile">foo :
@echo end.....
ifneq "$(MAKECMDGOALS)" "clean"
include $(sources:.c=.d) <span class="hljs-comment"># include 앞에 tab 문자가 포함됨</span>
endif
<span class="hljs-comment">######### 실행 결과 #########</span>
end.....
include
make: include: Command not found <span class="hljs-comment"># include 가 shell 명령으로 인식된다.</span>
make: *** [Makefile:3: foo] Error 127
</code></pre>
<h4 id="변수와-recipe-실행-순서"><code>$$</code> 변수와 recipe 실행 순서</h4>
<p>recipe 에서는 shell 명령문을 작성하지만 명령문 어떤 위치에서든지
make 변수와 함수도 사용할 수 있습니다.
그런데 여기서 문제는 변수를 나타내는 문자로 <code>$</code> 를 사용하므로
shell 에 <code>$</code> 문자가 전달되기 위해서는 <code>$$</code> 변수를 사용해야 합니다.
그러니까 <code>$(var)</code> 는 make 변수로 처리되지만 <code>$${var}</code> 는 <code>$$</code> 변수가 <code>$</code> 로 변경된 후에
shell 에 전달되면 <code>${var}</code> shell 변수가 됩니다.</p>
<p>recipe 가 실행될 때는 처음부터 shell 명령이 실행되지 않습니다.
먼저 recipe 전체에서 make 레이어에 해당하는 변수, 함수, <code>$$</code> 처리가 완료된 후에
결과에 따라 shell 명령이 실행됩니다.</p>
<pre><code class="lang-makefile">foo :
@echo shell 111
$(info make 222)
@echo shell 333$(info make 444)
@echo shell 555
$(info make 666)
<span class="hljs-comment">###### 실행 결과 ######</span>
make 222
make 444
make 666
shell 111 <span class="hljs-comment"># make 레이어 처리가 완료된 후에 shell 명령이 실행된다.</span>
shell 333
shell 555
</code></pre>
<p>따라서 다음과 같은 사용방법은 문제가 될수 있습니다.
shell script 가 실행되면서 call 함수가 실행되는 것이 아닙니다.
shell 명령이 실행되기 전에 먼저 make 레이어에 해당하는 <code>$(val)</code> 변수가 확장되고
<code>$(call ...)</code> 함수가 실행이 완료된 후에 그 결과를 가지고 shell script 가 실행되는 것입니다.
따라서 첫 번째 예제는 실행에 문제가 있지만 오른쪽 예제 같은 경우는
call 함수의 반환값으로 shell 에서 실행 가능한 명령문이 반환된다면 가능합니다.</p>
<pre><code class="lang-makefile">target : target :
@for v in $(val); do \ @if test "$(val)" = ""; then \
$(call foo,$$v); \ $(call foo ...); \
done fi
</code></pre>
<h4 id="명령-라인의-종료-상태-값">명령 라인의 종료 상태 값</h4>
<p>명령 라인에서 여러 개의 명령을 작성할 때 주의할 점은 make 실행이 중단될 때는
<code>sh -c</code> 의 종료 상태 값에 따라 중단된다는 것입니다.
이것은 해당 라인에서 마지막으로 실행되는 명령의 종료 상태 값을 말합니다.
다음 첫 번째 예제를 보면 cd 명령이 오류로 종료하였지만 뒤 이어지는 date 명령이 정상 종료하여
결과적으로 <code>sh -c</code> 의 종료 상태 값은 정상이 됩니다.
따라서 make 실행이 중단되지 않고 다음 date 명령도 실행되는 것을 볼 수 있습니다.
반면에 두 번째 예제의 경우는 명령 라인에서 <code>&&</code> 연산자를 사용하여
전체 명령의 종료 상태 값이 오류가 되므로 make 실행이 종료됩니다. </p>
<pre><code class="lang-makefile">foo : (';' 를 사용) foo : ('&&' 를 사용)
@cd not-exist; date @cd not-exist && date
@date @date
<span class="hljs-comment">###### 실행 결과 ###### ###### 실행 결과 ######</span>
/bin/sh: 1: cd: can't cd to not-exist /bin/sh: 1: cd: can't cd to not-exist
Wed 29 May 2019 02:52:54 PM KST make: *** [Makefile:3: foo] Error 2
Wed 29 May 2019 02:52:54 PM KST
</code></pre>
<p>또는 다음과 같이 <code>-e</code> ( errexit ) shell 옵션을 설정해 사용할 수도 있습니다.
그러면 <code>sh -e -c</code> 형태로 shell 이 실행되므로 실행 중에 명령이 오류로 종료할 경우 바로
<code>sh</code> 도 오류로 종료됩니다.
errexit 옵션은 사용시 몇가지 주의할 사항이 있는데 자세한 내용은 <a href="https://mug896.github.io/bash-shell/debugging.html#errexit" target="_blank">여기</a> 를 참고하세요.</p>
<pre><code class="lang-makefile">foo : .SHELLFLAGS += -e <span class="hljs-comment"># 또는 직접 해당 라인에 설정 </span>
foo : foo :
@cd not-exist; date @set -e; cd not-exist; date
@date @date
<span class="hljs-comment">###### 실행 결과 ######</span>
/bin/sh: 0: Can't open cd not-exist; date
make: *** [Makefile:4: foo] Error 127
</code></pre>
<p>pipe 로 명령을 연결해 사용할 경우도 마지막 명령의 종료 상태 값이 사용되므로
중간에 어떤 명령에서 오류가 발생하더라도 마지막 명령이 정상 종료하면
전체 명령의 종료 상태 값은 <code>0</code> 이됩니다.
따라서 이와 같은 경우도 make 실행이 종료돼야 한다면 bash 를 이용해 <code>pipefail</code> 옵션을 사용할 수 있습니다.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># error_command 에서 오류가 발생했지만 sed 명령은 항상 참이 되므로 종료 상태 값은 0 이된다.</span>
$ error_<span class="hljs-built_in">command</span> ... | sed <span class="hljs-string">'s/foo/bar/'</span> > file
$ <span class="hljs-built_in">echo</span> $?
<span class="hljs-number">0</span>
-----------------------------------------------
<span class="hljs-comment"># date -@ 명령에서 오류가 발생했지만 # bash 를 이용해 pipefail 옵션을 사용하면 된다.</span>
<span class="hljs-comment"># 종료되지 않고 끝까지 실행된다. # (exec 에의해 sh 프로세스가 bash 로 변경된다. )</span>
foo : foo :
@<span class="hljs-built_in">echo</span> <span class="hljs-variable">$@</span> start ... @<span class="hljs-built_in">echo</span> <span class="hljs-variable">$@</span> start ...
@date -@ | date @<span class="hljs-built_in">exec</span> /bin/bash -c <span class="hljs-string">'set -o pipefail && date -@ | date'</span>
@<span class="hljs-built_in">echo</span> <span class="hljs-variable">$@</span> end ... @<span class="hljs-built_in">echo</span> <span class="hljs-variable">$@</span> end ...
<span class="hljs-comment">######## 실행 결과 ######## ######## 실행 결과 ########</span>
foo start ... foo start ...
date: invalid option -- <span class="hljs-string">'@'</span> date: invalid option -- <span class="hljs-string">'@'</span>
Try <span class="hljs-string">'date --help'</span> <span class="hljs-keyword">for</span> more information. Try <span class="hljs-string">'date --help'</span> <span class="hljs-keyword">for</span> more information.
Mon Apr <span class="hljs-number">20</span> <span class="hljs-number">09</span>:<span class="hljs-number">48</span>:<span class="hljs-number">16</span> KST <span class="hljs-number">2020</span> Mon Apr <span class="hljs-number">20</span> <span class="hljs-number">10</span>:<span class="hljs-number">03</span>:<span class="hljs-number">05</span> KST <span class="hljs-number">2020</span>
foo end ... make: *** [Makefile:<span class="hljs-number">4</span>: foo] Error <span class="hljs-number">1</span>
$ <span class="hljs-built_in">echo</span> $? $ <span class="hljs-built_in">echo</span> $?
<span class="hljs-number">0</span> <span class="hljs-number">2</span>
</code></pre>
<h4 id="bash-c--또는-sh-c--사용시-multiline-명령-작성법">bash -c ... 또는 sh -c ... 사용시 multi-line 명령 작성법</h4>
<p>보통 <code>bash -c ...</code> 형태로 명령을 실행할 때는 quote 을 하고 single-line 으로 작성하는데요.
다음과 같이 multi-line 변수를 활용하면 shell 스크립트 파일을 작성할 때와 동일하게
작성해 실행시킬 수 있습니다.</p>
<pre><code class="lang-bash">define cmd
AA=<span class="hljs-number">100</span>
<span class="hljs-built_in">echo</span> <span class="hljs-operator">-e</span> <span class="hljs-string">"double quotes\n<span class="hljs-variable">$AA</span>"</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">'single quotes\n$AA'</span>
cat <<\EOF
<span class="hljs-string">'here'</span>
<span class="hljs-string">"document"</span>
EOF
endef
<span class="hljs-built_in">export</span> cmd := $(value cmd)<span class="hljs-comment"># child process 에서 사용할수 있게 export</span>
foo :
@<span class="hljs-built_in">echo</span> <span class="hljs-variable">$@</span> start ...
@<span class="hljs-built_in">exec</span> /bin/bash -c <span class="hljs-string">"$<span class="hljs-variable">$cmd</span>"</span>
@<span class="hljs-built_in">echo</span> <span class="hljs-variable">$@</span> end ...
<span class="hljs-comment">############ 실행 결과 ############</span>
$ make
foo start ...
double quotes
<span class="hljs-number">100</span>
single quotes\n<span class="hljs-variable">$AA</span>
<span class="hljs-string">'here'</span>
<span class="hljs-string">"document"</span>
foo end ...
</code></pre>
<h4 id="echo-명령-사용시-주의할-점">echo 명령 사용시 주의할 점</h4>
<p>shell 에서 실행할 명령이 하나만 존재하면 굳이 <code>sh -c</code> 를 실행하고 거기서 또 해당 명령을
실행할 필요가 없겠죠.
그래서 이때는 <code>sh -c</code> 실행 없이 바로 명령을 실행합니다.
그럼 echo 명령은 shell builtin 명령인데 어떻게 실행할까요?
이때는 <code>/bin/echo</code> 명령을 사용합니다.
그런데 <code>/bin/echo</code> 명령은 <code>sh</code> 에서와 달리 기본적으로 quotes 에서 escape 문자를 처리 안합니다
( <code>/bin/echo --help</code> 를 해보면 bash 와 같이 escape 문자 처리를 위한 옵션이 따로 있습니다 ).
따라서 echo 명령 사용시 주의할 필요가 있습니다.</p>
<p>다음은 명령 라인에서 실행할 명령이 echo 명령 하나만 있을 경우 quotes 사용 방식에 따라 어떻게 실행되는지를 비교해놓은 것입니다.
실행할 명령이 두 개 이상이면 물론 <code>sh -c</code> 에 의해 실행됩니다.</p>
<pre><code class="lang-makefile"> <span class="hljs-comment"># double quotes 이 존재하면 sh -c 에서 실행된다</span>
foo : foo :
@echo '111\n222' (/bin/echo) @echo "111\n222" (sh -c)
@ :; echo '333\n444' (sh -c) @echo '333\n444'"" (sh -c)
<span class="hljs-comment">##### 실행 결과 ##### ##### 실행 결과 #####</span>
111\n222 111
333 222
444 333
444
</code></pre>
<h4 id="recipe-실행-중에는-대입-연산은-정의할-수-있지만-rule-은-정의할-수-없다">recipe 실행 중에는 대입 연산은 정의할 수 있지만 rule 은 정의할 수 없다.</h4>
<p>rule 은 make 에 의해서 읽기가 완료된 후에 결과에 따라서 순서대로 recipe 가 실행되는 것이므로
recipe 실행 중에는 동적으로 다시 rule 을 정의해 사용할 수 없습니다.
하지만 대입 연산의 경우는 recipe 실행 중에 동적으로 정의해 사용하는 것이 가능합니다.</p>
<pre><code class="lang-makefile">foo : bar
@echo target $@ : $(AA)
$(if $(filter 100,$(AA)),$(eval AA := 200)) <span class="hljs-comment"># recipe 실행중에 동적으로 AA 값을 변경</span>
bar :
@echo target $@ : $(AA)
<span class="hljs-constant">AA</span> := 100
<span class="hljs-comment">######### 실행 결과 ##########</span>
target bar : 100
target foo : 200
</code></pre>
<p>recipe 실행 중에는 rule 을 정의할 수 없습니다.</p>
<pre><code class="lang-makefile">define onerule
$1 :
@echo target $$@
endef
<span class="hljs-comment"># $(eval $(call onerule,foo)) # OK : 여기서는 가능</span>
bar :
$(eval $(call onerule,foo)) <span class="hljs-comment"># Error</span>
<span class="hljs-comment">########## 실행 결과 ###########</span>
Makefile:7: *** prerequisites cannot be defined in recipes. Stop.
</code></pre>
<p><a name="prefix"></a></p>
<h3 id="prefix-문자">Prefix 문자</h3>
<table>
<thead>