폴더 속 Makefile 을 찾아 빌드하는 Script

Seunghso·2023년 12월 13일

script

목록 보기
1/3
post-thumbnail

작성 배경: 여러 개의 Makefile을 한꺼번에 다루기 위해서

42서울에서 진행하는 과제에서는 Makefile을 사용한다. 하나의 과제 속에는 여러 개의 작은 문제이 존재하고 make 명령어를 여러 번 작성하는 것을 번거롭다고 느꼈다.
(+제출할 Git Repository에 '.o' 와 같은 목적 파일과 실행 파일도 올라가면 안 된다)

스크립트 설명

  • ./buildTest.sh [-f | --fclean] [TargetDirectory]
  • 입력한 폴더 속 하위디렉토리들 Makefile 을 찾아내어 make (fclean) 한다.
  • 작성하는 명령어 개수 3N -> 2 로 단축

스크립트 미 사용 시 (N개의 Makefile -> 3N개의 명령어)

cd ex00
make
make fclean
cd ../ex01
make
make fclean
cd ../ex02
make
make fclean

...

cd ../ex0N
make
make fclean

(make -C 를 사용하여도 N개의 명령어)

스크립트 사용 시 (2개의 명령어)

buildTest 00
buildTest -f 00

buildTest.sh

#!/bin/bash

# Function to recursively find and run make in directories containing Makefiles
run_make() {
    local dir="$1"
    local fclean="$2"

    cd "$dir" || exit

    # Find directories containing a Makefile and run make on them
    find . -type f -name 'Makefile' | while read -r makefile; do
        subdir=$(dirname "$makefile")
        echo "Running make in $subdir..."

        if [[ "$fclean" == "true" ]]; then
            make -C "$subdir" fclean
        else
            make -C "$subdir"
        fi
    done
}

# Parse arguments
DIRECTORY=""
FCLEAN="false"

while [[ "$#" -gt 0 ]]; do
    case "$1" in
        -f|--fclean) FCLEAN="true"; shift ;;
        *) DIRECTORY="$1"; shift ;;
    esac
done

# Check if directory is specified
if [[ -z "$DIRECTORY" ]]; then
    echo "Please specify a directory."
    exit 1
fi

Alias 설정

echo "alias buildTest='sh ~/buildTest.sh'" >> ~/.zshrc
source ~/.zshrc

MacOS에서는 c++ 명령어의 -std=c++98 옵션이 유효하지 않다.
(Mac OS의 Clang 컴파일러가 이를 무시한다.) 따라서 42 과제의 요구사항인 your code will comply with the C++98 standard.을 지켰는지 확인하기 번거롭다.
GitHub Codespace에서 위 스크립트를 사용하면 편하게 98standard 를 지켰는지 확인할 수 있다.

profile
Better than yesterday

0개의 댓글