Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. TOOLCHAIN=util
  2. BASE=base
  3. export PATH := $(shell $(TOOLCHAIN)/activate.sh)
  4. include build/x86_64.mk
  5. CC = ${TARGET}-gcc
  6. NM = ${TARGET}-nm
  7. CXX= ${TARGET}-g++
  8. AR = ${TARGET}-ar
  9. AS = ${TARGET}-as
  10. OC = ${TARGET}-objcopy
  11. KERNEL_CFLAGS = -ffreestanding -O2 -std=gnu11 -g -static
  12. KERNEL_CFLAGS += -Wall -Wextra -Wno-unused-function -Wno-unused-parameter
  13. KERNEL_CFLAGS += -pedantic -Wwrite-strings ${ARCH_KERNEL_CFLAGS}
  14. # Defined constants for the kernel
  15. KERNEL_CFLAGS += -D_KERNEL_ -DKERNEL_ARCH=${ARCH}
  16. KERNEL_CFLAGS += -DKERNEL_GIT_TAG=`util/make-version`
  17. KERNEL_OBJS = $(patsubst %.c,%.o,$(wildcard kernel/*.c))
  18. KERNEL_OBJS += $(patsubst %.c,%.o,$(wildcard kernel/*/*.c))
  19. KERNEL_OBJS += $(patsubst %.c,%.o,$(wildcard kernel/arch/${ARCH}/*.c))
  20. KERNEL_ASMOBJS = $(filter-out kernel/symbols.o,$(patsubst %.S,%.o,$(wildcard kernel/arch/${ARCH}/*.S)))
  21. KERNEL_SOURCES = $(wildcard kernel/*.c) $(wildcard kernel/*/*.c) $(wildcard kernel/${ARCH}/*/*.c)
  22. KERNEL_SOURCES += $(wildcard kernel/arch/${ARCH}/*.S)
  23. MODULES = $(patsubst modules/%.c,$(BASE)/mod/%.ko,$(wildcard modules/*.c))
  24. # Configs you can override.
  25. SMP ?= 2
  26. RAM ?= 3G
  27. EXTRA_ARGS ?=
  28. EMU_KVM ?= -enable-kvm
  29. EMU_MACH ?= q35
  30. EMU = qemu-system-x86_64
  31. EMU_ARGS = -M q35
  32. EMU_ARGS += -m $(RAM)
  33. EMU_ARGS += -smp $(SMP)
  34. EMU_ARGS += ${EMU_KVM}
  35. EMU_ARGS += -no-reboot
  36. EMU_ARGS += -serial mon:stdio
  37. EMU_ARGS += -rtc base=utc
  38. EMU_ARGS += -soundhw pcspk,ac97
  39. # Configures two network devices on the same network
  40. EMU_ARGS += -net user
  41. EMU_ARGS += -netdev hubport,id=u1,hubid=0, -device e1000e,netdev=u1 -object filter-dump,id=f1,netdev=u1,file=qemu-e1000e.pcap
  42. #EMU_ARGS += -netdev hubport,id=u2,hubid=0, -device e1000e,netdev=u2
  43. # Add an XHCI tablet
  44. EMU_ARGS += -device qemu-xhci -device usb-tablet
  45. APPS=$(patsubst apps/%.c,%,$(wildcard apps/*.c)) $(patsubst apps/%.c++,%,$(wildcard apps/*.c++))
  46. APPS_X=$(foreach app,$(APPS),$(BASE)/bin/$(app))
  47. APPS_Y=$(foreach app,$(APPS),.make/$(app).mak)
  48. APPS_SH=$(patsubst apps/%.sh,%.sh,$(wildcard apps/*.sh))
  49. APPS_SH_X=$(foreach app,$(APPS_SH),$(BASE)/bin/$(app))
  50. APPS_KRK=$(patsubst apps/%.krk,%.krk,$(wildcard apps/*.krk))
  51. APPS_KRK_X=$(foreach app,$(APPS_KRK),$(BASE)/bin/$(app))
  52. LIBS=$(patsubst lib/%.c,%,$(wildcard lib/*.c))
  53. LIBS_X=$(foreach lib,$(LIBS),$(BASE)/lib/libtoaru_$(lib).so)
  54. LIBS_Y=$(foreach lib,$(LIBS),.make/$(lib).lmak)
  55. KRK_MODS = $(patsubst kuroko/src/modules/module_%.c,$(BASE)/lib/kuroko/%.so,$(wildcard kuroko/src/modules/module_*.c))
  56. KRK_MODS += $(patsubst kuroko/modules/%,$(BASE)/lib/kuroko/%,$(wildcard kuroko/modules/*.krk kuroko/modules/*/*/.krk kuroko/modules/*/*/*.krk))
  57. KRK_MODS_X = $(patsubst lib/kuroko/%.c,$(BASE)/lib/kuroko/%.so,$(wildcard lib/kuroko/*.c))
  58. KRK_MODS_Y = $(patsubst lib/kuroko/%.c,.make/%.kmak,$(wildcard lib/kuroko/*.c))
  59. CFLAGS= -O2 -std=gnu11 -I. -Iapps -fplan9-extensions -Wall -Wextra -Wno-unused-parameter
  60. LIBC_OBJS = $(patsubst %.c,%.o,$(wildcard libc/*.c))
  61. LIBC_OBJS += $(patsubst %.c,%.o,$(wildcard libc/*/*.c))
  62. LIBC_OBJS += $(patsubst %.c,%.o,$(wildcard libc/arch/${ARCH}/*.c))
  63. GCC_SHARED = $(BASE)/usr/lib/libgcc_s.so.1 $(BASE)/usr/lib/libgcc_s.so
  64. LIBSTDCXX = $(BASE)/usr/lib/libstdc++.so.6.0.28 $(BASE)/usr/lib/libstdc++.so.6 $(BASE)/usr/lib/libstdc++.so
  65. CRTS = $(BASE)/lib/crt0.o $(BASE)/lib/crti.o $(BASE)/lib/crtn.o
  66. LC = $(BASE)/lib/libc.so $(GCC_SHARED) $(LIBSTDCXX)
  67. .PHONY: all system clean run shell
  68. all: system
  69. system: image.iso
  70. $(BASE)/mod/%.ko: modules/%.c | dirs
  71. ${CC} -c ${KERNEL_CFLAGS} -mcmodel=large -o $@ $<
  72. ramdisk.igz: $(wildcard $(BASE)/* $(BASE)/*/* $(BASE)/*/*/* $(BASE)/*/*/*/* $(BASE)/*/*/*/*/*) $(APPS_X) $(LIBS_X) $(KRK_MODS_X) $(BASE)/bin/kuroko $(BASE)/lib/ld.so $(APPS_KRK_X) $(KRK_MODS) $(APPS_SH_X) $(MODULES)
  73. python3 util/createramdisk.py
  74. KRK_SRC = $(sort $(wildcard kuroko/src/*.c))
  75. $(BASE)/bin/kuroko: $(KRK_SRC) $(CRTS) lib/rline.c | $(LC)
  76. $(CC) -O2 -g -o $@ -Wl,--export-dynamic -Ikuroko/src $(KRK_SRC) lib/rline.c
  77. $(BASE)/lib/kuroko/%.so: kuroko/src/modules/module_%.c| dirs $(LC)
  78. $(CC) -O2 -shared -fPIC -Ikuroko/src -o $@ $<
  79. $(BASE)/lib/kuroko/%.krk: kuroko/modules/%.krk | dirs
  80. mkdir -p $(dir $@)
  81. cp $< $@
  82. $(BASE)/lib/libkuroko.so: $(KRK_SRC) | $(LC)
  83. $(CC) -O2 -shared -fPIC -Ikuroko/src -o $@ $(filter-out kuroko/src/kuroko.c,$(KRK_SRC))
  84. $(BASE)/lib/ld.so: linker/linker.c $(BASE)/lib/libc.a | dirs $(LC)
  85. $(CC) -g -static -Wl,-static $(CFLAGS) -z max-page-size=0x1000 -o $@ -Os -T linker/link.ld $<
  86. run: system
  87. ${EMU} ${EMU_ARGS} -cdrom image.iso
  88. fast: system
  89. ${EMU} ${EMU_ARGS} -cdrom image.iso \
  90. -fw_cfg name=opt/org.toaruos.bootmode,string=normal \
  91. run-vga: system
  92. ${EMU} ${EMU_ARGS} -cdrom image.iso \
  93. -fw_cfg name=opt/org.toaruos.bootmode,string=vga \
  94. test: system
  95. ${EMU} -M ${EMU_MACH} -m $(RAM) -smp $(SMP) ${EMU_KVM} -kernel misaka-kernel -initrd ramdisk.igz,util/init.krk -append "root=/dev/ram0 init=/dev/ram1" \
  96. -nographic -no-reboot -audiodev none,id=id -serial null -serial mon:stdio \
  97. -device qemu-xhci -device usb-tablet -trace "usb*"
  98. shell: system
  99. ${EMU} -M ${EMU_MACH} -m $(RAM) -smp $(SMP) ${EMU_KVM} -cdrom image.iso \
  100. -nographic -no-reboot -audiodev none,id=id -serial null -serial mon:stdio \
  101. -fw_cfg name=opt/org.toaruos.gettyargs,string="-a local /dev/ttyS1" \
  102. -fw_cfg name=opt/org.toaruos.bootmode,string=headless \
  103. -fw_cfg name=opt/org.toaruos.term,string=${TERM}
  104. misaka-kernel: ${KERNEL_ASMOBJS} ${KERNEL_OBJS} kernel/symbols.o
  105. ${CC} -g -T kernel/arch/${ARCH}/link.ld ${KERNEL_CFLAGS} -o $@.64 ${KERNEL_ASMOBJS} ${KERNEL_OBJS} kernel/symbols.o -lgcc
  106. ${OC} --strip-debug -I elf64-x86-64 -O elf32-i386 $@.64 $@
  107. kernel/sys/version.o: ${KERNEL_SOURCES}
  108. kernel/symbols.o: ${KERNEL_ASMOBJS} ${KERNEL_OBJS} util/gensym.krk
  109. -rm -f kernel/symbols.o
  110. ${CC} -T kernel/arch/${ARCH}/link.ld ${KERNEL_CFLAGS} -o misaka-kernel.64 ${KERNEL_ASMOBJS} ${KERNEL_OBJS} -lgcc
  111. ${NM} misaka-kernel.64 -g | kuroko util/gensym.krk > kernel/symbols.S
  112. ${CC} -c kernel/symbols.S -o $@
  113. kernel/%.o: kernel/%.S
  114. ${CC} -c $< -o $@
  115. HEADERS = $(wildcard base/usr/include/kernel/*.h) $(wildcard base/usr/include/kernel/*/*.h)
  116. kernel/%.o: kernel/%.c ${HEADERS}
  117. ${CC} ${KERNEL_CFLAGS} -nostdlib -g -Iinclude -c -o $@ $<
  118. clean:
  119. -rm -f ${KERNEL_ASMOBJS}
  120. -rm -f ${KERNEL_OBJS}
  121. -rm -f kernel/symbols.o misaka-kernel misaka-kernel.64
  122. -rm -f ramdisk.tar ramdisk.igz
  123. -rm -f $(APPS_Y) $(LIBS_Y) $(KRK_MODS_Y) $(KRK_MODS)
  124. -rm -f $(APPS_X) $(LIBS_X) $(KRK_MODS_X) $(APPS_KRK_X) $(APPS_SH_X)
  125. -rm -f $(BASE)/lib/crt0.o $(BASE)/lib/crti.o $(BASE)/lib/crtn.o
  126. -rm -f $(BASE)/lib/libc.so $(BASE)/lib/libc.a
  127. -rm -f $(LIBC_OBJS) $(BASE)/lib/ld.so $(BASE)/lib/libkuroko.so $(BASE)/lib/libm.so
  128. -rm -f $(BASE)/bin/kuroko
  129. -rm -f $(GCC_SHARED) $(LIBSTDCXX)
  130. -rm -f boot/efi/*.o boot/bios/*.o
  131. libc/%.o: libc/%.c base/usr/include/syscall.h
  132. $(CC) -O2 -std=gnu11 -Wall -Wextra -Wno-unused-parameter -fPIC -c -o $@ $<
  133. .PHONY: libc
  134. libc: $(BASE)/lib/libc.a $(BASE)/lib/libc.so
  135. $(BASE)/lib/libc.a: ${LIBC_OBJS} $(CRTS)
  136. $(AR) cr $@ $(LIBC_OBJS)
  137. $(BASE)/lib/libc.so: ${LIBC_OBJS} | $(CRTS)
  138. ${CC} -nodefaultlibs -shared -fPIC -o $@ $^
  139. $(BASE)/lib/crt%.o: libc/arch/${ARCH}/crt%.S
  140. ${AS} -o $@ $<
  141. $(BASE)/usr/lib/%: $(TOOLCHAIN)/local/${TARGET}/lib/% | dirs
  142. cp -a $< $@
  143. -strip $@
  144. $(BASE)/lib/libm.so: util/libm.c
  145. $(CC) -shared -nostdlib -fPIC -o $@ $<
  146. $(BASE)/dev:
  147. mkdir -p $@
  148. $(BASE)/tmp:
  149. mkdir -p $@
  150. $(BASE)/proc:
  151. mkdir -p $@
  152. $(BASE)/bin:
  153. mkdir -p $@
  154. $(BASE)/lib:
  155. mkdir -p $@
  156. $(BASE)/cdrom:
  157. mkdir -p $@
  158. $(BASE)/var:
  159. mkdir -p $@
  160. $(BASE)/mod:
  161. mkdir -p $@
  162. $(BASE)/lib/kuroko:
  163. mkdir -p $@
  164. $(BASE)/usr/lib:
  165. mkdir -p $@
  166. $(BASE)/usr/bin:
  167. mkdir -p $@
  168. boot/efi:
  169. mkdir -p $@
  170. boot/bios:
  171. mkdir -p $@
  172. fatbase/efi/boot:
  173. mkdir -p $@
  174. cdrom:
  175. mkdir -p $@
  176. .make:
  177. mkdir -p .make
  178. dirs: $(BASE)/dev $(BASE)/tmp $(BASE)/proc $(BASE)/bin $(BASE)/lib $(BASE)/cdrom $(BASE)/usr/lib $(BASE)/usr/bin $(BASE)/lib/kuroko cdrom $(BASE)/var fatbase/efi/boot .make $(BASE)/mod boot/efi boot/bios
  179. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  180. -include ${APPS_Y}
  181. -include ${LIBS_Y}
  182. -include ${KRK_MODS_Y}
  183. endif
  184. .make/%.lmak: lib/%.c util/auto-dep.krk | dirs $(CRTS)
  185. kuroko util/auto-dep.krk --makelib $< > $@
  186. .make/%.mak: apps/%.c util/auto-dep.krk | dirs $(CRTS)
  187. kuroko util/auto-dep.krk --make $< > $@
  188. .make/%.mak: apps/%.c++ util/auto-dep.krk | dirs $(CRTS)
  189. kuroko util/auto-dep.krk --make $< > $@
  190. .make/%.kmak: lib/kuroko/%.c util/auto-dep.krk | dirs
  191. kuroko util/auto-dep.krk --makekurokomod $< > $@
  192. $(BASE)/bin/%.sh: apps/%.sh
  193. cp $< $@
  194. chmod +x $@
  195. $(BASE)/bin/%.krk: apps/%.krk
  196. cp $< $@
  197. chmod +x $@
  198. .PHONY: libs
  199. libs: $(LIBS_X)
  200. .PHONY: apps
  201. apps: $(APPS_X)
  202. .PHONY: libstdcxx
  203. libstdcxx: $(LIBSTDCXX)
  204. util/local/${TARGET}/lib/libstdc++.so.6.0.28: | $(BASE)/lib/libm.so
  205. cd util/build/gcc && make all-target-libstdc++-v3 && make install-target-libstdc++-v3
  206. SOURCE_FILES = $(wildcard kernel/*.c kernel/*/*.c kernel/*/*/*.c kernel/*/*/*/*.c)
  207. SOURCE_FILES += $(wildcard apps/*.c linker/*.c libc/*.c libc/*/*.c lib/*.c lib/kuroko/*.c)
  208. SOURCE_FILES += $(wildcard kuroko/src/*.c kuroko/src/*.h kuroko/src/*/*.c kuroko/src/*/*.h)
  209. SOURCE_FILES += $(wildcard $(BASE)/usr/include/*.h $(BASE)/usr/include/*/*.h $(BASE)/usr/include/*/*/*.h)
  210. tags: $(SOURCE_FILES)
  211. ctags -f tags $(SOURCE_FILES)
  212. # Loader stuff, legacy CDs
  213. fatbase/ramdisk.igz: ramdisk.igz
  214. cp $< $@
  215. fatbase/kernel: misaka-kernel
  216. cp $< $@
  217. strip $@
  218. cdrom/fat.img: fatbase/ramdisk.igz fatbase/kernel fatbase/efi/boot/bootx64.efi util/mkdisk.sh | dirs
  219. util/mkdisk.sh $@ fatbase
  220. cdrom/boot.sys: boot/bios/boot.o $(patsubst boot/%.c,boot/bios/%.o,$(wildcard boot/*.c)) boot/link.ld | dirs
  221. ${LD} -melf_i386 -T boot/link.ld -o $@ boot/bios/boot.o $(patsubst boot/%.c,boot/bios/%.o,$(wildcard boot/*.c))
  222. boot/bios/%.o: boot/%.c boot/*.h | dirs
  223. ${CC} -m32 -c -Os -fno-pic -fno-pie -fno-strict-aliasing -finline-functions -ffreestanding -mgeneral-regs-only -o $@ $<
  224. boot/bios/boot.o: boot/boot.S | dirs
  225. ${AS} --32 -o $@ $<
  226. EFI_CFLAGS=-fno-stack-protector -fpic -DEFI_PLATFORM -ffreestanding -fshort-wchar -I /usr/include/efi -mno-red-zone
  227. EFI_SECTIONS=-j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .reloc
  228. EFI_LINK=/usr/lib/crt0-efi-x86_64.o -nostdlib -znocombreloc -T /usr/lib/elf_x86_64_efi.lds -shared -Bsymbolic -L /usr/lib -lefi -lgnuefi
  229. boot/efi/%.o: boot/%.c boot/*.h | dirs
  230. $(CC) ${EFI_CFLAGS} -I /usr/include/efi/x86_64 -DEFI_FUNCTION_WRAPPER -c -o $@ $<
  231. boot/efi64.so: $(patsubst boot/%.c,boot/efi/%.o,$(wildcard boot/*.c)) boot/*.h
  232. $(LD) $(patsubst boot/%.c,boot/efi/%.o,$(wildcard boot/*.c)) ${EFI_LINK} -o $@
  233. fatbase/efi/boot/bootx64.efi: boot/efi64.so
  234. mkdir -p fatbase/efi/boot
  235. objcopy ${EFI_SECTIONS} --target=efi-app-x86_64 $< $@
  236. BUILD_KRK=$(TOOLCHAIN)/local/bin/kuroko
  237. $(TOOLCHAIN)/local/bin/kuroko: kuroko/src/*.c
  238. cc -Ikuroko/src -DNO_RLINE -DSTATIC_ONLY -DKRK_DISABLE_THREADS -o "${TOOLCHAIN}/local/bin/kuroko" kuroko/src/*.c
  239. image.iso: cdrom/fat.img cdrom/boot.sys boot/mbr.S util/update-extents.krk | $(BUILD_KRK)
  240. xorriso -as mkisofs -R -J -c bootcat \
  241. -b boot.sys -no-emul-boot -boot-load-size full \
  242. -eltorito-alt-boot -e fat.img -no-emul-boot -isohybrid-gpt-basdat \
  243. -o image.iso cdrom
  244. ${AS} --32 $$(kuroko util/make_mbr.krk) -o boot/mbr.o boot/mbr.S
  245. ${LD} -melf_i386 -T boot/link.ld -o boot/mbr.sys boot/mbr.o
  246. tail -c +513 image.iso > image.dat
  247. cat boot/mbr.sys image.dat > image.iso
  248. rm image.dat
  249. kuroko util/update-extents.krk