Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. all: image.iso
  18. TARGET_TRIPLET=i686-pc-toaru
  19. # Userspace flags
  20. CC=$(TARGET_TRIPLET)-gcc
  21. AR=$(TARGET_TRIPLET)-ar
  22. AS=$(TARGET_TRIPLET)-as
  23. CFLAGS= -O3 -g -std=gnu99 -I. -Iapps -pipe -mmmx -msse -msse2 -fplan9-extensions -Wall -Wextra -Wno-unused-parameter
  24. ##
  25. # C library objects from libc/ C sources (and setjmp, which is assembly)
  26. LIBC_OBJS = $(patsubst %.c,%.o,$(wildcard libc/*.c))
  27. LIBC_OBJS += $(patsubst %.c,%.o,$(wildcard libc/*/*.c))
  28. LIBC_OBJS += libc/setjmp.o
  29. LC=base/lib/libc.so
  30. ##
  31. # APPS = C sources from apps/
  32. # APPS_X = binaries
  33. # APPS_Y = generated makefiles for binaries (except init)
  34. # APPS_SH = shell scripts to copy to base/bin/ and mark executable
  35. # APPS_SH_X = destinations for shell scripts
  36. APPS=$(patsubst apps/%.c,%,$(wildcard apps/*.c))
  37. APPS_X=$(foreach app,$(APPS),base/bin/$(app))
  38. APPS_Y=$(foreach app,$(filter-out init,$(APPS)),.make/$(app).mak)
  39. APPS_SH=$(patsubst apps/%.sh,%.sh,$(wildcard apps/*.sh))
  40. APPS_SH_X=$(foreach app,$(APPS_SH),base/bin/$(app))
  41. ##
  42. # LIBS = C sources from lib/
  43. # LIBS_X = Shared libraries (.so)
  44. # LIBS_Y = Generated makefiles for libraries
  45. LIBS=$(patsubst lib/%.c,%,$(wildcard lib/*.c))
  46. LIBS_X=$(foreach lib,$(LIBS),base/lib/libtoaru_$(lib).so)
  47. LIBS_Y=$(foreach lib,$(LIBS),.make/$(lib).lmak)
  48. ##
  49. # Files that must be present in the ramdisk (apps, libraries)
  50. RAMDISK_FILES= ${APPS_X} ${APPS_SH_X} ${LIBS_X} base/lib/ld.so base/lib/libm.so
  51. # Kernel / module flags
  52. KCC = $(TARGET_TRIPLET)-gcc
  53. KAS = $(TARGET_TRIPLET)-as
  54. KLD = $(TARGET_TRIPLET)-ld
  55. KNM = $(TARGET_TRIPLET)-nm
  56. KCFLAGS = -O2 -std=c99
  57. KCFLAGS += -finline-functions -ffreestanding
  58. KCFLAGS += -Wall -Wextra -Wno-unused-function -Wno-unused-parameter -Wno-format
  59. KCFLAGS += -pedantic -fno-omit-frame-pointer
  60. KCFLAGS += -D_KERNEL_
  61. KCFLAGS += -DKERNEL_GIT_TAG=$(shell util/make-version)
  62. KASFLAGS = --32
  63. ##
  64. # Kernel objects from kernel/ C sources
  65. KERNEL_OBJS = $(patsubst %.c,%.o,$(wildcard kernel/*.c))
  66. KERNEL_OBJS += $(patsubst %.c,%.o,$(wildcard kernel/*/*.c))
  67. KERNEL_OBJS += $(patsubst %.c,%.o,$(wildcard kernel/*/*/*.c))
  68. ##
  69. # Kernel objects from kernel/ assembly sources
  70. KERNEL_ASMOBJS = $(filter-out kernel/symbols.o,$(patsubst %.S,%.o,$(wildcard kernel/*.S)))
  71. # Kernel
  72. fatbase/kernel: ${KERNEL_ASMOBJS} ${KERNEL_OBJS} kernel/symbols.o
  73. ${KCC} -T kernel/link.ld ${KCFLAGS} -nostdlib -o $@ ${KERNEL_ASMOBJS} ${KERNEL_OBJS} kernel/symbols.o -lgcc
  74. ##
  75. # Symbol table for the kernel. Instead of relying on getting
  76. # the symbol table from our bootloader (eg. through ELF
  77. # headers provided via multiboot structure), we have a dedicated
  78. # object that build with all the symbols. This allows us to
  79. # build the kernel as a flat binary or load it with less-capable
  80. # multiboot loaders and still get symbols, which we need to
  81. # load kernel modules and link them properly.
  82. kernel/symbols.o: ${KERNEL_ASMOBJS} ${KERNEL_OBJS} util/generate_symbols.py
  83. -rm -f kernel/symbols.o
  84. ${KCC} -T kernel/link.ld ${KCFLAGS} -nostdlib -o .toaruos-kernel ${KERNEL_ASMOBJS} ${KERNEL_OBJS} -lgcc
  85. ${KNM} .toaruos-kernel -g | util/generate_symbols.py > kernel/symbols.S
  86. ${KAS} ${KASFLAGS} kernel/symbols.S -o $@
  87. -rm -f .toaruos-kernel
  88. ##
  89. # version.o should be rebuilt whenever the kernel changes
  90. # in order to get fresh git commit hash information.
  91. kernel/sys/version.o: kernel/*/*.c kernel/*.c
  92. kernel/%.o: kernel/%.S
  93. ${KAS} ${ASFLAGS} $< -o $@
  94. kernel/%.o: kernel/%.c ${HEADERS}
  95. ${KCC} ${KCFLAGS} -nostdlib -g -c -o $@ $<
  96. # Modules
  97. fatbase/mod:
  98. @mkdir -p $@
  99. ##
  100. # Modules need to be installed on the boot image
  101. MODULES = $(patsubst modules/%.c,fatbase/mod/%.ko,$(wildcard modules/*.c))
  102. HEADERS = $(shell find base/usr/include/kernel -type f -name '*.h')
  103. fatbase/mod/%.ko: modules/%.c ${HEADERS} | fatbase/mod
  104. ${KCC} -T modules/link.ld -nostdlib ${KCFLAGS} -c -o $@ $<
  105. modules: ${MODULES}
  106. # Root Filesystem
  107. base/dev:
  108. mkdir -p $@
  109. base/tmp:
  110. mkdir -p $@
  111. base/proc:
  112. mkdir -p $@
  113. base/bin:
  114. mkdir -p $@
  115. base/lib:
  116. mkdir -p $@
  117. base/cdrom:
  118. mkdir -p $@
  119. fatbase/efi/boot:
  120. mkdir -p $@
  121. .make:
  122. mkdir -p .make
  123. dirs: base/dev base/tmp base/proc base/bin base/lib base/cdrom fatbase/efi/boot .make
  124. # C Library
  125. crts: base/lib/crt0.o base/lib/crti.o base/lib/crtn.o | dirs
  126. base/lib/crt%.o: libc/crt%.s
  127. yasm -f elf -o $@ $<
  128. libc/setjmp.o: libc/setjmp.S
  129. $(AS) -o $@ $<
  130. libc/%.o: libc/%.c
  131. $(CC) $(CFLAGS) -fPIC -c -o $@ $<
  132. base/lib/libc.a: ${LIBC_OBJS} | dirs crts
  133. $(AR) cr $@ $^
  134. base/lib/libc.so: ${LIBC_OBJS} | dirs crts
  135. $(CC) -nodefaultlibs -o $@ $(CFLAGS) -shared -fPIC $^ -lgcc
  136. base/lib/libm.so: util/lm.c | dirs crts
  137. $(CC) -nodefaultlibs -o $@ $(CFLAGS) -shared -fPIC $^ -lgcc
  138. # Userspace Linker/Loader
  139. base/lib/ld.so: linker/linker.c base/lib/libc.a | dirs
  140. $(CC) -static -Wl,-static $(CFLAGS) -o $@ -Os -T linker/link.ld $<
  141. # Shared Libraries
  142. .make/%.lmak: lib/%.c util/auto-dep.py | dirs
  143. util/auto-dep.py --makelib $< > $@
  144. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  145. -include ${LIBS_Y}
  146. endif
  147. # Init (static)
  148. base/bin/init: apps/init.c base/lib/libc.a | dirs
  149. $(CC) -static -Wl,-static $(CFLAGS) -o $@ $<
  150. fatbase/netinit: util/netinit.c base/lib/libc.a | dirs
  151. $(CC) -static -Wl,-static $(CFLAGS) -o $@ $<
  152. # Userspace applications
  153. .make/%.mak: apps/%.c util/auto-dep.py | dirs
  154. util/auto-dep.py --make $< > $@
  155. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  156. -include ${APPS_Y}
  157. endif
  158. base/bin/%.sh: apps/%.sh
  159. cp $< $@
  160. chmod +x $@
  161. # Ramdisk
  162. util/devtable: ${RAMDISK_FILES} $(shell find base) util/update-devtable.py
  163. util/update-devtable.py
  164. fatbase/ramdisk.img: ${RAMDISK_FILES} $(shell find base) Makefile util/devtable | dirs
  165. genext2fs -B 4096 -d base -D util/devtable -U -b `util/calc-size.sh` -N 2048 $@
  166. # CD image
  167. ifeq (,$(wildcard /usr/lib32/crt0-efi-ia32.o))
  168. $(error Missing GNU-EFI.)
  169. endif
  170. EFI_XORRISO=-eltorito-alt-boot -e fat.img -no-emul-boot -isohybrid-gpt-basdat
  171. EFI_BOOT=cdrom/fat.img
  172. EFI_UPDATE=util/update-extents.py
  173. image.iso: ${EFI_BOOT} cdrom/boot.sys fatbase/netinit ${MODULES} util/update-extents.py
  174. xorriso -as mkisofs -R -J -c bootcat \
  175. -b boot.sys -no-emul-boot -boot-load-size 24 \
  176. ${EFI_XORRISO} \
  177. -o image.iso cdrom
  178. ${EFI_UPDATE}
  179. # Boot loader
  180. ##
  181. # FAT EFI payload
  182. # This is the filesystem the EFI loaders see, so it must contain
  183. # the kernel, modules, and ramdisk, plus anything else we want
  184. # available to the bootloader (eg., netinit).
  185. cdrom/fat.img: fatbase/ramdisk.img ${MODULES} fatbase/kernel fatbase/netinit fatbase/efi/boot/bootia32.efi fatbase/efi/boot/bootx64.efi util/mkdisk.sh
  186. util/mkdisk.sh $@ fatbase
  187. ##
  188. # For EFI, we build two laoders: ia32 and x64
  189. # We build them as ELF shared objects and the use objcopy to convert
  190. # them to PE executables / DLLs (as expected by EFI).
  191. EFI_CFLAGS=-fno-stack-protector -fpic -DEFI_PLATFORM -ffreestanding -fshort-wchar -I /usr/include/efi -mno-red-zone
  192. EFI_SECTIONS=-j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .reloc
  193. # ia32
  194. boot/efi.so: boot/cstuff.c boot/*.h
  195. $(CC) ${EFI_CFLAGS} -I /usr/include/efi/ia32 -c -o boot/efi.o $<
  196. $(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
  197. fatbase/efi/boot/bootia32.efi: boot/efi.so
  198. objcopy ${EFI_SECTIONS} --target=efi-app-ia32 $< $@
  199. # x64
  200. boot/efi64.so: boot/cstuff.c boot/*.h
  201. gcc ${EFI_CFLAGS} -I /usr/include/efi/x86_64 -DEFI_FUNCTION_WRAPPER -c -o boot/efi64.o $<
  202. $(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
  203. fatbase/efi/boot/bootx64.efi: boot/efi64.so
  204. objcopy ${EFI_SECTIONS} --target=efi-app-x86_64 $< $@
  205. # BIOS loader
  206. cdrom/boot.sys: boot/boot.o boot/cstuff.o boot/link.ld | dirs
  207. ${KLD} -T boot/link.ld -o $@ boot/boot.o boot/cstuff.o
  208. boot/cstuff.o: boot/cstuff.c boot/*.h
  209. ${KCC} -c -Os -o $@ $<
  210. boot/boot.o: boot/boot.s
  211. yasm -f elf -o $@ $<
  212. .PHONY: clean
  213. clean:
  214. rm -f base/lib/*.so
  215. rm -f base/lib/libc.a
  216. rm -f ${APPS_X} ${APPS_SH_X}
  217. rm -f libc/*.o libc/*/*.o
  218. rm -f image.iso
  219. rm -f fatbase/ramdisk.img
  220. rm -f cdrom/boot.sys
  221. rm -f boot/*.o
  222. rm -f boot/*.efi
  223. rm -f boot/*.so
  224. rm -f cdrom/fat.img cdrom/kernel cdrom/mod/* cdrom/ramdisk.img
  225. rm -f fatbase/kernel fatbase/efi/boot/bootia32.efi fatbase/efi/boot/bootx64.efi
  226. rm -f cdrom/netinit fatbase/netinit
  227. rm -f ${KERNEL_OBJS} ${KERNEL_ASMOBJS} kernel/symbols.o kernel/symbols.S
  228. rm -f base/lib/crt*.o
  229. rm -f ${MODULES}
  230. rm -f ${APPS_Y} ${LIBS_Y} ${EXT_LIBS_Y}
  231. ifneq (,$(findstring Microsoft,$(shell uname -r)))
  232. QEMU_ARGS=-serial mon:stdio -m 1G -rtc base=localtime -vnc :0
  233. else
  234. ifeq (,${NO_KVM})
  235. KVM=-enable-kvm
  236. else
  237. KVM=
  238. endif
  239. QEMU_ARGS=-serial mon:stdio -m 1G -soundhw ac97,pcspk ${KVM} -rtc base=localtime
  240. endif
  241. .PHONY: run
  242. run: image.iso
  243. qemu-system-i386 -cdrom $< ${QEMU_ARGS}
  244. .PHONY: fast
  245. fast: image.iso
  246. qemu-system-i386 -cdrom $< ${QEMU_ARGS} \
  247. -fw_cfg name=opt/org.toaruos.bootmode,string=normal
  248. .PHONY: headless
  249. headless: image.iso
  250. @qemu-system-i386 -cdrom $< ${QEMU_ARGS} \
  251. -nographic -no-reboot \
  252. -fw_cfg name=opt/org.toaruos.bootmode,string=headless
  253. .PHONY: shell
  254. shell: image.iso
  255. @qemu-system-i386 -cdrom $< ${QEMU_ARGS} \
  256. -nographic -no-reboot \
  257. -fw_cfg name=opt/org.toaruos.bootmode,string=headless \
  258. -fw_cfg name=opt/org.toaruos.forceuser,string=local
  259. .PHONY: efi64
  260. efi64: image.iso
  261. qemu-system-x86_64 -cdrom $< ${QEMU_ARGS} \
  262. -bios /usr/share/qemu/OVMF.fd
  263. VMNAME=ToaruOS CD
  264. define virtualbox-runner =
  265. .PHONY: $1
  266. $1: image.iso
  267. -VBoxManage unregistervm "$(VMNAME)" --delete
  268. VBoxManage createvm --name "$(VMNAME)" --ostype $2 --register
  269. VBoxManage modifyvm "$(VMNAME)" --memory 1024 --vram 32 --audio pulse --audiocontroller ac97 --bioslogodisplaytime 1 --bioslogofadeout off --bioslogofadein off --biosbootmenu disabled $3
  270. VBoxManage storagectl "$(VMNAME)" --add ide --name "IDE"
  271. VBoxManage storageattach "$(VMNAME)" --storagectl "IDE" --port 0 --device 0 --medium $$(shell pwd)/image.iso --type dvddrive
  272. VBoxManage setextradata "$(VMNAME)" GUI/DefaultCloseAction PowerOff
  273. VBoxManage startvm "$(VMNAME)" --type separate
  274. endef
  275. $(eval $(call virtualbox-runner,virtualbox,"Other",))
  276. $(eval $(call virtualbox-runner,virtualbox-efi,"Other",--firmware efi))
  277. $(eval $(call virtualbox-runner,virtualbox-efi64,"Other_64",--firmware efi))
  278. ##
  279. # Optional Extensions
  280. #
  281. # These optional extension libraries require third-party components to build,
  282. # but allow the native applications to make use of functionality such as
  283. # TrueType fonts or PNG images. You must have the necessary elements to build
  284. # these already installed into your sysroot for this to work.
  285. EXT_LIBS=$(patsubst ext/%.c,%,$(wildcard ext/*.c))
  286. EXT_LIBS_X=$(foreach lib,$(EXT_LIBS),base/lib/libtoaru_$(lib).so)
  287. EXT_LIBS_Y=$(foreach lib,$(EXT_LIBS),.make/$(lib).elmak)
  288. .make/%.elmak: ext/%.c util/auto-dep.py | dirs
  289. util/auto-dep.py --makelib $< > $@
  290. ifeq (,$(findstring clean,$(MAKECMDGOALS)))
  291. -include ${EXT_LIBS_Y}
  292. endif
  293. # Freetype: Terminal text rendering backend
  294. ext-freetype: base/lib/libtoaru_ext_freetype_fonts.so
  295. # Cairo: Compositor rendering backend
  296. ext-cairo: base/lib/libtoaru_ext_cairo_renderer.so