Makefile 12 KB

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