Skip to content

Commit e0e5703

Browse files
committed
better
1 parent d69efbb commit e0e5703

File tree

1 file changed

+97
-7
lines changed

1 file changed

+97
-7
lines changed

Linux/rbuilder.sh

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ declare -g ARTIFACT_DIR=""
2222
declare -g CONTAINER_ID=""
2323
declare -a CARGO_ARGS=()
2424
declare -a MOUNT_ARGS=()
25-
declare -a ENV_ARGS=()
2625

2726
# Color codes for output
2827
readonly RED='\033[0;31m'
@@ -371,7 +370,7 @@ readonly CONTAINER_WORKSPACE=${escaped_workspace}
371370
readonly CONTAINER_RUST_TARGET=${escaped_rust_target}
372371
readonly CONTAINER_TOOLCHAIN=${escaped_toolchain}
373372
readonly CONTAINER_RBUILD_STATIC=${escaped_rbuild_static}
374-
readonly CONTAINER_RUSTFLAGS=${escaped_rustflags}
373+
CONTAINER_RUSTFLAGS=${escaped_rustflags}
375374
376375
# Sanity checks for required variables
377376
if [[ -z "\${CONTAINER_RUST_TARGET}" ]]; then
@@ -519,6 +518,7 @@ else
519518
rustup target add "\${RUST_TARGET}" || { echo "ERROR: Failed to add target \${RUST_TARGET}" >&2; exit 1; }
520519
fi
521520
521+
readonly CONTAINER_RUSTFLAGS=${escaped_rustflags}
522522
echo "Final Rust target: \${RUST_TARGET}"
523523
524524
# Handle RUSTFLAGS setup
@@ -557,11 +557,16 @@ if [[ "\${CONTAINER_RBUILD_STATIC}" == "1" ]]; then
557557
558558
# Convert array to space-separated string
559559
export RUSTFLAGS="\${rust_flags[*]}"
560-
echo "Static build RUSTFLAGS: \${RUSTFLAGS}"
561-
560+
echo "Static build RUSTFLAGS: \${RUSTFLAGS}"
562561
elif [[ -n "\${CONTAINER_RUSTFLAGS}" ]]; then
563-
export RUSTFLAGS="\${CONTAINER_RUSTFLAGS}"
564-
echo "Using provided RUSTFLAGS: \${RUSTFLAGS}"
562+
if echo "\${RUST_TARGET}" | grep -Eqi "alpine|gnu"; then
563+
RUSTFLAGS_TMP="\$(echo "\${CONTAINER_RUSTFLAGS}" | sed -E 's/-C[[:space:]]+link-self-contained[[:space:]]*=[[:space:]]*yes//g' | xargs)"
564+
export RUSTFLAGS="\${RUSTFLAGS_TMP}"
565+
echo "Using RUSTFLAGS (Provided + Sanitized): \${RUSTFLAGS}"
566+
else
567+
export RUSTFLAGS="\${CONTAINER_RUSTFLAGS}"
568+
echo "Using RUSTFLAGS (Provided): \${RUSTFLAGS}"
569+
fi
565570
else
566571
echo "No custom RUSTFLAGS specified"
567572
fi
@@ -594,6 +599,84 @@ pull_image() {
594599
return 0
595600
}
596601

602+
# Get total memory in bytes with multiple fallbacks
603+
get_total_memory_bytes() {
604+
local memory_bytes=0
605+
606+
# Method 1: Try /proc/meminfo (most reliable on Linux)
607+
if [[ -r "/proc/meminfo" ]]; then
608+
# Extract MemTotal, handle various whitespace patterns
609+
memory_bytes="$(awk '/^[[:space:]]*MemTotal[[:space:]]*:/ {
610+
# Remove all non-digit characters except for the number
611+
gsub(/[^0-9]/, "", $2);
612+
print $2 * 1024
613+
}' /proc/meminfo 2>/dev/null)"
614+
fi
615+
616+
# Method 2: Fallback to free command if /proc/meminfo failed
617+
if [[ -z "${memory_bytes}" || "${memory_bytes}" -eq 0 ]]; then
618+
# Use free with bytes, handle various output formats
619+
memory_bytes="$(free -b 2>/dev/null | awk '
620+
/^[[:space:]]*Mem[[:space:]]*:/ {
621+
gsub(/[^0-9]/, "", $2);
622+
if ($2 > 0) print $2
623+
}
624+
/^[[:space:]]*Memory[[:space:]]*:/ {
625+
gsub(/[^0-9]/, "", $2);
626+
if ($2 > 0) print $2
627+
}
628+
')"
629+
fi
630+
631+
# Method 3: Try alternative free output parsing
632+
if [[ -z "${memory_bytes}" || "${memory_bytes}" -eq 0 ]]; then
633+
memory_bytes="$(free 2>/dev/null | awk '
634+
NR==2 {
635+
gsub(/[^0-9]/, "", $2);
636+
if ($2 > 0) print $2 * 1024
637+
}
638+
')"
639+
fi
640+
641+
# Method 4: macOS fallback using sysctl
642+
if [[ -z "${memory_bytes}" || "${memory_bytes}" -eq 0 ]] && command -v sysctl >/dev/null 2>&1; then
643+
memory_bytes="$(sysctl -n 'hw.memsize' 2>/dev/null)"
644+
fi
645+
646+
# Validation: ensure we got a reasonable memory value (at least 100MB)
647+
if [[ -n "${memory_bytes}" && "${memory_bytes}" -gt 104857600 ]]; then
648+
echo "${memory_bytes}"
649+
else
650+
# Ultimate fallback: assume 2GB if all methods fail
651+
echo "2147483648"
652+
fi
653+
}
654+
655+
# Function to calculate 80% of memory with proper formatting
656+
calculate_memory_limit() {
657+
local total_memory
658+
total_memory="$(get_total_memory_bytes)"
659+
660+
# Calculate 80% and format appropriately
661+
local memory_limit
662+
memory_limit="$(printf "%.0f" "$(echo "$total_memory * 0.8" | bc 2>/dev/null || echo "$total_memory * 8 / 10" | awk '{print int($1)}')")"
663+
664+
echo "${memory_limit}"
665+
}
666+
667+
# Detect available resources
668+
detect_resources() {
669+
local available_cpus available_memory
670+
671+
available_cpus="$(nproc 2>/dev/null || echo '1')"
672+
available_memory="$(calculate_memory_limit)"
673+
674+
log_verbose "Available CPUs: ${available_cpus}"
675+
log_verbose "Available memory: $((available_memory / 1024 / 1024))MB"
676+
677+
return 0
678+
}
679+
597680
# Run container
598681
run_container() {
599682
local setup_script
@@ -627,6 +710,11 @@ run_container() {
627710
"--rm"
628711
"--platform=${CONTAINER_PLATFORM}"
629712
"--workdir=${DEFAULT_WORKSPACE}"
713+
"--cpus=$(nproc)"
714+
"--memory=$(calculate_memory_limit)"
715+
"--shm-size=1g"
716+
"--ulimit=nofile=65536:65536"
717+
"--security-opt=seccomp=unconfined"
630718
)
631719

632720
# Add mount arguments
@@ -721,8 +809,10 @@ main() {
721809
# Detect container engine
722810
if ! detect_container_engine; then
723811
exit 1
812+
else
813+
detect_resources
724814
fi
725-
815+
726816
# Determine target and container image
727817
if ! determine_target_and_image; then
728818
exit 1

0 commit comments

Comments
 (0)