这是一个基于Ubuntu和GCC的操作系统编译Makefile
可以用来生成二进制程序和APP,以及Image镜像文件。
详细的Makefile内容如下:


ifndef GCCPREFIX
GCCPREFIX :=
endif

ifndef ARCH
ARCH := x86
endif

ifndef DEBUG
DEBUG := 1
endif

TARGET := target
AS  := nasm
CC  := $(GCCPREFIX)gcc
LD  := $(GCCPREFIX)ld
OBJCOPY := $(GCCPREFIX)objcopy
ifeq ($(ARCH), x86_64)
QEMU    := qemu-system-i386
CFLAGS += -Wall -Wno-format -Wno-unused -fno-pic
CFLAGS += -gstabs -std=gnu99 -static -m64 -fno-stack-protector
CFLAGS += -I ./sys/include -I ./libc/include
CFLAGS += -ffunction-sections -nostdlib -nostdinc -fno-builtin -ffreestanding 
QEMU_FLAGS  += -no-reboot -d in_asm -m size=128
else
QEMU    := qemu-system-i386
CFLAGS += -Wall -Wno-format -Wno-unused -fno-pic
CFLAGS += -gstabs -std=gnu99 -static -m32 -fno-stack-protector
CFLAGS += -I ./sys/include -I ./libc/include
CFLAGS += -ffunction-sections -nostdlib -nostdinc -fno-builtin -ffreestanding
QEMU_FLAGS  += -no-reboot -d in_asm -m size=128
endif
# C Library Objects
CLIB_OBJS  := \
        libc/stdio/stdio.bin \
        libc/string/strcmp.bin \
        libc/string/strncmp.bin \
        libc/stdlib/rand.bin \
        libc/graphics/arc.bin \
        libc/graphics/ATT_driver.bin \
        libc/graphics/bar3d.bin \
        libc/graphics/bar.bin \
        libc/graphics/CGA_driver.bin \
        libc/graphics/circle.bin \
        libc/graphics/cleardevice.bin \
        libc/graphics/clearviewport.bin \
        libc/graphics/closegraph.bin \
        libc/graphics/detectgraph.bin \
        libc/graphics/drawpoly.bin \
        libc/graphics/EGAVGA_driver.bin \
        libc/graphics/ellipse.bin \
        libc/graphics/fillellipse.bin \
        libc/graphics/fillpoly.bin \
        libc/graphics/floodfill.bin \
        libc/graphics/getarccoords.bin \
        libc/graphics/getaspectratio.bin \
        libc/graphics/getbkcolor.bin \
        libc/graphics/getcanvas.bin \
        libc/graphics/getcolor.bin \
        libc/graphics/getdefaultpalette.bin \
        libc/graphics/getdrivername.bin \
        libc/graphics/getfillpattern.bin \
        libc/graphics/getfillsettings.bin \
        libc/graphics/getgraphmode.bin \
        libc/graphics/getimage.bin \
        libc/graphics/getlinesettings.bin \
        libc/graphics/getmaxcolor.bin \
        libc/graphics/getmaxmode.bin \
        libc/graphics/getmaxx.bin \
        libc/graphics/getmaxy.bin \
        libc/graphics/getmodename.bin \
        libc/graphics/getmoderange.bin \
        libc/graphics/getpalette.bin \
        libc/graphics/getpalettesize.bin \
        libc/graphics/getpixel.bin \
        libc/graphics/gettextsettings.bin \
        libc/graphics/getviewsettings.bin \
        libc/graphics/getx.bin \
        libc/graphics/gety.bin \
        libc/graphics/gothic_font.bin \
        libc/graphics/graphdefaults.bin \
        libc/graphics/grapherrormsg.bin \
        libc/graphics/_graphfreemem.bin \
        libc/graphics/_graphgetmem.bin \
        libc/graphics/graphresult.bin \
        libc/graphics/Herc_driver.bin \
        libc/graphics/IBM8514_driver.bin \
        libc/graphics/imagesize.bin \
        libc/graphics/initgraph.bin \
        libc/graphics/installuserdriver.bin \
        libc/graphics/installuserfont.bin \
        libc/graphics/line.bin \
        libc/graphics/linerel.bin \
        libc/graphics/lineto.bin \
        libc/graphics/moverel.bin \
        libc/graphics/moveto.bin \
        libc/graphics/outtext.bin \
        libc/graphics/outtextxy.bin \
        libc/graphics/PC3270_driver.bin \
        libc/graphics/pieslice.bin \
        libc/graphics/putimage.bin \
        libc/graphics/putpixel.bin \
        libc/graphics/rectangle.bin \
        libc/graphics/registerbgidriver.bin \
        libc/graphics/registerbgifont.bin \
        libc/graphics/restorecrtmode.bin \
        libc/graphics/sansserif_font.bin \
        libc/graphics/sector.bin \
        libc/graphics/setactivepage.bin \
        libc/graphics/setallpalette.bin \
        libc/graphics/setaspectratio.bin \
        libc/graphics/setbkcolor.bin \
        libc/graphics/setcanvas.bin \
        libc/graphics/setcolor.bin \
        libc/graphics/setfillpattern.bin \
        libc/graphics/setfillstyle.bin \
        libc/graphics/setgraphbufsize.bin \
        libc/graphics/setgraphmode.bin \
        libc/graphics/setlinestyle.bin \
        libc/graphics/setpalette.bin \
        libc/graphics/setrgbcolor.bin \
        libc/graphics/setrgbpalette.bin \
        libc/graphics/settextjustify.bin \
        libc/graphics/settextstyle.bin \
        libc/graphics/setusercharsize.bin \
        libc/graphics/setviewport.bin \
        libc/graphics/setvisualpage.bin \
        libc/graphics/setwritemode.bin \
        libc/graphics/small_font.bin \
        libc/graphics/textheight.bin \
        libc/graphics/textwidth.bin \
        libc/graphics/triplex_font.bin

