Tips

불필요한 재 컴파일 방지하기

소스파일을 수정하였지만 실제 재 컴파일이 필요 없는 경우가 있습니다. 예를 들어 헤더 파일에 단순히 주석을 하나 추가한 경우에는 해당 헤더 파일을 사용 중인 소스파일들을 모두 다시 컴파일할 필요가 없겠죠. 이럴 때는 다음과 같은 순서로 처리하여 재 컴파일을 방지할 수 있습니다.

1. 먼저 헤더 파일을 수정하기 전에 갱신이 필요한 파일들이 있을 경우 make 명령을 실행해서 컴파일합니다.
2. 해당 헤더 파일을 수정합니다.
3. "make -t" 명령으로 모든 오브젝트 파일들을 최신으로 만듭니다.

작업이 완료되면 다음 make 명령 실행부터는 컴파일 작업이 일어나지 않습니다. 만약에 컴파일이 필요한 파일이 있었는데도 먼저 make 명령을 실행하지 않고 헤더 파일을 수정했을 경우에는 다음과 같이하면 됩니다.

1. "make -o headerfile" 명령은 현재 최신 상태인 headerfile 을 old 상태로 간주하여 실행하므로
    이전에 갱신이 필요했던 파일들만 컴파일 할 수 있습니다.
2. "make -t" 명령으로 모든 오브젝트 파일들을 최신으로 만듭니다.

무조건 전체 재 컴파일하기

-B 옵션을 사용하면 됩니다.

recipe 실행시 사용되는 shell 프로그램 변경하기

recipe 가 실행될 때는 shell 프로그램으로 기본적으로 sh 이 사용되는데요. SHELL 변수를 이용하면 변경해 사용할 수 있습니다. 또한 .SHELLFLAGS 변수를 이용하면 shell 프로그램에 전달되는 옵션도 추가할 수 있습니다.

SHELL, .SHELLFLAGS 변수는 target-specific 변수로도 사용이 가능합니다.

.ONESHELL:

foo : bar
    @cat <<EOF
    $@ : $$0
    EOF

bar : SHELL := /bin/bash
bar : .SHELLFLAGS += -e          # errexit shell 옵션
bar :
    @cat <( echo $@ : $$0 )      # bash 에서만 사용 가능한 프로세스 치환을 사용할 수 있다.

######  실행 결과  #######
bar : /bin/bash
foo : /bin/sh

------------------------------------

# 간단한 명령일 경우는 다음과 같이 직접 실행해도 되겠죠.
foo :
    @bash -e -c 'cat <( echo $@ : $$0 )'

shell script 파일을 작성할 때처럼 recipe 작성하기

recipe 에서 one-line 으로 명령을 작성하는 것이 불편할 경우 다음과 같이 multi-line 변수와 export 지시자를 활용하면 shell script 파일을 작성할 때와 동일하게 작성하여 실행시킬 수 있습니다 ( 실행될 때도 하나의 shell 프로세스에서 실행됩니다 ). 한가지 shell 에서 eval 명령을 이용해 실행시키기 때문에 인수를 전달할 수가 없는데 이때는 make 의 eval 함수를 이용하면 recipe 실행전에 필요한 변수값을 설정할 수 있습니다. 해당 룰이 실행될 때만 script 변수를 export 해 사용하고 이후에는 unexport 하려면 오른쪽 예제와 같이 작성하면 됩니다.

AA := 100                                        AA := 100

define script                                    define script
echo shell PID : $$$$                            echo shell PID : $$$$
cat <<EOF                                        cat <<EOF
make variable AA is : $(AA)                      make variable AA is : $(AA)
\$$PWD variable is : "$$PWD"                     \$$PWD variable is : "$$PWD"
shell PID : $$$$                                 shell PID : $$$$
EOF   # EOF 뒤에 공백이 붙지 않도록 주의               EOF
endef                                            endef

export script                                    foo ::
                                                     $(eval export script)
foo :                                                $(eval AA := 200)
    @eval "$$script"                             foo ::
    $(eval AA := 200)                                @eval "$$script"
                                                 foo ::
                                                     $(eval unexport script)
#############  실행 결과  ##############
shell PID : 28413
make variable AA is : 200
$PWD variable is : "/home/mug896"
shell PID : 28413     # 동일한 PID

만약에 스크립트에서 make 변수를 사용할 필요가 전혀없을 경우에는 다음과 같이 value 함수를 이용해 export 를 하면 $$ 변수를 사용할 필요도 없고 shell script 파일을 작성할때와 동일하게 작성할 수 있습니다.

define script                                     define script   
echo shell PID : $$                               echo shell PID : $$
cat <<EOF                                         cat <<EOF
\$PWD variable is : "$PWD"                        \$PWD variable is : "$PWD"
shell PID : $$                                    shell PID : $$
EOF                                               EOF
endef                                             endef
# 대입연산 끝부분에 공백이 붙지 않도록 주의
export script := $(value script)                  script := $(value script)

foo :                                             foo ::
    @eval "$$script"                                  $(eval export script)
                                                  foo ::
##########  실행 결과  ###########                      @eval "$$script"
shell PID : 28527                                 foo ::
$PWD variable is : "/home/mug896"                     $(eval unexport script)
shell PID : 28527

shell 함수를 정의해 사용하기

Multi-line 변수를 이용해 작성한 shell script 를 foreach 함수를 이용해 여러번 call 해야될 경우가 생길수 있는데 이때는 해당 shell script 가 그대로 여러번 중복 생성되어 실행되게 됩니다. 따라서 shell script 사이즈가 클 경우에는 다음과 같이 shell 함수를 정의하고 foreach 에서는 해당 shell 함수를 호출하는 것이 좋습니다.

