Makefile 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ifeq ($(TOOLCHAIN),)
  2. ifeq ($(shell util/check.sh),y)
  3. export PATH := $(shell util/activate.sh)
  4. else
  5. FOO := $(shell util/prompt.sh)
  6. ifeq ($(shell util/check.sh),y)
  7. export PATH := $(shell util/activate.sh)
  8. else
  9. $(error "No toolchain, and you did not ask to build it.")
  10. endif
  11. endif
  12. endif
  13. # Prevents Make from removing intermediary files on failure
  14. .SECONDARY:
  15. # Disable built-in rules
  16. .SUFFIXES:
  17. TARGET_TRIPLET=i686-pc-toaru
  18. # Userspace flags
  19. CC=$(TARGET_TRIPLET)-gcc
  20. AR=$(TARGET_TRIPLET)-ar
  21. CFLAGS= -O3 -g -std=gnu99 -I. -Iapps -pipe -mfpmath=sse -mmmx -msse -msse2 -fplan9-extensions -Wall -Wextra -Wno-unused-parameter
  22. LIBC_OBJS = $(patsubst %.c,%.o,$(wildcard libc/*.c))
  23. LIBC_OBJS += $(patsubst %.c,%.o,$(wildcard libc/*/*.c))
  24. LC=base/lib/libc.so
  25. APPS=$(patsubst apps/%.c,%,$(wildcard apps/*.c))
  26. APPS_X=$(foreach app,$(APPS),base/bin/$(app))
  27. APPS_Y=$(foreach app,$(filter-out init,$(APPS)),.make/$(app).mak)
  28. LIBS=$(patsubst lib/%.c,%,$(wildcard lib/*.c))
  29. LIBS_X=$(foreach lib,$(LIBS),base/lib/libtoaru_$(lib).so)
  30. LIBS_Y=$(foreach lib,$(LIBS),.make/$(lib).lmak)
  31. all: image.iso
  32. # Kernel / module flags
  33. KCC = $(TARGET_TRIPLET)-gcc
  34. KAS = $(TARGET_TRIPLET)-as
  35. KLD = $(TARGET_TRIPLET)-ld
  36. KNM = $(TARGET_TRIPLET)-nm
  37. KCFLAGS = -O2 -std=c99
  38. KCFLAGS += -finline-functions -ffreestanding
  39. KCFLAGS += -Wall -Wextra -Wno-unused-function -Wno-unused-parameter -Wno-format
  40. KCFLAGS += -pedantic -fno-omit-frame-pointer
  41. KCFLAGS += -D_KERNEL_
  42. KCFLAGS += -DKERNEL_GIT_TAG=$(shell util/make-version)
  43. KASFLAGS = --32
  44. KERNEL_OBJS = $(patsubst %.c,%.o,$(wildcard kernel/*.c))
  45. KERNEL_OBJS += $(patsubst %.c,%.o,$(wildcard kernel/*/*.c))
  46. KERNEL_OBJS += $(patsubst %.c,%.o,$(wildcard kernel/*/*/*.c))
  47. KERNEL_ASMOBJS = $(filter-out kernel/symbols.o,$(patsubst %.S,%.o,$(wildcard kernel/*.S)))
  48. # Kernel
  49. cdrom/kernel: ${KERNEL_ASMOBJS} ${KERNEL_OBJS} kernel/symbols.o
  50. ${KCC} -T kernel/link.ld ${KCFLAGS} -nostdlib -o $@ ${KERNEL_ASMOBJS} ${KERNEL_OBJS} kernel/symbols.o -lgcc
  51. kernel/symbols.o: ${KERNEL_ASMOBJS} ${KERNEL_OBJS} util/generate_symbols.py
  52. -rm -f kernel/symbols.o
  53. ${KCC} -T kernel/link.ld ${KCFLAGS} -nostdlib -o .toaruos-kernel ${KERNEL_ASMOBJS} ${KERNEL_OBJS} -lgcc
  54. ${KNM} .toaruos-kernel -g | util/generate_symbols.py > kernel/symbols.S
  55. ${KAS} ${KASFLAGS} kernel/symbols.S -o $@
  56. -rm -f .toaruos-kernel
  57. kernel/sys/version.o: kernel/*/*.c kernel/*.c
  58. kernel/%.o: kernel/%.S
  59. ${KAS} ${ASFLAGS} $< -o $@
  60. kernel/%.o: kernel/%.c ${HEADERS}
  61. ${KCC} ${KCFLAGS} -nostdlib -g -c -o $@ $<
  62. # Modules
  63. cdrom/mod:
  64. @mkdir -p $@
  65. MODULES = $(patsubst modules/%.c,cdrom/mod/%.ko,$(wildcard modules/*.c))
  66. HEADERS = $(shell find base/usr/include/kernel -type f -name '*.h')
  67. cdrom/mod/%.ko: modules/%.c ${HEADERS} | cdrom/mod
  68. ${KCC} -T modules/link.ld -nostdlib ${KCFLAGS} -c -o $@ $<
  69. modules: ${MODULES}
  70. # Root Filesystem
  71. base/dev:
  72. mkdir -p base/dev
  73. base/tmp:
  74. mkdir -p base/tmp
  75. base/proc:
  76. mkdir -p base/proc
  77. base/bin:
  78. mkdir -p base/bin
  79. base/lib:
  80. mkdir -p base/lib
  81. cdrom/boot:
  82. mkdir -p cdrom/boot
  83. .make:
  84. mkdir -p .make
  85. dirs: base/dev base/tmp base/proc base/bin base/lib cdrom/boot .make
  86. # C Library
  87. crts: base/lib/crt0.o base/lib/crti.o base/lib/crtn.o | dirs
  88. base/lib/crt%.o: libc/crt%.s
  89. yasm -f elf -o $@ $<
  90. libc/%.o: libc/%.c
  91. $(CC) $(CFLAGS) -fPIC -c -o $@ $<
  92. base/lib/libc.a: ${LIBC_OBJS} | dirs crts
  93. $(AR) cr $@ $^
  94. base/lib/libc.so: ${LIBC_OBJS} | dirs crts
  95. $(CC) -nodefaultlibs -o $@ $(CFLAGS) -shared -fPIC $^ -lgcc
  96. # Userspace Linker/Loader
  97. base/lib/ld.so: linker/linker.c base/lib/libc.a | dirs
  98. $(CC) -static -Wl,-static $(CFLAGS) -o $@ -Os -T linker/link.ld $<
  99. # Shared Libraries
  100. .make/%.lmak: lib/%.c util/auto-dep.py | dirs
  101. util/auto-dep.py --makelib $< > $@
  102. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  103. -include ${LIBS_Y}
  104. endif
  105. # Init (static)
  106. base/bin/init: apps/init.c base/lib/libc.a | dirs
  107. $(CC) -static -Wl,-static $(CFLAGS) -o $@ $<
  108. # Userspace applications
  109. .make/%.mak: apps/%.c util/auto-dep.py | dirs
  110. util/auto-dep.py --make $< > $@
  111. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  112. -include ${APPS_Y}
  113. endif
  114. # Ramdisk
  115. cdrom/ramdisk.img: ${APPS_X} ${LIBS_X} base/lib/ld.so $(shell find base) Makefile | dirs
  116. genext2fs -B 4096 -d base -D util/devtable -U -b 4096 -N 2048 cdrom/ramdisk.img
  117. # CD image
  118. image.iso: cdrom/ramdisk.img cdrom/boot/boot.sys cdrom/kernel ${MODULES}
  119. xorriso -as mkisofs -R -J -c boot/bootcat -b boot/boot.sys -no-emul-boot -boot-load-size 20 -o image.iso cdrom
  120. # Boot loader
  121. cdrom/boot/boot.sys: boot/boot.o boot/cstuff.o boot/link.ld | cdrom/boot
  122. ${KLD} -T boot/link.ld -o $@ boot/boot.o boot/cstuff.o
  123. boot/cstuff.o: boot/cstuff.c boot/ata.h boot/atapi_imp.h boot/elf.h boot/iso9660.h boot/multiboot.h boot/text.h boot/types.h boot/util.h
  124. ${KCC} -c -Os -o $@ $<
  125. boot/boot.o: boot/boot.s
  126. yasm -f elf -o $@ $<
  127. .PHONY: clean
  128. clean:
  129. rm -f base/lib/*.so
  130. rm -f base/lib/libc.a
  131. rm -f ${APPS_X}
  132. rm -f libc/*.o libc/*/*.o
  133. rm -f image.iso
  134. rm -f cdrom/ramdisk.img
  135. rm -f cdrom/boot/boot.sys
  136. rm -f boot/*.o
  137. rm -f cdrom/kernel
  138. rm -f ${KERNEL_OBJS} ${KERNEL_ASMOBJS} kernel/symbols.o kernel/symbols.S
  139. rm -f base/lib/crt*.o
  140. rm -f ${MODULES}
  141. rm -f ${APPS_Y} ${LIBS_Y}
  142. .PHONY: run
  143. run: image.iso
  144. qemu-system-i386 -cdrom $< \
  145. -serial stdio \
  146. -m 1G \
  147. -soundhw ac97,pcspk \
  148. -enable-kvm
  149. .PHONY: virtualbox
  150. VMNAME=ToaruOS-NIH CD
  151. virtualbox: image.iso
  152. -VBoxManage unregistervm "$(VMNAME)" --delete
  153. VBoxManage createvm --name "$(VMNAME)" --ostype "Other" --register
  154. VBoxManage modifyvm "$(VMNAME)" --memory 1024 --vram 32 --audio pulse --audiocontroller ac97 --bioslogodisplaytime 1 --bioslogofadeout off --bioslogofadein off --biosbootmenu disabled
  155. VBoxManage storagectl "$(VMNAME)" --add ide --name "IDE"
  156. VBoxManage storageattach "$(VMNAME)" --storagectl "IDE" --port 0 --device 0 --medium $(shell pwd)/image.iso --type dvddrive
  157. VBoxManage setextradata "$(VMNAME)" GUI/DefaultCloseAction PowerOff
  158. VBoxManage startvm "$(VMNAME)" --type separate