Makefile 12 KB

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