ci_generate.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ##[
  2. avoid code duplication in CI pipelines.
  3. For now, this is only used for openbsd + freebsd, but there is a lot of other code
  4. duplication that could be removed.
  5. ## usage
  6. edit this file as needed and then re-generate via:
  7. ```
  8. nim r tools/ci_generate.nim
  9. ```
  10. ]##
  11. import std/[strformat, os]
  12. const doNotEdit = "DO NO EDIT DIRECTLY! auto-generated by `nim r tools/ci_generate.nim`"
  13. proc genCiBsd(header: string, batch: int, num: int): string =
  14. result = fmt"""
  15. ## {doNotEdit}
  16. {header}
  17. sources:
  18. - https://github.com/nim-lang/Nim
  19. environment:
  20. NIM_TESTAMENT_BATCH: "{batch}_{num}"
  21. CC: /usr/bin/clang
  22. tasks:
  23. - setup: |
  24. set -e
  25. cd Nim
  26. . ci/funs.sh && nimBuildCsourcesIfNeeded
  27. echo 'export PATH=$HOME/Nim/bin:$PATH' >> $HOME/.buildenv
  28. - test: |
  29. set -e
  30. cd Nim
  31. . ci/funs.sh && nimInternalBuildKochAndRunCI
  32. triggers:
  33. - action: email
  34. condition: failure
  35. to: Andreas Rumpf <rumpf_a@web.de>
  36. """
  37. proc genBuildExtras(echoRun, koch, nim: string): string =
  38. result = fmt"""
  39. {echoRun} {nim} c --noNimblePath --skipUserCfg --skipParentCfg --hints:off koch
  40. {echoRun} {koch} boot -d:release --skipUserCfg --skipParentCfg --hints:off
  41. {echoRun} {koch} tools --skipUserCfg --skipParentCfg --hints:off
  42. """
  43. proc genWindowsScript(buildAll: bool): string =
  44. result = fmt"""
  45. @echo off
  46. rem {doNotEdit}
  47. rem Build development version of the compiler; can be rerun safely
  48. rem bare bones version of ci/funs.sh adapted for windows.
  49. rem Read in some common shared variables (shared with other tools),
  50. rem see https://stackoverflow.com/questions/3068929/how-to-read-file-contents-into-a-variable-in-a-batch-file
  51. for /f "delims== tokens=1,2" %%G in (config/build_config.txt) do set %%G=%%H
  52. SET nim_csources=bin\nim_csources_%nim_csourcesHash%.exe
  53. echo "building from csources: %nim_csources%"
  54. if not exist %nim_csourcesDir% (
  55. git clone -q --depth 1 -b %nim_csourcesBranch% %nim_csourcesUrl% %nim_csourcesDir%
  56. )
  57. if not exist %nim_csources% (
  58. cd %nim_csourcesDir%
  59. git checkout %nim_csourcesHash%
  60. echo "%PROCESSOR_ARCHITECTURE%"
  61. if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
  62. SET ARCH=64
  63. )
  64. CALL build.bat
  65. cd ..
  66. copy /y bin\nim.exe %nim_csources%
  67. )
  68. """
  69. if buildAll:
  70. result.add genBuildExtras("", "koch", r"bin\nim.exe")
  71. proc genPosixScript(): string =
  72. result = fmt"""
  73. #!/bin/sh
  74. # {doNotEdit}
  75. # build development version of the compiler; can be rerun safely.
  76. # arguments can be passed, e.g.:
  77. # CC=gcc ucpu=amd64 uos=darwin
  78. set -u # error on undefined variables
  79. set -e # exit on first error
  80. . ci/funs.sh
  81. nimBuildCsourcesIfNeeded "$@"
  82. {genBuildExtras("echo_run", "./koch", "bin/nim")}
  83. """
  84. proc main()=
  85. let dir = ".builds"
  86. # not too large to be resource friendly, refs bug #17107
  87. let num = 2
  88. # if you reduce this, make sure to remove files that shouldn't be generated,
  89. # or better, do the cleanup logic here e.g.: `rm .builds/openbsd_*`
  90. let headerFreebsd = """
  91. # see https://man.sr.ht/builds.sr.ht/compatibility.md#freebsd
  92. image: freebsd/latest
  93. packages:
  94. - databases/sqlite3
  95. - devel/boehm-gc-threaded
  96. - devel/pcre
  97. - devel/sdl20
  98. - devel/sfml
  99. - www/node
  100. - devel/gmake
  101. """
  102. let headerOpenbsd = """
  103. image: openbsd/latest
  104. packages:
  105. - gmake
  106. - sqlite3
  107. - node
  108. - boehm-gc
  109. - pcre
  110. - sfml
  111. - sdl2
  112. - libffi
  113. """
  114. for i in 0..<num:
  115. writeFile(dir / fmt"openbsd_{i}.yml", genCiBsd(headerOpenbsd, i, num))
  116. writeFile(dir / "freebsd.yml", genCiBsd(headerFreebsd, 0, 1))
  117. writeFile("build_all.sh", genPosixScript())
  118. writeFile("build_all.bat", genWindowsScript(buildAll = true))
  119. writeFile("ci/build_autogen.bat", genWindowsScript(buildAll = false))
  120. when isMainModule:
  121. main()