stage0-posix.nix 13 KB

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