Makefile 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. # Copyright 2026 Aarav Ravindra Kharade
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # ═══════════════════════════════════════════════════════════════════
  16. # ArkOS Build System v4.0 — Top-Level Makefile
  17. # ═══════════════════════════════════════════════════════════════════
  18. #
  19. # Setup:
  20. # source envsetup.sh — Load build environment
  21. # type arm64 — Select ARM64 target (Raspberry Pi 4)
  22. # type x86_64 — Select x86_64 target (PC / QEMU)
  23. #
  24. # Build Targets:
  25. # make — Full incremental build for selected target
  26. # make clean — Remove all build artifacts and caches
  27. #
  28. # Test Targets (cross-architecture):
  29. # make test-uefi — x86_64 UEFI (auto-detects host arch)
  30. # make test-bios — x86_64 BIOS (auto-detects host arch)
  31. # make test-uefi-arm64 — ARM64 UEFI via QEMU
  32. # make test-bios-arm64 — ARM64 direct kernel boot via QEMU
  33. #
  34. # ═══════════════════════════════════════════════════════════════════
  35. # Project root is one level above this Makefile (arkos/Makefile)
  36. ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))/.. )
  37. TOOLS := $(ROOT)/tools
  38. ARKOS := $(ROOT)/arkos
  39. # ── Host Architecture Detection ───────────────────────────────────
  40. HOST_ARCH := $(shell uname -m)
  41. # ── Clang Toolchain (all compilation uses clang) ──────────────────
  42. # Prefer prebuilt clang, fall back to system clang
  43. CLANG := $(shell if [ -x $(ARKOS)/prebuilts/clang/bin/clang ]; then \
  44. echo $(ARKOS)/prebuilts/clang/bin/clang; \
  45. else echo clang; fi)
  46. CLANGXX := $(shell if [ -x $(ARKOS)/prebuilts/clang/bin/clang++ ]; then \
  47. echo $(ARKOS)/prebuilts/clang/bin/clang++; \
  48. else echo clang++; fi)
  49. # ── Target Architecture (set by envsetup.sh type command) ─────────
  50. # Default to x86_64 if no target selected
  51. TARGET_ARCH ?= x86_64
  52. TARGET_TRIPLE ?= x86_64-linux-gnu
  53. # ── Architecture-Specific Compiler Flags ──────────────────────────
  54. X86_CC := $(CLANG) --target=x86_64-linux-gnu
  55. X86_CXX := $(CLANGXX) --target=x86_64-linux-gnu
  56. ARM64_CC := $(CLANG) --target=aarch64-linux-gnu -mcpu=cortex-a72 -mno-outline-atomics
  57. ARM64_CXX := $(CLANGXX) --target=aarch64-linux-gnu -mcpu=cortex-a72 -mno-outline-atomics
  58. .PHONY: build clean \
  59. test-uefi test-bios test-uefi-arm64 test-bios-arm64
  60. # ── Build ──────────────────────────────────────────────────────────
  61. # Reads TARGET_ARCH from envsetup.sh type command.
  62. # Defaults to x86_64 if no target was selected.
  63. # Set NO_IMAGE=1 to skip assembling individual .img files into rpi4.img
  64. build:
  65. @$(MAKE) --no-print-directory -C $(TOOLS) && chmod +x $(TOOLS)/build
  66. ifeq ($(TARGET_ARCH),aarch64)
  67. ifeq ($(NO_IMAGE),1)
  68. @$(TOOLS)/build $(ARKOS) --device rpi4 --no-image
  69. else
  70. @$(TOOLS)/build $(ARKOS) --device rpi4
  71. endif
  72. else
  73. @$(TOOLS)/build $(ARKOS)
  74. endif
  75. # ── Clean ──────────────────────────────────────────────────────────
  76. clean:
  77. rm -rf out_staging finished boot/assets
  78. @$(MAKE) --no-print-directory -C $(TOOLS) clean
  79. # ═══════════════════════════════════════════════════════════════════
  80. # Test Targets — Cross-Architecture QEMU Emulation
  81. # ═══════════════════════════════════════════════════════════════════
  82. # These targets auto-detect the host CPU architecture and use
  83. # KVM when possible (same arch), or TCG software emulation when
  84. # running cross-architecture (e.g., x86_64 on ARM64 host).
  85. # ═══════════════════════════════════════════════════════════════════
  86. # ── test-uefi: Boot x86_64 UEFI image ────────────────────────────
  87. # On ARM64 host: uses qemu-system-x86_64 with TCG (no KVM)
  88. # On x86_64 host: uses KVM for near-native speed
  89. test-uefi:
  90. @echo "Testing ArkOS x86_64 UEFI (host: $(HOST_ARCH))..."
  91. @if [ "$(HOST_ARCH)" = "x86_64" ] || [ "$(HOST_ARCH)" = "amd64" ]; then \
  92. ACCEL="-enable-kvm"; \
  93. else \
  94. ACCEL="-accel tcg"; \
  95. fi; \
  96. qemu-system-x86_64 \
  97. $$ACCEL \
  98. -bios /usr/share/edk2/x64/OVMF.4m.fd \
  99. -m 2048 -smp 2 \
  100. -drive file=finished/boot.img,format=raw,index=0,media=disk,if=virtio \
  101. -drive file=finished/sys.img,format=raw,index=1,media=disk,if=virtio \
  102. -drive file=finished/vend.img,format=raw,index=2,media=disk,if=virtio \
  103. -device virtio-vga,edid=on,xres=1920,yres=1080 \
  104. -debugcon file:ovmf.log -global isa-debugcon.iobase=0x402 \
  105. -usb -device usb-tablet \
  106. -netdev user,id=net0 \
  107. -device virtio-net-pci,netdev=net0 \
  108. -serial stdio
  109. # ── test-bios: Boot x86_64 BIOS image ────────────────────────────
  110. test-bios:
  111. @echo "Testing ArkOS x86_64 BIOS (host: $(HOST_ARCH))..."
  112. @if [ "$(HOST_ARCH)" = "x86_64" ] || [ "$(HOST_ARCH)" = "amd64" ]; then \
  113. ACCEL="-enable-kvm"; \
  114. else \
  115. ACCEL="-accel tcg"; \
  116. fi; \
  117. qemu-system-x86_64 \
  118. $$ACCEL \
  119. -m 2048 -smp 2 \
  120. -drive file=finished/boot.img,format=raw,index=0,media=disk,if=virtio \
  121. -drive file=finished/sys.img,format=raw,index=1,media=disk,if=virtio \
  122. -drive file=finished/vend.img,format=raw,index=2,media=disk,if=virtio \
  123. -vga std \
  124. -debugcon file:ovmf.log -global isa-debugcon.iobase=0x402 \
  125. -usb -device usb-tablet \
  126. -netdev user,id=net0 \
  127. -device virtio-net-pci,netdev=net0 \
  128. -serial file:qemu_serial.log
  129. # ── test-uefi-arm64: Boot ARM64 image with UEFI ──────────────────
  130. # On x86_64 host: uses qemu-system-aarch64 with TCG
  131. # On ARM64 host: uses KVM for near-native speed
  132. test-uefi-arm64:
  133. @echo "Testing ArkOS ARM64 UEFI (host: $(HOST_ARCH))..."
  134. @if [ "$(HOST_ARCH)" = "aarch64" ] || [ "$(HOST_ARCH)" = "arm64" ]; then \
  135. ACCEL="-enable-kvm"; \
  136. else \
  137. ACCEL="-accel tcg"; \
  138. fi; \
  139. qemu-system-aarch64 \
  140. $$ACCEL \
  141. -M virt -cpu cortex-a72 \
  142. -m 2048 -smp 4 \
  143. -bios /usr/share/AAVMF/AAVMF_CODE.fd \
  144. -drive file=finished/rpi4/rpi4.img,format=raw,if=virtio \
  145. -device virtio-gpu-pci \
  146. -device usb-ehci -device usb-kbd -device usb-mouse \
  147. -netdev user,id=net0 \
  148. -device virtio-net-pci,netdev=net0 \
  149. -serial stdio
  150. # ── test-bios-arm64: Boot ARM64 direct kernel (RPi4 style) ───────
  151. test-bios-arm64:
  152. @echo "Testing ArkOS ARM64 direct kernel boot (host: $(HOST_ARCH))..."
  153. @if [ "$(HOST_ARCH)" = "aarch64" ] || [ "$(HOST_ARCH)" = "arm64" ]; then \
  154. ACCEL="-enable-kvm"; \
  155. else \
  156. ACCEL="-accel tcg"; \
  157. fi; \
  158. qemu-system-aarch64 \
  159. $$ACCEL \
  160. -M raspi4b \
  161. -m 2048 \
  162. -kernel finished/rpi4/kernel8.img \
  163. -dtb kernel/prebuilts/bcm2711-rpi-4-b.dtb \
  164. -initrd finished/rpi4/initramfs.img \
  165. -append "console=ttyAMA0,115200 loglevel=0 logo.nologo quiet init=/init rw vt.global_cursor_default=0" \
  166. -serial mon:stdio \
  167. -usb -device usb-kbd -device usb-mouse \
  168. -device usb-net,netdev=net0 \
  169. -netdev user,id=net0
  170. # ═══════════════════════════════════════════════════════════════════
  171. # ARM64 (Raspberry Pi 4) Binary Compilation Targets
  172. # ═══════════════════════════════════════════════════════════════════
  173. # These are invoked by tools/build via `make -C <arkos-dir> <target>`
  174. # when building with --device rpi4.
  175. # All C/C++ compilation uses clang with --target=aarch64-linux-gnu.
  176. # ═══════════════════════════════════════════════════════════════════
  177. # ── ARM64 fb_helper.o — Framebuffer ioctl bridge ─────────────────
  178. out_staging/rpi4/fb_helper.o: system/core/fb_helper.c
  179. @mkdir -p out_staging/rpi4
  180. $(ARM64_CC) -c system/core/fb_helper.c -o out_staging/rpi4/fb_helper.o
  181. # ── ARM64 ArkFontRobotoBoldData.o — Font data ────────────────────
  182. out_staging/rpi4/ArkFontRobotoBoldData.o: arkrt/ArkGraphics/ArkFontRobotoBoldData.c
  183. @mkdir -p out_staging/rpi4
  184. $(ARM64_CC) -O3 -c arkrt/ArkGraphics/ArkFontRobotoBoldData.c -o out_staging/rpi4/ArkFontRobotoBoldData.o
  185. # ── ARM64 init — PID 1 process ───────────────────────────────────
  186. out_staging/rpi4/init: system/core/init.c vendor/verify/sha256.c
  187. @mkdir -p out_staging/rpi4
  188. $(ARM64_CC) -static vendor/verify/sha256.c system/core/init.c \
  189. -Ivendor/verify -DARK_KEY=\""$(ARK_KEY)"\" \
  190. -o out_staging/rpi4/init
  191. SWIFT_AARCH64_SDK := $(shell if [ -d $(HOME)/.swiftpm/swift-sdks/swift-6.3.2-RELEASE_static-linux.artifactbundle/swift-6.3.2-RELEASE_static-linux-0.1.0/swift-linux-musl/musl-1.2.5.sdk/aarch64 ]; then \
  192. echo $(HOME)/.swiftpm/swift-sdks/swift-6.3.2-RELEASE_static-linux.artifactbundle/swift-6.3.2-RELEASE_static-linux-0.1.0/swift-linux-musl/musl-1.2.5.sdk/aarch64; \
  193. else echo $(CURDIR)/prebuilts/Swift/swift-6.3.2-RELEASE_static-linux-0.1.0/swift-linux-musl/musl-1.2.5.sdk/aarch64; fi)
  194. SWIFT_AARCH64_LIB := $(SWIFT_AARCH64_SDK)/usr/lib/swift_static/linux-static
  195. SWIFT_AARCH64_LINK := $(ARM64_CC) -static \
  196. $(SWIFT_AARCH64_LIB)/aarch64/swiftrt.o \
  197. $(CURDIR)/prebuilts/mimalloc.o \
  198. -L$(SWIFT_AARCH64_SDK)/usr/lib \
  199. -L$(SWIFT_AARCH64_LIB)
  200. # Foundation and other modules live in swift_static/ which swiftc doesn't
  201. # auto-search. We must add explicit -I flags for module discovery.
  202. SWIFT_AARCH64_SWIFTC := swiftc -c -whole-module-optimization -O \
  203. -target aarch64-swift-linux-musl -target-cpu cortex-a72 \
  204. -Xcc -mno-outline-atomics \
  205. -sdk $(SWIFT_AARCH64_SDK) \
  206. -I $(SWIFT_AARCH64_LIB) \
  207. -I $(SWIFT_AARCH64_LIB)/aarch64
  208. SWIFT_AARCH64_LIBS := -Wl,--start-group -lswiftCore -lswiftMusl -lswiftDispatch -ldispatch -lBlocksRuntime \
  209. -lFoundation -lFoundationEssentials -l_FoundationCShims -l_FoundationCollections \
  210. -l_FoundationICU -lFoundationInternationalization -lFoundationNetworking -lFoundationXML \
  211. -lCoreFoundation -l_CFURLSessionInterface -l_CFXMLInterface -lswift_Concurrency \
  212. -lswiftRuntime -lswift_StringProcessing -lswiftSynchronization -lswift_RegexParser \
  213. -lfts -lunwind -lc++ -lc++abi -lc -Wl,--end-group -lpthread -ldl -lm
  214. # ── ARM64 arkrt — ArkOS Runtime Daemon ───────────────────────────
  215. out_staging/rpi4/early_init.o: arkrt/early_init.c
  216. @mkdir -p out_staging/rpi4
  217. $(ARM64_CC) -c arkrt/early_init.c -o out_staging/rpi4/early_init.o
  218. out_staging/rpi4/arkrt: arkrt/main.swift arkrt/KernelBridge.swift \
  219. arkrt/CommandRouter.swift arkrt/IPC.swift \
  220. arkrt/ServiceManager.swift arkrt/NetworkService.swift \
  221. arkrt/InputService.swift out_staging/rpi4/early_init.o
  222. @mkdir -p out_staging/rpi4
  223. @$(SWIFT_AARCH64_SWIFTC) $(filter %.swift,$^) -o out_staging/rpi4/arkrt.o && \
  224. $(SWIFT_AARCH64_LINK) out_staging/rpi4/arkrt.o out_staging/rpi4/early_init.o -o $@ $(SWIFT_AARCH64_LIBS) || \
  225. echo " \033[1;33m[NOTICE]\033[0m Swift aarch64 target stdlib missing. Skipping arkrt (ARM64) binary."
  226. # ── ARM64 ui_daemon — Display Compositor ─────────────────────────
  227. out_staging/rpi4/system/ui_daemon: system/display/main.swift system/display/splash.swift out_staging/rpi4/fb_helper.o
  228. @mkdir -p out_staging/rpi4/system
  229. @$(SWIFT_AARCH64_SWIFTC) system/display/main.swift system/display/splash.swift -o out_staging/rpi4/system/ui_daemon.o && \
  230. $(SWIFT_AARCH64_LINK) out_staging/rpi4/system/ui_daemon.o out_staging/rpi4/fb_helper.o -o $@ $(SWIFT_AARCH64_LIBS) || \
  231. echo " \033[1;33m[NOTICE]\033[0m Swift aarch64 target stdlib missing. Skipping ui_daemon (ARM64) binary."
  232. # ── ARM64 setup_app — Setup / Welcome app ──────────────────────────
  233. out_staging/rpi4/system/setup_app: system/apps/setup_app.swift arkrt/ArkGraphics/ArkGraphics.swift arkrt/ArkGraphics/ArkFontRobotoBold.swift arkrt/ArkGraphics/ArkWrite.swift arkrt/ArkGraphics/ArkShapes.swift arkrt/ArkGraphics/ArkUI.swift arkrt/ArkGraphics/ArkInput.swift out_staging/rpi4/ArkFontRobotoBoldData.o
  234. @mkdir -p out_staging/rpi4/system
  235. @$(SWIFT_AARCH64_SWIFTC) system/apps/setup_app.swift arkrt/ArkGraphics/ArkGraphics.swift arkrt/ArkGraphics/ArkFontRobotoBold.swift arkrt/ArkGraphics/ArkWrite.swift arkrt/ArkGraphics/ArkShapes.swift arkrt/ArkGraphics/ArkUI.swift arkrt/ArkGraphics/ArkInput.swift -o out_staging/rpi4/system/setup_app.o && \
  236. $(SWIFT_AARCH64_LINK) out_staging/rpi4/system/setup_app.o out_staging/rpi4/ArkFontRobotoBoldData.o -o $@ $(SWIFT_AARCH64_LIBS) || \
  237. echo " \033[1;33m[NOTICE]\033[0m Swift aarch64 target stdlib missing. Skipping setup_app (ARM64) binary."
  238. # ═══════════════════════════════════════════════════════════════════
  239. # x86_64 Binary Compilation Targets
  240. # ═══════════════════════════════════════════════════════════════════
  241. # These are invoked by tools/build via `make -C <arkos-dir> <target>`.
  242. # All C/C++ compilation uses clang.
  243. # ═══════════════════════════════════════════════════════════════════
  244. # Extract the Verified Boot signing key from vendor config
  245. ARK_KEY := $(shell if [ -f vendor/verify/securebuild.ark ]; then \
  246. grep "^ARK-OS-" vendor/verify/securebuild.ark | tr -d '[:space:]'; \
  247. else echo "NO_KEY"; fi)
  248. SWIFT_X86_64_SDK := $(CURDIR)/prebuilts/Swift/swift-6.3.2-RELEASE_static-linux-0.1.0/swift-linux-musl/musl-1.2.5.sdk/x86_64
  249. SWIFT_X86_64_LIB := $(SWIFT_X86_64_SDK)/usr/lib/swift_static/linux-static
  250. SWIFT_X86_64_SWIFTC := swiftc -O -static-executable -target x86_64-swift-linux-musl -sdk $(SWIFT_X86_64_SDK) -resource-dir $(SWIFT_X86_64_SDK)/usr/lib/swift_static -I $(SWIFT_X86_64_LIB) -I $(SWIFT_X86_64_LIB)/x86_64 -I system/modules
  251. # ── fb_helper.o — Framebuffer ioctl bridge (C → Swift interop) ────
  252. out_staging/fb_helper.o: system/core/fb_helper.c
  253. $(X86_CC) -c system/core/fb_helper.c -o out_staging/fb_helper.o
  254. # ── arkrt — ArkOS Runtime Daemon (PID 2) ──────────────────────────
  255. ARKRT_SRCS := $(wildcard arkrt/*.swift) $(wildcard arkrt/compositor/*.swift) $(wildcard arkrt/compositor/globals/*.swift) $(wildcard arkrt/ArkGraphics/*.swift)
  256. out_staging/early_init.o: arkrt/early_init.c
  257. $(X86_CC) -c arkrt/early_init.c -o out_staging/early_init.o
  258. out_staging/arkrt: $(ARKRT_SRCS) out_staging/ArkFontRobotoBoldData.o out_staging/early_init.o
  259. $(SWIFT_X86_64_SWIFTC) $(ARKRT_SRCS) out_staging/ArkFontRobotoBoldData.o out_staging/early_init.o -o out_staging/arkrt
  260. out_staging/init: system/core/init.c vendor/verify/sha256.c
  261. $(X86_CC) -static vendor/verify/sha256.c system/core/init.c \
  262. -Ivendor/verify -DARK_KEY=\""$(ARK_KEY)"\" \
  263. -o out_staging/init
  264. # ── Bootloader binaries (BIOS + UEFI) ────────────────────────────
  265. out_staging/bootloader.bin: boot/source/bootloader.asm
  266. nasm -f bin boot/source/bootloader.asm -o out_staging/bootloader.bin
  267. out_staging/stage2.bin: boot/source/stage2.asm
  268. nasm -f bin boot/source/stage2.asm -o out_staging/stage2.bin
  269. out_staging/BOOTX64.EFI: boot/source/bootloader.c
  270. $(X86_CC) -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol \
  271. -fno-stack-protector -fpic -fshort-wchar -mno-red-zone -DEFI_FUNCTION_WRAPPER \
  272. -c boot/source/bootloader.c -o out_staging/bootloader_uefi.o
  273. ld -nostdlib -znocombreloc -T /usr/lib/elf_x86_64_efi.lds -shared -Bsymbolic \
  274. /usr/lib/crt0-efi-x86_64.o out_staging/bootloader_uefi.o \
  275. -o out_staging/bootloader_uefi.so -lgnuefi -lefi -L/usr/lib
  276. objcopy -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym \
  277. -j .rel -j .rela -j .reloc -j .eh_frame \
  278. -O efi-app-x86_64 out_staging/bootloader_uefi.so out_staging/BOOTX64.EFI
  279. # ── ui_daemon — Display Compositor ─────────────────────────────────
  280. out_staging/system/ui_daemon: system/display/main.swift system/display/splash.swift system/display/wayland_client.swift arkrt/compositor/WireProtocol.swift arkrt/compositor/ArkSocket.swift arkrt/ArkSys/*.swift
  281. @mkdir -p out_staging/system
  282. @$(SWIFT_X86_64_SWIFTC) \
  283. system/display/main.swift system/display/splash.swift system/display/wayland_client.swift arkrt/compositor/WireProtocol.swift arkrt/compositor/ArkSocket.swift arkrt/ArkSys/ArkSys.swift arkrt/ArkSys/ArkSysFile.swift arkrt/ArkSys/ArkSysTime.swift \
  284. -o out_staging/system/ui_daemon
  285. # ── setup_app — Setup / Welcome app ────────────────────────────────
  286. out_staging/ArkFontRobotoBoldData.o: arkrt/ArkGraphics/ArkFontRobotoBoldData.c
  287. @mkdir -p out_staging
  288. @$(X86_CC) -O3 -c arkrt/ArkGraphics/ArkFontRobotoBoldData.c -o out_staging/ArkFontRobotoBoldData.o
  289. out_staging/system/setup_app: system/apps/setup_app.swift arkrt/ArkGraphics/ArkGraphics.swift arkrt/ArkGraphics/ArkFontRobotoBold.swift arkrt/ArkGraphics/ArkWrite.swift arkrt/ArkGraphics/ArkShapes.swift arkrt/ArkGraphics/ArkUI.swift arkrt/ArkGraphics/ArkInput.swift out_staging/ArkFontRobotoBoldData.o arkrt/ArkSys/*.swift
  290. mkdir -p out_staging/system
  291. $(SWIFT_X86_64_SWIFTC) \
  292. system/apps/setup_app.swift arkrt/ArkGraphics/ArkGraphics.swift arkrt/ArkGraphics/ArkFontRobotoBold.swift arkrt/ArkGraphics/ArkWrite.swift arkrt/ArkGraphics/ArkShapes.swift arkrt/ArkGraphics/ArkUI.swift arkrt/ArkGraphics/ArkInput.swift out_staging/ArkFontRobotoBoldData.o arkrt/ArkSys/ArkSys.swift arkrt/ArkSys/ArkSysFile.swift arkrt/ArkSys/ArkSysTime.swift \
  293. -o out_staging/system/setup_app