aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/.bashrc_cxx
blob: 82431671d194f5cb9c2c5d1e540399b885836bad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
[ ! -z "${BASHRC_CXX+x}" ] && return || readonly BASHRC_CXX=1

C_FLAGS=('-Wall' '-Wextra')

runc() (
  set -o errexit

  local -a c_flags=("${C_FLAGS[@]}")
  local -a src_files=()
  local -a prog_flags=()

  while [ "$#" -gt 0 ]; do
    case "$1" in
      -c|--c-flags)
        if [ "$#" -le 1 ]; then
          echo "$FUNCNAME: missing argument for parameter: $1" >&2
          return 1
        fi
        shift ; c_flags+=("$1") ; shift ;;

      -h)
        echo "usage: $FUNCNAME [-h] [-c|--c-flags FLAG]"
        return 0
        ;;

      --)
        shift ; break ;;

      *)
        src_files+=("$( realpath "$1" )") ; shift ;;
    esac
  done

  prog_flags=("$@")

  local build_dir="$( mktemp --directory )"
  trap "$( printf 'popd > /dev/null && rm -rf %q' "$build_dir" )" 0
  pushd "$build_dir" > /dev/null
  local output_name="$( mktemp --tmpdir=. "${FUNCNAME}XXX.exe" )"

  gcc -o "$output_name" \
      "${c_flags[@]+"${c_flags[@]}"}" \
      "${src_files[@]+"${src_files[@]}"}"

  "$output_name" "${prog_flags[@]+"${prog_flags[@]}"}"
)

CXX_FLAGS=('-Wall' '-Wextra' '-std=c++14')

runcxx() (
  set -o errexit

  local cxx_flags=("${CXX_FLAGS[@]}")
  local -a src_files=()
  local -a prog_flags=()

  while [ "$#" -gt 0 ]; do
    case "$1" in
      -c|--cxx-flags)
        if [ "$#" -le 1 ]; then
          echo "$FUNCNAME: missing argument for parameter: $1" >&2
          return 1
        fi
        shift ; cxx_flags+=("$1") ; shift ;;

      -h)
        echo "usage: $FUNCNAME [-h] [-c|--cxx-flags FLAG]"
        return 0
        ;;

      --)
        shift ; break ;;

      *)
        src_files+=("$( realpath "$1" )") ; shift ;;
    esac
  done

  prog_flags=("$@")

  local build_dir="$( mktemp --directory )"
  trap "$( printf 'popd > /dev/null && rm -rf %q' "$build_dir" )" 0
  pushd "$build_dir" > /dev/null
  local output_name="$( mktemp --tmpdir=. "${FUNCNAME}XXX.exe" )"

  g++ -o "$output_name" \
      "${cxx_flags[@]+"${cxx_flags[@]}"}" \
      "${src_files[@]+"${src_files[@]}"}"

  "$output_name" "${prog_flags[@]+"${prog_flags[@]}"}"
)