-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinclude.html
More file actions
1154 lines (724 loc) · 64.3 KB
/
include.html
File metadata and controls
1154 lines (724 loc) · 64.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>Include | 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="./one_DAG.html" />
<link rel="prev" href="./recursive_make.html" />
</head>
<body>
<div class="book"
data-level="8"
data-chapter-title="Include"
data-filepath="include.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 " 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 active" 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="include">include</h1>
<p>makefile 에는 보이지는 않지만 recipe 가 실행되기 전에 먼저 실행되는 rule 이 하나 있습니다.
바로 target 으로 자기 자신의 파일 이름을 갖는 rule 입니다.
이 rule 은 주로 makefile 자신을 update 하거나 생성하는 용도로 사용됩니다.
그러면 최종 update 된 버전으로 make 이 실행되게 됩니다.
이것은 make 명령을 실행했을 때 default 로 읽혀지는 top makefile 뿐만 아니라
include 문에서 사용되는 파일들도 모두 해당됩니다.</p>
<p>이 rule 의 타겟 라인은 다음과 같은 형식입니다.
그러니까 외부 파일( prerequisites ) 로부터 자기 자신( Makefile, 또는 include 파일 ) 을
recipe 를 이용해 update 하거나 새로 생성합니다.
include 문에서 사용되는 makefile 의 경우는 파일이 존재하지 않을 경우 생성하기 위한
target only 룰도 사용할 수 있습니다.</p>
<pre><code class="lang-makefile"><span class="hljs-comment"># top makefile # include 문에 사용되는 makefile</span>
Makefile : prerequisites include.mk : prerequisites
include.mk : <span class="hljs-comment"># target only 룰</span>
</code></pre>
<p>아무것도 없는 디렉토리에 touch 명령으로 Makefile 을 하나 생성한 다음 <code>make -d</code>
명령을 실행해보면 다음과 같이 Makefile 을 읽어들인 후에 자기 자신을 update 하기 위해
매칭되는 rule 들을 검색하는 것을 볼 수 있습니다.
여기서 출력되는 다양한 확장자를 가진 prerequisites 들은
make 에서 builtin 으로 제공하는 rule 들입니다.</p>
<pre><code>Reading makefiles...
Reading makefile 'Makefile'...
Updating makefiles....
Considering target file 'Makefile'.
Looking for an implicit rule for 'Makefile'.
. . . . .
. . . . .
. . . . .
Trying pattern rule with stem 'Makefile.sh'.
Trying implicit prerequisite 's.Makefile.sh'.
Trying pattern rule with stem 'Makefile.sh'.
Trying implicit prerequisite 'SCCS/s.Makefile.sh'.
No implicit rule found for 'Makefile'.
Finished prerequisites of target file 'Makefile'.
No need to remake target 'Makefile'.
</code></pre><p>위 출력에서 마지막 부분에 보이는 Makefile.sh 파일을 prerequisite 으로 사용해서
실제 자기 자신을 update 하는 룰이 실행되는지 테스트해보겠습니다.</p>
<blockquote>
<p>자기 자신을 update 하는 것을 보통 remake 한다고 합니다.</p>
</blockquote>
<pre><code class="lang-bash">sh$ cat Makefile <span class="hljs-comment"># 현재 Makefile 은 공백인 상태</span>
-----------------------------
sh$ cat Makefile.sh <span class="hljs-comment"># 다음과 같은 rule 을 포함하는 Makefile.sh 파일 작성</span>
foo : ;@<span class="hljs-built_in">echo</span> hello remake
-----------------------------
sh$ ls <span class="hljs-comment"># 현재 디렉토리에 두개의 파일이 존재</span>
Makefile Makefile.sh
-----------------------------
sh$ make <span class="hljs-comment"># make 명령 실행</span>
cat Makefile.sh >Makefile <span class="hljs-comment"># make 에 default 로 내장되어 있는 '.sh' 확장자 룰에의해</span>
chmod a+x Makefile <span class="hljs-comment"># Makefile.sh 의 내용이 Makefile 로 copy 되고</span>
hello remake <span class="hljs-comment"># remake 이 완료된 Makefile 이 실행되어 foo 룰이 실행된다.</span>
-----------------------------
sh$ make <span class="hljs-comment"># 이제 prerequisite 에 해당하는 Makefile.sh 보다</span>
hello remake <span class="hljs-comment"># 타겟인 Makefile 이 최신이므로 다시 remake 되지 않는다.</span>
-----------------------------
sh$ cat Makefile <span class="hljs-comment"># 공백이었던 Makefile 파일이 Makefile.sh 로부터 remake 되었다.</span>
foo : ;@<span class="hljs-built_in">echo</span> hello remake
</code></pre>
<p>현재 상태에서 다시 Makefile.sh 파일을 수정하게 되면 Makefile 보다 최신이 되므로 다음에
make 명령 실행시 다시 remake 하게 됩니다.</p>
<h4 id="사용자가-직접-rule-을-정의해-사용">사용자가 직접 rule 을 정의해 사용</h4>
<p>makefile 자신을 생성, 갱신하는 builtin rule 은 먼저 makefile 이 읽혀지고
global 영역에서 처리가 완료된 후에 recipe 가 실행되기 전에
실행되는 것이므로 makefile 과 동일한 이름의 타겟을 갖는 rule 을 정의하면
builtin rule 대신에 사용자가 정의한 rule 이 사용될 수 있습니다.
이때는 prerequisites 으로 사용자가 원하는 이름을 사용할 수 있습니다.</p>
<pre><code class="lang-bash">sh$ cat Makefile
foo ::
@<span class="hljs-built_in">echo</span> hello remake <span class="hljs-number">111</span>
Makefile : update.mk <span class="hljs-comment"># Makefile 과 동일한 이름의 타겟을 갖는 룰을 정의</span>
cat $< >> <span class="hljs-variable">$@</span> <span class="hljs-comment"># update.mk 파일의 내용을 Makefile 에 append </span>
---------------------------
sh$ cat update.mk <span class="hljs-comment"># prerequisite 에 해당하는 파일</span>
foo ::
@<span class="hljs-built_in">echo</span> hello remake <span class="hljs-number">222</span>
---------------------------
sh$ ls
Makefile update.mk
---------------------------
sh$ make
cat update.mk >> Makefile <span class="hljs-comment"># 먼저 remake 을 위한 룰이 실행되고, 완료되면</span>
hello remake <span class="hljs-number">111</span> <span class="hljs-comment"># 최종 update 된 버전으로 make 이 다시 실행된다.</span>
hello remake <span class="hljs-number">222</span> <span class="hljs-comment"># update.mk 에서 추가된 룰의 메시지</span>
---------------------------
sh$ cat Makefile
foo ::
@<span class="hljs-built_in">echo</span> hello remake <span class="hljs-number">111</span>
Makefile : update.mk
cat $< >> <span class="hljs-variable">$@</span>
foo :: <span class="hljs-comment"># 기존 Makefile 에 update.mk 파일 내용이 추가됨</span>
@<span class="hljs-built_in">echo</span> hello remake <span class="hljs-number">222</span>
</code></pre>
<h4 id="include-에-사용되는-파일-이름은-rule-의-타겟과-같다">include 에 사용되는 파일 이름은 rule 의 타겟과 같다.</h4>
<p>include 문에서 사용되는 makefile 들도 동일하게 동작합니다.
따라서 include 문에서 사용되는 파일 이름은 동일한 이름을 타겟으로 가진
rule 을 실행하는데 사용됩니다.
실제 remake 은 다음과같은 순서로 진행됩니다.</p>
<ol>
<li><p><strong>global 영역 1차 파싱</strong><br> 모든 rule 정보를 읽어 들이고 include 파일이 존재할 경우 해당 위치에
include 해서 함께 파싱합니다.
이때 파일이 존재하지 않는 것은 오류가 되지 않고 pending 상태가 됩니다.</p>
</li>
<li><p><strong>remake</strong><br> makefile 과 동일한 이름의 rule 이 존재하고 타겟 생성, 갱신이 필요하면 실행합니다.
만약에 include 파일이 존재하지 않는데 remake 단계에서
rule 도 존재하지 않으면 오류가 되고 make 실행이 종료됩니다.</p>
</li>
<li><p><strong>global 영역 2차 파싱</strong><br> update 된 makefile 을 가지고 다시 최종 파싱을 합니다.</p>
<blockquote>
<p>remake 단계에서 rule 이 실행되기만 하면 실제 타겟 파일 생성에 상관없이
( include 성공에 상관없이 ) 정상적으로 진행됩니다.
이것은 make 에서 기본적으로 rule 이 처리되는 방식과 동일한 것입니다.</p>
</blockquote>
</li>
<li><p><strong>recipe 실행 시작</strong></p>
</li>
</ol>
<pre><code class="lang-bash">sh$ cat Makefile
include aaa.mk
aaa.mk : <span class="hljs-comment"># include 파일과 동일한 이름의 rule 설정</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">'foo : ;@echo hello remake'</span> > <span class="hljs-variable">$@</span>
------------------------------------------
sh$ ls <span class="hljs-comment"># 현재 aaa.mk 파일은 존재하지 않는상태</span>
Makefile
------------------------------------------
sh$ make <span class="hljs-comment"># aaa.mk 파일과 동일한 이름의 rule 이 존재하므로 remake 단계에서 실행된다.</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">'foo : ;@echo hello remake'</span> > aaa.mk <span class="hljs-comment"># 여기서 aaa.mk 파일이 생성됨</span>
<span class="hljs-comment"># 2차 파싱 단계에서 생성된 aaa.mk 파일이 include 되고 </span>
hello remake <span class="hljs-comment"># recipe 실행 단계에서 foo 룰이 실행된다.</span>
------------------------------------------
sh$ ls
aaa.mk Makefile <span class="hljs-comment"># aaa.mk 파일이 새로 생성됨</span>
------------------------------------------
sh$ make <span class="hljs-comment"># aaa.mk 파일이 이미 존재하므로 aaa.mk 룰은 다시 실행되지 않는다.</span>
hello remake
</code></pre>
<h4 id="makefile-을-동적으로-수정해-include">makefile 을 동적으로 수정해 include</h4>
<p>위에서 알아본 remake 기능을 활용하면 makefile 을 동적으로 수정해 include 할 수 있습니다.
다음은 실제 사용자가 작성하는 makefile 과 include 되는 makefile 이 다른 것입니다.
그러니까 사용자가 <code>aaa.mk</code> 파일을 작성하면
remake 룰에서 정의된 recipe 에의해 파일이 동적으로 변경되어 <code>.aaa.mk</code> 파일로 저장되고
<code>aaa.mk</code> 파일 대신에 <code>.aaa.mk</code> 파일이 include 되어 사용됩니다.</p>
<pre><code class="lang-bash">sh$ cat Makefile
include .aaa.mk <span class="hljs-comment"># 실제 include 되는 파일은 dot 파일</span>
.aaa.mk : aaa.mk <span class="hljs-comment"># prerequisite 은 aaa.mk 파일이 된다.</span>
sed <span class="hljs-string">'s/11111/22222/'</span> $< > <span class="hljs-variable">$@</span> <span class="hljs-comment"># recipe 에서 동적으로 파일을 변경</span>
------------------------------------
sh$ cat aaa.mk
foo :
@<span class="hljs-built_in">echo</span> hello <span class="hljs-number">11111</span> <span class="hljs-comment"># hello 11111</span>
------------------------------------
sh$ ls <span class="hljs-operator">-a</span>
./ ../ aaa.mk Makefile <span class="hljs-comment"># 현재 두개의 파일만 존재</span>
------------------------------------
sh$ make <span class="hljs-comment"># remake 룰에 의해 dot 파일이 생성되고 include 된다.</span>
sed <span class="hljs-string">'s/11111/22222/'</span> aaa.mk > .aaa.mk
hello <span class="hljs-number">22222</span> <span class="hljs-comment"># 11111 -> 22222 로 변경되어 출력된다.</span>
------------------------------------
sh$ make <span class="hljs-comment"># 현재 dot 파일이 최신이므로 다시 룰이 실행되지 않는다.</span>
hello <span class="hljs-number">22222</span>
------------------------------------
sh$ ls <span class="hljs-operator">-a</span>
./ ../ .aaa.mk aaa.mk Makefile <span class="hljs-comment"># .aaa.mk 파일이 새로 생성됨</span>
------------------------------------
sh$ cat .aaa.mk <span class="hljs-comment"># 실제 include 되는 dot 파일</span>
foo :
@<span class="hljs-built_in">echo</span> hello <span class="hljs-number">22222</span> <span class="hljs-comment"># 11111 -> 22222 로 변경됨</span>
</code></pre>
<p>이 방법을 사용할 때 한가지 주의할 점은 만약에 <code>aaa.mk</code> 파일을 수정하였는데
구문 오류가 발생하여 종료된다면 실제 include 되는 <code>.aaa.mk</code> 파일에서도 오류가 나겠죠.
이때 <code>aaa.mk</code> 파일의 오류를 수정하고 make 을 실행하면 1차 파싱 단계에서는 기존의 <code>.aaa.mk</code>
파일을 읽어들이게 되므로 오류가 지속됩니다.
따라서 먼저 <code>.aaa.mk</code> 파일을 삭제한 후에 <code>aaa.mk</code> 파일을 수정해야 합니다.</p>
<h4 id="-minus--prefix">- ( minus ) Prefix</h4>
<p>만약에 include 파일도 존재하지 않고 파일 생성을 위한 rule 도 존재하지 않는다면
그것은 오류가 됩니다.
다음 첫 번째 예제를 보면 1차 파싱을 거친 후에 ( end... 메시지까지 출력 )
remake 단계에서 매칭되는 rule 이 없다는 메시지와 함께 종료되는 것을 볼 수 있습니다.
이때 오류로 종료되는 것을 무시하고 싶으면 <a href="recipes.html#prefix">recipe prefix</a> 에서와 같이
include 지시자 앞에 <code>-</code> 문자를 추가해 주면 됩니다.</p>
<blockquote>
<p>이것은 include 된 파일 자체에서 발생하는 오류는 무시되지 않습니다.</p>
</blockquote>
<pre><code class="lang-makefile">$(info start...) $(info start...)
include fooooo.mk -include fooooo.mk <span class="hljs-comment"># '-' prefix 사용</span>
bar : bar :
@echo barrrr @echo barrrr
$(info end...) $(info end...)
<span class="hljs-comment">#### 실행 결과 #### #### 실행 결과 ####</span>
start... start...
end... end...
Makefile:2: fooooo.mk: No such file or directory barrrr <span class="hljs-comment"># 오류가 무시되고</span>
make: *** No rule to make target 'fooooo.mk'. Stop. <span class="hljs-comment"># 정상 실행된다.</span>
</code></pre>
<h3 id="automatic-dependency-generation">Automatic dependency generation</h3>
<p>프로그래머가 작성한 각각의 C/C++ 소스파일들은 오브젝트 파일을 타겟으로
의존 관계를 명시한 타겟 라인을 작성해야 합니다.
그래야지 나중에 헤더 파일이나 소스파일이 수정되었을때 해당 타겟 파일이 갱신될 수 있습니다.
하지만 이것을 사람이 일일이 작성하기는 어려운데요.
왜냐하면 아래와 같은 소스파일이 있을 경우 실제 의존관계에 명시해야될 헤더 파일은 3 개
이상이 될수도 있고 ( 헤더 파일에서 다른 헤더 파일을 include 할수 있으므로 )<br>전처리기에 의해 추가되거나 제외될 수도 있기 때문입니다.
사실 이와 같은 정보를 제일 잘 알수있는 프로그램은 컴파일러입니다.
따라서 컴파일러는 makefile 에서 사용할 수 있는 의존파일 <code>.d</code> 생성을 위한 전처리기 옵션들을 제공합니다.</p>
<pre><code class="lang-sh">$ cat pty/pty_fork.c
<span class="hljs-comment">#include <fcntl.h></span>
<span class="hljs-comment">#include <termios.h></span>
<span class="hljs-comment">#include <sys/ioctl.h></span>
<span class="hljs-comment">#include "pty_master_open.h"</span>
<span class="hljs-comment">#include "pty_fork.h"</span>
<span class="hljs-comment">#include "lib/tlpi_hdr.h"</span>
. . . .
. . . .
<span class="hljs-comment"># pty/pty_fork.o 타겟 파일의 의존 관계를 명시한 타겟 라인</span>
pty/pty_fork.o: pty/pty_fork.c pty/pty_master_open.h pty/pty_fork.h \
lib/tlpi_hdr.h lib/get_num.h lib/error_functions.h
</code></pre>
<p>예전에는 의존파일 생성을 위해 makedepend 명령을 사용하고,
빌드 전에 make depend 명령을 실행하고 했었는데 지금은 대부분이 컴파일러에서
제공하는 옵션을 이용해 처리합니다.
아래는 makefile 작성시 종종 사용되는 형식들인데
그런데 이 예제들은 정상 동작하기는 하지만 좀 생각해봐야될 점들이 있습니다.</p>
<p>먼저 첫 번째 예제의 경우 include 문에서 파일이 존재하지 않을 경우
매칭되는 룰이 존재하므로 ( <code>%.d : %.c</code> ) remake 단계에서 생성되어 최종 include 된후
makefile 이 실행됩니다.
그런데 프로젝트를 처음 빌드 하는 경우에도 의존파일이 필요할까요?
파일의 의존관계를 검사해서 필요한 타겟을 갱신하는 것은 두 번째 빌드때부터 필요한 것이죠.
그리고 오른쪽 예제 같은 경우는 소스파일이 수정될 때마다 빌드전에
직접 <code>make depend</code> 명령을 먼저 실행해주어야 합니다.</p>
<pre><code class="lang-makefile"><span class="hljs-constant">OBJS</span> := .... SRCS := ....
<span class="hljs-constant">DEPS</span> := <span class="hljs-variable">$(OBJS:.o=.d)</span> OBJS := ....
all : foo all : foo
foo : $(OBJS) foo : $(OBJS)
$(CC) $+ -o $@ $(CC) $+ -o $@
$(OBJS) : $(BUILD_DIR)/%.o : %.c $(OBJS) : $(BUILD_DIR)/%.o : %.c
$(CC) -c $(CFLAGS) -o $@ $< $(CC) -c $(CFLAGS) -o $@ $<
$(BUILD_DIR)/%.d : %.c depend :
@$(CC) -MM -o $@ $< @$(CC) -MM $(SRCS) > $(DEPEND_FILE)
ifneq "$(MAKECMDGOALS)" "clean" ifeq "$(filter clean depend,$(MAKECMDGOALS))" ""
-include $(DEPS) -include $(DEPEND_FILE)
endif endif
</code></pre>
<p>이와 같은 문제점들은 CFLAGS 에 전처리기 옵션인 <code>-MMD -MP</code> 옵션만 추가하면
쉽게 해결이 됩니다.
다음 예제를 보면 처음 프로젝트 빌드 시에는 의존파일이 없지만
include 지시자 앞에 추가된 <code>-</code> prefix 에의해 종료되지 않고 빌드가 진행됩니다.
소스파일이 컴파일 될때마다 <code>-MMD</code> 옵션에 의해 오브젝트 파일과 동일한 위치에
의존파일인 <code>.d</code> 파일이 자동으로 생성됩니다.
다음에 두 번째 빌드부터는 의존파일이 존재하므로 include 되어 사용되겠죠.
소스파일이 수정됐을 경우에도 다시 컴파일이 될때 자동으로 <code>.d</code> 파일이 갱신됩니다.</p>
<pre><code class="lang-makefile"><span class="hljs-constant">OBJS</span> := ....
<span class="hljs-constant">DEPS</span> := <span class="hljs-variable">$(OBJS:.o=.d)</span>
all : foo
foo : $(OBJS)
$(CC) $+ -o $@
<span class="hljs-constant">CFLAGS</span> += -MMD -MP
$(OBJS) : $(BUILD_DIR)/%.o : %.c
$(CC) -c $(CFLAGS) -o $@ $<
ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEPS)
endif
</code></pre>
<h4 id="make-clean-시에는-include-문을-제외">make clean 시에는 include 문을 제외</h4>
<p>의존파일을 읽어들여서 의존관계를 검사하는 것은 프로젝트 빌드시에 필요한 것이지
make clean 시에는 불필요한 것입니다.
따라서 다음과 같이 조건 지시자를 이용해 include 문이 처리되는 것을 제외하는 것이 좋습니다.</p>
<pre><code class="lang-makefile">ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEPS)
endif
ifeq "$(filter clean depend,$(MAKECMDGOALS))" ""
-include $(DEPEND_FILE)
endif
</code></pre>
<hr>
<h3 id="의존파일-자동-생성을-위한-gcc-전처리기-옵션">의존파일 자동 생성을 위한 gcc 전처리기 옵션</h3>
<h4 id="m">-M</h4>
<p>의존 타겟 라인을 생성할 때 시스템 헤더까지 모두 포함해서 출력합니다.
이 옵션은 기본적으로 <code>-E</code> ( 전처리 완료후 종료 ) 옵션과 <code>-w</code> ( suppress warning )
옵션을 포함하므로 출력 후 컴파일 작업까지 이어지지 않습니다.</p>
<pre><code class="lang-sh">sh$ cat pty/unbuffer.c
<span class="hljs-comment">#include <termios.h></span>
<span class="hljs-comment">#include <sys/select.h></span>
<span class="hljs-comment">#include "pty_fork.h"</span>