build.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7. #include "ark_parser.h"
  8. void run_cmd(const char* cmd) {
  9. printf("Executing: %s\n", cmd);
  10. int ret = system(cmd);
  11. if(ret != 0) {
  12. printf("Command failed: %s\n", cmd);
  13. }
  14. }
  15. int main() {
  16. const char* base_dir = "/home/arkos/arkos";
  17. const char* out_dir = "/home/arkos/arkos/finished";
  18. const char* staging_sys = "/home/arkos/arkos/out_staging/system";
  19. const char* staging_vend = "/home/arkos/arkos/out_staging/vendor";
  20. const char* staging_boot = "/home/arkos/arkos/out_staging/boot";
  21. printf("Cleaning up previous build directories...\n");
  22. run_cmd("rm -rf /home/arkos/arkos/finished /home/arkos/arkos/out_staging");
  23. run_cmd("mkdir -p /home/arkos/arkos/finished /home/arkos/arkos/out_staging/system/frameworks /home/arkos/arkos/out_staging/vendor /home/arkos/arkos/out_staging/boot");
  24. printf("Phase 1: Frameworks Integration\n");
  25. DIR* d = opendir("/home/arkos/arkos/frameworks");
  26. if(d) {
  27. struct dirent* dir;
  28. while((dir = readdir(d)) != NULL) {
  29. if(strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) continue;
  30. if(strcmp(dir->d_name, "DRM") == 0 || strcmp(dir->d_name, "SwiftUIFramework") == 0) {
  31. printf("Skipping explicitly excluded framework: %s\n", dir->d_name);
  32. continue;
  33. }
  34. char cmd[512];
  35. snprintf(cmd, sizeof(cmd), "cp -r /home/arkos/arkos/frameworks/%s /home/arkos/arkos/out_staging/system/frameworks/", dir->d_name);
  36. run_cmd(cmd);
  37. }
  38. closedir(d);
  39. }
  40. printf("Phase 2: Boot Folder Perfectification\n");
  41. d = opendir("/home/arkos/arkos/boot");
  42. if(d) {
  43. struct dirent* dir;
  44. while((dir = readdir(d)) != NULL) {
  45. if(strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) continue;
  46. if(strcmp(dir->d_name, "DRM") == 0) {
  47. printf("Skipping boot/DRM folder\n");
  48. continue;
  49. }
  50. char cmd[512];
  51. snprintf(cmd, sizeof(cmd), "cp -r /home/arkos/arkos/boot/%s /home/arkos/arkos/out_staging/boot/", dir->d_name);
  52. run_cmd(cmd);
  53. }
  54. closedir(d);
  55. }
  56. printf("Phase 3: Vendor Folder Preparation\n");
  57. run_cmd("cp -r /home/arkos/arkos/vendor/* /home/arkos/arkos/out_staging/vendor/ 2>/dev/null || true");
  58. printf("Phase 4: Kernel Config Verification\n");
  59. if(access("/home/arkos/arkos/kernel/.config", F_OK) != -1) {
  60. printf("Kernel config found at /home/arkos/arkos/kernel/.config. Kernel is prepared.\n");
  61. } else {
  62. printf("WARNING: Kernel .config not found!\n");
  63. }
  64. printf("Phase 5: Prebuilts Tagging\n");
  65. run_cmd("mkdir -p /home/arkos/arkos/prebuilts");
  66. ArkConfig* prebuilts_ark = ark_parse("/home/arkos/arkos/prebuilts/prebuilts.ark");
  67. d = opendir("/home/arkos/src");
  68. if(d) {
  69. struct dirent* dir;
  70. while((dir = readdir(d)) != NULL) {
  71. char path[512];
  72. snprintf(path, sizeof(path), "/home/arkos/src/%s", dir->d_name);
  73. if(strstr(dir->d_name, "LLVM")) ark_set_global(prebuilts_ark, "LLVM", path);
  74. else if(strstr(dir->d_name, "Swift")) ark_set_global(prebuilts_ark, "Swift", path);
  75. else if(strstr(dir->d_name, "glibc")) ark_set_global(prebuilts_ark, "glibc", path);
  76. else if(strstr(dir->d_name, "linux-kernel")) ark_set_global(prebuilts_ark, "Kernel-Src", path);
  77. }
  78. closedir(d);
  79. }
  80. ark_dump(prebuilts_ark, "/home/arkos/arkos/prebuilts/prebuilts.ark");
  81. ark_free(prebuilts_ark);
  82. printf("Prebuilts tagged and mapped to /home/arkos/arkos/prebuilts/prebuilts.ark\n");
  83. printf("Phase 6: Image Generation\n");
  84. run_cmd("dd if=/dev/zero of=/home/arkos/arkos/finished/sys.img bs=1M count=2048");
  85. run_cmd("mkfs.ext4 -d /home/arkos/arkos/out_staging/system /home/arkos/arkos/finished/sys.img");
  86. run_cmd("dd if=/dev/zero of=/home/arkos/arkos/finished/vend.img bs=1M count=100");
  87. run_cmd("mkfs.ext4 -d /home/arkos/arkos/out_staging/vendor /home/arkos/arkos/finished/vend.img");
  88. run_cmd("dd if=/dev/zero of=/home/arkos/arkos/finished/boot.img bs=1M count=100");
  89. run_cmd("mkfs.ext4 -d /home/arkos/arkos/out_staging/boot /home/arkos/arkos/finished/boot.img");
  90. printf("Preparing Linux Kernel & Native Swift Initramfs...\n");
  91. // We use the host kernel since we haven't compiled the massive Linux kernel tree yet
  92. run_cmd("cp /boot/vmlinuz-linux-zen /home/arkos/arkos/finished/bzImage");
  93. // Compile Swift splash screen as static executable
  94. run_cmd("swiftc -static-executable /home/arkos/arkos/system/swift_splash.swift -o /home/arkos/arkos/system/swift_splash");
  95. // Compile C init wrapper
  96. run_cmd("gcc -static /home/arkos/arkos/system/init.c -o /home/arkos/arkos/system/init");
  97. // Pack into initramfs with glibc libraries
  98. run_cmd("rm -rf /tmp/extract_new && mkdir -p /tmp/extract_new");
  99. run_cmd("cd /tmp/extract_new && zcat /home/arkos/arkos/boot/initramfs.img | cpio -id 2>/dev/null");
  100. run_cmd("cp /home/arkos/arkos/system/init /tmp/extract_new/init && chmod +x /tmp/extract_new/init");
  101. run_cmd("cp /home/arkos/arkos/system/swift_splash /tmp/extract_new/swift_splash && chmod +x /tmp/extract_new/swift_splash");
  102. run_cmd("cd /tmp/extract_new && find . | cpio -H newc -o 2>/dev/null | gzip > /home/arkos/arkos/finished/initramfs.img");
  103. printf("Phase 7: Verify keys and generate avb.img\n");
  104. ArkConfig* securebuild = ark_parse("/home/arkos/arkos/vendor/verify/securebuild.ark");
  105. int has_keys = 0;
  106. for(int i = 0; i < securebuild->standalone_count; i++) {
  107. if(strstr(securebuild->standalone[i], "ARK-OS-") != NULL) {
  108. has_keys = 1;
  109. break;
  110. }
  111. }
  112. if(has_keys) {
  113. printf("Found signing keys, generating avb.img\n");
  114. run_cmd("dd if=/dev/zero of=/home/arkos/arkos/finished/avb.img bs=1M count=1");
  115. } else {
  116. printf("No signing keys found, skipping avb.img\n");
  117. }
  118. ark_free(securebuild);
  119. printf("Phase 8: vbmeta and dtbo (extra)\n");
  120. run_cmd("dd if=/dev/zero of=/home/arkos/arkos/finished/vbmeta.img bs=1M count=1");
  121. run_cmd("dd if=/dev/zero of=/home/arkos/arkos/finished/dtbo.img bs=1M count=1");
  122. printf("Phase 9: Zipping output\n");
  123. run_cmd("cd /home/arkos/arkos && zip -r finished/ArkOS.zip finished/ > /dev/null");
  124. printf("Build complete. Outputs are in %s\n", out_dir);
  125. return 0;
  126. }