Guest User

kernel-Makefile

a guest
Feb 8th, 2019
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. # using clang enables simple and fast cross-compilation
  3. c = clang
  4. asm = nasm
  5. target-flag = --target=i686-elf
  6. compile-flags = -std=c89 -Weverything -pedantic -ffreestanding -nostdlib -nostdinc #-fno-builtin
  7. link-flags = -ffreestanding -nostdlib -nostdinc #-fno-builtin
  8. full-bin-flags = -w -std=c89 -ffreestanding -nostdlib
  9. ld-flags = -nostdlib
  10. kernel = kernel.out
  11.  
  12. .PHONY: sim-bochs sim-qemu clean
  13.  
  14. default: disk.img
  15. sim: sim-bochs
  16.  
  17. sim-qemu-bin: $(kernel)
  18.     qemu-system-i386 -kernel $(kernel)
  19.  
  20. sim-bochs: disk.img
  21.     bochs -f bochs.conf
  22.  
  23. kernel-files := kernel.o
  24.     # always put kernel.o first (the bootloader relies on the entry point being at 0x0)
  25.    
  26. kernel.bin: kernel.elf
  27.     # kernel.elf --> kernel.bin
  28.     # -mX: gcc option that produces 8/16/32-bit code
  29.     @echo --- Linking $(kernel-files) to $@ ---
  30.     #ld -melf_i386 -T link-floppy.ld -o $@ $(kernel-files) --oformat binary
  31.     objcopy -Obinary kernel.elf $@
  32.     objdump -d kernel.elf -M intel > result-kernel.tmp
  33.     #ndisasm -b 32 $@ > disasm-kernel.tmp
  34.    
  35. kernel-bin := kernel.bin   
  36. disk.img: boot.bin $(kernel-bin)
  37.     dd if=boot.bin of=$@ bs=512
  38.     dd if=$(kernel-bin) of=$@ bs=512 seek=6 conv=notrunc #seek = skip n sectors before write
  39.     dd if=/dev/zero of=$@ bs=512 seek=2879 count=1
  40.     hexdump disk.img -C > disk.hex
  41.    
  42.  
  43. # Check the i686-elf directory for the GRUB module
  44. kernel.elf: $(kernel-files) link-floppy.ld
  45.     @echo --- Linking $(kernel-files) to $@ ---
  46.     clang $(target-flag) $(link-flags) -T link-floppy.ld -o $@ $(kernel-files)
  47.  
  48. %.bin: %.asm
  49.     @echo --- Assembling $@ ---
  50.     nasm -fbin -o $@ $<
  51.  
  52. %.o: %.asm
  53.     @echo --- Assembling $@ ---
  54.     nasm -felf32 -o $@ $<
  55.  
  56. %.o: %.c
  57.     @echo --- Compiling $@ ---
  58.     clang $(target-flag) -c $(compile-flags) -o $@ $<
  59.  
  60. git-add:
  61.     git add *.c *.h *.asm *.ld bochs.conf Makefile
  62.     cd i686-elf && git add *.c *.h *.asm *.ld Makefile
  63.    
  64.    
  65. clean:
  66.     find . -type f -iname '*.o' -delete;
  67.     find . -type f -iname '*.bin' -delete;
  68.     find . -type f -iname '*.out' -delete;
  69.     find . -type f -iname '*.img' -delete;
  70.     find . -type f -iname '*.tmp' -delete;
  71.     find . -type f -iname '*.iso' -delete;
  72.     if [ -e ./iso ]; then rm -r iso; fi;
RAW Paste Data