Advertisement
funcelot

logs.sh

Jan 18th, 2023
1,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.92 KB | Science | 0 0
  1. #!/usr/bin/env bash
  2. set -e
  3.  
  4. err_report() {
  5.     echo "ERROR: on line $*: $(cat $0 | sed $1!d)" >&2
  6. }
  7.  
  8. trap 'err_report $LINENO' ERR
  9.  
  10. uid=$(id -u)
  11.  
  12. if [ "${uid}" -eq 0 ]; then
  13.     echo "Please run as user"
  14.     exit
  15. fi
  16.  
  17. pwd=$(pwd)
  18.  
  19. install="$1"
  20.  
  21. opts=( "${@:2}" )
  22.  
  23. function help() {
  24.         commands=$(cat $0 | sed -e 's/^[ \t]*//;' | sed -e '/^[ \t]*$/d' | sed -n -e 's/^"\(.*\)".*#/    \1:/p' | sed -n -e 's/: /:\n        /p')
  25.         script="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
  26.        help=$(\
  27. cat << EOF
  28. Builds and logs binaries
  29. Usage: ${script} <option> [optional]
  30. ${commands}
  31. EOF
  32. )
  33.        echo "${help}"
  34.        exit
  35. }
  36.  
  37. case "${install}" in
  38.  
  39.    "--memory") # builds and runs '-memory' target
  40.        array=("-memory")
  41.        ;;
  42.  
  43.    "--playground") # builds and runs '-playground' target
  44.        array=("-playground")
  45.        ;;
  46.  
  47.    "--alloc") # builds and runs '-alloc' target
  48.        array=("-alloc")
  49.        ;;
  50.  
  51.    "--experimental") # builds and runs '-experimental' target
  52.        array=("-experimental")
  53.        ;;
  54.  
  55.    "--micro") # builds and runs '-micro' target
  56.        array=("-micro")
  57.        ;;
  58.  
  59.    "--light") # builds and runs '-light' target
  60.        array=("-light")
  61.        ;;
  62.  
  63.    "--all") # builds and runs all targets
  64.        array=("" "-light" "-micro" "-experimental" "-alloc" "-playground" "-memory")
  65.        ;;
  66.  
  67.    *)
  68.        help
  69.        ;;
  70. esac
  71.  
  72. for opt in "${opts[@]}"; do
  73.    case "${opt}" in
  74.  
  75.        "")
  76.            ;;
  77.  
  78.        "--clean") # [optional] cleans up directories before run
  79.            clean="--clean"
  80.            ;;
  81.  
  82.        "--sanitize") # [optional] builds using sanitizer
  83.            sanitize="--sanitize"
  84.            ;;
  85.  
  86.        "--silent") # [optional] suppress verbose output
  87.            silent="--silent"
  88.            ;;
  89.  
  90.        *)
  91.            help
  92.            ;;
  93.  
  94.    esac
  95. done
  96.  
  97. if [ "${silent}" == "--silent" ]; then
  98.    exec 2>&1 >/dev/null
  99. fi
  100.  
  101. [ ! -d "${pwd}/logs" ] && mkdir "${pwd}/logs"
  102.  
  103. if [ "${clean}" == "--clean" ]; then
  104.    rm -rf "${pwd}/logs"
  105.    mkdir "${pwd}/logs"
  106. fi
  107.  
  108. if [ "${sanitize}" == "--sanitize" ]; then
  109.    SANITIZER_OPTIONS=-DCODE_SANITIZER:BOOL=TRUE
  110. else
  111.    SANITIZER_OPTIONS=
  112. fi
  113.  
  114. OPTIONS=${SANITIZER_OPTIONS}
  115.  
  116. export MAKEFLAGS=-j8
  117.  
  118. cmake \
  119.    -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
  120.    -DCMAKE_BUILD_TYPE:STRING=Debug \
  121.    -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc \
  122.    -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ \
  123.    ${OPTIONS} \
  124.    -S"${pwd}" \
  125.    -B"${pwd}/logs" \
  126.    -G "Ninja"
  127.  
  128. for m in "${array[@]}"; do
  129.    cmake --build "${pwd}/logs" --target "main${m}"
  130.    "${pwd}/logs/main${m}" > "${pwd}/logs/log${m}.txt"
  131. done
  132.  
  133. find "${pwd}/logs" -type f -not -name "log*" -delete
  134. find "${pwd}/logs" -type d -empty -delete
  135.  
  136. if [ "${silent}" == "--silent" ]; then
  137.    exec 1>&2 2>&-
  138. fi
  139.  
  140. [[ $SHLVL -gt 2 ]] || echo OK
  141.  
  142. cd "${pwd}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement