stage0-posix.nix 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. let
  2. system = "x86_64-linux";
  3. ARCH_DIR = "AMD64";
  4. ARCH = "amd64";
  5. stage0-src = builtins.fetchGit {
  6. url = "https://github.com/oriansj/stage0-posix.git";
  7. rev = "74f1e36f35c28d7854eddfde963917c404764ede";
  8. submodules = true;
  9. };
  10. arch-src = "${stage0-src}/${ARCH_DIR}";
  11. out = builtins.placeholder "out";
  12. run = name: args: derivation {
  13. inherit name system;
  14. builder = builtins.head args;
  15. args = builtins.tail args;
  16. };
  17. hex0 = run "hex0" [
  18. "${stage0-src}/bootstrap-seeds/POSIX/${ARCH_DIR}/hex0-seed"
  19. "${arch-src}/hex0_AMD64.hex0"
  20. out
  21. ];
  22. hex1 = run "hex1" [
  23. hex0
  24. "${arch-src}/hex1_AMD64.hex0"
  25. out
  26. ];
  27. catm = run "catm" [
  28. hex0
  29. "${arch-src}/catm_AMD64.hex0"
  30. out
  31. ];
  32. hex2-0 = run "hex2-0" [
  33. hex1
  34. "${arch-src}/hex2_AMD64.hex1"
  35. out
  36. ];
  37. arch-elf-hex2 = "${arch-src}/ELF-amd64.hex2";
  38. cat = name: args: run name ([
  39. catm
  40. out
  41. ] ++ args);
  42. buildHex2 = name: elf: src: run name [
  43. hex2-0
  44. (cat "${name}-src" [ elf src ])
  45. out
  46. ];
  47. M0 = buildHex2 "M0" arch-elf-hex2 "${arch-src}/M0_AMD64.hex2";
  48. buildM0 = name: elf: src: buildHex2 name elf (run "${name}-m0" [
  49. M0
  50. src
  51. out
  52. ]);
  53. cc_amd64 = buildM0 "cc_amd64" arch-elf-hex2 "${arch-src}/cc_amd64.M1";
  54. M2 =
  55. let
  56. M2-0-c = cat "M2-0.c" [
  57. "${stage0-src}/M2libc/amd64/linux/bootstrap.c"
  58. "${stage0-src}/M2-Planet/cc.h"
  59. "${stage0-src}/M2libc/bootstrappable.c"
  60. "${stage0-src}/M2-Planet/cc_globals.c"
  61. "${stage0-src}/M2-Planet/cc_reader.c"
  62. "${stage0-src}/M2-Planet/cc_strings.c"
  63. "${stage0-src}/M2-Planet/cc_types.c"
  64. "${stage0-src}/M2-Planet/cc_core.c"
  65. "${stage0-src}/M2-Planet/cc_macro.c"
  66. "${stage0-src}/M2-Planet/cc.c"
  67. ];
  68. M2-0-M1 = run "M2-0.M1" [
  69. cc_amd64
  70. M2-0-c
  71. out
  72. ];
  73. M2-0-0-M1 = cat "M2-0-0.m1" [
  74. "${arch-src}/amd64_defs.M1"
  75. "${arch-src}/libc-core.M1"
  76. M2-0-M1
  77. ];
  78. in
  79. buildM0 "M2" arch-elf-hex2 M2-0-0-M1;
  80. libc-elf-hex2 = "${stage0-src}/M2libc/amd64/ELF-amd64.hex2";
  81. libc-elf-debug-hex2 = "${stage0-src}/M2libc/amd64/ELF-amd64-debug.hex2";
  82. blood-elf-0 =
  83. let
  84. blood-elf-0-M1 = run "blood-elf-0.M1" [
  85. M2
  86. "--architecture" "amd64"
  87. "-f" "${stage0-src}/M2libc/amd64/linux/bootstrap.c"
  88. "-f" "${stage0-src}/M2libc/bootstrappable.c"
  89. "-f" "${stage0-src}/mescc-tools/stringify.c"
  90. "-f" "${stage0-src}/mescc-tools/blood-elf.c"
  91. "--bootstrap-mode"
  92. "-o" out
  93. ];
  94. blood-elf-0-0-M1 = cat "blood-elf-0-0.M1" [
  95. "${arch-src}/amd64_defs.M1"
  96. "${arch-src}/libc-core.M1"
  97. blood-elf-0-M1
  98. ];
  99. in
  100. buildM0 "blood-elf-0" libc-elf-hex2 blood-elf-0-0-M1;
  101. runBloodelf = blood-elf: name: src: run name [
  102. blood-elf
  103. "--64" "--little-endian"
  104. "-f" src
  105. "-o" out
  106. ];
  107. M1-0 =
  108. let
  109. M1-macro-0-M1 = run "M1-macro-0.M1" [
  110. M2
  111. "--architecture" "amd64"
  112. "-f" "${stage0-src}/M2libc/amd64/linux/bootstrap.c"
  113. "-f" "${stage0-src}/M2libc/bootstrappable.c"
  114. "-f" "${stage0-src}/mescc-tools/stringify.c"
  115. "-f" "${stage0-src}/mescc-tools/M1-macro.c"
  116. "--bootstrap-mode"
  117. "--debug"
  118. "-o" out
  119. ];
  120. M1-macro-0-footer-M1 = runBloodelf blood-elf-0 "M1-macro-0-footer.M1" M1-macro-0-M1;
  121. M1-macro-0-0-M1 = cat "M1-macro-0-0.M1" [
  122. "${arch-src}/amd64_defs.M1"
  123. "${arch-src}/libc-core.M1"
  124. M1-macro-0-M1
  125. M1-macro-0-footer-M1
  126. ];
  127. in
  128. buildM0 "M1-0" libc-elf-debug-hex2 M1-macro-0-0-M1;
  129. hex2-1 =
  130. let
  131. hex2_linker-1-M1 = run "hex2_linker-1.M1" [
  132. M2
  133. "--architecture" "amd64"
  134. "-f" "${stage0-src}/M2libc/sys/types.h"
  135. "-f" "${stage0-src}/M2libc/stddef.h"
  136. "-f" "${stage0-src}/M2libc/amd64/linux/fcntl.c"
  137. "-f" "${stage0-src}/M2libc/amd64/linux/unistd.c"
  138. "-f" "${stage0-src}/M2libc/amd64/linux/sys/stat.c"
  139. "-f" "${stage0-src}/M2libc/stdlib.c"
  140. "-f" "${stage0-src}/M2libc/stdio.c"
  141. "-f" "${stage0-src}/M2libc/bootstrappable.c"
  142. "-f" "${stage0-src}/mescc-tools/hex2.h"
  143. "-f" "${stage0-src}/mescc-tools/hex2_linker.c"
  144. "-f" "${stage0-src}/mescc-tools/hex2_word.c"
  145. "-f" "${stage0-src}/mescc-tools/hex2.c"
  146. "--debug"
  147. "-o" out
  148. ];
  149. hex2_linker-1-footer-M1 = runBloodelf blood-elf-0 "hex2_linker-1-footer.M1" hex2_linker-1-M1;
  150. hex2_linker-1-hex2 = run "hex2_linker-1.hex2" [
  151. M1-0
  152. "--architecture" "amd64"
  153. "--little-endian"
  154. "-f" "${stage0-src}/M2libc/amd64/amd64_defs.M1"
  155. "-f" "${stage0-src}/M2libc/amd64/libc-full.M1"
  156. "-f" hex2_linker-1-M1
  157. "-f" hex2_linker-1-footer-M1
  158. "-o" out
  159. ];
  160. in
  161. buildHex2 "hex2-1" libc-elf-debug-hex2 hex2_linker-1-hex2;
  162. buildM1 = deps: name: srcs:
  163. let
  164. M1-src = run "${name}.M1" (
  165. [ M2 "--architecture" "amd64" ]
  166. ++ (builtins.concatMap (x: [ "-f" x ]) srcs)
  167. ++ [ "--debug" "-o" out ]
  168. );
  169. M1-footer-src = runBloodelf deps.blood-elf "${name}-footer.M1" M1-src;
  170. hex2-src = run "${name}.hex2" [
  171. deps.M1
  172. "--architecture" "amd64"
  173. "--little-endian"
  174. "-f" "${stage0-src}/M2libc/amd64/amd64_defs.M1"
  175. "-f" "${stage0-src}/M2libc/amd64/libc-full.M1"
  176. "-f" M1-src
  177. "-f" M1-footer-src
  178. "-o" out
  179. ];
  180. in
  181. run name [
  182. deps.hex2
  183. "--architecture" "amd64"
  184. "--little-endian"
  185. "--base-address" "0x00600000"
  186. "-f" libc-elf-debug-hex2
  187. "-f" hex2-src
  188. "-o" out
  189. ];
  190. M1 = buildM1 {
  191. M1 = M1-0;
  192. hex2 = hex2-1;
  193. blood-elf = blood-elf-0;
  194. } "M1" [
  195. "${stage0-src}/M2libc/sys/types.h"
  196. "${stage0-src}/M2libc/stddef.h"
  197. "${stage0-src}/M2libc/amd64/linux/fcntl.c"
  198. "${stage0-src}/M2libc/amd64/linux/unistd.c"
  199. "${stage0-src}/M2libc/string.c"
  200. "${stage0-src}/M2libc/stdlib.c"
  201. "${stage0-src}/M2libc/stdio.c"
  202. "${stage0-src}/M2libc/bootstrappable.c"
  203. "${stage0-src}/mescc-tools/stringify.c"
  204. "${stage0-src}/mescc-tools/M1-macro.c"
  205. ];
  206. hex2 = buildM1 {
  207. inherit M1;
  208. hex2 = hex2-1;
  209. blood-elf = blood-elf-0;
  210. } "hex2" [
  211. "${stage0-src}/M2libc/sys/types.h"
  212. "${stage0-src}/M2libc/stddef.h"
  213. "${stage0-src}/M2libc/amd64/linux/fcntl.c"
  214. "${stage0-src}/M2libc/amd64/linux/unistd.c"
  215. "${stage0-src}/M2libc/amd64/linux/sys/stat.c"
  216. "${stage0-src}/M2libc/stdlib.c"
  217. "${stage0-src}/M2libc/stdio.c"
  218. "${stage0-src}/M2libc/bootstrappable.c"
  219. "${stage0-src}/mescc-tools/hex2.h"
  220. "${stage0-src}/mescc-tools/hex2_linker.c"
  221. "${stage0-src}/mescc-tools/hex2_word.c"
  222. "${stage0-src}/mescc-tools/hex2.c"
  223. ];
  224. buildPhase11 = buildM1 {
  225. inherit M1 hex2;
  226. blood-elf = blood-elf-0;
  227. };
  228. kaem = buildPhase11 "kaem" [
  229. "${stage0-src}/M2libc/sys/types.h"
  230. "${stage0-src}/M2libc/stddef.h"
  231. "${stage0-src}/M2libc/string.c"
  232. "${stage0-src}/M2libc/amd64/linux/fcntl.c"
  233. "${stage0-src}/M2libc/amd64/linux/unistd.c"
  234. "${stage0-src}/M2libc/stdlib.c"
  235. "${stage0-src}/M2libc/stdio.c"
  236. "${stage0-src}/M2libc/bootstrappable.c"
  237. "${stage0-src}/mescc-tools/Kaem/kaem.h"
  238. "${stage0-src}/mescc-tools/Kaem/variable.c"
  239. "${stage0-src}/mescc-tools/Kaem/kaem_globals.c"
  240. "${stage0-src}/mescc-tools/Kaem/kaem.c"
  241. ];
  242. M2-Mesoplanet = buildPhase11 "M2-Mesoplanet" [
  243. "${stage0-src}/M2libc/sys/types.h"
  244. "${stage0-src}/M2libc/stddef.h"
  245. "${stage0-src}/M2libc/${ARCH}/linux/fcntl.c"
  246. "${stage0-src}/M2libc/${ARCH}/linux/unistd.c"
  247. "${stage0-src}/M2libc/${ARCH}/linux/sys/stat.c"
  248. "${stage0-src}/M2libc/stdlib.c"
  249. "${stage0-src}/M2libc/stdio.c"
  250. "${stage0-src}/M2libc/string.c"
  251. "${stage0-src}/M2libc/bootstrappable.c"
  252. "${stage0-src}/M2-Mesoplanet/cc.h"
  253. "${stage0-src}/M2-Mesoplanet/cc_globals.c"
  254. "${stage0-src}/M2-Mesoplanet/cc_env.c"
  255. "${stage0-src}/M2-Mesoplanet/cc_reader.c"
  256. "${stage0-src}/M2-Mesoplanet/cc_spawn.c"
  257. "${stage0-src}/M2-Mesoplanet/cc_core.c"
  258. "${stage0-src}/M2-Mesoplanet/cc_macro.c"
  259. "${stage0-src}/M2-Mesoplanet/cc.c"
  260. ];
  261. blood-elf = buildPhase11 "blood-elf" [
  262. "${stage0-src}/M2libc/sys/types.h"
  263. "${stage0-src}/M2libc/stddef.h"
  264. "${stage0-src}/M2libc/${ARCH}/linux/fcntl.c"
  265. "${stage0-src}/M2libc/${ARCH}/linux/unistd.c"
  266. "${stage0-src}/M2libc/stdlib.c"
  267. "${stage0-src}/M2libc/stdio.c"
  268. "${stage0-src}/M2libc/bootstrappable.c"
  269. "${stage0-src}/mescc-tools/stringify.c"
  270. "${stage0-src}/mescc-tools/blood-elf.c"
  271. ];
  272. buildPhase14 = buildM1 {
  273. inherit M1 hex2 blood-elf;
  274. };
  275. get_machine = buildPhase14 "get_machine" [
  276. "${stage0-src}/M2libc/sys/types.h"
  277. "${stage0-src}/M2libc/stddef.h"
  278. "${stage0-src}/M2libc/${ARCH}/linux/fcntl.c"
  279. "${stage0-src}/M2libc/${ARCH}/linux/unistd.c"
  280. "${stage0-src}/M2libc/stdlib.c"
  281. "${stage0-src}/M2libc/stdio.c"
  282. "${stage0-src}/M2libc/bootstrappable.c"
  283. "${stage0-src}/mescc-tools/get_machine.c"
  284. ];
  285. M2-Planet = buildPhase14 "M2-Planet" [
  286. "${stage0-src}/M2libc/sys/types.h"
  287. "${stage0-src}/M2libc/stddef.h"
  288. "${stage0-src}/M2libc/${ARCH}/linux/fcntl.c"
  289. "${stage0-src}/M2libc/${ARCH}/linux/unistd.c"
  290. "${stage0-src}/M2libc/stdlib.c"
  291. "${stage0-src}/M2libc/stdio.c"
  292. "${stage0-src}/M2libc/bootstrappable.c"
  293. "${stage0-src}/M2-Planet/cc.h"
  294. "${stage0-src}/M2-Planet/cc_globals.c"
  295. "${stage0-src}/M2-Planet/cc_reader.c"
  296. "${stage0-src}/M2-Planet/cc_strings.c"
  297. "${stage0-src}/M2-Planet/cc_types.c"
  298. "${stage0-src}/M2-Planet/cc_core.c"
  299. "${stage0-src}/M2-Planet/cc_macro.c"
  300. "${stage0-src}/M2-Planet/cc.c"
  301. ];
  302. # after this point, we rely on PATH so we have to build up a bin dir that we
  303. # can use for that so we need mkdir, chmod and cp
  304. mkdir0 = buildPhase14 "mkdir0" [
  305. "${stage0-src}/M2libc/sys/types.h"
  306. "${stage0-src}/M2libc/stddef.h"
  307. "${stage0-src}/M2libc/string.c"
  308. "${stage0-src}/M2libc/amd64/linux/fcntl.c"
  309. "${stage0-src}/M2libc/amd64/linux/unistd.c"
  310. "${stage0-src}/M2libc/amd64/linux/sys/stat.c"
  311. "${stage0-src}/M2libc/stdlib.c"
  312. "${stage0-src}/M2libc/stdio.c"
  313. "${stage0-src}/M2libc/bootstrappable.c"
  314. "${stage0-src}/mescc-tools-extra/mkdir.c"
  315. ];
  316. chmod0 = buildPhase14 "chmod0" [
  317. "${stage0-src}/M2libc/sys/types.h"
  318. "${stage0-src}/M2libc/stddef.h"
  319. "${stage0-src}/M2libc/string.c"
  320. "${stage0-src}/M2libc/amd64/linux/fcntl.c"
  321. "${stage0-src}/M2libc/amd64/linux/unistd.c"
  322. "${stage0-src}/M2libc/amd64/linux/sys/stat.c"
  323. "${stage0-src}/M2libc/stdlib.c"
  324. "${stage0-src}/M2libc/stdio.c"
  325. "${stage0-src}/M2libc/bootstrappable.c"
  326. "${stage0-src}/mescc-tools-extra/chmod.c"
  327. ];
  328. cp0 = buildPhase14 "cp0" [
  329. "${stage0-src}/M2libc/sys/types.h"
  330. "${stage0-src}/M2libc/stddef.h"
  331. "${stage0-src}/M2libc/string.c"
  332. "${stage0-src}/M2libc/amd64/linux/fcntl.c"
  333. "${stage0-src}/M2libc/amd64/linux/unistd.c"
  334. "${stage0-src}/M2libc/stdlib.c"
  335. "${stage0-src}/M2libc/stdio.c"
  336. "${stage0-src}/M2libc/bootstrappable.c"
  337. "${stage0-src}/mescc-tools-extra/cp.c"
  338. ];
  339. makeBin = name: attrs:
  340. let
  341. getEnv = builtins.replaceStrings ["-"] [""];
  342. script = map
  343. (x: ''
  344. ''${cp0} ''${${getEnv x}} ''${out}/bin/${x}
  345. ''${chmod0} 0700 ''${out}/bin/${x}
  346. '')
  347. (builtins.attrNames attrs);
  348. env = builtins.listToAttrs (
  349. map
  350. (x: { name = (getEnv x); value = attrs."${x}"; })
  351. (builtins.attrNames attrs));
  352. in
  353. derivation (env // {
  354. inherit system;
  355. name = "buildtools0";
  356. inherit mkdir0 chmod0 cp0;
  357. builder = kaem;
  358. args = ["--file" (builtins.toFile "builder.kaem" ''
  359. ''${mkdir0} -p ''${out}/bin
  360. ${builtins.concatStringsSep "\n" script}
  361. '')];
  362. });
  363. buildtools0 = makeBin "buildtools0" {
  364. inherit
  365. kaem
  366. hex2
  367. blood-elf
  368. get_machine
  369. M1
  370. M2-Planet
  371. M2-Mesoplanet
  372. ;
  373. };
  374. buildWithC = name: attrs: script: derivation (attrs // {
  375. inherit system name;
  376. src = stage0-src;
  377. mkdir = mkdir0;
  378. M2LIBC_PATH = "${stage0-src}/M2libc";
  379. PATH = "${buildtools0}/bin";
  380. builder = kaem;
  381. args = ["--file" (builtins.toFile "builder.kaem" script)];
  382. });
  383. mescc-tools-extra = buildWithC "mescc-tools-extra" {} ''
  384. set -ex
  385. alias CC="M2-Mesoplanet --architecture ${ARCH} -f"
  386. BINDIR=''${out}/bin
  387. ''${mkdir} -p ''${BINDIR}
  388. cd ''${src}/mescc-tools-extra
  389. CC sha256sum.c -o ''${BINDIR}/sha256sum
  390. CC match.c -o ''${BINDIR}/match
  391. CC mkdir.c -o ''${BINDIR}/mkdir
  392. CC untar.c -o ''${BINDIR}/untar
  393. CC ungz.c -o ''${BINDIR}/ungz
  394. CC catm.c -o ''${BINDIR}/catm
  395. CC cp.c -o ''${BINDIR}/cp
  396. CC chmod.c -o ''${BINDIR}/chmod
  397. '';
  398. in
  399. {
  400. inherit
  401. hex0
  402. hex1
  403. catm
  404. hex2-0
  405. M0
  406. cc_amd64
  407. M2
  408. blood-elf-0
  409. M1-0
  410. hex2-1
  411. M1
  412. hex2
  413. kaem
  414. M2-Mesoplanet
  415. blood-elf
  416. get_machine
  417. M2-Planet
  418. makeBin
  419. buildtools0
  420. mescc-tools-extra
  421. ;
  422. }