2017-11-25 2 views
0

를 작성하는 나는어떻게 일반 규칙 기반 메이크

--Makefile 
    --source/ 
     --boot.s 
     --kernel.c 
     --linker.ld 
    --build/ 

과 내가 패턴을 기반으로 규칙을 작성하는 방법을

############################################################################### 
# 
# A makefile script for generation of raspberry pi kernel images. 
# 
############################################################################### 

# The toolchain to use. arm-none-eabi works, but there does exist 
CC = arm-none-eabi-gcc 
LD = arm-none-eabi-gcc 

# The intermediate directory for compiled object files. 
BUILD = build/ 

# The directory in which source files are stored. 
SOURCE = source/ 

CFLAGS = -march=armv8-a+crc \ 
      -mcpu=cortex-a53 \ 
      -mtune=cortex-a53 \ 
      -mfpu=crypto-neon-fp-armv8 \ 
      -mfloat-abi=hard \ 
      -ftree-vectorize \ 
      -funsafe-math-optimizations \ 
      -O2 -pipe -ffreestanding 

LDFLAGS = -T $(SOURCE)linker.ld -ffreestanding -O2 -nostdlib 


# The name of the output file to generate. 
TARGET = kernel.img 

.PHONY: all clean run 

# Rule to make everything. 
all: $(TARGET) 

# Rule to remake everything. Does not include clean. 
rebuild: clean all 

#Rule to invoke qemu 
run: 
    qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $(BUILD)kernel.elf 

$(TARGET): kernel.elf 
    arm-none-eabi-objcopy $(BUILD)kernel.elf -O binary $(BUILD)kernel.img 

kernel.elf: boot.o kernel.o 
    $(LD) $(LDFLAGS) -o $(BUILD)kernel.elf $(BUILD)boot.o $(BUILD)kernel.o 

boot.o: $(SOURCE)boot.s 
    $(CC) $(CFLAGS) -c $(SOURCE)boot.s -o $(BUILD)boot.o 

kernel.o: $(SOURCE)boot.s 
    $(CC) $(CFLAGS) -c $(SOURCE)kernel.c -o $(BUILD)kernel.o 

# Rule to clean files. 
clean : 
    -rm -f $(BUILD)* 
    -rm -f *.o 
    -rm -f *.elf 
    -rm -f *.img 

메이크

다음 사용하여 내 소스를 컴파일 폴더 구조 다음습니까? 많은 스택 오버플로 해답을 시도했지만 제대로 작동하지 못했습니다.

처음에 와일드 카드를 사용하여 내 소스를 나열했지만 소스 목록에 [source/boot.s source/kernel.c]가 있었기 때문에 적절한 대상을 쓸 수 없었습니다. 소스 파일 자체에 오브젝트 파일을 생성합니다.

소스를 유지하면서 디렉토리를 다르게 만드는 데 어려움을 겪고있었습니다. 어떤 도움을 주셔서 감사합니다. @MadScientist에 따라

---------- 완벽한 솔루션 -----------

############################################################################### 
# 
# A makefile script for generation of raspberry pi kernel images. 
# 
############################################################################### 

# The toolchain to use. arm-none-eabi works, but there does exist 
CC = arm-none-eabi-gcc 
LD = arm-none-eabi-gcc 

# The intermediate directory for compiled object files. 
BUILD = build/ 

# The directory in which source files are stored. 
SOURCE = source/ 

CFLAGS = -march=armv8-a+crc \ 
      -mcpu=cortex-a53 \ 
      -mtune=cortex-a53 \ 
      -mfpu=crypto-neon-fp-armv8 \ 
      -mfloat-abi=hard \ 
      -ftree-vectorize \ 
      -funsafe-math-optimizations \ 
      -O2 -pipe -ffreestanding 

LDFLAGS = -T $(SOURCE)linker.ld -ffreestanding -O2 -nostdlib 

SOURCES := $(wildcard $(SOURCE)*.s) $(wildcard $(SOURCE)*.c) 
OBJECTS := $(patsubst $(SOURCE)%,$(BUILD)%.o,$(basename $(SOURCES))) 

# The name of the output file to generate. 
TARGET = kernel.img 

.PHONY: all clean run 

# Rule to make everything. 
all: $(BUILD)$(TARGET) 

# Rule to remake everything. Does not include clean. 
rebuild: clean all 

