| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -eu |
| 4 | |
| 5 | recstacksize=65520 |
| 6 | if command -v ulimit >/dev/null 2>/dev/null; then |
| 7 | hardstacksize="$(ulimit -H -s || true)" |
| 8 | ( |
| 9 | stacksize="$(ulimit -S -s || true)" |
| 10 | test ! -z "$stacksize" || { >&2 printf "Warning: Could not determine stack size.\n"; return 1; } |
| 11 | # unlimited stack is fine. N.B. == is not fully POSIX-compliant, so we need != or == |
| 12 | test "$stacksize" != "unlimited" || return 0 |
| 13 | # test for integer stacksize |
| 14 | test "$stacksize" -eq "$stacksize" 2>/dev/null || { >&2 printf "Warning: Stack size (%s) could not be parsed as a number.\n" "$stacksize"; return 1; } |
| 15 | test "$stacksize" -ge "$recstacksize" || { >&2 printf "Warning: Stack size (%s) may be too small.\n" "$stacksize"; return 1; } |
| 16 | ) || { |
| 17 | >&2 printf "Warning: To avoid stack overflows in OCaml compilation, setting stack size to the recommended minimum of %s kbytes\n" "$recstacksize" |
| 18 | ulimit -S -s "$recstacksize" || { >&2 printf "Warning: Failed to set stack size to %s, possibly because the hard stack size limit is %s.\n" "$recstacksize" "$hardstacksize"; return 1; } |
| 19 | } |
| 20 | else |
| 21 | >&2 printf "Warning: Could not determine stack size because ulimit was not found.\nWarning: We recommend a stack size of at least %s kbytes to avoid stack overflows in OCaml compilation.\n" "$recstacksize" |
| 22 | fi |