Please share your satisified Makefile, and perhaps we may improve each other
Here's mine, generated by a shell script I wrote, which take the form ./configure --arch=i586|x86_64
(ps. Thanks Solar for his Makefile tutorial in the wiki)
Code: Select all
# Auto Generated Configuration
# Configuration: ./configure --arch=x86_64
# Target Option
########################
ARCH =x86_64
TARGET =x86_64-elf
# Path
########################
PATH_MOUNT=/mnt/bluemoon/
PATH_ROOT =../../../
PATH_INC =$(PATH_ROOT)usr/include/
PATH_BIN =$(PATH_ROOT)usr/bin/$(ARCH)/
PATH_LIB =$(PATH_ROOT)usr/lib/$(ARCH)/
PATH_LIBC =$(PATH_ROOT)usr/libc/$(TARGET)/
IMAGE_FILE=$(PATH_ROOT)test/bluemoon.img
# Tools
########################
NASM =nasm -felf64
CC =$(TARGET)-gcc
CXX =$(TARGET)-g++
LD =$(TARGET)-ld
AR =$(TARGET)-ar -rcs
OBJCOPY =$(TARGET)-objcopy
# Compiler Flags
########################
INCLUDE =-I$(PATH_INC) -I$(PATH_LIBC)include/ -I$(ARCH)/
CWARN =-Wall -Wextra -Wattributes -Wbuiltin-macro-redefined -Wcast-align -Wconversion \
-Wdiv-by-zero -Wdouble-promotion -Wenum-compare -Wfloat-equal -Winit-self \
-Wint-to-pointer-cast -Wlogical-op -Wmissing-braces -Wmissing-field-initializers \
-Woverflow -Wpointer-arith -Wredundant-decls -Wreturn-type -Wshadow -Wsign-compare \
-Wtype-limits -Wuninitialized -Wwrite-strings \
-Wno-unused-parameter -Wno-unused-variable -Wno-multichar -Wno-unused-but-set-variable \
-Wdeclaration-after-statement -Wimplicit-int -Wjump-misses-init -Wpointer-sign \
-Wpointer-to-int-cast -Wmissing-parameter-type
CXXWARN =-Wall -Wextra -Wattributes -Wbuiltin-macro-redefined -Wcast-align -Wconversion \
-Wdiv-by-zero -Wdouble-promotion -Wenum-compare -Wfloat-equal -Winit-self \
-Wint-to-pointer-cast -Wlogical-op -Wmissing-braces -Wmissing-field-initializers \
-Woverflow -Wpointer-arith -Wredundant-decls -Wreturn-type -Wshadow -Wsign-compare \
-Wtype-limits -Wuninitialized -Wwrite-strings \
-Wno-unused-parameter -Wno-unused-variable -Wno-multichar -Wno-unused-but-set-variable \
-Wc++0x-compat -Wsign-promo
CFLAGS =-ffreestanding -masm=intel -std=c99 -O2 -mcmodel=kernel -mno-red-zone \
-mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -c -D_$(ARCH) $(INCLUDE) -Werror $(CWARN)
CXXFLAGS =-ffreestanding -masm=intel -O2 -mcmodel=kernel -mno-red-zone \
-mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow \
-fno-exceptions -fno-rtti -fno-stack-protector -c -D_$(ARCH) $(INCLUDE) -Werror $(CXXWARN)
LDFLAGS =-nostdinc -nostdlib
# Phony
########################
.PHONY: all clean kernel install
all: kernel
##############################################################
#
# KERNEL
#
##############################################################
KERNEL_TMP :=.build/$(ARCH)/
KERNEL_C :=$(wildcard *.c)
KERNEL_CPP :=$(wildcard *.cpp)
KERNEL_ARCH_ASM :=$(wildcard $(ARCH)/*.asm)
KERNEL_ARCH_C :=$(wildcard $(ARCH)/*.c)
KERNEL_ARCH_CPP :=$(wildcard $(ARCH)/*.cpp)
KERNEL_OBJ :=$(patsubst %.c, $(KERNEL_TMP)%_c.o, $(KERNEL_C)) \
$(patsubst %.cpp, $(KERNEL_TMP)%_cpp.o,$(KERNEL_CPP)) \
$(patsubst $(ARCH)/%.asm, $(KERNEL_TMP)$(ARCH)-%_a.o, $(KERNEL_ARCH_ASM)) \
$(patsubst $(ARCH)/%.c, $(KERNEL_TMP)$(ARCH)-%_c.o, $(KERNEL_ARCH_C)) \
$(patsubst $(ARCH)/%.cpp, $(KERNEL_TMP)$(ARCH)-%_cpp.o,$(KERNEL_ARCH_CPP))
KERNEL_DEP :=$(patsubst %.o,%.d,$(KERNEL_OBJ))
KERNEL_BIN :=$(PATH_BIN)kernel64.bin
KERNEL_SYM :=$(PATH_BIN)kernel64.sym
KERNEL_USELIB :=$(PATH_LIBC)lib/libc.a $(PATH_LIB)libelf.a
-include $(KERNEL_DEP)
kernel: $(KERNEL_BIN)
$(KERNEL_BIN): $(KERNEL_TMP) $(KERNEL_OBJ) $(KERNEL_USELIB) Makefile
@echo [LINK] $@
@$(LD) $(LDFLAGS) -T$(ARCH)/kernel.ld -o $@ $(KERNEL_OBJ) $(KERNEL_USELIB)
@$(OBJCOPY) --only-keep-debug $@ $(KERNEL_SYM)
@$(OBJCOPY) --strip-debug --strip-unneeded $@
$(KERNEL_TMP):
@mkdir -p $@
$(KERNEL_TMP)$(ARCH)-%_a.o: $(ARCH)/%.asm Makefile
@echo [NASM] $<
@$(NASM) -I$(ARCH)/ -MD $(KERNEL_TMP)$(ARCH)-$*_a.d -MP $< -o $@
$(KERNEL_TMP)$(ARCH)-%_c.o: $(ARCH)/%.c Makefile
@echo [GCC ] $<
@$(CC) $(CFLAGS) -I./ -MD -MP $< -o $@
$(KERNEL_TMP)$(ARCH)-%_cpp.o: $(ARCH)/%.cpp Makefile
@echo [CXX ] $<
@$(CXX) $(CXXFLAGS) -I./ -MD -MP $< -o $@
$(KERNEL_TMP)%_a.o: %.asm Makefile
@echo [NASM] $<
@$(NASM) -MD $(KERNEL_TMP)$*_$(TARGET)_a.d -MP $< -o $@
$(KERNEL_TMP)%_c.o: %.c Makefile
@echo [GCC ] $<
@$(CC) $(CFLAGS) -MD -MP $< -o $@
$(KERNEL_TMP)%_cpp.o: %.cpp Makefile
@echo [CXX ] $<
@$(CXX) $(CXXFLAGS) -MD -MP $< -o $@
##############################################################
install: kernel
@echo [COPY] $(KERNEL_BIN) TO $(PATH_MOUNT)
@cp $(KERNEL_BIN) $(PATH_MOUNT)
##############################################################
clean:
@rm $(KERNEL_OBJ) $(KERNEL_DEP)