#Rule to invoke qemu 
run: 
    qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $(BUILD)kernel.elf 

$(BUILD)%.o : $(SOURCE)%.s 
    $(CC) $(CFLAGS) -c $< -o [email protected] 

$(BUILD)%.o : $(SOURCE)%.c 
    $(CC) $(CFLAGS) -c $< -o [email protected] 

$(BUILD)$(TARGET): $(BUILD)kernel.elf 
    arm-none-eabi-objcopy $< -O binary [email protected] 

$(BUILD)kernel.elf: $(OBJECTS) 
    $(LD) $(LDFLAGS) -o [email protected] $^ 

# Rule to clean files. 
clean : 
    -rm -f $(BUILD)* 
    -rm -f *.o 
    -rm -f *.elf 
    -rm -f *.img 
+0

당신은 내가 많은 [것을] 시도 "라고하지만, 그것을 일하게 만들지 못했습니다. " 질문을 할 때, 시도한 것을 보여주고, 달린 명령을 보여주고, 결과를 보여주고 (의역을 말하지 않고 잘라내어 붙여 넣기) 결과가 왜 당신이 원하는 것이 아니 었는지 설명하십시오. – MadScientist

+0

오버플로 스택을 새로 도입했습니다. 내 질문을 편집하고 자세한 내용을 추가하겠습니다. – zero

답변

1

이 잘못 :

boot.o: $(SOURCE)boot.s 
     $(CC) $(CFLAGS) -c $(SOURCE)boot.s -o $(BUILD)boot.o 

당신은 조리법이 이라는 파일을 만들 것이라고 말하고 있지만 그렇지 않습니다. $(BUILD)boot.o이라는 파일이 생성됩니다.이 파일은 완전히 다릅니다. 규칙과 동일하게 kernel.imgkernel.elf을 작성하십시오.

패턴 규칙을 작성하려는 경우 %은 일치하는 부분 만 일치시킬 수 있습니다. SOURCEBUILD은 동일하지 않으므로 % 부분과 일치하지 않습니다. 그래서 당신은이를 작성해야 :

$(BUILD)%.o : $(SOURCE)%.s 
     $(CC) $(CFLAGS) -c $< -o [email protected] 

것은 당신이 $(BUILD)xxx.o를 구축하고 있기 때문에 당신이 작성해야하므로, 그 전제 조건으로도 사용할 수 있습니다

$(BUILD)$(TARGET): $(BUILD)kernel.elf 
     arm-none-eabi-objcopy $< -O binary [email protected] 

$(BUILD)kernel.elf: $(BUILD)boot.o $(BUILD)kernel.o 
     $(LD) $(LDFLAGS) -o [email protected] $^ 

ETA에게

와일드 카드를 통해 소스 파일을 가져 오려면 다음과 같이 접미사뿐만 아니라 디렉토리도 바꾸어야합니다.

SOURCES := $(wildcard $(SOURCE)*.s) 
OBJECTS := $(patsubst $(SOURCE)%.s,$(BUILD)%.o,$(SOURCES)) 

이있는 경우 조립 및 C 소스 파일 (당신이 당신의 예를 들어 메이크 파일에있는 C 소스 파일을 보여주지 않았다) 당신이 사용할 수있는 양 :

SOURCES := $(wildcard $(SOURCE)*.s) $(wildcard $(SOURCE)*.c) 
OBJECTS := $(patsubst $(SOURCE)%,$(BUILD)%.o,$(basename $(SOURCES))) 
+0

나는 그것을 내 Makefile에 포함시켰다. 그러나 그것에 무언가를 추가해야했습니다. '소스 = $으로 (와일드 카드 $ (SOURCE). * S) $ (와일드 카드 $ (SOURCE) *. c) \t $ (patsubst는 $으로 (SOURCE) % \ OBJECTS = $ (필터 %의 .o 인이. s, $ (BUILD) %., $ (SOURCES)) \ \t $ (patsubst $ (SOURCE) % .c, $ (BUILD) %)$ (LDFLAGS) -o $ @ $ ^' – zero

+0

당신이 볼 수 있듯이 형식화 된 내용을 주석에 추가 할 수 없습니다. 에서. 당신은 질문을 읽기 쉽게 형식을 바꾸기 위해 편집해야합니다. 나는 나의 대답을 업데이트했다. – MadScientist