|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | + |
| 9 | +# shellcheck source=tasks/scripts/build-env.sh |
| 10 | +source "${SCRIPT_DIR}/build-env.sh" |
| 11 | + |
| 12 | +pass() { echo "PASS: $1"; } |
| 13 | +fail() { |
| 14 | + echo "FAIL: $1" >&2 |
| 15 | + exit 1 |
| 16 | +} |
| 17 | + |
| 18 | +# The function must be defined by sourcing the helper. |
| 19 | +if ! declare -F ensure_build_nofile_limit >/dev/null; then |
| 20 | + fail "ensure_build_nofile_limit not defined after sourcing build-env.sh" |
| 21 | +fi |
| 22 | +pass "ensure_build_nofile_limit is defined" |
| 23 | + |
| 24 | +os="$(uname -s)" |
| 25 | + |
| 26 | +if [ "${os}" != "Darwin" ]; then |
| 27 | + # On non-Darwin hosts the helper must be a no-op regardless of the current |
| 28 | + # limit — CI Linux runners and native Linux dev must be unaffected. |
| 29 | + ( |
| 30 | + ulimit -Sn 256 2>/dev/null || true |
| 31 | + before="$(ulimit -n)" |
| 32 | + ensure_build_nofile_limit >/dev/null |
| 33 | + after="$(ulimit -n)" |
| 34 | + [ "${before}" = "${after}" ] || fail "limit changed on non-Darwin host (${before} -> ${after})" |
| 35 | + ) |
| 36 | + pass "no-op on non-Darwin host (${os})" |
| 37 | + echo "All build-env tests passed." |
| 38 | + exit 0 |
| 39 | +fi |
| 40 | + |
| 41 | +# Darwin below. |
| 42 | +if ! command -v cargo-zigbuild >/dev/null 2>&1; then |
| 43 | + # Without cargo-zigbuild the helper must be a no-op even on macOS. |
| 44 | + ( |
| 45 | + ulimit -Sn 256 2>/dev/null || true |
| 46 | + before="$(ulimit -n)" |
| 47 | + ensure_build_nofile_limit >/dev/null |
| 48 | + after="$(ulimit -n)" |
| 49 | + [ "${before}" = "${after}" ] || fail "limit changed on macOS without cargo-zigbuild (${before} -> ${after})" |
| 50 | + ) |
| 51 | + pass "no-op on macOS without cargo-zigbuild" |
| 52 | + echo "All build-env tests passed." |
| 53 | + exit 0 |
| 54 | +fi |
| 55 | + |
| 56 | +# Darwin + cargo-zigbuild: the helper should raise a low soft limit. |
| 57 | +( |
| 58 | + if ! ulimit -Sn 256 2>/dev/null; then |
| 59 | + echo "SKIP: unable to lower soft limit to 256 for test" |
| 60 | + exit 0 |
| 61 | + fi |
| 62 | + ensure_build_nofile_limit >/dev/null |
| 63 | + after="$(ulimit -n)" |
| 64 | + hard="$(ulimit -Hn 2>/dev/null || echo unlimited)" |
| 65 | + case "${hard}" in |
| 66 | + unlimited|infinity) |
| 67 | + [ "${after}" -ge 8192 ] || fail "expected soft limit >= 8192, got ${after}" |
| 68 | + ;; |
| 69 | + *[!0-9]*) |
| 70 | + [ "${after}" -gt 256 ] || fail "expected soft limit to rise above 256, got ${after}" |
| 71 | + ;; |
| 72 | + *) |
| 73 | + expected=8192 |
| 74 | + [ "${hard}" -lt "${expected}" ] && expected="${hard}" |
| 75 | + [ "${after}" -ge "${expected}" ] || fail "expected soft limit >= ${expected}, got ${after}" |
| 76 | + ;; |
| 77 | + esac |
| 78 | +) |
| 79 | +pass "raises low soft limit on macOS with cargo-zigbuild" |
| 80 | + |
| 81 | +# Idempotent: when the current limit already meets the desired value, the helper |
| 82 | +# leaves it unchanged (drive this via the env override so it holds regardless of |
| 83 | +# the host default). |
| 84 | +( |
| 85 | + before="$(ulimit -n)" |
| 86 | + OPENSHELL_BUILD_NOFILE_LIMIT=1 ensure_build_nofile_limit >/dev/null |
| 87 | + after="$(ulimit -n)" |
| 88 | + [ "${before}" = "${after}" ] || fail "limit changed when already above desired (${before} -> ${after})" |
| 89 | +) |
| 90 | +pass "no-op when current limit already meets desired" |
| 91 | + |
| 92 | +echo "All build-env tests passed." |
0 commit comments