funs.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if env | grep -q '^NIMCORES='; then
  55. echo $NIMCORES
  56. else
  57. echo $(nproc 2>/dev/null || sysctl -n hw.logicalcpu 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || 1)
  58. fi
  59. }
  60. _nimBuildCsourcesIfNeeded(){
  61. # if some systems cannot use make or gmake, we could add support for calling `build.sh`
  62. # but this is slower (not parallel jobs) and would require making build.sh
  63. # understand the arguments passed to the makefile (e.g. `CC=gcc ucpu=amd64 uos=darwin`),
  64. # instead of `--cpu amd64 --os darwin`.
  65. unamestr=$(uname)
  66. # uname values: https://en.wikipedia.org/wiki/Uname
  67. if [ "$unamestr" = 'FreeBSD' ]; then
  68. makeX=gmake
  69. elif [ "$unamestr" = 'OpenBSD' ]; then
  70. makeX=gmake
  71. elif [ "$unamestr" = 'NetBSD' ]; then
  72. makeX=gmake
  73. elif [ "$unamestr" = 'CROSSOS' ]; then
  74. makeX=gmake
  75. elif [ "$unamestr" = 'SunOS' ]; then
  76. makeX=gmake
  77. else
  78. makeX=make
  79. fi
  80. nCPU=$(_nimNumCpu)
  81. echo_run which $makeX
  82. # parallel jobs (5X faster on 16 cores: 10s instead of 50s)
  83. echo_run $makeX -C $nim_csourcesDir -j $((nCPU + 2)) -l $nCPU "$@"
  84. # keep $nim_csources in case needed to investigate bootstrap issues
  85. # without having to rebuild
  86. echo_run cp bin/nim $nim_csources
  87. }
  88. nimCiSystemInfo(){
  89. nimDefineVars
  90. echo_run eval echo '$'nim_csources
  91. echo_run pwd
  92. echo_run date
  93. echo_run uname -a
  94. echo_run git log --no-merges -1 --pretty=oneline
  95. echo_run eval echo '$'PATH
  96. echo_run gcc -v
  97. echo_run node -v
  98. echo_run make -v
  99. }
  100. nimCsourcesHash(){
  101. nimDefineVars
  102. echo $nim_csourcesHash
  103. }
  104. nimBuildCsourcesIfNeeded(){
  105. # goal: allow cachine each tagged version independently
  106. # to avoid rebuilding, so that tools like `git bisect`
  107. # can grab a cached past version without rebuilding.
  108. nimDefineVars
  109. (
  110. set -e
  111. # avoid polluting caller scope with internal variable definitions.
  112. if test -f "$nim_csources"; then
  113. echo "$nim_csources exists."
  114. else
  115. if test -d "$nim_csourcesDir"; then
  116. echo "$nim_csourcesDir exists."
  117. else
  118. # Note: using git tags would allow fetching just what's needed, unlike git hashes, e.g.
  119. # via `git clone -q --depth 1 --branch $tag $nim_csourcesUrl`.
  120. echo_run git clone -q --depth 1 -b $nim_csourcesBranch \
  121. $nim_csourcesUrl "$nim_csourcesDir"
  122. # old `git` versions don't support -C option, using `cd` explicitly:
  123. echo_run cd "$nim_csourcesDir"
  124. echo_run git checkout $nim_csourcesHash
  125. echo_run cd "$OLDPWD"
  126. # if needed we could also add: `git reset --hard $nim_csourcesHash`
  127. fi
  128. _nimBuildCsourcesIfNeeded "$@"
  129. fi
  130. echo_run rm -f bin/nim
  131. # fixes bug #17913, but it's unclear why it's needed, maybe specific to MacOS Big Sur 11.3 on M1 arch?
  132. echo_run cp $nim_csources bin/nim
  133. echo_run $nim_csources -v
  134. )
  135. }