foreach 에서 shell 함수 호출을 작성할 때는 마지막에 ; 문자를 붙여야 하고 오류가 발생했을 때 make 실행을 종료하려면 오른쪽 예제와 같이 set -e shell 옵션을 설정하면 됩니다.

define script                               define script
hello() {                                   hello() {
    echo variable AA is : $1                    echo variable AA is : $1
}                                               date -@
endef                                       }
                                            endef                        
export script := $(value script)                                         
                                            export script := $(value script)
foo :                                                                   
    $(eval AA := 100 200 300 )              foo :                        
    @eval "$$script";  \                        $(eval AA := 100 200 300 )
    $(foreach V, $(AA), hello $V ; )            @set -e; eval "$$script";  \
                                                $(foreach V, $(AA), hello $V ; )

###########  실행 결과  ###########           ##########  실행 결과  #########
variable AA is : 100                        variable AA is : 100
variable AA is : 200                        date: invalid option -- '@'
variable AA is : 300                        Try 'date --help' for more information.
                                            Makefile:11: recipe for target 'foo' failed
                                            make: *** [foo] Error 1

export 한 변수값을 shell script 파일로 저장하려면

만약에 export 한 script 변수값을 shell script 파일로 그대로 저장하려면 기본적으로 escape 문자가 처리되지 않는 /bin/echo 명령을 이용하거나 bash echo 명령을 사용해야 합니다. 다음 두 번째 예제에서처럼 sh 의 echo 명령을 사용하면 escape 문자가 처리되어 저장됩니다.

define script
#!/bin/bash
echo -e "escape\ttest"     # \t escape 문자
endef
                                           . . . .
export script                              . . . .

foo :                                      foo :
    @/bin/echo "$$script" > script.sh          @echo "$$script" > script.sh

########  실행 결과  ########                ########  실행 결과  ########
sh$ make                                   sh$ make
sh$ cat script.sh                          sh$ cat script.sh
#!/bin/bash                                #!/bin/bash
echo -e "escape\ttest"                     echo -e "escape    test"  # tab 처리가 된다.

recipe 에서 trap 의 사용

recipe 실행 중에 명령이 오류로 종료하여 make 실행이 종료될 경우 뒤처리 작업이 필요하면 shell 에서와 동일하게 trap 을 설정하면 됩니다. EXIT trap 은 set -e shell 옵션 설정에 의해 종료될 경우에도 동일하게 실행됩니다.

TRAP := set -e; trap 'test $$? != 0 && echo [ ERROR EXIT ] some cleaning ...' EXIT; 
foo :
    @echo $@ recipe start ...
    @$(TRAP) date; date -@; date         # 'date -@' 명령은 오류로 종료
    @echo $@ recipe end ...

########  실행 결과  ########
foo recipe start ...
Sun 30 Jun 2019 11:12:55 PM KST
date: invalid option -- '@'
Try 'date --help' for more information.      # 오류로 make 이 종료되기 전에
[ ERROR EXIT ] some cleaning ...             # trap 에서 설정한 명령이 실행된다.
Makefile:5: recipe for target 'foo' failed
make: *** [foo] Error 1

makefile 에서 실행할 수 있는 타겟 알아내기

프롬프트 상에서 make 명령을 실행할 때 tab 키를 이용하면 해당 makefile 에서 실행할 수 있는 타겟 정보를 볼 수 있습니다.

sh$ make [tab]
all    bar    clean  foo    zoo

Color 메시지 출력하기

color 메시지를 출력할 때 사용되는 color 코드는 메시지가 터미널로 출력될 때 필요한 것이므로 파이프나 파일로 출력될 경우는 제외하는 것이 좋습니다. 현재 실행 중인 make 명령의 stdout 과 stderr 가 터미널에 연결되어 있는지는 MAKE_TERMOUT, MAKE_TERMERR 변수값을 통해 알아볼 수 있습니다. 다음과 같이하면 stdout, stderr 가 터미널에 연결되어 있을 경우는 tput 명령의 실행 결과인 color 코드가 출력에 포함되지만 그렇지 않고 파이프나 파일에 연결되어 있을 경우는 제외되게 됩니다.

# color 이름 뒤에 1 이 붙은 것은 stdout 로 출력할때, 2 가 붙은 것은 stderr 로 출력할때 사용
# '!=' 연산자를 사용하였으므로 대입 연산시 tput 명령이 immediate 실행됩니다.
red1    != $(if $(MAKE_TERMOUT),tput setaf 1,)
green1  != $(if $(MAKE_TERMOUT),tput setaf 2,)
blue1   != $(if $(MAKE_TERMOUT),tput setaf 4,)
reset1  != $(if $(MAKE_TERMOUT),tput sgr0,)

red2    != $(if $(MAKE_TERMERR),tput setaf 1,)
green2  != $(if $(MAKE_TERMERR),tput setaf 2,)
blue2   != $(if $(MAKE_TERMERR),tput setaf 4,)
reset2  != $(if $(MAKE_TERMERR),tput sgr0,)

$(info $(green1)hello make stdout color$(reset1))    # stdout 출력
$(warning $(red2)hello make stderr color$(reset2))   # stderr 출력

color :
    @echo "$(blue1)hello recipe stdout color$(reset1)"      # stdout 출력
    @echo "$(red2)hello recipe stderr color$(reset2)" >&2   # stderr 출력

###############  실행 결과  ################

bash$ make                             # 터미널로 출력되므로 color 로 출력된다
hello make stdout color                 
Makefile:12: hello make stderr color
hello recipe stdout color
hello recipe stderr color

bash$ make |& cat                      # 파이프로 출력되므로 color 코드가 제외된다
hello make stdout color                 
Makefile:12: hello make stderr color
hello recipe stdout color
hello recipe stderr color