build.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * ArkOS Build System v3.0
  3. * -----------------------
  4. * Orchestrates the complete OS build pipeline from source to bootable images.
  5. *
  6. * Architecture:
  7. * 1. Clean previous output (preserving build caches)
  8. * 2. Stage frameworks, boot assets, vendor blobs, and service configs
  9. * 3. Compile all system binaries (init, arkrt, splash, display daemon, UI app)
  10. * 4. Build the SwiftUI framework via SPM (cached — only rebuilds on changes)
  11. * 5. Sign the runtime binary for Verified Boot
  12. * 6. Pack initramfs with all runtime binaries
  13. * 7. Assemble bootloader and generate disk images
  14. *
  15. * All intermediate artifacts go into out_staging/. Final outputs go into finished/.
  16. * The .swift_build/ directory inside out_staging/ is preserved across builds
  17. * to enable SPM incremental compilation caching.
  18. *
  19. * Usage: build <arkos-base-dir>
  20. */
  21. #include "ark_parser.h"
  22. #include <dirent.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. /* ── Build Configuration ───────────────────────────────────────── */
  29. #define TOTAL_STEPS 16
  30. static int current_step = 0;
  31. /* ── Progress Reporting ────────────────────────────────────────── */
  32. static void step(const char *desc) {
  33. current_step++;
  34. printf("\n\033[1;36m[%2d/%d]\033[0m %s\n", current_step, TOTAL_STEPS, desc);
  35. }
  36. /* ── Command Execution ─────────────────────────────────────────── */
  37. /**
  38. * Execute a shell command and print it. Non-zero exit codes are reported
  39. * but do NOT abort the build — callers must check return values for
  40. * critical steps.
  41. */
  42. static int run(const char *cmd) {
  43. printf(" \033[0;90m$ %s\033[0m\n", cmd);
  44. int ret = system(cmd);
  45. if (ret != 0) {
  46. fprintf(stderr, " \033[1;31m✗ Command failed (exit %d)\033[0m\n", ret);
  47. }
  48. return ret;
  49. }
  50. /* ── Path Utilities ────────────────────────────────────────────── */
  51. /** Join two path components. Caller must free the returned string. */
  52. static char *pjoin(const char *a, const char *b) {
  53. size_t len = strlen(a) + 1 + strlen(b) + 1;
  54. char *out = malloc(len);
  55. snprintf(out, len, "%s/%s", a, b);
  56. return out;
  57. }
  58. /* ── Incremental Build Helpers ─────────────────────────────────── */
  59. /**
  60. * Check if an output file exists and is newer than all listed source files.
  61. * Returns 1 if the output is up-to-date (build can be skipped), 0 otherwise.
  62. *
  63. * This enables incremental builds — Swift compilation is expensive, so we
  64. * only recompile when source files have actually changed.
  65. */
  66. static int is_up_to_date(const char *output, const char **sources,
  67. int source_count) {
  68. struct stat out_st;
  69. if (stat(output, &out_st) != 0) {
  70. return 0; /* Output doesn't exist — must build */
  71. }
  72. for (int i = 0; i < source_count; i++) {
  73. struct stat src_st;
  74. if (stat(sources[i], &src_st) != 0) {
  75. return 0; /* Source missing — must build */
  76. }
  77. if (src_st.st_mtime > out_st.st_mtime) {
  78. return 0; /* Source is newer — must rebuild */
  79. }
  80. }
  81. return 1; /* All sources are older than output — skip */
  82. }
  83. /* ── Main Build Pipeline ───────────────────────────────────────── */
  84. int main(int argc, char *argv[]) {
  85. if (argc < 2) {
  86. fprintf(stderr, "Usage: %s <arkos-base-dir>\n", argv[0]);
  87. return 1;
  88. }
  89. const char *base = argv[1]; /* e.g. /home/user/repo/arkos */
  90. /* Derive the repo root (one level up from the arkos directory) */
  91. char *repo = pjoin(base, "..");
  92. /* Output and staging directories */
  93. char *out_dir = pjoin(base, "finished");
  94. char *staging = pjoin(base, "out_staging");
  95. char *staging_sys = pjoin(staging, "system");
  96. char *staging_fw = pjoin(staging, "system/frameworks");
  97. char *staging_vend = pjoin(staging, "vendor");
  98. char *staging_boot = pjoin(staging, "boot");
  99. /* Source directories */
  100. char *fw_dir = pjoin(base, "frameworks");
  101. char *boot_dir = pjoin(base, "boot");
  102. char *vendor_dir = pjoin(base, "vendor");
  103. char *system_dir = pjoin(base, "system");
  104. char *kernel_dir = pjoin(base, "kernel");
  105. printf("\n\033[1;35m╔══════════════════════════════════════╗\033[0m\n");
  106. printf("\033[1;35m║ ArkOS Build System v3.0 ║\033[0m\n");
  107. printf("\033[1;35m╚══════════════════════════════════════╝\033[0m\n");
  108. /* ─── Step 1: Clean previous output ────────────────────────── */
  109. step("Cleaning previous output (preserving build caches)");
  110. {
  111. char cmd[1024];
  112. /* Preserve out_staging/ entirely for incremental builds.
  113. * Only wipe finished/ which contains the final disk images. */
  114. snprintf(cmd, sizeof(cmd), "rm -rf %s", out_dir);
  115. run(cmd);
  116. }
  117. /* ─── Step 2: Create staging directories ───────────────────── */
  118. step("Creating staging directories");
  119. {
  120. char cmd[1024];
  121. snprintf(cmd, sizeof(cmd), "mkdir -p %s %s %s %s %s/services",
  122. out_dir, staging_fw, staging_vend, staging_boot, staging_sys);
  123. run(cmd);
  124. }
  125. /* ─── Step 3: Stage frameworks ─────────────────────────────── */
  126. step("Staging frameworks");
  127. {
  128. DIR *d = opendir(fw_dir);
  129. if (d) {
  130. struct dirent *ent;
  131. while ((ent = readdir(d)) != NULL) {
  132. if (ent->d_name[0] == '.')
  133. continue;
  134. /* DRM framework is excluded from the base system image */
  135. if (strcmp(ent->d_name, "DRM") == 0) {
  136. printf(" Skipping excluded framework: %s\n", ent->d_name);
  137. continue;
  138. }
  139. char cmd[1024];
  140. snprintf(cmd, sizeof(cmd), "cp -ur %s/%s %s/", fw_dir, ent->d_name,
  141. staging_fw);
  142. run(cmd);
  143. }
  144. closedir(d);
  145. }
  146. }
  147. /* ─── Step 4: Stage boot assets ────────────────────────────── */
  148. step("Staging boot assets");
  149. {
  150. DIR *d = opendir(boot_dir);
  151. if (d) {
  152. struct dirent *ent;
  153. while ((ent = readdir(d)) != NULL) {
  154. if (ent->d_name[0] == '.')
  155. continue;
  156. char cmd[1024];
  157. snprintf(cmd, sizeof(cmd), "cp -ur %s/%s %s/", boot_dir, ent->d_name,
  158. staging_boot);
  159. run(cmd);
  160. }
  161. closedir(d);
  162. }
  163. }
  164. /* ─── Step 5: Stage system services ────────────────────────── */
  165. step("Staging system service configs");
  166. {
  167. char cmd[1024];
  168. snprintf(cmd, sizeof(cmd),
  169. "cp -ur %s/system/services/* %s/services/ 2>/dev/null || true",
  170. base, staging_sys);
  171. run(cmd);
  172. }
  173. /* ─── Step 6: Stage vendor files ───────────────────────────── */
  174. step("Staging vendor files");
  175. {
  176. char cmd[1024];
  177. snprintf(cmd, sizeof(cmd), "cp -ur %s/* %s/ 2>/dev/null || true",
  178. vendor_dir, staging_vend);
  179. run(cmd);
  180. }
  181. /* ─── Step 7: Verify kernel configuration ──────────────────── */
  182. step("Verifying kernel configuration");
  183. {
  184. char *kconfig = pjoin(kernel_dir, ".config");
  185. if (access(kconfig, F_OK) != -1) {
  186. printf(" \033[0;32m✓\033[0m Kernel .config present\n");
  187. } else {
  188. printf(" \033[1;33m⚠ WARNING:\033[0m Kernel .config not found!\n");
  189. }
  190. free(kconfig);
  191. }
  192. /* ─── Step 8: Compile fb_helper.c ──────────────────────────── */
  193. step("Compiling fb_helper.c (framebuffer ioctl bridge)");
  194. {
  195. char cmd[1024];
  196. snprintf(cmd, sizeof(cmd), "make -C %s out_staging/fb_helper.o", base);
  197. run(cmd);
  198. }
  199. /* ─── Step 9: Compile Swift system binaries (incremental) ──── */
  200. step("Compiling Swift system binaries (splash, arkrt, display daemon)");
  201. {
  202. /* Check each binary individually for incremental builds */
  203. char arkrt_out[512], daemon_out[512];
  204. snprintf(arkrt_out, sizeof(arkrt_out), "%s/arkrt", staging);
  205. snprintf(daemon_out, sizeof(daemon_out), "%s/system/ui_daemon", staging);
  206. /* arkrt sources */
  207. char a1[512], a2[512], a3[512], a4[512], a5[512], a6[512], a7[512];
  208. snprintf(a1, sizeof(a1), "%s/arkrt/main.swift", base);
  209. snprintf(a2, sizeof(a2), "%s/arkrt/KernelBridge.swift", base);
  210. snprintf(a3, sizeof(a3), "%s/arkrt/CommandRouter.swift", base);
  211. snprintf(a4, sizeof(a4), "%s/arkrt/IPC.swift", base);
  212. snprintf(a5, sizeof(a5), "%s/arkrt/ServiceManager.swift", base);
  213. snprintf(a6, sizeof(a6), "%s/arkrt/NetworkService.swift", base);
  214. snprintf(a7, sizeof(a7), "%s/arkrt/InputService.swift", base);
  215. const char *arkrt_sources[] = {a1, a2, a3, a4, a5, a6, a7};
  216. if (is_up_to_date(arkrt_out, arkrt_sources, 7)) {
  217. printf(" \033[0;32m✓\033[0m arkrt is up-to-date (skipping)\n");
  218. } else {
  219. char cmd[1024];
  220. snprintf(cmd, sizeof(cmd),
  221. "make -C %s out_staging/arkrt", base);
  222. run(cmd);
  223. }
  224. /* ui_daemon sources */
  225. char d1[512], d2[512];
  226. snprintf(d1, sizeof(d1), "%s/system/display/main.swift", base);
  227. snprintf(d2, sizeof(d2), "%s/system/core/fb_helper.c", base);
  228. const char *daemon_sources[] = {d1, d2};
  229. if (is_up_to_date(daemon_out, daemon_sources, 2)) {
  230. printf(" \033[0;32m✓\033[0m ui_daemon is up-to-date (skipping)\n");
  231. } else {
  232. char cmd[1024];
  233. snprintf(cmd, sizeof(cmd),
  234. "make -C %s out_staging/system/ui_daemon", base);
  235. run(cmd);
  236. }
  237. }
  238. /* ─── Step 10: Build SwiftUI framework (SPM cached) ────────── */
  239. step("Building SwiftUI framework (SPM incremental build)");
  240. {
  241. /*
  242. * Uses --scratch-path to store SPM build artifacts in out_staging/.swift_build.
  243. * This directory is preserved across builds so SPM only recompiles changed
  244. * modules instead of all 44+ every time. First build is slow (~5 min),
  245. * subsequent builds with no changes complete in seconds.
  246. *
  247. * The 2>/dev/null suppresses SPM's verbose diagnostic output.
  248. * Errors in this step are non-fatal (|| true) since the UI framework
  249. * is not yet linked into the boot chain.
  250. */
  251. char cmd[2048];
  252. snprintf(
  253. cmd, sizeof(cmd),
  254. "cd %s/frameworks/SwiftUI && swift build -c release "
  255. "-j $(nproc) -Xswiftc -use-ld=lld "
  256. "--scratch-path %s/.swift_build "
  257. "-Xcc -I%s/frameworks/SwiftUI/Sources/SwiftCorelibs/include "
  258. "-Xcxx -I%s/frameworks/SwiftUI/Sources/SwiftCorelibs/include "
  259. "2>/dev/null || true",
  260. base, staging, base, base);
  261. run(cmd);
  262. }
  263. /* ─── Step 11: Compile ui_test (incremental) ───────────────── */
  264. step("Compiling ui_test (setup app)");
  265. {
  266. char ut_out[512];
  267. snprintf(ut_out, sizeof(ut_out), "%s/system/ui_test", staging);
  268. char u1[512], u2[512], u3[512], u4[512], u5[512];
  269. snprintf(u1, sizeof(u1), "%s/system/apps/ui_test.swift", base);
  270. snprintf(u2, sizeof(u2), "%s/frameworks/ArkGraphics/ArkGraphics.swift", base);
  271. snprintf(u3, sizeof(u3), "%s/frameworks/ArkGraphics/ArkWrite.swift", base);
  272. snprintf(u4, sizeof(u4), "%s/frameworks/ArkGraphics/ArkShapes.swift", base);
  273. snprintf(u5, sizeof(u5), "%s/frameworks/ArkGraphics/ArkFontRobotoBold.swift", base);
  274. const char *ut_sources[] = {u1, u2, u3, u4, u5};
  275. if (is_up_to_date(ut_out, ut_sources, 5)) {
  276. printf(" \033[0;32m✓\033[0m ui_test is up-to-date (skipping)\n");
  277. } else {
  278. char cmd[1024];
  279. snprintf(cmd, sizeof(cmd), "make -C %s out_staging/system/ui_test", base);
  280. run(cmd);
  281. }
  282. }
  283. /* ─── Step 12: Sign arkrt (Verified Boot) ──────────────────── */
  284. step("Signing arkrt binary (Verified Boot)");
  285. {
  286. char cmd[1024];
  287. snprintf(cmd, sizeof(cmd),
  288. "python3 %s/verify/sign.py %s/verify/securebuild.ark "
  289. "%s/arkrt %s/signature.bin",
  290. vendor_dir, vendor_dir, staging, staging);
  291. run(cmd);
  292. }
  293. /* ─── Step 13: Compile init (PID 1) ────────────────────────── */
  294. step("Compiling init (PID 1 process)");
  295. {
  296. char init_out[512];
  297. snprintf(init_out, sizeof(init_out), "%s/init", staging);
  298. char i1[512], i2[512];
  299. snprintf(i1, sizeof(i1), "%s/system/core/init.c", base);
  300. snprintf(i2, sizeof(i2), "%s/vendor/verify/sha256.c", base);
  301. const char *init_sources[] = {i1, i2};
  302. if (is_up_to_date(init_out, init_sources, 2)) {
  303. printf(" \033[0;32m✓\033[0m init is up-to-date (skipping)\n");
  304. } else {
  305. char cmd[1024];
  306. snprintf(cmd, sizeof(cmd), "make -C %s out_staging/init", base);
  307. run(cmd);
  308. }
  309. }
  310. /* ─── Step 14: Pack initramfs ──────────────────────────────── */
  311. step("Packing initramfs (base + runtime + display + UI)");
  312. {
  313. char *initramfs_ext = pjoin(staging, "initramfs_ext");
  314. char *base_initramfs = pjoin(boot_dir, "initramfs.img");
  315. char cmd[2048];
  316. /* Extract base initramfs (contains busybox, kernel modules, etc.) */
  317. snprintf(cmd, sizeof(cmd), "rm -rf %s && mkdir -p %s", initramfs_ext,
  318. initramfs_ext);
  319. run(cmd);
  320. snprintf(cmd, sizeof(cmd), "cd %s && zcat %s | cpio -id --no-preserve-owner 2>/dev/null",
  321. initramfs_ext, base_initramfs);
  322. run(cmd);
  323. /* Create required directories in initramfs */
  324. snprintf(cmd, sizeof(cmd),
  325. "mkdir -p %s/system/services %s/run %s/tmp %s/var/log",
  326. initramfs_ext, initramfs_ext, initramfs_ext, initramfs_ext);
  327. run(cmd);
  328. /* Copy compiled binaries into initramfs */
  329. snprintf(cmd, sizeof(cmd), "cp %s/init %s/init && chmod +x %s/init",
  330. staging, initramfs_ext, initramfs_ext);
  331. run(cmd);
  332. snprintf(cmd, sizeof(cmd), "cp %s/arkrt %s/arkrt && chmod +x %s/arkrt",
  333. staging, initramfs_ext, initramfs_ext);
  334. run(cmd);
  335. snprintf(cmd, sizeof(cmd), "cp %s/signature.bin %s/signature.bin",
  336. staging, initramfs_ext);
  337. run(cmd);
  338. /* Copy display daemon and UI test app into /system/ */
  339. snprintf(cmd, sizeof(cmd),
  340. "cp %s/system/ui_daemon %s/system/ui_daemon && "
  341. "chmod +x %s/system/ui_daemon 2>/dev/null || true",
  342. staging, initramfs_ext, initramfs_ext);
  343. run(cmd);
  344. snprintf(cmd, sizeof(cmd),
  345. "cp %s/system/ui_test %s/system/ui_test && "
  346. "chmod +x %s/system/ui_test 2>/dev/null || true",
  347. staging, initramfs_ext, initramfs_ext);
  348. run(cmd);
  349. /* Copy service definition files */
  350. snprintf(cmd, sizeof(cmd),
  351. "cp -r %s/services/* %s/system/services/ 2>/dev/null || true",
  352. staging_sys, initramfs_ext);
  353. run(cmd);
  354. /* Repack initramfs as gzip-compressed cpio archive */
  355. snprintf(cmd, sizeof(cmd),
  356. "cd %s && find . | cpio -H newc -o 2>/dev/null | gzip > %s/initramfs.img",
  357. initramfs_ext, out_dir);
  358. run(cmd);
  359. free(initramfs_ext);
  360. free(base_initramfs);
  361. }
  362. /* ─── Step 15: Assemble bootloader + stage2 ────────────────── */
  363. step("Assembling bootloader");
  364. {
  365. char cmd[1024];
  366. /* Generate boot animation frames from Python script */
  367. snprintf(cmd, sizeof(cmd),
  368. "python3 %s/source/animationframes/generate_frames.py %s",
  369. boot_dir, staging);
  370. run(cmd);
  371. snprintf(cmd, sizeof(cmd),
  372. "make -C %s out_staging/bootloader.bin out_staging/stage2.bin "
  373. "out_staging/BOOTX64.EFI",
  374. base);
  375. run(cmd);
  376. }
  377. /* ─── Step 16: Generate disk images ────────────────────────── */
  378. step("Generating disk images");
  379. {
  380. char cmd[1024];
  381. /* boot.img — bootloader + kernel + initramfs packed by pack_boot.py */
  382. snprintf(cmd, sizeof(cmd), "python3 %s/tools/pack_boot.py --base-dir %s",
  383. repo, base);
  384. run(cmd);
  385. /* sys.img — system partition (frameworks, apps, services) */
  386. snprintf(cmd, sizeof(cmd),
  387. "dd if=/dev/zero of=%s/sys.img bs=1M count=2048 2>/dev/null",
  388. out_dir);
  389. run(cmd);
  390. snprintf(cmd, sizeof(cmd), "mkfs.ext4 -d %s %s/sys.img 2>/dev/null",
  391. staging_sys, out_dir);
  392. run(cmd);
  393. /* vend.img — vendor partition (OEM blobs, keys) */
  394. snprintf(cmd, sizeof(cmd),
  395. "dd if=/dev/zero of=%s/vend.img bs=1M count=100 2>/dev/null",
  396. out_dir);
  397. run(cmd);
  398. snprintf(cmd, sizeof(cmd), "mkfs.ext4 -d %s %s/vend.img 2>/dev/null",
  399. staging_vend, out_dir);
  400. run(cmd);
  401. /* avb.img — Android Verified Boot metadata (only if signing keys exist) */
  402. char *securebuild_path = pjoin(vendor_dir, "verify/securebuild.ark");
  403. ArkConfig *sbc = ark_parse(securebuild_path);
  404. int has_keys = 0;
  405. for (int i = 0; i < sbc->standalone_count; i++) {
  406. if (strstr(sbc->standalone[i], "ARK-OS-") != NULL) {
  407. has_keys = 1;
  408. break;
  409. }
  410. }
  411. if (has_keys) {
  412. printf(" \033[0;32m✓\033[0m Signing keys found — generating avb.img\n");
  413. snprintf(cmd, sizeof(cmd),
  414. "dd if=/dev/zero of=%s/avb.img bs=1M count=1 2>/dev/null",
  415. out_dir);
  416. run(cmd);
  417. } else {
  418. printf(" \033[1;33m⚠\033[0m No signing keys — skipping avb.img\n");
  419. }
  420. ark_free(sbc);
  421. free(securebuild_path);
  422. /* vbmeta.img + dtbo.img — boot verification and device tree overlay stubs */
  423. snprintf(cmd, sizeof(cmd),
  424. "dd if=/dev/zero of=%s/vbmeta.img bs=1M count=1 2>/dev/null",
  425. out_dir);
  426. run(cmd);
  427. snprintf(cmd, sizeof(cmd),
  428. "dd if=/dev/zero of=%s/dtbo.img bs=1M count=1 2>/dev/null",
  429. out_dir);
  430. run(cmd);
  431. }
  432. printf("\n\033[1;32m╔══════════════════════════════════════╗\033[0m\n");
  433. printf("\033[1;32m║ Build Complete! ║\033[0m\n");
  434. printf("\033[1;32m╚══════════════════════════════════════╝\033[0m\n");
  435. printf("Run \033[1mmake run\033[0m to launch ArkOS in QEMU.\n\n");
  436. /* Free all heap-allocated path strings */
  437. free(repo);
  438. free(out_dir);
  439. free(staging);
  440. free(staging_sys);
  441. free(staging_fw);
  442. free(staging_vend);
  443. free(staging_boot);
  444. free(fw_dir);
  445. free(boot_dir);
  446. free(vendor_dir);
  447. free(system_dir);
  448. free(kernel_dir);
  449. return 0;
  450. }