build.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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];
  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. const char *arkrt_sources[] = {a1, a2, a3, a4, a5, a6};
  215. if (is_up_to_date(arkrt_out, arkrt_sources, 6)) {
  216. printf(" \033[0;32m✓\033[0m arkrt is up-to-date (skipping)\n");
  217. } else {
  218. char cmd[1024];
  219. snprintf(cmd, sizeof(cmd),
  220. "make -C %s out_staging/arkrt", base);
  221. run(cmd);
  222. }
  223. /* ui_daemon sources */
  224. char d1[512], d2[512];
  225. snprintf(d1, sizeof(d1), "%s/system/display/main.swift", base);
  226. snprintf(d2, sizeof(d2), "%s/system/core/fb_helper.c", base);
  227. const char *daemon_sources[] = {d1, d2};
  228. if (is_up_to_date(daemon_out, daemon_sources, 2)) {
  229. printf(" \033[0;32m✓\033[0m ui_daemon is up-to-date (skipping)\n");
  230. } else {
  231. char cmd[1024];
  232. snprintf(cmd, sizeof(cmd),
  233. "make -C %s out_staging/system/ui_daemon", base);
  234. run(cmd);
  235. }
  236. }
  237. /* ─── Step 10: Build SwiftUI framework (SPM cached) ────────── */
  238. step("Building SwiftUI framework (SPM incremental build)");
  239. {
  240. /*
  241. * Uses --scratch-path to store SPM build artifacts in out_staging/.swift_build.
  242. * This directory is preserved across builds so SPM only recompiles changed
  243. * modules instead of all 44+ every time. First build is slow (~5 min),
  244. * subsequent builds with no changes complete in seconds.
  245. *
  246. * The 2>/dev/null suppresses SPM's verbose diagnostic output.
  247. * Errors in this step are non-fatal (|| true) since the UI framework
  248. * is not yet linked into the boot chain.
  249. */
  250. char cmd[2048];
  251. snprintf(
  252. cmd, sizeof(cmd),
  253. "cd %s/frameworks/SwiftUI && swift build -c release "
  254. "-j $(nproc) -Xswiftc -use-ld=lld "
  255. "--scratch-path %s/.swift_build "
  256. "-Xcc -I%s/frameworks/SwiftUI/Sources/SwiftCorelibs/include "
  257. "-Xcxx -I%s/frameworks/SwiftUI/Sources/SwiftCorelibs/include "
  258. "2>/dev/null || true",
  259. base, staging, base, base);
  260. run(cmd);
  261. }
  262. /* ─── Step 11: Compile ui_test (incremental) ───────────────── */
  263. step("Compiling ui_test (setup app)");
  264. {
  265. char ut_out[512];
  266. snprintf(ut_out, sizeof(ut_out), "%s/system/ui_test", staging);
  267. char u1[512], u2[512];
  268. snprintf(u1, sizeof(u1), "%s/system/apps/ui_test.swift", base);
  269. snprintf(u2, sizeof(u2), "%s/frameworks/ArkGraphics/ArkGraphics.swift", base);
  270. const char *ut_sources[] = {u1, u2};
  271. if (is_up_to_date(ut_out, ut_sources, 2)) {
  272. printf(" \033[0;32m✓\033[0m ui_test is up-to-date (skipping)\n");
  273. } else {
  274. char cmd[1024];
  275. snprintf(cmd, sizeof(cmd), "make -C %s out_staging/system/ui_test", base);
  276. run(cmd);
  277. }
  278. }
  279. /* ─── Step 12: Sign arkrt (Verified Boot) ──────────────────── */
  280. step("Signing arkrt binary (Verified Boot)");
  281. {
  282. char cmd[1024];
  283. snprintf(cmd, sizeof(cmd),
  284. "python3 %s/verify/sign.py %s/verify/securebuild.ark "
  285. "%s/arkrt %s/signature.bin",
  286. vendor_dir, vendor_dir, staging, staging);
  287. run(cmd);
  288. }
  289. /* ─── Step 13: Compile init (PID 1) ────────────────────────── */
  290. step("Compiling init (PID 1 process)");
  291. {
  292. char init_out[512];
  293. snprintf(init_out, sizeof(init_out), "%s/init", staging);
  294. char i1[512], i2[512];
  295. snprintf(i1, sizeof(i1), "%s/system/core/init.c", base);
  296. snprintf(i2, sizeof(i2), "%s/vendor/verify/sha256.c", base);
  297. const char *init_sources[] = {i1, i2};
  298. if (is_up_to_date(init_out, init_sources, 2)) {
  299. printf(" \033[0;32m✓\033[0m init is up-to-date (skipping)\n");
  300. } else {
  301. char cmd[1024];
  302. snprintf(cmd, sizeof(cmd), "make -C %s out_staging/init", base);
  303. run(cmd);
  304. }
  305. }
  306. /* ─── Step 14: Pack initramfs ──────────────────────────────── */
  307. step("Packing initramfs (base + runtime + display + UI)");
  308. {
  309. char *initramfs_ext = pjoin(staging, "initramfs_ext");
  310. char *base_initramfs = pjoin(boot_dir, "initramfs.img");
  311. char cmd[2048];
  312. /* Extract base initramfs (contains busybox, kernel modules, etc.) */
  313. snprintf(cmd, sizeof(cmd), "rm -rf %s && mkdir -p %s", initramfs_ext,
  314. initramfs_ext);
  315. run(cmd);
  316. snprintf(cmd, sizeof(cmd), "cd %s && zcat %s | cpio -id --no-preserve-owner 2>/dev/null",
  317. initramfs_ext, base_initramfs);
  318. run(cmd);
  319. /* Create required directories in initramfs */
  320. snprintf(cmd, sizeof(cmd),
  321. "mkdir -p %s/system/services %s/run %s/tmp %s/var/log",
  322. initramfs_ext, initramfs_ext, initramfs_ext, initramfs_ext);
  323. run(cmd);
  324. /* Copy compiled binaries into initramfs */
  325. snprintf(cmd, sizeof(cmd), "cp %s/init %s/init && chmod +x %s/init",
  326. staging, initramfs_ext, initramfs_ext);
  327. run(cmd);
  328. snprintf(cmd, sizeof(cmd), "cp %s/arkrt %s/arkrt && chmod +x %s/arkrt",
  329. staging, initramfs_ext, initramfs_ext);
  330. run(cmd);
  331. snprintf(cmd, sizeof(cmd), "cp %s/signature.bin %s/signature.bin",
  332. staging, initramfs_ext);
  333. run(cmd);
  334. /* Copy display daemon and UI test app into /system/ */
  335. snprintf(cmd, sizeof(cmd),
  336. "cp %s/system/ui_daemon %s/system/ui_daemon && "
  337. "chmod +x %s/system/ui_daemon 2>/dev/null || true",
  338. staging, initramfs_ext, initramfs_ext);
  339. run(cmd);
  340. snprintf(cmd, sizeof(cmd),
  341. "cp %s/system/ui_test %s/system/ui_test && "
  342. "chmod +x %s/system/ui_test 2>/dev/null || true",
  343. staging, initramfs_ext, initramfs_ext);
  344. run(cmd);
  345. /* Copy service definition files */
  346. snprintf(cmd, sizeof(cmd),
  347. "cp -r %s/services/* %s/system/services/ 2>/dev/null || true",
  348. staging_sys, initramfs_ext);
  349. run(cmd);
  350. /* Repack initramfs as gzip-compressed cpio archive */
  351. snprintf(cmd, sizeof(cmd),
  352. "cd %s && find . | cpio -H newc -o 2>/dev/null | gzip > %s/initramfs.img",
  353. initramfs_ext, out_dir);
  354. run(cmd);
  355. free(initramfs_ext);
  356. free(base_initramfs);
  357. }
  358. /* ─── Step 15: Assemble bootloader + stage2 ────────────────── */
  359. step("Assembling bootloader");
  360. {
  361. char cmd[1024];
  362. /* Generate boot animation frames from Python script */
  363. snprintf(cmd, sizeof(cmd),
  364. "python3 %s/source/animationframes/generate_frames.py %s",
  365. boot_dir, staging);
  366. run(cmd);
  367. snprintf(cmd, sizeof(cmd),
  368. "make -C %s out_staging/bootloader.bin out_staging/stage2.bin "
  369. "out_staging/BOOTX64.EFI",
  370. base);
  371. run(cmd);
  372. }
  373. /* ─── Step 16: Generate disk images ────────────────────────── */
  374. step("Generating disk images");
  375. {
  376. char cmd[1024];
  377. /* boot.img — bootloader + kernel + initramfs packed by pack_boot.py */
  378. snprintf(cmd, sizeof(cmd), "python3 %s/tools/pack_boot.py --base-dir %s",
  379. repo, base);
  380. run(cmd);
  381. /* sys.img — system partition (frameworks, apps, services) */
  382. snprintf(cmd, sizeof(cmd),
  383. "dd if=/dev/zero of=%s/sys.img bs=1M count=2048 2>/dev/null",
  384. out_dir);
  385. run(cmd);
  386. snprintf(cmd, sizeof(cmd), "mkfs.ext4 -d %s %s/sys.img 2>/dev/null",
  387. staging_sys, out_dir);
  388. run(cmd);
  389. /* vend.img — vendor partition (OEM blobs, keys) */
  390. snprintf(cmd, sizeof(cmd),
  391. "dd if=/dev/zero of=%s/vend.img bs=1M count=100 2>/dev/null",
  392. out_dir);
  393. run(cmd);
  394. snprintf(cmd, sizeof(cmd), "mkfs.ext4 -d %s %s/vend.img 2>/dev/null",
  395. staging_vend, out_dir);
  396. run(cmd);
  397. /* avb.img — Android Verified Boot metadata (only if signing keys exist) */
  398. char *securebuild_path = pjoin(vendor_dir, "verify/securebuild.ark");
  399. ArkConfig *sbc = ark_parse(securebuild_path);
  400. int has_keys = 0;
  401. for (int i = 0; i < sbc->standalone_count; i++) {
  402. if (strstr(sbc->standalone[i], "ARK-OS-") != NULL) {
  403. has_keys = 1;
  404. break;
  405. }
  406. }
  407. if (has_keys) {
  408. printf(" \033[0;32m✓\033[0m Signing keys found — generating avb.img\n");
  409. snprintf(cmd, sizeof(cmd),
  410. "dd if=/dev/zero of=%s/avb.img bs=1M count=1 2>/dev/null",
  411. out_dir);
  412. run(cmd);
  413. } else {
  414. printf(" \033[1;33m⚠\033[0m No signing keys — skipping avb.img\n");
  415. }
  416. ark_free(sbc);
  417. free(securebuild_path);
  418. /* vbmeta.img + dtbo.img — boot verification and device tree overlay stubs */
  419. snprintf(cmd, sizeof(cmd),
  420. "dd if=/dev/zero of=%s/vbmeta.img bs=1M count=1 2>/dev/null",
  421. out_dir);
  422. run(cmd);
  423. snprintf(cmd, sizeof(cmd),
  424. "dd if=/dev/zero of=%s/dtbo.img bs=1M count=1 2>/dev/null",
  425. out_dir);
  426. run(cmd);
  427. }
  428. printf("\n\033[1;32m╔══════════════════════════════════════╗\033[0m\n");
  429. printf("\033[1;32m║ Build Complete! ║\033[0m\n");
  430. printf("\033[1;32m╚══════════════════════════════════════╝\033[0m\n");
  431. printf("Run \033[1mmake run\033[0m to launch ArkOS in QEMU.\n\n");
  432. /* Free all heap-allocated path strings */
  433. free(repo);
  434. free(out_dir);
  435. free(staging);
  436. free(staging_sys);
  437. free(staging_fw);
  438. free(staging_vend);
  439. free(staging_boot);
  440. free(fw_dir);
  441. free(boot_dir);
  442. free(vendor_dir);
  443. free(system_dir);
  444. free(kernel_dir);
  445. return 0;
  446. }