build.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * ArkOS Build System
  3. * ------------------
  4. * Orchestrates the full OS build pipeline.
  5. * All paths are relative to the base directory passed as argv[1].
  6. *
  7. * Usage: build <arkos-base-dir>
  8. */
  9. #include "ark_parser.h"
  10. #include <dirent.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. /* ── Build step counter ─────────────────────────────────────────── */
  17. #define TOTAL_STEPS 14
  18. static int current_step = 0;
  19. static void step(const char *desc) {
  20. current_step++;
  21. printf("\n\033[1;36m[%2d/%d]\033[0m %s\n", current_step, TOTAL_STEPS, desc);
  22. }
  23. /* ── Helpers ────────────────────────────────────────────────────── */
  24. static void run(const char *cmd) {
  25. printf(" \033[0;90m$ %s\033[0m\n", cmd);
  26. int ret = system(cmd);
  27. if (ret != 0) {
  28. fprintf(stderr, " \033[1;31m✗ Command failed (exit %d)\033[0m\n", ret);
  29. }
  30. }
  31. /* Join two path components. Caller must free. */
  32. static char *pjoin(const char *a, const char *b) {
  33. size_t len = strlen(a) + 1 + strlen(b) + 1;
  34. char *out = malloc(len);
  35. snprintf(out, len, "%s/%s", a, b);
  36. return out;
  37. }
  38. /* ── Main ───────────────────────────────────────────────────────── */
  39. int main(int argc, char *argv[]) {
  40. if (argc < 2) {
  41. fprintf(stderr, "Usage: %s <arkos-base-dir>\n", argv[0]);
  42. return 1;
  43. }
  44. const char *base = argv[1]; /* e.g. /home/user/repo/arkos */
  45. /* Derive the repo root (one level up from base) */
  46. char *repo = pjoin(base, "..");
  47. /* Key directories */
  48. char *out_dir = pjoin(base, "finished");
  49. char *staging = pjoin(base, "out_staging");
  50. char *staging_sys = pjoin(staging, "system");
  51. char *staging_fw = pjoin(staging, "system/frameworks");
  52. char *staging_vend = pjoin(staging, "vendor");
  53. char *staging_boot = pjoin(staging, "boot");
  54. /* Compiler for OS components (host gcc for now) */
  55. const char *cc = "gcc";
  56. /* Source directories */
  57. char *fw_dir = pjoin(base, "frameworks");
  58. char *boot_dir = pjoin(base, "boot");
  59. char *vendor_dir = pjoin(base, "vendor");
  60. char *system_dir = pjoin(base, "system");
  61. char *kernel_dir = pjoin(base, "kernel");
  62. printf("\n\033[1;35mArkOS Build System v2.0\033[0m\n");
  63. /* ─── Step 1: Clean ─────────────────────────────────────────── */
  64. step("Cleaning previous build");
  65. {
  66. char cmd[1024];
  67. snprintf(cmd, sizeof(cmd),
  68. "chmod -R 777 %s 2>/dev/null || true", staging);
  69. run(cmd);
  70. snprintf(cmd, sizeof(cmd), "rm -rf %s %s", out_dir, staging);
  71. run(cmd);
  72. }
  73. /* ─── Step 2: Create staging directories ────────────────────── */
  74. step("Creating staging directories");
  75. {
  76. char cmd[1024];
  77. snprintf(cmd, sizeof(cmd),
  78. "mkdir -p %s %s %s %s",
  79. out_dir, staging_fw, staging_vend, staging_boot);
  80. run(cmd);
  81. }
  82. /* ─── Step 3: Copy frameworks ───────────────────────────────── */
  83. step("Integrating frameworks");
  84. {
  85. DIR *d = opendir(fw_dir);
  86. if (d) {
  87. struct dirent *ent;
  88. while ((ent = readdir(d)) != NULL) {
  89. if (ent->d_name[0] == '.')
  90. continue;
  91. /* Skip excluded frameworks */
  92. if (strcmp(ent->d_name, "DRM") == 0 ||
  93. strcmp(ent->d_name, "SwiftUIFramework") == 0) {
  94. printf(" Skipping excluded framework: %s\n", ent->d_name);
  95. continue;
  96. }
  97. char cmd[1024];
  98. snprintf(cmd, sizeof(cmd), "cp -r %s/%s %s/",
  99. fw_dir, ent->d_name, staging_fw);
  100. run(cmd);
  101. }
  102. closedir(d);
  103. }
  104. }
  105. /* ─── Step 4: Copy boot files ───────────────────────────────── */
  106. step("Preparing boot files");
  107. {
  108. DIR *d = opendir(boot_dir);
  109. if (d) {
  110. struct dirent *ent;
  111. while ((ent = readdir(d)) != NULL) {
  112. if (ent->d_name[0] == '.')
  113. continue;
  114. if (strcmp(ent->d_name, "DRM") == 0) {
  115. printf(" Skipping boot/DRM\n");
  116. continue;
  117. }
  118. char cmd[1024];
  119. snprintf(cmd, sizeof(cmd), "cp -r %s/%s %s/",
  120. boot_dir, ent->d_name, staging_boot);
  121. run(cmd);
  122. }
  123. closedir(d);
  124. }
  125. }
  126. /* ─── Step 5: Copy vendor files ─────────────────────────────── */
  127. step("Preparing vendor files");
  128. {
  129. char cmd[1024];
  130. snprintf(cmd, sizeof(cmd),
  131. "cp -r %s/* %s/ 2>/dev/null || true",
  132. vendor_dir, staging_vend);
  133. run(cmd);
  134. }
  135. /* ─── Step 6: Verify kernel config ──────────────────────────── */
  136. step("Verifying kernel configuration");
  137. {
  138. char *kconfig = pjoin(kernel_dir, ".config");
  139. if (access(kconfig, F_OK) != -1) {
  140. printf(" \033[0;32m✓\033[0m Kernel .config found\n");
  141. } else {
  142. printf(" \033[1;33m⚠ WARNING:\033[0m Kernel .config not found!\n");
  143. }
  144. free(kconfig);
  145. }
  146. /* ─── Step 7: Compile fb_helper.c ───────────────────────────── */
  147. step("Compiling : system/fb_helper.c");
  148. {
  149. char cmd[1024];
  150. snprintf(cmd, sizeof(cmd), "make -C %s out_staging/fb_helper.o", base);
  151. run(cmd);
  152. }
  153. /* ─── Step 8: Compile swift_splash.swift & arkrt ────────────── */
  154. step("Compiling : system/swift_splash.swift & arkrt");
  155. {
  156. char cmd[1024];
  157. snprintf(cmd, sizeof(cmd), "make -C %s out_staging/swift_splash out_staging/arkrt", base);
  158. run(cmd);
  159. }
  160. /* ─── Step 9: Sign arkrt (Verified Boot) ────────────────────── */
  161. step("Signing arkrt (Verified Boot)");
  162. {
  163. char cmd[1024];
  164. snprintf(cmd, sizeof(cmd),
  165. "python3 %s/verify/sign.py %s/verify/securebuild.ark "
  166. "%s/arkrt %s/signature.bin",
  167. vendor_dir, vendor_dir, staging, staging);
  168. run(cmd);
  169. }
  170. /* ─── Step 10: Compile init.c + vendor/verify/sha256.c ──── */
  171. step("Compiling : system/init.c + vendor/verify/sha256.c");
  172. {
  173. char cmd2[1024];
  174. snprintf(cmd2, sizeof(cmd2), "make -C %s out_staging/init", base);
  175. run(cmd2);
  176. }
  177. /* ─── Step 11: Pack initramfs ───────────────────────────────── */
  178. step("Packing initramfs");
  179. {
  180. char *initramfs_ext = pjoin(staging, "initramfs_ext");
  181. char *base_initramfs = pjoin(boot_dir, "initramfs.img");
  182. char cmd[2048];
  183. /* Extract base initramfs */
  184. snprintf(cmd, sizeof(cmd),
  185. "rm -rf %s && mkdir -p %s", initramfs_ext, initramfs_ext);
  186. run(cmd);
  187. snprintf(cmd, sizeof(cmd),
  188. "cd %s && zcat %s | cpio -id 2>/dev/null",
  189. initramfs_ext, base_initramfs);
  190. run(cmd);
  191. /* Copy compiled binaries from staging into initramfs */
  192. snprintf(cmd, sizeof(cmd),
  193. "cp %s/init %s/init && chmod +x %s/init",
  194. staging, initramfs_ext, initramfs_ext);
  195. run(cmd);
  196. snprintf(cmd, sizeof(cmd),
  197. "cp %s/swift_splash %s/swift_splash && chmod +x %s/swift_splash",
  198. staging, initramfs_ext, initramfs_ext);
  199. run(cmd);
  200. snprintf(cmd, sizeof(cmd),
  201. "cp %s/arkrt %s/arkrt && chmod +x %s/arkrt",
  202. staging, initramfs_ext, initramfs_ext);
  203. run(cmd);
  204. snprintf(cmd, sizeof(cmd),
  205. "cp %s/signature.bin %s/signature.bin",
  206. staging, initramfs_ext);
  207. run(cmd);
  208. /* Repack */
  209. snprintf(cmd, sizeof(cmd),
  210. "cd %s && find . | cpio -H newc -o > %s/initramfs.img 2>/dev/null",
  211. initramfs_ext, out_dir);
  212. run(cmd);
  213. free(initramfs_ext);
  214. free(base_initramfs);
  215. }
  216. /* ─── Step 12: Assemble bootloader + stage2 ─────────────────── */
  217. step("Assembling bootloader");
  218. {
  219. char cmd[1024];
  220. /* Generate animation frames */
  221. snprintf(cmd, sizeof(cmd),
  222. "python3 %s/source/animationframes/generate_frames.py %s",
  223. boot_dir, staging);
  224. run(cmd);
  225. snprintf(cmd, sizeof(cmd),
  226. "make -C %s out_staging/bootloader.bin out_staging/stage2.bin out_staging/BOOTX64.EFI",
  227. base);
  228. run(cmd);
  229. }
  230. /* ─── Step 13: Generate boot.img ────────────────────────────── */
  231. step("Generating boot.img");
  232. {
  233. char cmd[1024];
  234. snprintf(cmd, sizeof(cmd),
  235. "python3 %s/tools/pack_boot.py --base-dir %s",
  236. repo, base);
  237. run(cmd);
  238. }
  239. /* ─── Step 14: Generate disk images ─────────────────────────── */
  240. step("Generating disk images");
  241. {
  242. char cmd[1024];
  243. /* sys.img */
  244. snprintf(cmd, sizeof(cmd),
  245. "dd if=/dev/zero of=%s/sys.img bs=1M count=2048 2>/dev/null",
  246. out_dir);
  247. run(cmd);
  248. snprintf(cmd, sizeof(cmd),
  249. "mkfs.ext4 -d %s %s/sys.img 2>/dev/null",
  250. staging_sys, out_dir);
  251. run(cmd);
  252. /* vend.img */
  253. snprintf(cmd, sizeof(cmd),
  254. "dd if=/dev/zero of=%s/vend.img bs=1M count=100 2>/dev/null",
  255. out_dir);
  256. run(cmd);
  257. snprintf(cmd, sizeof(cmd),
  258. "mkfs.ext4 -d %s %s/vend.img 2>/dev/null",
  259. staging_vend, out_dir);
  260. run(cmd);
  261. /* avb.img (Verified Boot) */
  262. char *securebuild_path = pjoin(vendor_dir, "verify/securebuild.ark");
  263. ArkConfig *sbc = ark_parse(securebuild_path);
  264. int has_keys = 0;
  265. for (int i = 0; i < sbc->standalone_count; i++) {
  266. if (strstr(sbc->standalone[i], "ARK-OS-") != NULL) {
  267. has_keys = 1;
  268. break;
  269. }
  270. }
  271. if (has_keys) {
  272. printf(" \033[0;32m✓\033[0m Signing keys found, generating avb.img\n");
  273. snprintf(cmd, sizeof(cmd),
  274. "dd if=/dev/zero of=%s/avb.img bs=1M count=1 2>/dev/null",
  275. out_dir);
  276. run(cmd);
  277. } else {
  278. printf(" \033[1;33m⚠\033[0m No signing keys found, skipping avb.img\n");
  279. }
  280. ark_free(sbc);
  281. free(securebuild_path);
  282. /* vbmeta.img + dtbo.img */
  283. snprintf(cmd, sizeof(cmd),
  284. "dd if=/dev/zero of=%s/vbmeta.img bs=1M count=1 2>/dev/null",
  285. out_dir);
  286. run(cmd);
  287. snprintf(cmd, sizeof(cmd),
  288. "dd if=/dev/zero of=%s/dtbo.img bs=1M count=1 2>/dev/null",
  289. out_dir);
  290. run(cmd);
  291. }
  292. printf("\n\033[1;32mBuild complete!\033[0m Run \033[1mmake run\033[0m to test.\n\n");
  293. /* Cleanup heap */
  294. free(repo);
  295. free(out_dir);
  296. free(staging);
  297. free(staging_sys);
  298. free(staging_fw);
  299. free(staging_vend);
  300. free(staging_boot);
  301. /* cc is a string literal, no free needed */
  302. free(fw_dir);
  303. free(boot_dir);
  304. free(vendor_dir);
  305. free(system_dir);
  306. free(kernel_dir);
  307. return 0;
  308. }