Makefile 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. APPS_SH=$(patsubst apps/%.sh,%.sh,$(wildcard apps/*.sh))
  31. APPS_SH_X=$(foreach app,$(APPS_SH),base/bin/$(app))
  32. LIBS=$(patsubst lib/%.c,%,$(wildcard lib/*.c))
  33. LIBS_X=$(foreach lib,$(LIBS),base/lib/libtoaru_$(lib).so)
  34. LIBS_Y=$(foreach lib,$(LIBS),.make/$(lib).lmak)
  35. RAMDISK_FILES= ${APPS_X} ${APPS_SH_X} ${LIBS_X} base/lib/ld.so base/lib/libm.so
  36. all: image.iso
  37. # Kernel / module flags
  38. KCC = $(TARGET_TRIPLET)-gcc
  39. KAS = $(TARGET_TRIPLET)-as
  40. KLD = $(TARGET_TRIPLET)-ld
  41. KNM = $(TARGET_TRIPLET)-nm
  42. KCFLAGS = -O2 -std=c99
  43. KCFLAGS += -finline-functions -ffreestanding
  44. KCFLAGS += -Wall -Wextra -Wno-unused-function -Wno-unused-parameter -Wno-format
  45. KCFLAGS += -pedantic -fno-omit-frame-pointer
  46. KCFLAGS += -D_KERNEL_
  47. KCFLAGS += -DKERNEL_GIT_TAG=$(shell util/make-version)
  48. KASFLAGS = --32
  49. KERNEL_OBJS = $(patsubst %.c,%.o,$(wildcard kernel/*.c))
  50. KERNEL_OBJS += $(patsubst %.c,%.o,$(wildcard kernel/*/*.c))
  51. KERNEL_OBJS += $(patsubst %.c,%.o,$(wildcard kernel/*/*/*.c))
  52. KERNEL_ASMOBJS = $(filter-out kernel/symbols.o,$(patsubst %.S,%.o,$(wildcard kernel/*.S)))
  53. # Kernel
  54. fatbase/kernel: ${KERNEL_ASMOBJS} ${KERNEL_OBJS} kernel/symbols.o
  55. ${KCC} -T kernel/link.ld ${KCFLAGS} -nostdlib -o $@ ${KERNEL_ASMOBJS} ${KERNEL_OBJS} kernel/symbols.o -lgcc
  56. kernel/symbols.o: ${KERNEL_ASMOBJS} ${KERNEL_OBJS} util/generate_symbols.py
  57. -rm -f kernel/symbols.o
  58. ${KCC} -T kernel/link.ld ${KCFLAGS} -nostdlib -o .toaruos-kernel ${KERNEL_ASMOBJS} ${KERNEL_OBJS} -lgcc
  59. ${KNM} .toaruos-kernel -g | util/generate_symbols.py > kernel/symbols.S
  60. ${KAS} ${KASFLAGS} kernel/symbols.S -o $@
  61. -rm -f .toaruos-kernel
  62. kernel/sys/version.o: kernel/*/*.c kernel/*.c
  63. kernel/%.o: kernel/%.S
  64. ${KAS} ${ASFLAGS} $< -o $@
  65. kernel/%.o: kernel/%.c ${HEADERS}
  66. ${KCC} ${KCFLAGS} -nostdlib -g -c -o $@ $<
  67. # Modules
  68. fatbase/mod:
  69. @mkdir -p $@
  70. MODULES = $(patsubst modules/%.c,fatbase/mod/%.ko,$(wildcard modules/*.c))
  71. HEADERS = $(shell find base/usr/include/kernel -type f -name '*.h')
  72. fatbase/mod/%.ko: modules/%.c ${HEADERS} | fatbase/mod
  73. ${KCC} -T modules/link.ld -nostdlib ${KCFLAGS} -c -o $@ $<
  74. modules: ${MODULES}
  75. # Root Filesystem
  76. base/dev:
  77. mkdir -p $@
  78. base/tmp:
  79. mkdir -p $@
  80. base/proc:
  81. mkdir -p $@
  82. base/bin:
  83. mkdir -p $@
  84. base/lib:
  85. mkdir -p $@
  86. base/cdrom:
  87. mkdir -p $@
  88. fatbase/efi/boot:
  89. mkdir -p $@
  90. .make:
  91. mkdir -p .make
  92. dirs: base/dev base/tmp base/proc base/bin base/lib base/cdrom fatbase/efi/boot .make
  93. # C Library
  94. crts: base/lib/crt0.o base/lib/crti.o base/lib/crtn.o | dirs
  95. base/lib/crt%.o: libc/crt%.s
  96. yasm -f elf -o $@ $<
  97. libc/setjmp.o: libc/setjmp.S
  98. $(AS) -o $@ $<
  99. libc/%.o: libc/%.c
  100. $(CC) $(CFLAGS) -fPIC -c -o $@ $<
  101. base/lib/libc.a: ${LIBC_OBJS} | dirs crts
  102. $(AR) cr $@ $^
  103. base/lib/libc.so: ${LIBC_OBJS} | dirs crts
  104. $(CC) -nodefaultlibs -o $@ $(CFLAGS) -shared -fPIC $^ -lgcc
  105. base/lib/libm.so: util/lm.c | dirs crts
  106. $(CC) -nodefaultlibs -o $@ $(CFLAGS) -shared -fPIC $^ -lgcc
  107. # Userspace Linker/Loader
  108. base/lib/ld.so: linker/linker.c base/lib/libc.a | dirs
  109. $(CC) -static -Wl,-static $(CFLAGS) -o $@ -Os -T linker/link.ld $<
  110. # Shared Libraries
  111. .make/%.lmak: lib/%.c util/auto-dep.py | dirs
  112. util/auto-dep.py --makelib $< > $@
  113. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  114. -include ${LIBS_Y}
  115. endif
  116. # Init (static)
  117. base/bin/init: apps/init.c base/lib/libc.a | dirs
  118. $(CC) -static -Wl,-static $(CFLAGS) -o $@ $<
  119. fatbase/netinit: util/netinit.c base/lib/libc.a | dirs
  120. $(CC) -static -Wl,-static $(CFLAGS) -o $@ $<
  121. # Userspace applications
  122. .make/%.mak: apps/%.c util/auto-dep.py | dirs
  123. util/auto-dep.py --make $< > $@
  124. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  125. -include ${APPS_Y}
  126. endif
  127. base/bin/%.sh: apps/%.sh
  128. cp $< $@
  129. chmod +x $@
  130. # Ramdisk
  131. util/devtable: ${RAMDISK_FILES} $(shell find base) util/update-devtable.py
  132. util/update-devtable.py
  133. fatbase/ramdisk.img: ${RAMDISK_FILES} $(shell find base) Makefile util/devtable | dirs
  134. genext2fs -B 4096 -d base -D util/devtable -U -b `util/calc-size.sh` -N 2048 $@
  135. # CD image
  136. ifeq (,$(wildcard /usr/lib32/crt0-efi-ia32.o))
  137. $(error Missing GNU-EFI.)
  138. endif
  139. EFI_XORRISO=-eltorito-alt-boot -e fat.img -no-emul-boot -isohybrid-gpt-basdat
  140. EFI_BOOT=cdrom/fat.img
  141. EFI_UPDATE=util/update-extents.py
  142. image.iso: ${EFI_BOOT} cdrom/boot.sys fatbase/netinit ${MODULES} util/update-extents.py
  143. xorriso -as mkisofs -R -J -c bootcat \
  144. -b boot.sys -no-emul-boot -boot-load-size 24 \
  145. ${EFI_XORRISO} \
  146. -o image.iso cdrom
  147. ${EFI_UPDATE}
  148. # Boot loader
  149. cdrom/fat.img: fatbase/ramdisk.img ${MODULES} fatbase/kernel fatbase/netinit fatbase/efi/boot/bootia32.efi fatbase/efi/boot/bootx64.efi util/mkdisk.sh
  150. util/mkdisk.sh $@ fatbase
  151. EFI_CFLAGS=-fno-stack-protector -fpic -DEFI_PLATFORM -ffreestanding -fshort-wchar -I /usr/include/efi -mno-red-zone
  152. EFI_SECTIONS=-j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .reloc
  153. boot/efi.so: boot/cstuff.c boot/*.h
  154. $(CC) ${EFI_CFLAGS} -I /usr/include/efi/ia32 -c -o boot/efi.o $<
  155. $(LD) boot/efi.o /usr/lib32/crt0-efi-ia32.o -nostdlib -znocombreloc -T /usr/lib32/elf_ia32_efi.lds -shared -Bsymbolic -L /usr/lib32 -lefi -lgnuefi -o boot/efi.so
  156. fatbase/efi/boot/bootia32.efi: boot/efi.so
  157. objcopy ${EFI_SECTIONS} --target=efi-app-ia32 $< $@
  158. boot/efi64.so: boot/cstuff.c boot/*.h
  159. gcc ${EFI_CFLAGS} -I /usr/include/efi/x86_64 -DEFI_FUNCTION_WRAPPER -c -o boot/efi64.o $<
  160. $(LD) boot/efi64.o /usr/lib/crt0-efi-x86_64.o -nostdlib -znocombreloc -T /usr/lib/elf_x86_64_efi.lds -shared -Bsymbolic -L /usr/lib -lefi -lgnuefi -o boot/efi64.so
  161. fatbase/efi/boot/bootx64.efi: boot/efi64.so
  162. objcopy ${EFI_SECTIONS} --target=efi-app-x86_64 $< $@
  163. cdrom/boot.sys: boot/boot.o boot/cstuff.o boot/link.ld | dirs
  164. ${KLD} -T boot/link.ld -o $@ boot/boot.o boot/cstuff.o
  165. boot/cstuff.o: boot/cstuff.c boot/*.h
  166. ${KCC} -c -Os -o $@ $<
  167. boot/boot.o: boot/boot.s
  168. yasm -f elf -o $@ $<
  169. .PHONY: clean
  170. clean:
  171. rm -f base/lib/*.so
  172. rm -f base/lib/libc.a
  173. rm -f ${APPS_X} ${APPS_SH_X}
  174. rm -f libc/*.o libc/*/*.o
  175. rm -f image.iso
  176. rm -f fatbase/ramdisk.img
  177. rm -f cdrom/boot.sys
  178. rm -f boot/*.o
  179. rm -f boot/*.efi
  180. rm -f boot/*.so
  181. rm -f cdrom/fat.img cdrom/kernel cdrom/mod/* cdrom/ramdisk.img
  182. rm -f fatbase/kernel fatbase/efi/boot/bootia32.efi fatbase/efi/boot/bootx64.efi
  183. rm -f cdrom/netinit fatbase/netinit
  184. rm -f ${KERNEL_OBJS} ${KERNEL_ASMOBJS} kernel/symbols.o kernel/symbols.S
  185. rm -f base/lib/crt*.o
  186. rm -f ${MODULES}
  187. rm -f ${APPS_Y} ${LIBS_Y}
  188. ifneq (,$(findstring Microsoft,$(shell uname -r)))
  189. QEMU_ARGS=-serial mon:stdio -m 1G -rtc base=localtime -vnc :0
  190. else
  191. ifeq (,${NO_KVM})
  192. KVM=-enable-kvm
  193. else
  194. KVM=
  195. endif
  196. QEMU_ARGS=-serial mon:stdio -m 1G -soundhw ac97,pcspk ${KVM} -rtc base=localtime
  197. endif
  198. .PHONY: run
  199. run: image.iso
  200. qemu-system-i386 -cdrom $< ${QEMU_ARGS}
  201. .PHONY: fast
  202. fast: image.iso
  203. qemu-system-i386 -cdrom $< ${QEMU_ARGS} \
  204. -fw_cfg name=opt/org.toaruos.bootmode,string=normal
  205. .PHONY: headless
  206. headless: image.iso
  207. @qemu-system-i386 -cdrom $< ${QEMU_ARGS} \
  208. -nographic -no-reboot \
  209. -fw_cfg name=opt/org.toaruos.bootmode,string=headless
  210. .PHONY: shell
  211. shell: image.iso
  212. @qemu-system-i386 -cdrom $< ${QEMU_ARGS} \
  213. -nographic -no-reboot \
  214. -fw_cfg name=opt/org.toaruos.bootmode,string=headless \
  215. -fw_cfg name=opt/org.toaruos.forceuser,string=local
  216. .PHONY: efi64
  217. efi64: image.iso
  218. qemu-system-x86_64 -cdrom $< ${QEMU_ARGS} \
  219. -bios /usr/share/qemu/OVMF.fd
  220. VMNAME=ToaruOS-NIH CD
  221. define virtualbox-runner =
  222. .PHONY: $1
  223. $1: image.iso
  224. -VBoxManage unregistervm "$(VMNAME)" --delete
  225. VBoxManage createvm --name "$(VMNAME)" --ostype $2 --register
  226. VBoxManage modifyvm "$(VMNAME)" --memory 1024 --vram 32 --audio pulse --audiocontroller ac97 --bioslogodisplaytime 1 --bioslogofadeout off --bioslogofadein off --biosbootmenu disabled $3
  227. VBoxManage storagectl "$(VMNAME)" --add ide --name "IDE"
  228. VBoxManage storageattach "$(VMNAME)" --storagectl "IDE" --port 0 --device 0 --medium $$(shell pwd)/image.iso --type dvddrive
  229. VBoxManage setextradata "$(VMNAME)" GUI/DefaultCloseAction PowerOff
  230. VBoxManage startvm "$(VMNAME)" --type separate
  231. endef
  232. $(eval $(call virtualbox-runner,virtualbox,"Other",))
  233. $(eval $(call virtualbox-runner,virtualbox-efi,"Other",--firmware efi))
  234. $(eval $(call virtualbox-runner,virtualbox-efi64,"Other_64",--firmware efi))
  235. # Optional Extensions
  236. #
  237. # These optional extension libraries require third-party components to build,
  238. # but allow the native applications to make use of functionality such as
  239. # TrueType fonts or PNG images. You must have the necessary elements to build
  240. # these already installed into your sysroot for this to work.
  241. EXT_LIBS=$(patsubst ext/%.c,%,$(wildcard ext/*.c))
  242. EXT_LIBS_X=$(foreach lib,$(EXT_LIBS),base/lib/libtoaru_$(lib).so)
  243. EXT_LIBS_Y=$(foreach lib,$(EXT_LIBS),.make/$(lib).elmak)
  244. .make/%.elmak: ext/%.c util/auto-dep.py | dirs
  245. util/auto-dep.py --makelib $< > $@
  246. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  247. -include ${EXT_LIBS_Y}
  248. endif
  249. ext-freetype: base/lib/libtoaru_ext_freetype_fonts.so