diff --git a/dune.disabled b/dune.disabled index b26dfde8..e4c67a92 100644 --- a/dune.disabled +++ b/dune.disabled @@ -9,6 +9,7 @@ (../cpp2v_docker as cpp2v)) (env-vars ; (CPP2V_DOCKER_ENABLED "if-libstdc++") ; This will call `cpp2v` through `docker` only for files that require `libstdc++` + (CPP2V_DOCKER_MEM_CAP=5GB) (CPP2V_DOCKER_ENABLED "y") ; This will use docker for all calls of `cpp2v` )))) @@ -19,4 +20,5 @@ (../cpp2v_docker as cpp2v)) (env-vars ; (CPP2V_DOCKER_ENABLED "if-libstdc++") + (CPP2V_DOCKER_MEM_CAP=5GB) (CPP2V_DOCKER_ENABLED "y")))))) diff --git a/rocq-brick-libstdcpp/cpp2v_docker.in b/rocq-brick-libstdcpp/cpp2v_docker.in index cf2677fc..bc209a04 100644 --- a/rocq-brick-libstdcpp/cpp2v_docker.in +++ b/rocq-brick-libstdcpp/cpp2v_docker.in @@ -1,6 +1,7 @@ #!/usr/bin/env bash REALPATH=$(which grealpath || which realpath) +SED=$(which gsed || which sed) REL_PATH="$($REALPATH `pwd` --relative-to "$DUNE_SOURCEROOT")" map_absolute_paths () { @@ -46,6 +47,15 @@ if [ "$CPP2V_DOCKER_ENABLED" = "y" ] && ! which docker > /dev/null; then : fi +units () { + local pat='s/G/000M/;s/M/000K/;s/K/000B/;s/B//g' + if [[ $# -eq 1 ]]; then + echo "$1" | $SED $pat + else + $SED $pat + fi +} + if [ "$CPP2V_DOCKER_ENABLED" = "y" ] && which docker > /dev/null; then if [ -z "$CPP2V_DOCKER_IMAGE" ]; then # We skip the warning if no docker is available @@ -61,6 +71,21 @@ if [ "$CPP2V_DOCKER_ENABLED" = "y" ] && which docker > /dev/null; then ARGS+=( "--workdir" "$BRICK_LIBCPP/$REL_PATH" ) ARGS+=( $(map_absolute_paths "$@") ) + if [[ -n "$CPP2V_DOCKER_MEM_CAP" ]]; then + MEM_CAP=$(units $CPP2V_DOCKER_MEM_CAP) + if ! [[ $MEM_CAP =~ [0-9]+ ]]; then + echo "Misconfiguration: CPP2V_DOCKER_MEM_CAP = '$CPP2V_DOCKER_MEM_CAP', invalid memory amount" + exit 2 + fi + while true; do + used_mem=$(docker stats --no-stream --format "{{.MemUsage}}" | awk '{print $1}' | head -n 1 | units) + if [[ "$used_mem" -lt $MEM_CAP ]]; then + break + fi + sleep 0.01 + done + fi + cpp2v_args="$( printf '"%q" ' "$@" )" # ensure each argument is quoted as a whole. docker run "${ARGS[@]}" "$CPP2V_DOCKER_IMAGE" \ /bin/bash -c 'eval $(opam env)'"; cpp2v $cpp2v_args"