funs.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. else
  70. makeX=make
  71. fi
  72. nCPU=$(_nimNumCpu)
  73. echo_run which $makeX
  74. # parallel jobs (5X faster on 16 cores: 10s instead of 50s)
  75. echo_run $makeX -C $nim_csourcesDir -j $((nCPU + 2)) -l $nCPU "$@"
  76. # keep $nim_csources in case needed to investigate bootstrap issues
  77. # without having to rebuild
  78. echo_run cp bin/nim $nim_csources
  79. }
  80. nimCiSystemInfo(){
  81. nimDefineVars
  82. echo_run eval echo '$'nim_csources
  83. echo_run pwd
  84. echo_run date
  85. echo_run uname -a
  86. echo_run git log --no-merges -1 --pretty=oneline
  87. echo_run eval echo '$'PATH
  88. echo_run gcc -v
  89. echo_run node -v
  90. echo_run make -v
  91. }
  92. nimCsourcesHash(){
  93. nimDefineVars
  94. echo $nim_csourcesHash
  95. }
  96. nimBuildCsourcesIfNeeded(){
  97. # goal: allow cachine each tagged version independently
  98. # to avoid rebuilding, so that tools like `git bisect`
  99. # can grab a cached past version without rebuilding.
  100. nimDefineVars
  101. (
  102. set -e
  103. # avoid polluting caller scope with internal variable definitions.
  104. if test -f "$nim_csources"; then
  105. echo "$nim_csources exists."
  106. else
  107. if test -d "$nim_csourcesDir"; then
  108. echo "$nim_csourcesDir exists."
  109. else
  110. # Note: using git tags would allow fetching just what's needed, unlike git hashes, e.g.
  111. # via `git clone -q --depth 1 --branch $tag $nim_csourcesUrl`.
  112. echo_run git clone -q --depth 1 -b $nim_csourcesBranch \
  113. $nim_csourcesUrl "$nim_csourcesDir"
  114. # old `git` versions don't support -C option, using `cd` explicitly:
  115. echo_run cd "$nim_csourcesDir"
  116. echo_run git checkout $nim_csourcesHash
  117. echo_run cd "$OLDPWD"
  118. # if needed we could also add: `git reset --hard $nim_csourcesHash`
  119. fi
  120. _nimBuildCsourcesIfNeeded "$@"
  121. fi
  122. echo_run rm -f bin/nim
  123. # fixes bug #17913, but it's unclear why it's needed, maybe specific to MacOS Big Sur 11.3 on M1 arch?
  124. echo_run cp $nim_csources bin/nim
  125. echo_run $nim_csources -v
  126. )
  127. }