# Kernel Objects
KERNEL_OBJS := \
        sys/arch/$(ARCH)/kernel/system.bin \
        sys/arch/$(ARCH)/kernel/io.bin \
        sys/arch/$(ARCH)/kernel/pm.bin \
        sys/arch/$(ARCH)/kernel/desctbl.bin \
        sys/arch/$(ARCH)/kernel/int.bin \
        sys/arch/$(ARCH)/kernel/inthandler.bin \
        sys/arch/$(ARCH)/kernel/fifo.bin \
        sys/arch/$(ARCH)/kernel/memory.bin \
        sys/arch/$(ARCH)/kernel/timer.bin \
        sys/arch/$(ARCH)/kernel/task_asm.bin \
        sys/arch/$(ARCH)/kernel/task.bin \
        sys/arch/$(ARCH)/kernel/api.bin \
        sys/arch/$(ARCH)/kernel/api_asm.bin \
        sys/arch/$(ARCH)/kernel/app.bin \
        sys/arch/$(ARCH)/kernel/elf.bin

# FileSystem Objects
FILESYSTEM_OBJS := \
        sys/filesystem/fat/fat12/fat12.bin \
        sys/filesystem/fat/fat32/fat32.bin

# GUI Objects
GUI_OBJS    := \
        sys/gui/window.bin \
        sys/gui/command.bin \
        sys/gui/console.bin \
        sys/gui/cursor.bin \
        sys/gui/screen.bin \
        sys/gui/fonts.bin \
        sys/gui/layer.bin
# Drivers Objects
DRIVERS_OBJS    := \
        sys/arch/$(ARCH)/drivers/input/keyboard/keyboard.bin \
        sys/arch/$(ARCH)/drivers/input/mouse/mouse.bin \
        sys/arch/$(ARCH)/drivers/video/VESA/VESA.bin \
        sys/arch/$(ARCH)/drivers/video/VGA/VGA.bin \
        sys/arch/$(ARCH)/drivers/audio/sound.bin \
        sys/arch/$(ARCH)/drivers/network/LAN/LAN.bin \
        sys/arch/$(ARCH)/drivers/storage/harddrive/harddrive.bin
# Applications
APPS    := \
        app/bgidemo.app
#APPS :=
SYS := system.sys
IMG := appspace.img

ifdef DEBUG
QEMU_FLAGS += -gdb tcp::1234 -S
CFLAGS += -g
endif

