Makefile 6.0 KB

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