funs.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Utilities used in CI pipelines and tooling to avoid duplication.
  2. # Avoid top-level statements.
  3. # Prefer nim scripts whenever possible.
  4. # functions starting with `_` are considered internal, less stable.
  5. echo_run () {
  6. # echo's a command before running it, which helps understanding logs
  7. echo ""
  8. echo "cmd: $@" # in azure we could also use this: echo '##[section]"$@"'
  9. "$@"
  10. }
  11. nimGetLastCommit() {
  12. git log --no-merges -1 --pretty=format:"%s"
  13. }
  14. nimIsCiSkip(){
  15. # D20210329T004830:here refs https://github.com/microsoft/azure-pipelines-agent/issues/2944
  16. # `--no-merges` is needed to avoid merge commits which occur for PR's.
  17. # $(Build.SourceVersionMessage) is not helpful
  18. # nor is `github.event.head_commit.message` for github actions.
  19. # Note: `[skip ci]` is now handled automatically for github actions, see https://github.blog/changelog/2021-02-08-github-actions-skip-pull-request-and-push-workflows-with-skip-ci/
  20. commitMsg=$(nimGetLastCommit)
  21. echo commitMsg: "$commitMsg"
  22. if [[ $commitMsg == *"[skip ci]"* ]]; then
  23. echo "skipci: true"
  24. return 0
  25. else
  26. echo "skipci: false"
  27. return 1
  28. fi
  29. }
  30. nimInternalInstallDepsWindows(){
  31. echo_run mkdir dist
  32. echo_run curl -L https://nim-lang.org/download/mingw64.7z -o dist/mingw64.7z
  33. echo_run curl -L https://nim-lang.org/download/dlls.zip -o dist/dlls.zip
  34. echo_run 7z x dist/mingw64.7z -odist
  35. echo_run 7z x dist/dlls.zip -obin
  36. }
  37. nimInternalBuildKochAndRunCI(){
  38. echo_run nim c koch
  39. if ! echo_run ./koch runCI; then
  40. echo_run echo "runCI failed"
  41. echo_run nim r tools/ci_testresults.nim
  42. return 1
  43. fi
  44. }
  45. nimDefineVars(){
  46. . config/build_config.txt
  47. nim_csources=bin/nim_csources_$nim_csourcesHash
  48. }
  49. _nimNumCpu(){
  50. # linux: $(nproc)
  51. # FreeBSD | macOS: $(sysctl -n hw.ncpu)
  52. # OpenBSD: $(sysctl -n hw.ncpuonline)
  53. # windows: $NUMBER_OF_PROCESSORS ?
  54. echo $(nproc 2>/dev/null || sysctl -n hw.logicalcpu 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || 1)
  55. }
  56. _nimBuildCsourcesIfNeeded(){
  57. # if some systems cannot use make or gmake, we could add support for calling `build.sh`
  58. # but this is slower (not parallel jobs) and would require making build.sh
  59. # understand the arguments passed to the makefile (e.g. `CC=gcc ucpu=amd64 uos=darwin`),
  60. # instead of `--cpu amd64 --os darwin`.
  61. unamestr=$(uname)
  62. # uname values: https://en.wikipedia.org/wiki/Uname
  63. if [ "$unamestr" = 'FreeBSD' ]; then
  64. makeX=gmake
  65. elif [ "$unamestr" = 'OpenBSD' ]; then
  66. makeX=gmake
  67. elif [ "$unamestr" = 'NetBSD' ]; then
  68. makeX=gmake
  69. elif [ "$unamestr" = 'CROSSOS' ]; then
  70. makeX=gmake
  71. elif [ "$unamestr" = 'SunOS' ]; then
  72. makeX=gmake
  73. else
  74. makeX=make
  75. fi
  76. nCPU=$(_nimNumCpu)
  77. echo_run which $makeX
  78. # parallel jobs (5X faster on 16 cores: 10s instead of 50s)
  79. echo_run $makeX -C $nim_csourcesDir -j $((nCPU + 2)) -l $nCPU "$@"
  80. # keep $nim_csources in case needed to investigate bootstrap issues
  81. # without having to rebuild
  82. echo_run cp bin/nim $nim_csources
  83. }
  84. nimCiSystemInfo(){
  85. nimDefineVars
  86. echo_run eval echo '$'nim_csources
  87. echo_run pwd
  88. echo_run date
  89. echo_run uname -a
  90. echo_run git log --no-merges -1 --pretty=oneline
  91. echo_run eval echo '$'PATH
  92. echo_run gcc -v
  93. echo_run node -v
  94. echo_run make -v
  95. }
  96. nimCsourcesHash(){
  97. nimDefineVars
  98. echo $nim_csourcesHash
  99. }
  100. nimBuildCsourcesIfNeeded(){
  101. # goal: allow cachine each tagged version independently
  102. # to avoid rebuilding, so that tools like `git bisect`
  103. # can grab a cached past version without rebuilding.
  104. nimDefineVars
  105. (
  106. set -e
  107. # avoid polluting caller scope with internal variable definitions.
  108. if test -f "$nim_csources"; then
  109. echo "$nim_csources exists."
  110. else
  111. if test -d "$nim_csourcesDir"; then
  112. echo "$nim_csourcesDir exists."
  113. else
  114. # Note: using git tags would allow fetching just what's needed, unlike git hashes, e.g.
  115. # via `git clone -q --depth 1 --branch $tag $nim_csourcesUrl`.
  116. echo_run git clone -q --depth 1 -b $nim_csourcesBranch \
  117. $nim_csourcesUrl "$nim_csourcesDir"
  118. # old `git` versions don't support -C option, using `cd` explicitly:
  119. echo_run cd "$nim_csourcesDir"
  120. echo_run git checkout $nim_csourcesHash
  121. echo_run cd "$OLDPWD"
  122. # if needed we could also add: `git reset --hard $nim_csourcesHash`
  123. fi
  124. _nimBuildCsourcesIfNeeded "$@"
  125. fi
  126. echo_run rm -f bin/nim
  127. # fixes bug #17913, but it's unclear why it's needed, maybe specific to MacOS Big Sur 11.3 on M1 arch?
  128. echo_run cp $nim_csources bin/nim
  129. echo_run $nim_csources -v
  130. )
  131. }