sys/arch/$(ARCH)/boot/load.bin:
    $(AS) -f bin sys/arch/$(ARCH)/boot/load.asm -o sys/arch/$(ARCH)/boot/load.bin -l sys/arch/$(ARCH)/boot/load.lst

sys/arch/$(ARCH)/kernel/asmhead.bin:
    $(AS) -f bin sys/arch/$(ARCH)/kernel/asmhead.asm -o sys/arch/$(ARCH)/kernel/asmhead.bin -l sys/arch/$(ARCH)/kernel/asmhead.lst

sys/arch/$(ARCH)/kernel/%.bin: sys/arch/$(ARCH)/kernel/%.asm
    $(AS) -f elf $< -o $@ -l $(subst .asm,.lst,$<)

%.bin: %.c
    $(CC) $(CFLAGS) -c $< -o $@

# Applications
sys/api/api.lib:
    $(AS) -f elf  sys/arch/$(ARCH)/api/api.asm -o $@

sys/arch/$(ARCH)/kernel/app.lib:
    $(AS) -f elf  sys/arch/$(ARCH)/kernel/app.asm -o $@

%.app: %.asm
    $(AS) -f elf $< -o $@ -l $(subst .asm,.lst,$<)

app/%.o: app/%.c
    $(CC) $(CFLAGS) -c $< -o $@

app/%.app: app/%.o sys/arch/$(ARCH)/kernel/app.lib
ifeq ($(ARCH), x86_64)
    $(LD) -m elf_x86_64 --oformat elf32-x86-64 -shared -e main -pie $^ -o $@
else
    $(LD) -m elf_i386 --oformat elf32-i386 -shared -e main -pie $^ -o $@
endif

# Kernel
sys/arch/$(ARCH)/kernel/kernel.sys: ${CLIB_OBJS} ${DRIVERS_OBJS} ${FILESYSTEM_OBJS} ${KERNEL_OBJS} ${GUI_OBJS}
ifeq ($(ARCH), x86_64)
    $(LD) -m elf_x86_64 --oformat binary -o sys/arch/$(ARCH)/kernel/kernel.sys -T sys/arch/$(ARCH)/kernel/kernel.ld $^
else
    $(LD) -m elf_i386 --oformat binary -o sys/arch/$(ARCH)/kernel/kernel.sys -T sys/arch/$(ARCH)/kernel/kernel.ld $^
endif

system.sys: sys/arch/$(ARCH)/kernel/asmhead.bin sys/arch/$(ARCH)/kernel/kernel.sys
    cat sys/arch/$(ARCH)/kernel/asmhead.bin > system.sys
    cat sys/arch/$(ARCH)/kernel/kernel.sys >> system.sys

system-image: sys/arch/$(ARCH)/boot/load.bin system.sys ${APPS}
    dd if=/dev/zero of=$(IMG) bs=512 count=2880
    mkfs.msdos $(IMG)
    dd if=sys/arch/$(ARCH)/boot/load.bin of=$(IMG) bs=512 count=1 conv=notrunc

copyapps:
    mcopy -i $(IMG) system.sys ::/
    mcopy -i $(IMG) ${APPS} ::/
    mcopy -i $(IMG) sys/arch/$(ARCH)/boot/load.bin ::/
    mcopy -i $(IMG) sys/fonts/hzk16.fonts ::/

all: ${CLIB_OBJS} ${DRIVERS_OBJS} ${FILESYSTEM_OBJS} ${KERNEL_OBJS}  ${GUI_OBJS} ${APPS} system.sys system-image copyapps

clean:
    rm -rf $(IMG)
    rm -rf *.bin
    rm -rf *.lst
    rm -rf *.obj
    rm -rf *.sys
    rm -rf app/*.app
    rm -rf app/*.bin
    rm -rf sys/arch/$(ARCH)/kernel/*.bin
    rm -rf **/*.bin
    rm -rf **/*.lib
    rm -rf **/*.lst
    rm -rf **/*.o
    rm -rf **/*.sys
    rm -rf **/**/*.bin

qemu: clean all
    $(QEMU) -fda $(IMG) $(QEMU_FLAGS)

#run: clean all
#   $(QEMU) -drive file=$(IMG),if=floppy $(QEMU_FLAGS)